Dijkstra’s Algorithm C Program

 

C Program To Implement Dijkstra’s Algorithm To Find Shortest Path

Let us learn how to implement Dijkstra’s Algorithm in C programming using Adjacency Matrix.

The Dijkstra Algorithm is used to find the shortest path in a weighted graph.

 

What is Dijkstra’s Algorithm?

Dijkstra’s Algorithm is useful for finding the shortest path in a weighted graph. In any graph G, the shortest path from a source vertex to a destination vertex can be calculated using this algorithm.

In any graph G, the shortest path from a source vertex to a destination vertex can be calculated using Dijkstra Algorithm.

Must Read: C Program To Implement Kruskal’s Algorithm

Every vertex is labelled with pathLength and predecessor. The pathLength denotes the shortest path whereas the predecessor denotes the predecessor of a given vertex in a path.

The values of the pathLength and predecessor can be updated more than once in this algorithm. Initially, all the vertices are made temporary.

If a vertex is made Permanent, it means that the shortest distance has been found out. Once a vertex is made permanent, then the values of the pathLength and predecessor for it become fixed and cannot be changed thereafter.

This algorithm is similar to Prim’s Algorithm to find the minimum spanning trees. The Dijkstra’s algorithm works only for graphs having non – negative weights.

   

Dijkstra's Algorithm in C Programming using Adjacency Matrix

Steps for Dijkstra’s Sequence of Action

 

  1. Set the pathLength of all the vertices to Infinity, the predecessor of all the vertices to NIL and the status of all the vertices to temporary.
  2. Set pathLength of source vertex to Zero.
  3. Find the vertex that has the minimum value of pathLength by checking all the vertices in the graph and set its status as Permanent and consider it as the current vertex.
  4. Check all the temporary vertices adjacent to the current vertex. Now, the value of the pathLength is recalculated for all these temporary successors of current vertex, and relabelling is done if needed.
  5. Repeat the steps 3 and 4 until there is no temporary vertex remaining in the graph. In case if there are any vertices left, then they must have pathLength as Infinity.

Must Read: C Program To Implement Sliding Window Algorithm

   

C Program For Dijkstra’s Algorithm using Adjacency Matrix

Must Read: C Program For Travelling Salesman Problem

Output

C Program For Dijkstra's Algorithm To Find Shortest Path

In case you get any compilation errors or any doubts in this code for Dijkstra’s Algorithm in C programming for finding the shortest path, let us know about it in the comment section below.

7 thoughts on “Dijkstra’s Algorithm C Program

  1. Akash Katare

    What other algorithms are used to find out Shortest Path for a weighted graph apart from the Dijkstra’s Algorithm?

    Reply
  2. Ranu Negi

    I believe this is the perfect code for Dijkstra’s algorithm in c programming. It is well written and formatted properly.

    Reply
  3. Sakshi Sinha

    The Dijkstra algorithm explanation is really what I was looking for. Thank you so much.

    Reply
  4. Rahul Sharma

    One of the best of dijkstra’s algorithm using adjacency matrix that i have seen. Dijkstra algorithm is however comparatively difficult to understand.

    Reply
  5. Srishti Soni

    Dijkstra’ algorithm can also be implemented using a priority queue in C programming.

    Reply
  6. Choi Si Won

    Hello! Can I ask what is the complete condition in line 147? I cannot see what is after the ‘origin_vertex < 0 ||' with my browser. I'm just a newbie in programming, that's why I do not know how to fill the blanks

    Reply

Let's Discuss