-
Notifications
You must be signed in to change notification settings - Fork 0
/
kahn_cpp.cpp
90 lines (74 loc) · 1.74 KB
/
kahn_cpp.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Graph
{
public:
int V; //顶点vertex个数
vector<vector<int>>adj; //邻接表 adjacency List
Graph(int V)
{
this->V = V;
adj.resize(V);
}
void addEdge(int u,int v)
{
adj[u].push_back(v);
}
vector<int> kahnTopologicalSort()
{
vector<int>inDegree(V,0);//存储每个顶点的入度
for(int u = 0;u < V;u++)
{
for(int v : adj[u])
{
inDegree[v]++;
}
}
queue<int>q; //存储入度为0的顶点(box)
for(int u = 0;u < V;u++)
{
if(inDegree[u] == 0)
q.push(u);
}
vector<int> result; //存储拓扑排序的结果
while(!q.empty())
{
int u = q.front();
q.pop();
result.push_back(u);
for(int v : adj[u])
{
if(--inDegree[v] == 0)
q.push(v);
}
}
if(result.size() != V)
result.clear(); //图中存在环
return result;
}
};
int main()
{
int V = 6;
Graph graph(V);
//添加图的边
graph.addEdge(5, 2);
graph.addEdge(5, 0);
graph.addEdge(4, 0);
graph.addEdge(4, 1);
graph.addEdge(2, 3);
graph.addEdge(3, 1);
vector<int> result = graph.kahnTopologicalSort();
if(result.empty())
cout << "图中存在环" << endl;
else
{
cout << "拓扑排序结果为:";
for(int u : result)
cout << u << " ";
cout << endl;
}
return 0;
}