Shortest path in a directed graph by Dijkstraâ €™ s algorithm | C ++ implementation to find the shortest path in a directed graph from source vertex to the destination vertex ; Class of the node ; Adjacency list that shows the vertexNumber of child vertex and the weight of the edge ; Function to add the child for the given node ; Function to find the distance of the node from the given source vertex to the destination vertex ; Stores distance of each vertex from source vertex ; Boolean array that shows whether the vertex ' i ' is visited or not ; Set of vertices that has a parent ( one or more ) marked as visited ; Mark current as visited ; Inserting into the visited vertex ; Condition to check the distance is correct and update it if it is minimum from the previous computed distance ; The new current ; Loop to update the distance of the vertices of the graph ; Function to print the path from the source vertex to the destination vertex ; Condition to check if there is no path between the vertices ; Driver Code ; Loop to create the nodes ; Creating directed weighted edges ; Loop to print the distance of every node from source vertex
#include <bits/stdc++.h> NEW_LINE #define infi  1000000000 NEW_LINE using namespace std ; class Node { public : int vertexNumber ; vector < pair < int , int > > children ; Node ( int vertexNumber ) { this -> vertexNumber = vertexNumber ; } void add_child ( int vNumber , int length ) { pair < int , int > p ; p . first = vNumber ; p . second = length ; children . push_back ( p ) ; } } ; vector < int > dijkstraDist ( vector < Node * > g , int s , vector < int > & path ) { vector < int > dist ( g . size ( ) ) ; bool visited [ g . size ( ) ] ; for ( int i = 0 ; i < g . size ( ) ; i ++ ) { visited [ i ] = false ; path [ i ] = -1 ; dist [ i ] = infi ; } dist [ s ] = 0 ; path [ s ] = -1 ; int current = s ; unordered_set < int > sett ; while ( true ) { visited [ current ] = true ; for ( int i = 0 ; i < g [ current ] -> children . size ( ) ; i ++ ) { int v = g [ current ] -> children [ i ] . first ; if ( visited [ v ] ) continue ; sett . insert ( v ) ; int alt = dist [ current ] + g [ current ] -> children [ i ] . second ; if ( alt < dist [ v ] ) { dist [ v ] = alt ; path [ v ] = current ; } } sett . erase ( current ) ; if ( sett . empty ( ) ) break ; int minDist = infi ; int index = 0 ; for ( int a : sett ) { if ( dist [ a ] < minDist ) { minDist = dist [ a ] ; index = a ; } } current = index ; } return dist ; } void printPath ( vector < int > path , int i , int s ) { if ( i != s ) { if ( path [ i ] == -1 ) { cout << " Path ▁ not ▁ found ! ! " ; return ; } printPath ( path , path [ i ] , s ) ; cout << path [ i ] << " ▁ " ; } } int main ( ) { vector < Node * > v ; int n = 4 , s = 0 , e = 5 ; for ( int i = 0 ; i < n ; i ++ ) { Node * a = new Node ( i ) ; v . push_back ( a ) ; } v [ 0 ] -> add_child ( 1 , 1 ) ; v [ 0 ] -> add_child ( 2 , 4 ) ; v [ 1 ] -> add_child ( 2 , 2 ) ; v [ 1 ] -> add_child ( 3 , 6 ) ; v [ 2 ] -> add_child ( 3 , 3 ) ; vector < int > path ( v . size ( ) ) ; vector < int > dist = dijkstraDist ( v , s , path ) ; for ( int i = 0 ; i < dist . size ( ) ; i ++ ) { if ( dist [ i ] == infi ) { cout << i << " ▁ and ▁ " << s << " ▁ are ▁ not ▁ connected " << endl ; continue ; } cout << " Distance ▁ of ▁ " << i << " th ▁ vertex ▁ from ▁ source ▁ vertex ▁ " << s << " ▁ is : ▁ " << dist [ i ] << endl ; } return 0 ; }