拓扑排序

拓扑排序

一、拓扑排序

二、AOV网与 AOE

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
91
92
93
94
95
96
97
98
#include<iostream>
#include<cstring>
#include"ADL.h"
#define inp(i,x,y) for(i=x;i<=y;i++)
#define Max_Vertex_Num 20
//-------------------AOV-------------------//
Status FindInDegree(ALGraph G, int *indegree);
Status TopologicalSort(ALGraph G, int *T, int &Ttop);
Status CriticalPath(ALGraph G);
int ve[Max_Vertex_Num]; // 顶点事件最早发生时间
int vl[Max_Vertex_Num]; // 顶点事件最迟发生时间
int main()
{
ALGraph G, GR;
CreateGraph(G, GR);
GraphPrint(G, GR);
CriticalPath(G);
return 0;
}
Status TopologicalSort(ALGraph G, int *T, int &Ttop)
{
// 有向无环图的拓扑排序
//求各顶点时间的最早发生时间ve(全局变量)
//若G无回路,则用T返回G的一个拓扑序列,且函数返回OK,否则返回ERROR
int S[Max_Vertex_Num], Stop = -1, i = 0; /*栈St的指针为top*/
int *indegree = new int[G.vexnum];
if(!FindInDegree(G, indegree)) return false; //indegree顶点入度
inp(i,0,G.vexnum-1)
if(!indegree[i]) S[++Stop] = i;

int count = 0;
cout << "Directed acycline graph topologicalsort:\n";
while(Stop > -1) { /*栈不为空时循环*/
i = S[Stop--]; T[++Ttop] = i; // i号顶点入T栈
count++;
cout << G.Vextices[i].data << " "; // 输出拓扑排序
for(ArcNode* p=G.Vextices[i].firstarc; p; p=p->nextarc){
int k = p->adjvex; // 邻接弧头节点位置
if(--indegree[k]==0) S[++Stop] = k; // 零入度进栈
if(ve[i]+p->weight > ve[k]) ve[k] = ve[i] + p->weight;
}
}
cout << endl;
delete[] indegree;
if(count < G.vexnum) return false; // 有回路
else return true;
}// TopologicalOrder

Status FindInDegree(ALGraph G, int *indegree)
{
int i;
//inp(i,0,G.vexnum-1) indegree[i] = 0;
memset(indegree, 0, G.vexnum*sizeof(int));
inp(i,0,G.vexnum-1){ //扫描邻接表,计算各顶点的入度
for(ArcNode *p=G.Vextices[i].firstarc; p; p=p->nextarc)
indegree[p->adjvex]++;
}
return true;
}
Status CriticalPath(ALGraph G)
{
// G为有向网,输出G的各项关键活动
int T[Max_Vertex_Num], Ttop = -1, j = 0; // 存储拓扑排序序列,模拟栈 //建立用于产生拓扑逆序的栈T
if(!TopologicalSort(G, T, Ttop)){
cout << "This graph has a cicle.\n";
return false; //该有向网有回路返回false
}else cout << "--------------Critical Path Table-----------------\n";
inp(j,0,G.vexnum-1) vl[j] = ve[G.vexnum-1]; //初始化顶点事件的最迟发生时间
while (Ttop > -1){ //按拓扑逆序求各顶点的vl值
j = T[Ttop--];
for(ArcNode *p=G.Vextices[j].firstarc; p; p=p->nextarc){
int k = p->adjvex, dut = p->weight; // dut<j,k>
// vl[k]已经求得,与j各邻接弧k的所有vl[k]-dut要最小,保证耗时最长的相对于vl[k]能准时
if(vl[k] - dut < vl[j]) vl[j] = vl[k] - dut; // min(vl(k)-dut<j,k>)
}// for
} //while

// 求e、l和关键活动
cout << "Activities on edge:\n";
cout << "---------------------------------------------\n";
cout << "| Edge | duration | e | l | l-e | CriActi |\n"; // title
inp(j,0,G.vexnum-1)
for (ArcNode *p = G.Vextices[j].firstarc; p; p = p->nextarc) {
int k = p->adjvex, dut= p->weight;
int ee = ve[j], el = vl[k]-dut;
string tag = (ve[j]==(vl[k]-dut))? "Yes":"No ";
cout << "|<" << G.Vextices[j].data << ", " << G.Vextices[k].data << ">| ";
cout << dut << " | " << ee << " | " << el << " | " << el-ee << " | " << tag << " |\n"; //输出关键活动
}
cout << "---------------------------------------------\n";
cout << "Events on vertex:\n";
cout << "--------------------\n";
cout << "| Vertex | ve | vl |\n"; //title
inp(j,0,G.vexnum-1)
cout << "| " << G.Vextices[j].data << " | " << ve[j] << " | " << vl[j] << " |" << endl;
cout << "--------------------\n";
return true;
}//CriticalPath

