C++ Program to Find the Longest Path in a DAG - Computer Programming

# Computer Programming

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

## Tuesday, September 26, 2017

Write a C++ Program to Find the Longest Path in a DAG

Program:
#include <iostream>
#include <list>
#include <stack>
#include <limits.h>
#define NINF INT_MIN
using namespace std;

// Graph is represented using adjacency list. Every node of adjacency list
// contains vertex number of the vertex to which edge connects. It also
// contains weight of the edge
{
int v;
int weight;
public:
{
v = _v;
weight = _w;
}
int getV()
{
return v;
}
int getWeight()
{
return weight;
}
};

// Class to represent a graph using adjacency list representation
class Graph
{
int V; // No. of vertices’

// Pointer to an array containing adjacency lists

// A function used by longestPath
void topologicalSortUtil(int v, bool visited[], stack<int> &Stack);
public:
Graph(int V); // Constructor

// function to add an edge to graph
void addEdge(int u, int v, int weight);

// Finds longest distances from given source vertex
void longestPath(int s);
};

Graph::Graph(int V) // Constructor
{
this->V = V;
}

void Graph::addEdge(int u, int v, int weight)
{
}

// A recursive function used by longestPath. See below link for details
// http://www.geeksforgeeks.org/topological-sorting/
void Graph::topologicalSortUtil(int v, bool visited[], stack<int> &Stack)
{
// Mark the current node as visited
visited[v] = true;

// Recur for all the vertices adjacent to this vertex
{
if (!visited[node.getV()])
topologicalSortUtil(node.getV(), visited, Stack);
}

// Push current vertex to stack which stores topological sort
Stack.push(v);
}

// The function to find longest distances from a given vertex. It uses
// recursive topologicalSortUtil() to get topological sorting.
void Graph::longestPath(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] = NINF;
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
if (dist[u] != NINF)
{
if (dist[i->getV()] < dist[u] + i->getWeight())
dist[i->getV()] = dist[u] + i->getWeight();
}
}

// Print the calculated longest distances
for (int i = 0; i < V; i++)
(dist[i] == NINF) ? cout << "INF " : cout << dist[i] << " ";
}

// Driver program to test above functions
int main()
{
// Create a graph given in the above diagram.  Here vertex numbers are
// 0, 1, 2, 3, 4, 5 with following mappings:
// 0=r, 1=s, 2=t, 3=x, 4=y, 5=z
Graph g(6);

int s = 1;
cout << "Following are longest distances from source vertex " << s << " \n";
g.longestPath(s);

return 0;
}

Output:
Following are longest distances from source vertex 1
INF 0 2 9 8 10
------------------
(program exited with code: 0)