Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 920d8ad

Browse files
Merge pull request fnplus#236 from orientor/TopologicalSort
Added Topological Sort Algorithm in C++.
2 parents 1b079a0 + c956830 commit 920d8ad

File tree

1 file changed

+94
-0
lines changed

1 file changed

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /