snippets/graph/ShortestPath/acyclicGraph.cpp
void Graph::shortestPath(int s)
{
    stack<int> Stack;
    int dist[V];

    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    for (int i = 0; i < V; i++)
        visited[i] = false;

    // Call the recursive helper function to store Topological Sort
    // starting from all vertices one by one
    for (int i = 0; i < V; i++)
        if (visited[i] == false)
            topologicalSortUtil(i, visited, Stack);

    // Initialize distances to all vertices as infinite and distance
    // to source as 0
    for (int i = 0; i < V; i++)
        dist[i] = INF;
    dist[s] = 0;

    // Process vertices in topological order
    while (Stack.empty() == false)
    {
        // Get the next vertex from topological order
        int u = Stack.top();
        Stack.pop();

        // Update distances of all adjacent vertices
        list<AdjListNode>::iterator i;
        if (dist[u] != INF)
        {
          for (i = adj[u].begin(); i != adj[u].end(); ++i)
             if (dist[i->getV()] > dist[u] + i->getWeight())
                dist[i->getV()] = dist[u] + i->getWeight();
        }
    }

    // Print the calculated shortest distances
    for (int i = 0; i < V; i++)
        (dist[i] == INF)? cout << "INF ": cout << dist[i] << " ";
}
// The function to find shortest paths from given vertex. It uses recursive
// topologicalSortUtil() to get topological sorting of given graph.
The function to find shortest paths from given vertex. It uses recursive
topologicalSortUtil() to get topological sorting of given graph.
The function to find shortest paths from given vertex . It uses recursive topologicalSortUtil () to get topological sorting of given graph .
void Graph::shortestPath(int s)
{
    stack<int> Stack;
    int dist[V];
    bool *visited = new bool[V];
    for (int i = 0; i < V; i++)
        visited[i] = false;
    for (int i = 0; i < V; i++)
        if (visited[i] == false)
            topologicalSortUtil(i, visited, Stack);
    for (int i = 0; i < V; i++)
        dist[i] = INF;
    dist[s] = 0;
    while (Stack.empty() == false)
    {
        int u = Stack.top();
        Stack.pop();
        list<AdjListNode>::iterator i;
        if (dist[u] != INF)
        {
          for (i = adj[u].begin(); i != adj[u].end(); ++i)
             if (dist[i->getV()] > dist[u] + i->getWeight())
                dist[i->getV()] = dist[u] + i->getWeight();
        }
    }
    for (int i = 0; i < V; i++)
        (dist[i] == INF)? cout << "INF ": cout << dist[i] << " ";
}
void Graph :: shortestPath ( int s ) { stack < int > Stack ; int dist [ V ] ; bool * visited = new bool [ V ] ; for ( int i = 0 ; i < V ; i ++ ) visited [ i ] = false ; for ( int i = 0 ; i < V ; i ++ ) if ( visited [ i ] == false ) topologicalSortUtil ( i , visited , Stack ) ; for ( int i = 0 ; i < V ; i ++ ) dist [ i ] = INF ; dist [ s ] = 0 ; while ( Stack . empty ( ) == false ) { int u = Stack . top ( ) ; Stack . pop ( ) ; list < AdjListNode > :: iterator i ; if ( dist [ u ] != INF ) { for ( i = adj [ u ] . begin ( ) ; i != adj [ u ] . end ( ) ; ++ i ) if ( dist [ i -> getV ( ) ] > dist [ u ] + i -> getWeight ( ) ) dist [ i -> getV ( ) ] = dist [ u ] + i -> getWeight ( ) ; } } for ( int i = 0 ; i < V ; i ++ ) ( dist [ i ] == INF ) ? cout << " " : cout << dist [ i ] << " " ; }
The function to find shortest paths from given vertex.
The function to find shortest paths from given vertex .
// Mark all the vertices as not visited // Call the recursive helper function to store Topological Sort // starting from all vertices one by one // Initialize distances to all vertices as infinite and distance // to source as 0 // Process vertices in topological order // Get the next vertex from topological order // Update distances of all adjacent vertices // Print the calculated shortest distances