-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTopological_Sort.cpp
56 lines (55 loc) · 1.2 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
51
52
53
54
55
56
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
//拓扑排序算法
vector<int> Topological_Sort(vector<vector<int>>& Adj, vector<int>& InDegree){
stack<int> S;
vector<int> res;
//将入度为0的节点入栈
for (int i = 0; i < InDegree.size();i++){
if (InDegree[i] == 0)
S.push(i);
}
//深度搜索,拓扑排序
while (!S.empty()){
int node = S.top();
S.pop();
//搜索与节点相连的点
for (int i : Adj[node]){
InDegree[i]--;//更新节点的入度信息
//入度为0了,加入结果数组
if (InDegree[i] == 0)
S.push(i);
}
//node成为孤立节点
res.push_back(node);
}
return res;
}
int main(){
cout << "输入节点数和有向边的个数:";
int n, m;
cin >> n >> m;
vector<vector<int>> Adj(n);//邻接表
cout << Adj.size() << endl;
vector<int> InDegree(n, 0);//计算节点的入度
cout << "输入有向边的起点,终点(节点编号从0开始):" << endl;
int input1, input2;
for (int i = 0; i < m; i++){
cin >> input1 >> input2 ;
Adj[input1].push_back(input2);
InDegree[input2]++;
}
vector<int> SortRes = Topological_Sort(Adj, InDegree);
if (SortRes.size() != n){
cout << "图中存在环路,不能拓扑排序" << endl;
}
else{
for (int i : SortRes)
cout << i << " ";
cout << endl;
}
system("pause");
return 0;
}