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 ; bool 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
using System ; using System . Collections ; using System . Collections . Generic ; class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG { static int infi = 1000000000 ; class Node { public int vertexNumber ; public List < Pair > children ; public Node ( int vertexNumber ) { this . vertexNumber = vertexNumber ; children = new List < Pair > ( ) ; } public void Add_child ( int vNumber , int length ) { Pair p = new Pair ( vNumber , length ) ; children . Add ( p ) ; } } static int [ ] dijkstraDist ( List < Node > g , int s , int [ ] path ) { int [ ] dist = new int [ g . Count ] ; bool [ ] visited = new bool [ g . Count ] ; for ( int i = 0 ; i < g . Count ; i ++ ) { visited [ i ] = false ; path [ i ] = - 1 ; dist [ i ] = infi ; } dist [ s ] = 0 ; path [ s ] = - 1 ; int current = s ; HashSet < int > sett = new HashSet < int > ( ) ; while ( true ) { visited [ current ] = true ; for ( int i = 0 ; i < g [ current ] . children . Count ; i ++ ) { int v = g [ current ] . children [ i ] . first ; if ( visited [ v ] ) continue ; sett . Add ( v ) ; int alt = dist [ current ] + g [ current ] . children [ i ] . second ; if ( alt < dist [ v ] ) { dist [ v ] = alt ; path [ v ] = current ; } } sett . Remove ( current ) ; if ( sett . Count == 0 ) break ; int minDist = infi ; int index = 0 ; foreach ( int a in sett ) { if ( dist [ a ] < minDist ) { minDist = dist [ a ] ; index = a ; } } current = index ; } return dist ; } void printPath ( int [ ] path , int i , int s ) { if ( i != s ) { if ( path [ i ] == - 1 ) { Console . WriteLine ( " Path ▁ not ▁ found ! ! " ) ; return ; } printPath ( path , path [ i ] , s ) ; Console . WriteLine ( path [ i ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { List < Node > v = new List < Node > ( ) ; int n = 4 , s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Node a = new Node ( i ) ; v . Add ( 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 ) ; int [ ] path = new int [ v . Count ] ; int [ ] dist = dijkstraDist ( v , s , path ) ; for ( int i = 0 ; i < dist . Length ; i ++ ) { if ( dist [ i ] == infi ) { Console . Write ( " { 0 } ▁ and ▁ { 1 } ▁ are ▁ not ▁ " + " connected STRNEWLINE " , i , s ) ; continue ; } Console . Write ( " Distance ▁ of ▁ { 0 } th ▁ vertex ▁ " + " from ▁ source ▁ vertex ▁ { 1 } ▁ is : ▁ { 2 } STRNEWLINE " , i , s , dist [ i ] ) ; } } }