|
| 1 | +// A C++ program to print topological |
| 2 | +// output 5 4 2 3 1 0 |
| 3 | +// sorting of a DAG |
| 4 | +#include <iostream> |
| 5 | +#include <list> |
| 6 | +#include <stack> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +// Class to represent a graph |
| 10 | +class Graph { |
| 11 | +// No. of vertices' |
| 12 | +int V; |
| 13 | + |
| 14 | +// Pointer to an array containing adjacency listsList |
| 15 | +list<int>* adj; |
| 16 | + |
| 17 | +// A function used by topologicalSort |
| 18 | +void topologicalSortUtil(int v, bool visited[], |
| 19 | +stack<int>& Stack); |
| 20 | + |
| 21 | +public: |
| 22 | +// Constructor |
| 23 | +Graph(int V); |
| 24 | + |
| 25 | +// function to add an edge to graph |
| 26 | +void addEdge(int v, int w); |
| 27 | + |
| 28 | +// prints a Topological Sort of |
| 29 | +// the complete graph |
| 30 | +void topologicalSort(); |
| 31 | +}; |
| 32 | + |
| 33 | +Graph::Graph(int V) |
| 34 | +{ |
| 35 | +this->V = V; |
| 36 | +adj = new list<int>[V]; |
| 37 | +} |
| 38 | + |
| 39 | +void Graph::addEdge(int v, int w) |
| 40 | +{ |
| 41 | +// Add w to v’s list. |
| 42 | +adj[v].push_back(w); |
| 43 | +} |
| 44 | + |
| 45 | +// A recursive function used by topologicalSort |
| 46 | +void Graph::topologicalSortUtil(int v, bool visited[], |
| 47 | +stack<int>& Stack) |
| 48 | +{ |
| 49 | +// Mark the current node as visited. |
| 50 | +visited[v] = true; |
| 51 | + |
| 52 | +// Recur for all the vertices |
| 53 | +// adjacent to this vertex |
| 54 | +list<int>::iterator i; |
| 55 | +for (i = adj[v].begin(); i != adj[v].end(); ++i) |
| 56 | +if (!visited[*i]) |
| 57 | +topologicalSortUtil(*i, visited, Stack); |
| 58 | + |
| 59 | +// Push current vertex to stack |
| 60 | +// which stores result |
| 61 | +Stack.push(v); |
| 62 | +} |
| 63 | + |
| 64 | +// The function to do Topological Sort. |
| 65 | +// It uses recursive topologicalSortUtil() |
| 66 | +void Graph::topologicalSort() |
| 67 | +{ |
| 68 | +stack<int> Stack; |
| 69 | + |
| 70 | +// Mark all the vertices as not visited |
| 71 | +bool* visited = new bool[V]; |
| 72 | +for (int i = 0; i < V; i++) |
| 73 | +visited[i] = false; |
| 74 | + |
| 75 | +// Call the recursive helper function |
| 76 | +// to store Topological |
| 77 | +// Sort starting from all |
| 78 | +// vertices one by one |
| 79 | +for (int i = 0; i < V; i++) |
| 80 | +if (visited[i] == false) |
| 81 | +topologicalSortUtil(i, visited, Stack); |
| 82 | + |
| 83 | +// Print contents of stack |
| 84 | +while (Stack.empty() == false) { |
| 85 | +cout << Stack.top() << " "; |
| 86 | +Stack.pop(); |
| 87 | +} |
| 88 | +} |
| 89 | + |
| 90 | +// Driver Code |
| 91 | +int main() |
| 92 | +{ |
| 93 | +// Create a graph given in the above diagram |
| 94 | +Graph g(6); |
| 95 | +g.addEdge(5, 2); |
| 96 | +g.addEdge(5, 0); |
| 97 | +g.addEdge(4, 0); |
| 98 | +g.addEdge(4, 1); |
| 99 | +g.addEdge(2, 3); |
| 100 | +g.addEdge(3, 1); |
| 101 | + |
| 102 | +cout << "Following is a Topological Sort of the given " |
| 103 | +"graph \n"; |
| 104 | + |
| 105 | +// Function Call |
| 106 | +g.topologicalSort(); |
| 107 | + |
| 108 | +return 0; |
| 109 | +} |
0 commit comments