-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtopological-sort.cpp
50 lines (42 loc) · 1.16 KB
/
topological-sort.cpp
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
// This function uses performs a non-recursive topological sort.
//
// Running time: O(|V|^2). If you use adjacency lists (vector<map<int> >),
// the running time is reduced to O(|E|).
//
// INPUT: w[i][j] = 1 if i should come before j, 0 otherwise
// OUTPUT: a permutation of 0,...,n-1 (stored in a vector)
// which represents an ordering of the nodes which
// is consistent with w
//
// If no ordering is possible, false is returned.
#include <iostream>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
typedef double T;
typedef vector<T> VT;
typedef vector<VT> VVT;
typedef vector<int> VI;
typedef vector<VI> VVI;
bool TopologicalSort (const VVI &w, VI &order){
int n = w.size();
VI parents (n);
queue<int> q;
order.clear();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
if (w[j][i]) parents[i]++;
if (parents[i] == 0) q.push (i);
}
while (q.size() > 0){
int i = q.front();
q.pop();
order.push_back (i);
for (int j = 0; j < n; j++) if (w[i][j]){
parents[j]--;
if (parents[j] == 0) q.push (j);
}
}
return (order.size() == n);
}