-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopologicalSort.cpp
More file actions
76 lines (70 loc) · 1.67 KB
/
topologicalSort.cpp
File metadata and controls
76 lines (70 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
struct node {
char value;
vector<node*> out_neighbours;
vector<int> in_neighbours;
node(char v) {
value = v;
in_neighbours.clear();
out_neighbours.clear();
}
};
void addNode(vector<node*> &graph, char value) {
node *n = new node(value);
graph.push_back(n);
}
void addDirectedEdge(vector<node*> graph, int endVertices[2]) {
if(endVertices[0] <= graph.size() && endVertices[1] <= graph.size()) {
graph[endVertices[0] - 1]->out_neighbours.push_back(graph[endVertices[1] - 1]);
graph[endVertices[1] - 1]->in_neighbours.push_back(endVertices[0] - 1);
}
}
void topologicalSort(vector<node*> graph) {
vector<bool> visited;
visited.assign(graph.size(), false);
node *temp;
bool looper = false;
while(!looper) {
looper = true;
for(int i = 0; i < graph.size(); i++) {
if(visited[i] == false) {
bool canVisit = true;
for(int j = 0; j < graph[i]->in_neighbours.size(); j++) {
if(!visited[graph[i]->in_neighbours[j]]) {
canVisit = false;
}
}
if(canVisit) {
cout << graph[i]->value;
visited[i] = true;
}
}
if(!visited[i]) {
looper = false;
}
}
}
}
int main() {
vector<node*> graph;
int n, e[] = {0, 0};
char v;
cout << "Enter number of vertices: ";
cin >> n;
for(int i = 0; i < n; i++) {
cout << "Enter value of vertice " << i + 1 << ": ";
cin >> v;
addNode(graph, v);
}
cout << "Enter number of directed edges: ";
cin >> n;
for(int i = 0; i < n; i++) {
cout << "Enter end vertices indices of directed edge " << i + 1 << ": ";
cin >> e[0] >> e[1];
addDirectedEdge(graph, e);
}
cout << "Topological Sort: ";
topologicalSort(graph);
cout << endl;
}