给出一个算例演示:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//------------------Here is a demo----------------//
/*
Input the type of graph(0:DG,1:DN,2:UDG,3:UDN):
1
Input Vextices number, Arcs number and Incinformation(1:Yes,0:NO):
6 8 0
Input Graph Node Sign:
V1 V2 V3 V4 V5 V6
Input two vertices and weight of the edges:
V1 V2 3
V2 V5 3
V2 V4 2
V1 V3 2
V3 V4 4
V4 V6 2
V3 V6 3
V5 V6 1
----------------Output Graph Information-----------------
Graph Kind: directed nets
Vextices number: 6 Arcs number: 8

----------------Vextices Vector Information----------------
The Nodes Path:
Out path: V1-[3]->V2 V1-[2]->V3
In path:
V1 Vextice Degree: 2 Outdegree: 2 Indegree: 0

Out path: V2-[3]->V5 V2-[2]->V4
In path: V1-[3]->-V2
V2 Vextice Degree: 3 Outdegree: 2 Indegree: 1

Out path: V3-[4]->V4 V3-[3]->V6
In path: V1-[2]->-V3
V3 Vextice Degree: 3 Outdegree: 2 Indegree: 1

Out path: V4-[2]->V6
In path: V2-[2]->-V4 V3-[4]->-V4
V4 Vextice Degree: 3 Outdegree: 1 Indegree: 2

Out path: V5-[1]->V6
In path: V2-[3]->-V5
V5 Vextice Degree: 2 Outdegree: 1 Indegree: 1

Out path:
In path: V4-[2]->-V6 V3-[3]->-V6 V5-[1]->-V6
V6 Vextice Degree: 3 Outdegree: 0 Indegree: 3

Directed acycline graph topologicalsort:
V1 V3 V2 V4 V5 V6
--------------Critical Path Table-----------------
Activities on edge:
---------------------------------------------
| Edge | duration | e | l | l-e | CriActi |
|<V1, V2>| 3 | 0 | 1 | 1 | No |
|<V1, V3>| 2 | 0 | 0 | 0 | Yes |
|<V2, V5>| 3 | 3 | 4 | 1 | No |
|<V2, V4>| 2 | 3 | 4 | 1 | No |
|<V3, V4>| 4 | 2 | 2 | 0 | Yes |
|<V3, V6>| 3 | 2 | 5 | 3 | No |
|<V4, V6>| 2 | 6 | 6 | 0 | Yes |
|<V5, V6>| 1 | 6 | 7 | 1 | No |
---------------------------------------------
Events on vertex:
--------------------
| Vertex | ve | vl |
| V1 | 0 | 0 |
| V2 | 3 | 4 |
| V3 | 2 | 2 |
| V4 | 6 | 6 |
| V5 | 6 | 7 |
| V6 | 8 | 8 |
--------------------
*/
//----------------Here is another demo------------//
/*
Input the type of graph(0:DG,1:DN,2:UDG,3:UDN):
1
Input Vextices number, Arcs number and Incinformation(1:Yes,0:NO):
9 11 0
Input Graph Node Sign:
V1 V2 V3 V4 V5 V6 V7 V8 V9
Input two vertices and weight of the edges:
V1 V2 6
V1 V3 4
V1 V4 5
V2 V5 1
V3 V5 1
V4 V6 2
V5 V7 9
V5 V8 7
V6 V8 4
V7 V9 2
V8 V9 4
----------------Output Graph Information-----------------
Graph Kind: directed nets
Vextices number: 9 Arcs number: 11

----------------Vextices Vector Information----------------
The Nodes Path:
Out path: V1-[6]->V2 V1-[4]->V3 V1-[5]->V4
In path:
V1 Vextice Degree: 3 Outdegree: 3 Indegree: 0

Out path: V2-[1]->V5
In path: V1-[6]->-V2
V2 Vextice Degree: 2 Outdegree: 1 Indegree: 1

Out path: V3-[1]->V5
In path: V1-[4]->-V3
V3 Vextice Degree: 2 Outdegree: 1 Indegree: 1

Out path: V4-[2]->V6
In path: V1-[5]->-V4
V4 Vextice Degree: 2 Outdegree: 1 Indegree: 1

Out path: V5-[9]->V7 V5-[7]->V8
In path: V2-[1]->-V5 V3-[1]->-V5
V5 Vextice Degree: 4 Outdegree: 2 Indegree: 2

Out path: V6-[4]->V8
In path: V4-[2]->-V6
V6 Vextice Degree: 2 Outdegree: 1 Indegree: 1

Out path: V7-[2]->V9
In path: V5-[9]->-V7
V7 Vextice Degree: 2 Outdegree: 1 Indegree: 1

Out path: V8-[4]->V9
In path: V5-[7]->-V8 V6-[4]->-V8
V8 Vextice Degree: 3 Outdegree: 1 Indegree: 2

Out path:
In path: V7-[2]->-V9 V8-[4]->-V9
V9 Vextice Degree: 2 Outdegree: 0 Indegree: 2

Directed acycline graph topologicalsort:
V1 V4 V6 V3 V2 V5 V8 V7 V9
--------------Critical Path Table-----------------
Activities on edge:
---------------------------------------------
| Edge | duration | e | l | l-e | CriActi |
|<V1, V2>| 6 | 0 | 0 | 0 | Yes |
|<V1, V3>| 4 | 0 | 2 | 2 | No |
|<V1, V4>| 5 | 0 | 3 | 3 | No |
|<V2, V5>| 1 | 6 | 6 | 0 | Yes |
|<V3, V5>| 1 | 4 | 6 | 2 | No |
|<V4, V6>| 2 | 5 | 8 | 3 | No |
|<V5, V7>| 9 | 7 | 7 | 0 | Yes |
|<V5, V8>| 7 | 7 | 7 | 0 | Yes |
|<V6, V8>| 4 | 7 | 10 | 3 | No |
|<V7, V9>| 2 | 16 | 16 | 0 | Yes |
|<V8, V9>| 4 | 14 | 14 | 0 | Yes |
---------------------------------------------
Events on vertex:
--------------------
| Vertex | ve | vl |
| V1 | 0 | 0 |
| V2 | 6 | 6 |
| V3 | 4 | 6 |
| V4 | 5 | 8 |
| V5 | 7 | 7 |
| V6 | 7 | 10 |
| V7 | 16 | 16 |
| V8 | 14 | 14 |
| V9 | 18 | 18 |
--------------------
*/
作者

Mark Stiff

发布于

2022-11-02

更新于

2024-01-21

许可协议


评论