MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / topo_sort

Function topo_sort

graph/code9.cpp:15–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13}
14
15bool topo_sort(vector<int> vec[], int v)
16{
17 vector<int> indegree(v, 0);
18 for (int i = 0; i < v; i++)
19 {
20 for (int j = 0; j < vec[i].size(); j++)
21 {
22 int adjacent = vec[i][j];
23 indegree[adjacent]++;
24 }
25 }
26 queue<int> q;
27 for (int i = 0; i < indegree.size(); i++)
28 {
29 if (indegree[i] == 0)
30 {
31 q.push(i);
32 }
33 }
34 int count = 0;
35 while (q.empty() == false)
36 {
37 int curr = q.front();
38 q.pop();
39
40 count++;
41 for (int i = 0; i < vec[curr].size(); i++)
42 {
43 int adjacent = vec[curr][i];
44
45 indegree[adjacent]--;
46 if (indegree[adjacent] == 0)
47 {
48 q.push(adjacent);
49 }
50 }
51 }
52 return count == v;
53}
54
55int main()
56{

Callers 1

mainFunction · 0.70

Calls 3

sizeMethod · 0.45
pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected