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

Function topo_sort

graph/code13.cpp:20–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20vector<int> topo_sort(vector<pair<int, int>> vec[], int v)
21{
22 // here we will find topo sort by using bfs code for that
23 vector<int> indegree(v, 0);
24 for (int i = 0; i < v; i++)
25 {
26 for (int j = 0; j < vec[i].size(); j++)
27 {
28 indegree[vec[i][j].first]++;
29 }
30 }
31
32 queue<int> q;
33 for (int i = 0; i < indegree.size(); i++)
34 {
35 if (indegree[i] == 0)
36 {
37 q.push(i);
38 }
39 }
40
41 vector<int> ans;
42 while (q.empty() == false)
43 {
44 int curr = q.front();
45 q.pop();
46
47 ans.push_back(curr);
48 for (int i = 0; i < vec[curr].size(); i++)
49 {
50 int adjacent = vec[curr][i].first;
51 indegree[adjacent]--;
52
53 if (indegree[adjacent] == 0)
54 {
55 q.push(adjacent);
56 }
57 }
58 }
59
60 return ans;
61}
62
63void shortest_dist(vector<pair<int, int>> vec[], vector<int> &sort_vec, int v, int source)
64{

Callers 1

mainFunction · 0.70

Calls 4

push_backMethod · 0.80
sizeMethod · 0.45
pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected