# shortest path problem dijkstra’s algorithm

**shortest path problem**

**shortest path problem**

*Shortest path problem can be defined as a problem to find minimum distance between two vertices/nodes of connected weighted graph. Dijkstra’s algorithm is an efficient algorithm for finding shortest path in a connected weighted graph. for example in below graph the shortest path to reach from A to D is ACED because the total distance between A to D in this path is 9, which is minimum if we travel from any other path or route from A to D.
*

**Note: To apply Dijkstra’s algorithm for finding shortest path following conditions need to be satisfied.**

*Graph ( directed or undirected ) must be connected.**All weights of edges must be positive. Dijkstra’s algorithm does not work with negative weight.*

*How Dijkstra’s algorithm works*

*How Dijkstra’s algorithm works*

*The below is the steps of Dijkstra’s algorithm ( from Wikipedia.org)*.

*Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.**Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes called the unvisited set.**For the current node, consider all of its unvisited neighbors and calculate their tentative distances. Compare the newly calculated tentative distance to the current assigned value and assign the smaller one. For example in above example, if the current node A is marked with a distance of 0, and the edge connecting it with a neighbor C has length 2, then the distance to C (through A) will be 0 + 2 = 2. If C was previously marked with a distance greater than 2 then change it to 2. Otherwise, keep the current value.**When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again.**If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a complete traversal; occurs when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished.**Select the unvisited node that is marked with the smallest tentative distance, and set it as the new “current node” then go back to step 3.*

* Example graph based on above steps:*

## shortest path problem dijkstra’s algorithm sample code

#include<iostream> #include<string.h> using namespace std; #define N 6 #define D 999999 void djk(int mat[N][N], int startNode) { int *visited = new int[6]; int *dist = new int[6]; for (int i = 0; i < 6; i++) visited[i] = 0; for (int i = 0; i < 6; i++) dist[i] = D; dist[startNode] = 0; // visit every node for (int p = 0; p < 6; p++) { int min_index = -1; int mini = D; for (int v = 0; v < 6; v++) { if (mini > dist[v] && !visited[v]) { mini = dist[v]; min_index = v; } } visited[min_index] = 1; for (int pv = 0; pv < 6; pv++) { if (!visited[pv] && dist[pv] > dist[min_index] + mat[min_index][pv] && mat[min_index][pv]) { dist[pv] = dist[min_index] + mat[min_index][pv]; } } } // print distance for (int i = 0; i < 6; i++) cout << dist[i] << " "; cout << "\n"; } int main() { // represent mat // A B C D E F // A 0 3 4 0 0 0 // B 3 0 5 2 0 0 // C 4 5 0 4 6 3 // D 0 2 4 0 6 0 // E 0 0 6 6 0 7 // F 0 0 3 0 7 0 int mat[][6] = { {0, 3, 4, 0, 0, 0}, {3, 0, 5, 2, 0, 0}, {4, 5, 0, 4, 6, 3}, {0, 2, 4, 0, 6, 0}, {0, 0, 6, 6, 0, 7}, {0, 0, 3, 0, 7, 0} }; djk(mat, 0); return 0; }

## Leave a Reply