前言

算法导论专题的最后一部分,也是数据结构中最难最综合的内容。图论的算法基于离散数学的结论,给出了具体的算法实现过程,属于是计算机解决数学问题的一大典型。

并且在编写图论算法中,大量使用了相对高级的数据结构例如不相交集合以及斐波那契堆等,一个良好的数据结构或许决定着算法的运算速度,这个概念在之前是无论如何也不会想到的。也难怪,数据结构的算法中并不会强调关于图论的实现,属于是超出能力范畴了…

邻接表与邻接矩阵

邻接表数据结构

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
// 边表结点
typedef struct EdgeNode
{
int adjvex; // 邻接点域,存储该顶点对应的下标
EdgeType weight; // 用于存储权值,对于非网图可以不需要
struct EdgeNode *next; // 链域,指向下一个邻接点
} EdgeNode;

// 顶点表结点
typedef struct VextexNode
{
int data; // 顶点域,存储顶点信息
EdgeNode *next; // 边表头指针
int color; // 用于DFS和BFS的颜色标记(白色0:未发现,灰色1:已发现,黑色2:已完成)
int d; // DFS和BFS的发现时间
int f; // DFS的完成时间
struct VextexNode* predecessor; // BFS和DFS的前驱节点
EdgeType key; // 连接MST最小权重边的权重 或 最短路径权重估计上界
} VextexNode;

// 图邻接表表示法
typedef struct GraphAdjList
{
VextexNode* adjList;
int numNodes; // 图中当前顶点数
bool weighted; // 是否为加权图
} GraphAdjList;

邻接矩阵数据结构

1
2
3
4
5
6
7
8
// 图邻接矩阵表示法
typedef struct GraphAdjMatrix
{
int numNodes; // 图中当前顶点数
int* data; // 储存顶点信息
EdgeType** adjarr; // 图的邻接矩阵
bool weighted; // 是否为加权图
}GraphAdjMatrix;

相关功能

无向图邻接表边的数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 输出无向图邻接表边的数量
int GraphAdjList_Edge_Count(GraphAdjList* G)
{
int count_edge = 0;
EdgeNode* node;
for (int i = 0; i < G->numNodes; i++)
{
node = (&G->adjList[i])->next;
while (node != NULL)
{
count_edge++;
node = node->next;
}
}
return count_edge;
}

创建图邻接表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 创建图邻接表
GraphAdjList* Create_GraphAdjList(int numVertices, bool weighted)
{
GraphAdjList* G = (GraphAdjList*)malloc(sizeof(GraphAdjList));
G->numNodes = numVertices;
G->weighted = weighted;
G->adjList = (VextexNode*)malloc(sizeof(VextexNode) * numVertices);

for (int i = 0; i < numVertices; i++)
{
G->adjList[i].data = i;
G->adjList[i].next = NULL;
}
return G;
}

图邻接表添加边

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 图邻接表添加边
void AddEdge_GraphAdjList(GraphAdjList* G, int pre_data, int after_data, EdgeType weight)
{
EdgeNode* newEdge = (EdgeNode*)malloc(sizeof(EdgeNode));
newEdge->adjvex = after_data;
if (G->weighted == 1)
{
newEdge->weight = weight;
}
else
{
newEdge->weight = 1;
}
newEdge->next = G->adjList[pre_data].next;
G->adjList[pre_data].next = newEdge;
}


图邻接表修改边

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 图邻接表修改边
void ChangeEdge_GraphAdjList(GraphAdjList* G, int pre_data, int after_data, EdgeType change_weight)
{
EdgeNode* edge;
edge = G->adjList[pre_data].next;
while (edge != NULL && edge->adjvex != after_data)
{
edge = edge->next;
}
if (edge != NULL)
{
edge->weight += change_weight;
}
else
{
AddEdge_GraphAdjList(G, pre_data, after_data, change_weight); // 没有找到边,添加边
}
}

输出图邻接表

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
// 输出图邻接表
void Print_GraphAdjList(GraphAdjList* graphadjlist)
{
printf("邻接表:\n");
if (graphadjlist->weighted == 1)
{
printf("加权图\n");
}
else
{
printf("非加权图\n");
}
for (int i = 0; i < graphadjlist->numNodes; i++)
{
printf("节点: %d 边: ", i);
EdgeNode* p;
p = graphadjlist->adjList[i].next;
while (p != NULL)
{
printf("(%d, %d) ", i, p->adjvex);
if (graphadjlist->weighted == 1)
{
printf("[%d] ", p->weight);
}
p = p->next;
}
printf("\n");
}
}

删除图邻接表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 删除图邻接表
void Delete_GraphAdjList(GraphAdjList* graphadjlist)
{
for (int i = 0; i < graphadjlist->numNodes; i++)
{
EdgeNode* p;
p = graphadjlist->adjList[i].next;
while (p != NULL)
{
graphadjlist->adjList[i].next = p->next;
free(p);
p = graphadjlist->adjList[i].next;
}
}
free(graphadjlist->adjList);
free(graphadjlist);
}

输出图邻接矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 输出图邻接矩阵
void Print_GraphAdjMatrix(GraphAdjMatrix* graphadjmatrix)
{
printf("邻接矩阵:\n");
if (graphadjmatrix->weighted == 1)
{
printf("加权图\n");
}
else
{
printf("非加权图\n");
}
for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
for (int j = 0; j < graphadjmatrix->numNodes; j++)
{
printf("%d ", graphadjmatrix->adjarr[i][j]);
}
printf("\n");
}
}

删除图邻接表

1
2
3
4
5
6
7
8
9
10
11
12
// 删除图邻接表
void Delete_GraphAdjMatrix(GraphAdjMatrix* graphadjmatrix)
{
for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
free(graphadjmatrix->adjarr[i]);
}
free(graphadjmatrix->adjarr);
free(graphadjmatrix->data);
free(graphadjmatrix);
}

邻接矩阵转化图的邻接表

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
// 邻接矩阵转化图的邻接表
GraphAdjList* GraphMatrix_Transfor_GraphAdjList(GraphAdjMatrix* graphadjmatrix)
{
GraphAdjList* graphadjlist;
graphadjlist = (GraphAdjList*)malloc(sizeof(GraphAdjList));
graphadjlist->numNodes = graphadjmatrix->numNodes;
graphadjlist->weighted = graphadjmatrix->weighted;
graphadjlist->adjList = (VextexNode*)malloc(sizeof(VextexNode) * graphadjlist->numNodes);

for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
graphadjlist->adjList->data = graphadjmatrix->data[i];
graphadjlist->adjList->next = NULL;
EdgeNode* last;
last = NULL;
for (int j = 0; j < graphadjmatrix->numNodes; j++)
{
if (graphadjmatrix->adjarr[i][j] != 0)
{
EdgeNode* edge;
edge = (EdgeNode*)malloc(sizeof(EdgeNode));
edge->adjvex = j;
edge->weight = graphadjmatrix->adjarr[i][j];
edge->next = NULL;
if (last == NULL)
{
graphadjlist->adjList[i].next = edge;
}
else
{
last->next = edge;
}
last = edge;
}
}
}

return graphadjlist;
}

邻接表转化邻接矩阵

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
// 邻接表转化邻接矩阵
GraphAdjMatrix* GraphAdjList_Transfor_GraphMatrix(GraphAdjList* graphadjlist)
{
GraphAdjMatrix* graphadjmatrix;
graphadjmatrix = (GraphAdjMatrix*)malloc(sizeof(GraphAdjMatrix));
graphadjmatrix->numNodes = graphadjlist->numNodes;
graphadjmatrix->weighted = graphadjlist->weighted;
graphadjmatrix->adjarr = (EdgeType **)calloc(graphadjmatrix->numNodes, sizeof(EdgeType *));
for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
graphadjmatrix->adjarr[i] = (EdgeType *)calloc(graphadjmatrix->numNodes, sizeof(EdgeType));
}

for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
EdgeNode* node = graphadjlist->adjList[i].next;
while (node != NULL)
{
graphadjmatrix->adjarr[i][node->adjvex] = node->weight;
node = node->next;
}
}

return graphadjmatrix;
}

广度优先遍历/深度优先遍历

链队列数据结构及功能

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
//链队列结构类型
typedef struct QueueNode
{
VextexNode* node;
QueueNode* next;
}QueueNode;

typedef struct LinkQueue
{
QueueNode* front;//队头指针
QueueNode* rear;//队尾指针
}LinkQueue;

LinkQueue* InitLinkQueue()
{
LinkQueue* q;
q = (LinkQueue*)malloc(sizeof(LinkQueue));
q->front = (QueueNode*)malloc(sizeof(QueueNode));
if (q->front == NULL)
{
printf("开辟空间失败\n");
exit(0);
}
q->rear = q->front;
q->front->next = NULL;
q->front->node = NULL;
return q;
}

//空队 1-非空 0-空
bool EmptyLinkQueue(LinkQueue* q)
{
if (q->front == q->rear)
{
return 0;
}
else
{
return 1;
}
}

//入队
void EnLinkQueue(LinkQueue* q, VextexNode* e)
{
QueueNode* p;
p = (QueueNode*)malloc(sizeof(QueueNode));
if (p == NULL)
{
printf("开辟空间失败\n");
exit(0);
}
p->node = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}

//出队
VextexNode* DeLinkQueue(LinkQueue* q)
{
if (EmptyLinkQueue(q) == 0)
{
printf("队空\n");
return NULL;
}
else
{
VextexNode* node;
QueueNode* queuenode;
queuenode = q->front->next;
node = queuenode->node;
q->front->next = queuenode->next;
if (queuenode == q->rear)
{
q->rear = q->front;
}
free(queuenode);
return node;
}
}

广度优先遍历

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
// 广度优先遍历
void BFS_GraphAdjList(GraphAdjList* graphadjlist, VextexNode* s)
{
// 先标记
for (int i = 0; i < graphadjlist->numNodes; i++)
{
if (i != s->data)
{
graphadjlist->adjList[i].color = 0;
graphadjlist->adjList[i].predecessor = NULL;
graphadjlist->adjList[i].d = -1;
}
}

// 入队s
s->color = 1;
s->d = 0;
s->predecessor = NULL;
LinkQueue* Q;
Q = InitLinkQueue();
EnLinkQueue(Q, s);

while (EmptyLinkQueue(Q) == 1)
{
VextexNode* u;
u = DeLinkQueue(Q);
EdgeNode* p;
p = u->next;
while (p != NULL)
{
VextexNode* v;
v = &(graphadjlist->adjList[p->adjvex]);
// 发现为标记节点
if(v->color == 0)
{
v->color = 1;
v->d = u->d + 1;
v->predecessor = u;
EnLinkQueue(Q, v);
}
p = p->next;
}
u->color = 2; //标黑
}
}

打印广度优先树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 打印广度优先树
void Print_BFTree(GraphAdjList* graphadjlist, VextexNode* s)
{
printf("广度优先遍历树:\n");
for (int i = 0; i < graphadjlist->numNodes; i++)
{
printf("节点: %d 距离 %d 最短路径为 %d : %d ", i, s->data, graphadjlist->adjList[i].d, i);
VextexNode* p;
p = graphadjlist->adjList[i].predecessor;
while (p != NULL)
{
printf("<-- %d ", p->data);
p = p->predecessor;
}
printf("\n");
}
}

深度优先遍历

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
// 深度优先访问
void DFS_Visit(GraphAdjList* graphadjlist, VextexNode* u, int* time)
{
// u被发现,时间戳+1
(*time) ++;
u->d = *time;
u->color = 1;

EdgeNode* p;
p = u->next;
while (p != NULL)
{
VextexNode* v;
v = &(graphadjlist->adjList[p->adjvex]);
if (v->color == 0)
{
v->predecessor = u;
DFS_Visit(graphadjlist, v, time);
}
p = p->next;
}

//全部扫描完成
u->color = 2;
(*time)++;
u->f = *time;

}

// 深度优先遍历
void DFS_GraphAdjList(GraphAdjList* graphadjlist)
{
// 先标记
for (int i = 0; i < graphadjlist->numNodes; i++)
{
graphadjlist->adjList[i].color = 0;
graphadjlist->adjList[i].predecessor = NULL;
}
int time;
time = 0; // 时间戳

for (int i = 0; i < graphadjlist->numNodes; i++)
{
if (graphadjlist->adjList[i].color == 0)
{
DFS_Visit(graphadjlist, &(graphadjlist->adjList[i]), &time);
}
}
}

打印深度优先树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 打印深度优先树
void Print_DFTree(GraphAdjList* graphadjlist)
{
printf("深度优先遍历树:\n");
for (int i = 0; i < graphadjlist->numNodes; i++)
{
printf("节点: %d 发现时间为: %d 终止时间为: %d ,深度优先树路径为: %d ", i, graphadjlist->adjList[i].d, graphadjlist->adjList[i].f, i);
VextexNode* p;
p = graphadjlist->adjList[i].predecessor;
if (graphadjlist->weighted == true)
{
printf(",路径权重为: %d ", graphadjlist->adjList[i].key);
}

printf("%d ", graphadjlist->adjList[i].data);
while (p != NULL)
{
printf("<-- %d ", p->data);
p = p->predecessor;
}
printf("\n");
}
}

最小生成树

不相交集合数据结构及功能

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
// 不相交集合森林表示
typedef struct Set_Tree_Node
{
VextexNode* x;
struct Set_Tree_Node* p;
int rank;
}Set_Tree_Node;

// 建立新的树
Set_Tree_Node* Make_Set_Tree(VextexNode* x)
{
Set_Tree_Node* t;
t = (Set_Tree_Node*)malloc(sizeof(Set_Tree_Node));
t->rank = 0;
t->x = x;
t->p = t;

return t;
}

// 路径压缩
Set_Tree_Node* Find_Set_Tree(Set_Tree_Node* t)
{
if (t->p != t)
{
t->p = Find_Set_Tree(t->p);
}
return t->p;
}

// 按秩合并
void Union_Set_Tree(Set_Tree_Node* x, Set_Tree_Node* y)
{
Set_Tree_Node* t1, *t2;
t1 = Find_Set_Tree(x);
t2 = Find_Set_Tree(y);

if (t1 != t2)
{
if (t1->rank > t2->rank)
{
t2->p = t1;
}
else
{
t1->p = t2;
if (t1->rank == t2->rank)
{
t2->rank ++;
}
}
}
}

边数据结构

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
// 边表示
typedef struct Edge
{
int pre_data;
int afte_data;
EdgeType weight;
}Edge;

// 图邻接表转化边链表
Edge** nonDirected_GraphAdjList_Transfor_EdgeList(GraphAdjList* G)
{
Edge** edgelist;

edgelist = (Edge**)malloc(sizeof(Edge*) * GraphAdjList_Edge_Count(G)/2);
int j = 0;
for (int i = 0; i < G->numNodes; i++)
{
EdgeNode* node;
node = (&G->adjList[i])->next;
while (node != NULL)
{
if (node->adjvex > i)
{
edgelist[j] = (Edge*)malloc(sizeof(Edge));
edgelist[j]->pre_data = i;
edgelist[j]->afte_data = node->adjvex;
edgelist[j]->weight = node->weight;
j++;
}
node = node->next;
}
}

return edgelist;
}

随机快速排序

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
//(随机)快速排序
void Swap_Array_ij(Edge** a, int num, int i, int j)
{
int t1, t2;
t1 = a[i]->pre_data;
a[i]->pre_data = a[j]->pre_data;
a[j]->pre_data = t1;

t2 = a[i]->afte_data;
a[i]->afte_data = a[j]->afte_data;
a[j]->afte_data = t2;

EdgeType t;
t = a[i]->weight;
a[i]->weight = a[j]->weight;
a[j]->weight = t;
}

int Random_Partition(Edge** a, int p, int r)
{
//随机主元
int random_num = rand() % (r - p + 1) + p;
EdgeType x = a[random_num]->weight;
Swap_Array_ij(a, r - p + 1, random_num, r);

int i = p - 1;
for (int j = p; j < r; j++)
{
if (a[j]->weight <= x)
{
i++;
//交换i与j
Swap_Array_ij(a, r - p + 1, i, j);
}
}
//交换i+1与r
Swap_Array_ij(a, r - p + 1, i + 1, r);

return i + 1;
}

void Random_Quick_Sort(Edge** a, int p, int r)
{
if (p < r)
{
int q;
q = Random_Partition(a, p, r);
Random_Quick_Sort(a, p, q - 1);
Random_Quick_Sort(a, q + 1, r);
}
}

Kruskal算法

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
// Kruskal算法
GraphAdjList* MST_Kruskal(GraphAdjList* G)
{
GraphAdjList* A;
A = (GraphAdjList*)malloc(sizeof(GraphAdjList));
A->numNodes = G->numNodes;
A->weighted = G->weighted;
A->adjList = (VextexNode*)malloc(sizeof(VextexNode) * G->numNodes);
for (int i = 0; i < A->numNodes; i++)
{
A->adjList[i].data = G->adjList[i].data; // 设置顶点数据
A->adjList[i].next = NULL; // 初始化为空
}

Set_Tree_Node** set_list;
set_list = (Set_Tree_Node**)malloc(sizeof(Set_Tree_Node*) * A->numNodes);

for (int i = 0; i < A->numNodes; i++)
{
set_list[i] = Make_Set_Tree(&G->adjList[i]);
}

Edge** edgelist;
edgelist = nonDirected_GraphAdjList_Transfor_EdgeList(G);
int edge_count = GraphAdjList_Edge_Count(G)/2;
Random_Quick_Sort(edgelist, 0, edge_count - 1);

int finish_count = 0;
for (int i = 0; i < edge_count; i++)
{
if ((Find_Set_Tree(set_list[edgelist[i]->pre_data]) != Find_Set_Tree(set_list[edgelist[i]->afte_data])) && finish_count < A->numNodes - 1)
{
// 将边添加入A中
AddEdge_GraphAdjList(A, edgelist[i]->pre_data, edgelist[i]->afte_data, edgelist[i]->weight);
AddEdge_GraphAdjList(A, edgelist[i]->afte_data, edgelist[i]->pre_data, edgelist[i]->weight);

// 合并
Union_Set_Tree(set_list[edgelist[i]->pre_data], set_list[edgelist[i]->afte_data]);
finish_count++;
}
}

// 删除边链表
for (int i = 0; i < edge_count; i++)
{
free(edgelist[i]);
}
free(edgelist);
free(set_list);
return A;
}

斐波那契堆数据结构及功能

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// 斐波那契堆节点
typedef struct Fib_Heap_Node
{
VextexNode* x;
Fib_Heap_Node* p; // 父节点
Fib_Heap_Node* child; // 某一个孩子节点
Fib_Heap_Node* left; // 左兄弟
Fib_Heap_Node* right; // 右兄弟
int degree; // 孩子数目(度)
bool mark; // 标记
}Fib_Heap_Node;

// 斐波那契堆
typedef struct Fib_Heap
{
int size;
Fib_Heap_Node* min_node;
}Fib_Heap;

// 创建斐波那契堆
Fib_Heap* Create_Fib_Heap()
{
Fib_Heap* H;
H = (Fib_Heap*)malloc(sizeof(Fib_Heap));
H->min_node = NULL;
H->size = 0;
return H;
}

// 递归删除斐波那契堆
void Free_Fib_Heap_Node(Fib_Heap_Node* node)
{
if (node != NULL)
{
Fib_Heap_Node* start = node;
do {
Fib_Heap_Node* child = node->child;
Fib_Heap_Node* next = node->right;

// 递归释放子节点
Free_Fib_Heap_Node(child);

// 释放当前节点
free(node);

node = next;
} while (node != start);
}
}

void Free_Fib_Heap(Fib_Heap* H)
{
if (H != NULL) {
// 释放所有根节点
Free_Fib_Heap_Node(H->min_node);

// 释放堆结构本身
free(H);
}
}

// 斐波那契堆树根的个数
int Fib_Heap_Root_Count(Fib_Heap* H)
{
if (H->min_node == NULL)
{
return 0;
}
else
{
int count = 1;
Fib_Heap_Node* p;
p = H->min_node->right;
while (p != H->min_node)
{
count++;
p = p->right;
}
return count;
}
}

// 插入根节点 插在最小节点与根节点右边节点之间
void Fib_Heap_Insert_Root(Fib_Heap* H, Fib_Heap_Node* node)
{
// 只有一个根
if (H->min_node->right == H->min_node)
{
H->min_node->right = node;
H->min_node->left = node;
node->left = H->min_node;
node->right = H->min_node;
}
else
{
node->p = NULL;
Fib_Heap_Node* p;
p = H->min_node->right;

H->min_node->right = node;
p->left = node;
node->left = H->min_node;
node->right = p;
}
node->p = NULL;
}

// 插入节点
Fib_Heap_Node* Fib_Heap_Insert(Fib_Heap* H, VextexNode* x)
{
Fib_Heap_Node* node;
node = (Fib_Heap_Node*)malloc(sizeof(Fib_Heap_Node));
node->x = x;
node->child = NULL;
node->degree = 0;
node->mark = 0;

// 堆空
if (H->min_node == NULL)
{
H->min_node = node;
node->p = NULL;
node->left = node;
node->right = node;
}
else
{
// 插入节点为根
Fib_Heap_Insert_Root(H, node);

if (x->key < H->min_node->x->key)
{
H->min_node = node; // 改变min指针
}
}
H->size ++;

return node;
}

// 合并堆
Fib_Heap* Fib_Heap_Union(Fib_Heap* H1, Fib_Heap* H2)
{
Fib_Heap* H;
H = Create_Fib_Heap();
H->min_node = H1->min_node;
H->size = H1->size + H2->size;
// 根合并
if (H1->min_node == NULL)
{
// 跳过
}
else if (H->min_node->right == H->min_node) // H1只有一棵树
{
if (H2->min_node->right == H2->min_node) // H2只有一棵树
{
H->min_node->right = H2->min_node;
H->min_node->left = H2->min_node;
H2->min_node->right = H->min_node;
H2->min_node->left = H->min_node;
}
else
{
Fib_Heap_Insert_Root(H2, H->min_node);
}
}
else // H1有多棵树
{
if (H2->min_node->right == H2->min_node) // H2只有一棵树
{
Fib_Heap_Insert_Root(H, H2->min_node);
}
else
{
Fib_Heap_Node* p,* q;
p = H->min_node->right;
q = H2->min_node->left;

H->min_node->right = H2->min_node;
H2->min_node->left = H->min_node;
q->right = p;
p->left = q;
}
}

if ((H1->min_node == NULL) || (H2->min_node && H2->min_node->x->key < H1->min_node->x->key))
{
H->min_node = H2->min_node;
}
free(H1);
free(H2);
return H;
}

// 链接根节点 把y接为x的儿子
void Fib_Heap_Link(Fib_Heap* H, Fib_Heap_Node* y, Fib_Heap_Node* x)
{
// 把y从根节点中移除
y->left->right = y->right;
y->right->left = y->left;

// 把y填入x的孩子中
y->p = x;
Fib_Heap_Node* p;
p = x->child;
if (p == NULL) // x原来没孩子
{
x->child = y;
y->left = y;
y->right = y;
}
else
{
p->right->left = y;
y->right = p->right;
p->right = y;
y->left = p;
}
x->degree ++;

y->mark = 0;
}

// 合并根链表
void Fib_Heap_Consolidate(Fib_Heap* H)
{
Fib_Heap_Node** A;
A = (Fib_Heap_Node**)malloc(sizeof(Fib_Heap_Node*) * H->size);
for (int i = 0; i < H->size; i++)
{
A[i] = NULL;
}
Fib_Heap_Node** root_list;
int count;
count = Fib_Heap_Root_Count(H);
Fib_Heap_Node* p;
p = H->min_node;
Fib_Heap_Node* x, *y;
int d;
for (int i = 0; i < count; i++)
{
x = p;
p = p->right;
d = x->degree; // 当前节点的度数
while (A[d] != NULL)
{
y = A[d];
// 比谁小,然后交换
if (x->x->key > y->x->key)
{
Fib_Heap_Node* t;
t = x;
x = y;
y = t;
}
Fib_Heap_Link(H, y, x);
A[d] = NULL; //清空
d++; //存入下一个
}
A[d] = x;
}

// 根据 A 重建 H
H->min_node = NULL;
for (int i = 0; i < H->size; i++)
{
if (A[i] != NULL)
{
if (H->min_node == NULL)
{
H->min_node = A[i];
H->min_node->right = H->min_node;
H->min_node->left = H->min_node;
}
else
{
Fib_Heap_Insert_Root(H, A[i]);
if (A[i]->x->key < H->min_node->x->key)
{
H->min_node = A[i];
}
}
}
}

free(A);
}

// 提取最小节点
VextexNode* Fib_Heap_Extract_Min(Fib_Heap* H)
{
Fib_Heap_Node* z;
z = H->min_node;
if (z != NULL)
{
if (z->child != NULL)
{
// 令z的孩子都为根
Fib_Heap_Node* child, *next;
child = z->child;
for (int i = 0; i < z->degree; i++)
{
next = child->right;
Fib_Heap_Insert_Root(H, child);
child = next;
}
}

VextexNode* element;
element = z->x;

// z为唯一节点
if (H->size == 1)
{
H->min_node = NULL;
}
else
{
// 从斐波那契堆移除z
z->left->right = z->right;
z->right->left = z->left;

H->min_node = z->right;
Fib_Heap_Consolidate(H);
}
H->size--;

free(z);
return element;
}
else
{
printf("空堆");
return NULL;
}
}

// 切断 切断x与父节点y,使x为根节点
void Fib_Heap_Cut(Fib_Heap* H, Fib_Heap_Node* x, Fib_Heap_Node* y)
{
// x为y的孩子指针
if (y->child == x)
{
// y只有一个孩子
if (x->right == x)
{
y->child = NULL;
Fib_Heap_Insert_Root(H, x);
}
else
{
y->child = x->right;
x->right->left = x->left;
x->left->right = x->right;
Fib_Heap_Insert_Root(H, x);
}
}
else
{
x->right->left = x->left;
x->left->right = x->right;
Fib_Heap_Insert_Root(H, x);
}
x->mark = 0;
}

// 级联切断
void Fib_Heap_Cascading_Cut(Fib_Heap* H, Fib_Heap_Node* y)
{
Fib_Heap_Node* z;
z = y->p;
// y非根节点
if (z != NULL)
{
if (y->mark == 0)
{
y->mark = 1;
}
else
{
Fib_Heap_Cut(H, y, z);
Fib_Heap_Cascading_Cut(H, z);
}
}
}

// 关键字减值
void Fib_Heap_Decrease_Node(Fib_Heap* H, Fib_Heap_Node* x, EdgeType k)
{
if (k > x->x->key)
{
printf("不能让值变大\n");
}
else
{
x->x->key = k;
Fib_Heap_Node *y;
y = x->p;

if (y != NULL && x->x->key < y->x->key)
{
Fib_Heap_Cut(H, x, y);
Fib_Heap_Cascading_Cut(H, y);
}
if (x->x->key < H->min_node->x->key)
{
H->min_node = x;
}
}
}

Prim算法

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
// Prim算法
GraphAdjList* MST_Prim(GraphAdjList* G, VextexNode* r)
{
GraphAdjList* A;
A = (GraphAdjList*)malloc(sizeof(GraphAdjList));
A->numNodes = G->numNodes;
A->weighted = G->weighted;
A->adjList = (VextexNode*)malloc(sizeof(VextexNode) * G->numNodes);

EdgeNode* node;
node = G->adjList[r->data].next;
if (node == NULL)
{
printf("图不连通\n");
Delete_GraphAdjList(A);
return NULL;
}
else
{
bool *vextexlist; // 标记已经发现的顶点
vextexlist = (bool*)calloc(G->numNodes, sizeof(bool));

Fib_Heap_Node** heap_node_list;
heap_node_list = (Fib_Heap_Node**)malloc(sizeof(Fib_Heap_Node*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
heap_node_list[i] = NULL;
(&G->adjList[i])->key = INT_MAX;
}

Fib_Heap* H;
H = Create_Fib_Heap();
r->key = 0;
heap_node_list[r->data] = Fib_Heap_Insert(H, r);

while (H->min_node != NULL)
{
VextexNode* u;
u = Fib_Heap_Extract_Min(H);
vextexlist[u->data] = 1;

EdgeNode* edge;
edge = u->next;
while (edge != NULL)
{
if (vextexlist[edge->adjvex] != 1 && edge->weight <= (&G->adjList[edge->adjvex])->key)
{
if (heap_node_list[edge->adjvex] == NULL)
{
(&G->adjList[edge->adjvex])->key = edge->weight;
heap_node_list[edge->adjvex] = Fib_Heap_Insert(H, &G->adjList[edge->adjvex]);
}
else
{
Fib_Heap_Decrease_Node(H, heap_node_list[edge->adjvex], edge->weight);
}
}
edge = edge->next;
}

if (u != r)
{
edge = u->next;
while (edge != NULL)
{
if (vextexlist[edge->adjvex] == 1 && edge->weight == u->key)
{
break;
}
edge = edge->next;
}
// 将边添加入A中
AddEdge_GraphAdjList(A, u->data, edge->adjvex, edge->weight);
AddEdge_GraphAdjList(A, edge->adjvex, u->data, edge->weight);
}
}
Free_Fib_Heap(H);
free(vextexlist);
free(heap_node_list);

return A;
}
}

单源最短路径

松弛边

1
2
3
4
5
6
7
8
9
10
// 松弛边 u -> v
void Relax_Edge(VextexNode* u, VextexNode* v, EdgeType weight)
{
if (u->key != INT_MAX && v->key > u->key + weight)
{
v->key = u->key + weight;
v->predecessor = u;
}
}

Bellman_Ford算法

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
// Bellman_Ford算法
bool Bellman_Ford(GraphAdjList* G, VextexNode* s)
{
// 初始化
for (int i = 0; i < G->numNodes; i++)
{
G->adjList[i].predecessor = NULL;
G->adjList[i].key = INT_MAX;
}
s->key = 0;

for (int k = 0; k < G->numNodes - 1; k++)
{
for (int i = 0; i < G->numNodes; i++)
{
EdgeNode* edge = G->adjList[i].next;
while (edge != NULL)
{
Relax_Edge(&G->adjList[i], &G->adjList[edge->adjvex], edge->weight);
edge = edge->next;
}
}
}

// 检查是否有负环路
for (int i = 0; i < G->numNodes; i++)
{
EdgeNode* edge;
edge = G->adjList[i].next;
while (edge != NULL)
{
if(G->adjList[edge->adjvex].key > G->adjList[i].key + edge->weight)
{
return false;
}
edge = edge->next;
}
}
return true;
}

Dijkstra算法

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
// Dijkstra算法
void Dijkstra(GraphAdjList* G, VextexNode* s)
{
// 初始化
for (int i = 0; i < G->numNodes; i++)
{
G->adjList[i].predecessor = NULL;
G->adjList[i].key = INT_MAX;
}
s->key = 0;

Fib_Heap_Node** heap_node_list;
heap_node_list = (Fib_Heap_Node**)malloc(sizeof(Fib_Heap_Node*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
heap_node_list[i] = NULL;
}

// 与s连接的点集合
Fib_Heap* H;
H = Create_Fib_Heap();
heap_node_list[s->data] = Fib_Heap_Insert(H, s);

while (H->min_node != NULL)
{
VextexNode* u;
u = Fib_Heap_Extract_Min(H);

EdgeNode* edge;
edge = u->next;
while (edge != NULL)
{
if (heap_node_list[edge->adjvex] != NULL)
{
Relax_Edge(u, &G->adjList[edge->adjvex], edge->weight);
if ((&G->adjList[edge->adjvex])->predecessor == u)
{
Fib_Heap_Decrease_Node(H, heap_node_list[edge->adjvex], u->key + edge->weight);
}
}
else
{
G->adjList[edge->adjvex].key = u->key + edge->weight;
G->adjList[edge->adjvex].predecessor = u;
heap_node_list[edge->adjvex] = Fib_Heap_Insert(H, &G->adjList[edge->adjvex]);
}
edge = edge->next;
}
}
Free_Fib_Heap(H);
free(heap_node_list);
}

节点对最短路径

最短路径数据结构及功能

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
typedef struct Graph_Shortest_Paths
{
int numNodes; // 节点个数
EdgeType** D; // 路径权重
int** Pai; // 前驱图
}Graph_Shortest_Paths;

// 删除节点对最短路径
void Delete_Graph_Shortest_Paths(Graph_Shortest_Paths* P)
{
for (int i = 0; i < P->numNodes; i++)
{
free(P->D[i]);
free(P->Pai[i]);
}

free(P);
}

// 输出节点对最短路径
void Print_Graph_Shortest_Paths(Graph_Shortest_Paths* P)
{
printf("节点对最短路径权重\n");
for (int i = 0; i < P->numNodes; i++)
{
for (int j = 0; j < P->numNodes; j++)
{
printf("%d ", P->D[i][j]);
}
printf("\n");
}

printf("节点对最短路径前驱\n");
for (int i = 0; i < P->numNodes; i++)
{
for (int j = 0; j < P->numNodes; j++)
{
printf("%d ", P->Pai[i][j]);
}
printf("\n");
}
}

// 初始化节点对最短路径
Graph_Shortest_Paths* Init__Graph_Shortest_Paths(EdgeType** W, int numNodes)
{
Graph_Shortest_Paths* PP;
PP = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
PP->numNodes = numNodes;

EdgeType** L;
int** Pai;
L = (EdgeType**)malloc(sizeof(EdgeType*) * numNodes);
Pai = (int**)malloc(sizeof(int*) * numNodes);
for (int i = 0; i < numNodes; i++)
{
L[i] = (EdgeType*)malloc(sizeof(EdgeType) * numNodes);
Pai[i] = (int*)malloc(sizeof(int) * numNodes);
for (int j = 0; j < numNodes; j++)
{
L[i][j] = W[i][j];
if (i == j || W[i][j] == INT_MAX)
{
Pai[i][j] = -1;
}
else
{
Pai[i][j] = i;
}
}
}

PP->D = L;
PP->Pai = Pai;
return PP;
}

自底向上最短路径权重算法

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
// 自底向上最短路径权重算法
Graph_Shortest_Paths* Extend_Shortest_Paths(EdgeType** W, Graph_Shortest_Paths* P)
{
EdgeType** LL;
int** PPai;
LL = (EdgeType**)malloc(sizeof(EdgeType*) * P->numNodes);
PPai = (int**)malloc(sizeof(int*) * P->numNodes);
for (int i = 0; i < P->numNodes; i++)
{
LL[i] = (EdgeType*)malloc(sizeof(EdgeType) * P->numNodes);
PPai[i] = (int*)malloc(sizeof(int) * P->numNodes);
for (int j = 0; j < P->numNodes; j++)
{
PPai[i][j] = P->Pai[i][j];
}
}

Graph_Shortest_Paths* PP;
PP = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
PP->numNodes = P->numNodes;
PP->D = LL;
PP->Pai = PPai;

for (int i = 0; i < P->numNodes; i++)
{
for (int j = 0; j < P->numNodes; j++)
{
if (i == j)
{
LL[i][j] = 0;
}
else
{
LL[i][j] = INT_MAX;
for (int k = 0; k < P->numNodes; k++)
{
if (i != j && k != j && P->D[i][k] != INT_MAX && W[k][j] != INT_MAX && P->D[i][k] + W[k][j] < LL[i][j])
{
LL[i][j] = P->D[i][k] + W[k][j];
// 快速算法
if (P->D == W)
{
PPai[i][j] = P->Pai[k][j];
}
else
{
PPai[i][j] = k;
}
}
}
}

}
}

return PP;
}

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
// 慢速算法
Graph_Shortest_Paths* Slow_All_Pairs_Shortest_Paths(GraphAdjList* G)
{
//构造边权重矩阵
EdgeType** W;
W = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
W[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
if (i == j)
{
W[i][j] = 0;
}
else
{
W[i][j] = INT_MAX;
}
}
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
W[i][edge->adjvex] = edge->weight;
edge = edge->next;
}
}

Graph_Shortest_Paths* P, *Pm, *t;
P = Init__Graph_Shortest_Paths(W, G->numNodes);

for (int m = 1; m < G->numNodes - 1; m++)
{
Pm = Extend_Shortest_Paths(W, P);
t = P;
P = Pm;
Delete_Graph_Shortest_Paths(t);
}

for (int i = 0; i < G->numNodes; i++)
{
free(W[i]);
}
free(W);

return P;
}

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
// 快速算法
Graph_Shortest_Paths* Faster_All_Pairs_Shortest_Paths(GraphAdjList* G)
{
//构造边权重矩阵
EdgeType** W;
W = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
W[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
if (i == j)
{
W[i][j] = 0;
}
else
{
W[i][j] = INT_MAX;
}
}
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
W[i][edge->adjvex] = edge->weight;
edge = edge->next;
}
}

Graph_Shortest_Paths* P, *Pm, *t;
P = Init__Graph_Shortest_Paths(W, G->numNodes);

int m = 1;
while (m < G->numNodes)
{
Pm = Extend_Shortest_Paths(P->D, P);
t = P;
P = Pm;
Delete_Graph_Shortest_Paths(t);
m = 2 * m;
}

for (int i = 0; i < G->numNodes; i++)
{
free(W[i]);
}
free(W);

return P;
}

Floyd_Warshall算法

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
// Floyd_Warshall算法
Graph_Shortest_Paths* Floyd_Warshall(GraphAdjList* G)
{
//构造边权重矩阵
EdgeType** D, **Dn;
int** Pai, ** PPai;
D = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
Dn = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
Pai = (int**)malloc(sizeof(int*) * G->numNodes);
PPai = (int**)malloc(sizeof(int*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
D[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
Dn[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
Pai[i] = (int*)malloc(sizeof(int) * G->numNodes);
PPai[i] = (int*)malloc(sizeof(int) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
if (i == j)
{
D[i][j] = 0;
}
else
{
D[i][j] = INT_MAX;
}
PPai[i][j] = -1;
}
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
D[i][edge->adjvex] = edge->weight;
PPai[i][edge->adjvex] = i;
edge = edge->next;
}
}

Graph_Shortest_Paths* PP;
PP = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
PP->numNodes = G->numNodes;
PP->D = D;
PP->Pai = Pai;

for (int k = 0; k < G->numNodes; k++)
{
for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
if (D[i][k] != INT_MAX && D[k][j] != INT_MAX && D[i][k] + D[k][j] < D[i][j])
{
D[i][j] = D[i][k] + D[k][j];
Pai[i][j] = PPai[k][j];
}
else
{
Pai[i][j] = PPai[i][j];
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
PPai[i][j] = Pai[i][j];
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
free(PPai[i]);
}
free(PPai);

return PP;
}

传递闭包

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
// 传递闭包
bool** Graph_Transitive_Closure(GraphAdjList* G)
{
// 初始化T
bool** T, ** TT;
T = (bool**)malloc(sizeof(bool*) * G->numNodes);
TT = (bool**)malloc(sizeof(bool*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
T[i] = (bool*)calloc(G->numNodes, sizeof(bool));
TT[i] = (bool*)calloc(G->numNodes, sizeof(bool));
T[i][i] = TT[i][i] = true;
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
T[i][edge->adjvex] = TT[i][edge->adjvex] = true;
edge = edge->next;
}
}

for (int k = 0; k < G->numNodes; k++)
{
for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
if (T[i][j] == 0 && (TT[i][k] == true && TT[k][j] == true))
{
T[i][j] = true;
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
TT[i][j] = T[i][j];
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
free(TT[i]);
}
free(TT);

return T;
}

Johnson算法

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
// Johnson算法
Graph_Shortest_Paths* Graph_Johnson(GraphAdjList* G)
{
// 增加点
GraphAdjList* GG;
GG = Create_GraphAdjList(G->numNodes + 1, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(GG, i, p->adjvex, p->weight);
p = p->next;
}
AddEdge_GraphAdjList(GG, GG->numNodes - 1, i, 0);
}

Graph_Shortest_Paths* P;
P = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
P->numNodes = G->numNodes;
P->D = NULL;
P->Pai = NULL;

if (Bellman_Ford(GG, &GG->adjList[GG->numNodes - 1]) == false)
{
printf("有负环路\n");
}
else
{
EdgeType* h;
h = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
h[i] = GG->adjList[i].key;
}
// 更新边
GraphAdjList* Gh;
Gh = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < Gh->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Gh, i, p->adjvex, p->weight + h[i] - h[p->adjvex]);
p = p->next;
}
}

P->D = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
P->Pai = (int**)malloc(sizeof(int*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
P->D[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
P->Pai[i] = (int*)malloc(sizeof(int) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
P->D[i][j] = INT_MAX;
}
P->Pai[i][i] = -1;
}

for (int i = 0; i < Gh->numNodes; i++)
{
Dijkstra(Gh, &Gh->adjList[i]);
for (int j = 0; j < Gh->numNodes; j++)
{
P->D[i][j] = Gh->adjList[j].key - h[i] + h[j];
if (i != j)
{
P->Pai[i][j] = Gh->adjList[j].predecessor->data;
}
}
}

free(h);
Delete_GraphAdjList(Gh);
}
return P;
}

最大流

Ford_Fulkerson

深度优先遍历寻找增广路径

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
// 深度优先遍历寻找增广路径
bool DFS_FindAugmentingPath(GraphAdjList* G, int u, int t, bool* visited, int* path)
{
if (u == t)
{
return true; // 如果到达汇点,返回成功
}
visited[u] = 1; // 标记当前节点为已访问

EdgeNode* edge;
edge = G->adjList[u].next;
while (edge != NULL)
{
int v = edge->adjvex;
if (!visited[v] && edge->weight > 0) // 如果未访问且残存容量大于0
{
path[v] = u; // 记录路径
if (DFS_FindAugmentingPath(G, v, t, visited, path))
{
return true; // 如果找到一条路径,返回成功
}
}
edge = edge->next;
}
return false; // 未找到路径,返回失败
}

增广路径的残存容量

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
// 增广路径的残存容量
EdgeType Augmenting_Path_Capacity(GraphAdjList* G, int* path, int s, int t)
{
int path_flow = INT_MAX;
for (int v = t; v != s; v = path[v])
{
// 前驱节点
int u = path[v];
// 查找边(u, v)的残存容量
EdgeNode* edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v) //找到节点v
{
if (path_flow > edge->weight)
{
path_flow = edge->weight;
}
break;
}
edge = edge->next;
}
}
return path_flow;
}

Ford_Fulkerson算法

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
// Ford_Fulkerson算法
GraphAdjList* Ford_Fulkerson(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

bool* visit_list;
visit_list = (bool*)malloc(sizeof(bool) * G->numNodes);
int* pre_path;
pre_path = (int*)malloc(sizeof(int) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
visit_list[i] = 0;
pre_path[i] = -1;
}

EdgeType path_capacity;
while (DFS_FindAugmentingPath(Ef, s, t, visit_list, pre_path))
{
path_capacity = Augmenting_Path_Capacity(Ef, pre_path, s, t);
// 更新流网络与残差网络
for (int v = t; pre_path[v] != -1; v = pre_path[v]) // 遍历path路径上的节点
{
int u = pre_path[v];
// 更新流
EdgeNode* edge;
edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
ChangeEdge_GraphAdjList(Flow, u, v, path_capacity);
break;
}
edge = edge->next;
}
if (edge == NULL)
{
ChangeEdge_GraphAdjList(Flow, v, u, -path_capacity);
}

// 更新残差网络
ChangeEdge_GraphAdjList(Ef, u, v, -path_capacity);
ChangeEdge_GraphAdjList(Ef, v, u, path_capacity);
}
for (int i = 0; i < G->numNodes; i++)
{
visit_list[i] = 0;
pre_path[i] = -1;
}
}

free(visit_list);
free(pre_path);
Delete_GraphAdjList(Ef); // 删除残差网络

return Flow;
}

Edmonds_Karp

广度优先遍历寻找增广路径

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
// 广度优先遍历寻找增广路径
bool BFS_FindAugmentingPath(GraphAdjList* G, int s, int t, int* path)
{
LinkQueue* q = InitLinkQueue(); // 初始化队列
bool visited[G->numNodes]; // 访问标记数组
for (int i = 0; i < G->numNodes; i++)
{
visited[i] = false;
path[i] = -1; // 初始化父节点数组
}

visited[s] = true; // 标记源点为已访问
EnLinkQueue(q, &G->adjList[s]); // 源点入队

while (EmptyLinkQueue(q)) // 队列非空
{
VextexNode* uNode = DeLinkQueue(q); // 出队
int u = uNode->data; // 获取当前顶点编号

EdgeNode* e = uNode->next; // 获取邻接边
while (e != NULL)
{
int v = e->adjvex; // 邻接顶点编号
if (!visited[v] && e->weight > 0) // 如果未访问且边权重(容量)大于0
{
visited[v] = true;
path[v] = u; // 记录路径
EnLinkQueue(q, &G->adjList[v]); // 邻接顶点入队
if (v == t) // 如果到达汇点
{
free(q->front); // 清理队列
free(q);
return true; // 找到增广路径
}
}
e = e->next; // 移动到下一条边
}
}

free(q->front); // 清理队列
free(q);
return false; // 未找到增广路径
}

Edmonds_Karp算法

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
// Edmonds_Karp算法
GraphAdjList* Edmonds_Karp(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

int* pre_path;
pre_path = (int*)malloc(sizeof(int) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
pre_path[i] = -1;
}

EdgeType path_capacity;
while (BFS_FindAugmentingPath(Ef, s, t, pre_path))
{
path_capacity = Augmenting_Path_Capacity(Ef, pre_path, s, t);
// 更新流网络与残差网络
for (int v = t; pre_path[v] != -1; v = pre_path[v]) // 遍历path路径上的节点
{
int u = pre_path[v];
// 更新流
EdgeNode* edge;
edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
ChangeEdge_GraphAdjList(Flow, u, v, path_capacity);
break;
}
edge = edge->next;
}
if (edge == NULL)
{
ChangeEdge_GraphAdjList(Flow, v, u, -path_capacity);
}

// 更新残差网络
ChangeEdge_GraphAdjList(Ef, u, v, -path_capacity);
ChangeEdge_GraphAdjList(Ef, v, u, path_capacity);
}
for (int i = 0; i < G->numNodes; i++)
{
pre_path[i] = -1;
}
}

free(pre_path);
Delete_GraphAdjList(Ef); // 删除残差网络

return Flow;
}

重贴标签算法

溢出节点链表

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
// 溢出节点链表
typedef struct OverflowingNode{
int vertexIndex; // 存储顶点索引
OverflowingNode* next; // 指向下一个活跃节点
}OverflowingNode;

// 创建空头节点
OverflowingNode* Init_OverflowingList()
{
OverflowingNode* head = (OverflowingNode*)malloc(sizeof(OverflowingNode));
head->vertexIndex = -1; // 空头节点不存储有效的顶点索引
head->next = NULL;
return head;
}

// 判断空链表
bool Empty_OverflowingList(OverflowingNode* head)
{
if (head->next == NULL)
{
return true;
}
else
{
return false;
}
}

// 头插法添加节点
void Add_OverflowingNode(OverflowingNode* head, int vertexIndex)
{
OverflowingNode* newNode;
newNode = (OverflowingNode*)malloc(sizeof(OverflowingNode));
newNode->vertexIndex = vertexIndex;
// 插入到头节点之后
newNode->next = head->next;
head->next = newNode;
}

// 移除溢出节点
void Remove_OverflowingNode(OverflowingNode* head, int vertexIndex)
{
OverflowingNode* prev = head;
OverflowingNode* current = head->next;
while (current != NULL)
{
if (current->vertexIndex == vertexIndex)
{
prev->next = current->next;
free(current);
break;
}
prev = current;
current = current->next;
}
}

// 删除溢出节点链表
void Delete_OverflowingList(OverflowingNode* head)
{
OverflowingNode* current = head;
while (current != NULL)
{
OverflowingNode* temp = current;
current = current->next;
free(temp);
}
}

Push算法

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
// Push算法
void Push(GraphAdjList* G, GraphAdjList* Ef, GraphAdjList* Flow, int u, int v)
{
EdgeNode* edge;
edge = Ef->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
break;
}
edge = edge->next;
}

EdgeType delta;
delta = (edge->weight < Ef->adjList[u].key) ? edge->weight : Ef->adjList[u].key;

edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
ChangeEdge_GraphAdjList(Flow, u, v, delta);
ChangeEdge_GraphAdjList(Ef, u, v, -delta);
ChangeEdge_GraphAdjList(Ef, v, u, delta);
break;
}
edge = edge->next;
}
if (edge == NULL)
{
ChangeEdge_GraphAdjList(Flow, v, u, -delta);
ChangeEdge_GraphAdjList(Ef, u, v, -delta);
ChangeEdge_GraphAdjList(Ef, v, u, delta);
}

Ef->adjList[u].key -= delta;
Ef->adjList[v].key += delta;
}

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
// 判断能否使用Push算法
bool exists_Push(GraphAdjList* Ef, int u, int v)
{
EdgeNode* edge;
edge = Ef->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
break;
}
edge = edge->next;
}

if (edge != NULL && edge->weight > 0)
{
if (Ef->adjList[u].f == Ef->adjList[v].f + 1)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

Relabel算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Relabel算法
void Relabel(GraphAdjList* Ef, int u)
{
int min_height = INT_MAX; // 初始化为最大整数,以确保能找到最小的高度
for (EdgeNode* edge = Ef->adjList[u].next; edge != NULL; edge = edge->next) // 只考虑残余容量大于0的边
{
if (edge->weight > 0)
{
int neighbor_height = Ef->adjList[edge->adjvex].f;
if (neighbor_height < min_height)
{
min_height = neighbor_height;
}
}
}
// 只有当找到至少一个有效的邻接节点时,才更新高度
if (min_height != INT_MAX)
{
Ef->adjList[u].f = min_height + 1;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
// 判断能否使用Relabel算法
bool exists_Relabel(GraphAdjList* Ef, int u)
{
for (EdgeNode* edge = Ef->adjList[u].next; edge != NULL; edge = edge->next)
{
if (Ef->adjList[edge->adjvex].f <= Ef->adjList[u].f && edge->weight > 0)
{
return false;
}
}
return true;
}

Push_Relabel算法

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
// Push_Relabel算法
GraphAdjList* Generic_Push_Relabel(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

// 创建残差网络
GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

// 初始化
for (int i = 0; i < Ef->numNodes; i++)
{
Ef->adjList[i].f = 0;
Ef->adjList[i].key = 0;
}
Ef->adjList[s].f = Ef->numNodes;

// 溢出节点链表
OverflowingNode* overflowinglist;
overflowinglist = Init_OverflowingList();

// 初始化预流
EdgeNode* edge;
edge = Ef->adjList[s].next;
for (EdgeNode* edge = Ef->adjList[s].next; edge != NULL; edge = edge->next)
{
EdgeType flow = edge->weight;
AddEdge_GraphAdjList(Flow, s, edge->adjvex, flow);
ChangeEdge_GraphAdjList(Ef, s, edge->adjvex, -flow);
ChangeEdge_GraphAdjList(Ef, edge->adjvex, s, flow);

Ef->adjList[edge->adjvex].key = flow;
Ef->adjList[s].key -= flow;
Add_OverflowingNode(overflowinglist, edge->adjvex);
}

while (!Empty_OverflowingList(overflowinglist))
{
OverflowingNode* currentNode;
currentNode = overflowinglist->next;
while (currentNode != NULL)
{
int u = currentNode->vertexIndex;

// 尝试对所有邻接节点v进行Push操作
bool pushed = false;
for (EdgeNode* edge = Ef->adjList[u].next; edge != NULL; edge = edge->next)
{
int v = edge->adjvex;
if (exists_Push(Ef, u, v))
{
Push(G, Ef, Flow, u, v);
pushed = true;

// 如果v变成了新的活跃节点,添加到活跃节点列表
if (Ef->adjList[v].key > 0 && v != s && v != t)
{
Add_OverflowingNode(overflowinglist, v);
}
break; // 只要成功进行一次Push,就跳出循环
}
}

// 如果没有成功进行Push操作,则尝试Relabel
if (!pushed)
{
Relabel(Ef, u);
}

// 移动到下一个活跃节点
currentNode = currentNode->next;

// 检查u是否仍然是活跃节点
if (Ef->adjList[u].key <= 0)
{
Remove_OverflowingNode(overflowinglist, u); // 移除不再活跃的节点u
}
}
}

Delete_GraphAdjList(Ef);
free(overflowinglist);
return Flow;
}

释放溢出节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 释放溢出节点
void Discharge(GraphAdjList* G, GraphAdjList* Ef, GraphAdjList* Flow, int u)
{
EdgeNode* currentNode;
currentNode = Ef->adjList[u].next;
while (Ef->adjList[u].key > 0)
{
if (currentNode == NULL) // 重贴标签
{
Ef->adjList[u].f += 1;
currentNode = Ef->adjList[u].next;
}
else if (Ef->adjList[u].f == Ef->adjList[currentNode->adjvex].f + 1 && currentNode->weight > 0) // push许可边
{
Push(G, Ef, Flow, u, currentNode->adjvex);
}
else
{
currentNode = currentNode->next;
}
}
}

前置重贴标签算法

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
// 前置重贴标签算法
GraphAdjList* Relabel_To_Front(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

// 创建残差网络
GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

// 初始化
for (int i = 0; i < Ef->numNodes; i++)
{
Ef->adjList[i].f = 0;
Ef->adjList[i].key = 0;
}
Ef->adjList[s].f = Ef->numNodes;

// 溢出节点链表
OverflowingNode* OverflowingList;
OverflowingList = Init_OverflowingList();
for (int i = 0; i < G->numNodes; i++)
{
if (i != s && i != t)
{
Add_OverflowingNode(OverflowingList, i);
}
}

// 初始化预流
EdgeNode* edge;
edge = Ef->adjList[s].next;
for (EdgeNode* edge = Ef->adjList[s].next; edge != NULL; edge = edge->next)
{
EdgeType flow = edge->weight;
AddEdge_GraphAdjList(Flow, s, edge->adjvex, flow);
ChangeEdge_GraphAdjList(Ef, s, edge->adjvex, -flow);
ChangeEdge_GraphAdjList(Ef, edge->adjvex, s, flow);

Ef->adjList[edge->adjvex].key = flow;
Ef->adjList[s].key -= flow;
}

OverflowingNode* u,* pre;
u = OverflowingList->next;
pre = OverflowingList;
int old_height;
while (u != NULL)
{
if (Ef->adjList[u->vertexIndex].key > 0)
{
old_height = Ef->adjList[u->vertexIndex].f;
Discharge(G, Ef, Flow, u->vertexIndex);
int old_index = u->vertexIndex;

if (Ef->adjList[u->vertexIndex].f > old_height)
{
// 移动u到表头
pre->next = u->next;
u->next = OverflowingList->next;
OverflowingList->next = u;

pre = u;
u = u->next;
}
}
else
{
u = u->next;
pre = pre->next;
}
}

Delete_GraphAdjList(Ef);
Delete_OverflowingList(OverflowingList);

return Flow;
}

总代码

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
#include <limits.h>
#include <math.h>

typedef int EdgeType; // 边上的权值类型应由用户定义

// 边表结点
typedef struct EdgeNode
{
int adjvex; // 邻接点域,存储该顶点对应的下标
EdgeType weight; // 用于存储权值,对于非网图可以不需要
struct EdgeNode *next; // 链域,指向下一个邻接点
} EdgeNode;

// 顶点表结点
typedef struct VextexNode
{
int data; // 顶点域,存储顶点信息
EdgeNode *next; // 边表头指针
int color; // 用于DFS和BFS的颜色标记(白色0:未发现,灰色1:已发现,黑色2:已完成)
int d; // DFS和BFS的发现时间
int f; // DFS的完成时间
struct VextexNode* predecessor; // BFS和DFS的前驱节点
EdgeType key; // 连接MST最小权重边的权重 或 最短路径权重估计上界
} VextexNode;

// 图邻接表表示法
typedef struct GraphAdjList
{
VextexNode* adjList;
int numNodes; // 图中当前顶点数
bool weighted; // 是否为加权图
} GraphAdjList;

// 图邻接矩阵表示法
typedef struct GraphAdjMatrix
{
int numNodes; // 图中当前顶点数
int* data; // 储存顶点信息
EdgeType** adjarr; // 图的邻接矩阵
bool weighted; // 是否为加权图
}GraphAdjMatrix;

// 输出无向图邻接表边的数量
int GraphAdjList_Edge_Count(GraphAdjList* G)
{
int count_edge = 0;
EdgeNode* node;
for (int i = 0; i < G->numNodes; i++)
{
node = (&G->adjList[i])->next;
while (node != NULL)
{
count_edge++;
node = node->next;
}
}
return count_edge;
}

// 创建图邻接表
GraphAdjList* Create_GraphAdjList(int numVertices, bool weighted)
{
GraphAdjList* G = (GraphAdjList*)malloc(sizeof(GraphAdjList));
G->numNodes = numVertices;
G->weighted = weighted;
G->adjList = (VextexNode*)malloc(sizeof(VextexNode) * numVertices);

for (int i = 0; i < numVertices; i++)
{
G->adjList[i].data = i;
G->adjList[i].next = NULL;
}
return G;
}

// 图邻接表添加边
void AddEdge_GraphAdjList(GraphAdjList* G, int pre_data, int after_data, EdgeType weight)
{
EdgeNode* newEdge = (EdgeNode*)malloc(sizeof(EdgeNode));
newEdge->adjvex = after_data;
if (G->weighted == 1)
{
newEdge->weight = weight;
}
else
{
newEdge->weight = 1;
}
newEdge->next = G->adjList[pre_data].next;
G->adjList[pre_data].next = newEdge;
}

// 图邻接表修改边
void ChangeEdge_GraphAdjList(GraphAdjList* G, int pre_data, int after_data, EdgeType change_weight)
{
EdgeNode* edge;
edge = G->adjList[pre_data].next;
while (edge != NULL && edge->adjvex != after_data)
{
edge = edge->next;
}
if (edge != NULL)
{
edge->weight += change_weight;
}
else
{
AddEdge_GraphAdjList(G, pre_data, after_data, change_weight); // 没有找到边,添加边
}
}

// 输出图邻接表
void Print_GraphAdjList(GraphAdjList* graphadjlist)
{
printf("邻接表:\n");
if (graphadjlist->weighted == 1)
{
printf("加权图\n");
}
else
{
printf("非加权图\n");
}
for (int i = 0; i < graphadjlist->numNodes; i++)
{
printf("节点: %d 边: ", i);
EdgeNode* p;
p = graphadjlist->adjList[i].next;
while (p != NULL)
{
printf("(%d, %d) ", i, p->adjvex);
if (graphadjlist->weighted == 1)
{
printf("[%d] ", p->weight);
}
p = p->next;
}
printf("\n");
}
}

// 删除图邻接表
void Delete_GraphAdjList(GraphAdjList* graphadjlist)
{
for (int i = 0; i < graphadjlist->numNodes; i++)
{
EdgeNode* p;
p = graphadjlist->adjList[i].next;
while (p != NULL)
{
graphadjlist->adjList[i].next = p->next;
free(p);
p = graphadjlist->adjList[i].next;
}
}
free(graphadjlist->adjList);
free(graphadjlist);
}

// 输出图邻接矩阵
void Print_GraphAdjMatrix(GraphAdjMatrix* graphadjmatrix)
{
printf("邻接矩阵:\n");
if (graphadjmatrix->weighted == 1)
{
printf("加权图\n");
}
else
{
printf("非加权图\n");
}
for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
for (int j = 0; j < graphadjmatrix->numNodes; j++)
{
printf("%d ", graphadjmatrix->adjarr[i][j]);
}
printf("\n");
}
}

// 删除图邻接表
void Delete_GraphAdjMatrix(GraphAdjMatrix* graphadjmatrix)
{
for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
free(graphadjmatrix->adjarr[i]);
}
free(graphadjmatrix->adjarr);
free(graphadjmatrix->data);
free(graphadjmatrix);
}

// 邻接矩阵转化图的邻接表
GraphAdjList* GraphMatrix_Transfor_GraphAdjList(GraphAdjMatrix* graphadjmatrix)
{
GraphAdjList* graphadjlist;
graphadjlist = (GraphAdjList*)malloc(sizeof(GraphAdjList));
graphadjlist->numNodes = graphadjmatrix->numNodes;
graphadjlist->weighted = graphadjmatrix->weighted;
graphadjlist->adjList = (VextexNode*)malloc(sizeof(VextexNode) * graphadjlist->numNodes);

for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
graphadjlist->adjList->data = graphadjmatrix->data[i];
graphadjlist->adjList->next = NULL;
EdgeNode* last;
last = NULL;
for (int j = 0; j < graphadjmatrix->numNodes; j++)
{
if (graphadjmatrix->adjarr[i][j] != 0)
{
EdgeNode* edge;
edge = (EdgeNode*)malloc(sizeof(EdgeNode));
edge->adjvex = j;
edge->weight = graphadjmatrix->adjarr[i][j];
edge->next = NULL;
if (last == NULL)
{
graphadjlist->adjList[i].next = edge;
}
else
{
last->next = edge;
}
last = edge;
}
}
}

return graphadjlist;
}

// 邻接表转化邻接矩阵
GraphAdjMatrix* GraphAdjList_Transfor_GraphMatrix(GraphAdjList* graphadjlist)
{
GraphAdjMatrix* graphadjmatrix;
graphadjmatrix = (GraphAdjMatrix*)malloc(sizeof(GraphAdjMatrix));
graphadjmatrix->numNodes = graphadjlist->numNodes;
graphadjmatrix->weighted = graphadjlist->weighted;
graphadjmatrix->adjarr = (EdgeType **)calloc(graphadjmatrix->numNodes, sizeof(EdgeType *));
for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
graphadjmatrix->adjarr[i] = (EdgeType *)calloc(graphadjmatrix->numNodes, sizeof(EdgeType));
}

for (int i = 0; i < graphadjmatrix->numNodes; i++)
{
EdgeNode* node = graphadjlist->adjList[i].next;
while (node != NULL)
{
graphadjmatrix->adjarr[i][node->adjvex] = node->weight;
node = node->next;
}
}

return graphadjmatrix;
}

//链队列结构类型
typedef struct QueueNode
{
VextexNode* node;
QueueNode* next;
}QueueNode;

typedef struct LinkQueue
{
QueueNode* front;//队头指针
QueueNode* rear;//队尾指针
}LinkQueue;

LinkQueue* InitLinkQueue()
{
LinkQueue* q;
q = (LinkQueue*)malloc(sizeof(LinkQueue));
q->front = (QueueNode*)malloc(sizeof(QueueNode));
if (q->front == NULL)
{
printf("开辟空间失败\n");
exit(0);
}
q->rear = q->front;
q->front->next = NULL;
q->front->node = NULL;
return q;
}

//空队 1-非空 0-空
bool EmptyLinkQueue(LinkQueue* q)
{
if (q->front == q->rear)
{
return 0;
}
else
{
return 1;
}
}

//入队
void EnLinkQueue(LinkQueue* q, VextexNode* e)
{
QueueNode* p;
p = (QueueNode*)malloc(sizeof(QueueNode));
if (p == NULL)
{
printf("开辟空间失败\n");
exit(0);
}
p->node = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}

//出队
VextexNode* DeLinkQueue(LinkQueue* q)
{
if (EmptyLinkQueue(q) == 0)
{
printf("队空\n");
return NULL;
}
else
{
VextexNode* node;
QueueNode* queuenode;
queuenode = q->front->next;
node = queuenode->node;
q->front->next = queuenode->next;
if (queuenode == q->rear)
{
q->rear = q->front;
}
free(queuenode);
return node;
}
}

// 广度优先遍历
void BFS_GraphAdjList(GraphAdjList* graphadjlist, VextexNode* s)
{
// 先标记
for (int i = 0; i < graphadjlist->numNodes; i++)
{
if (i != s->data)
{
graphadjlist->adjList[i].color = 0;
graphadjlist->adjList[i].predecessor = NULL;
graphadjlist->adjList[i].d = -1;
}
}

// 入队s
s->color = 1;
s->d = 0;
s->predecessor = NULL;
LinkQueue* Q;
Q = InitLinkQueue();
EnLinkQueue(Q, s);

while (EmptyLinkQueue(Q) == 1)
{
VextexNode* u;
u = DeLinkQueue(Q);
EdgeNode* p;
p = u->next;
while (p != NULL)
{
VextexNode* v;
v = &(graphadjlist->adjList[p->adjvex]);
// 发现为标记节点
if(v->color == 0)
{
v->color = 1;
v->d = u->d + 1;
v->predecessor = u;
EnLinkQueue(Q, v);
}
p = p->next;
}
u->color = 2; //标黑
}
}

// 打印广度优先树
void Print_BFTree(GraphAdjList* graphadjlist, VextexNode* s)
{
printf("广度优先遍历树:\n");
for (int i = 0; i < graphadjlist->numNodes; i++)
{
printf("节点: %d 距离 %d 最短路径为 %d : %d ", i, s->data, graphadjlist->adjList[i].d, i);
VextexNode* p;
p = graphadjlist->adjList[i].predecessor;
while (p != NULL)
{
printf("<-- %d ", p->data);
p = p->predecessor;
}
printf("\n");
}
}

// 深度优先访问
void DFS_Visit(GraphAdjList* graphadjlist, VextexNode* u, int* time)
{
// u被发现,时间戳+1
(*time) ++;
u->d = *time;
u->color = 1;

EdgeNode* p;
p = u->next;
while (p != NULL)
{
VextexNode* v;
v = &(graphadjlist->adjList[p->adjvex]);
if (v->color == 0)
{
v->predecessor = u;
DFS_Visit(graphadjlist, v, time);
}
p = p->next;
}

//全部扫描完成
u->color = 2;
(*time)++;
u->f = *time;

}

// 深度优先遍历
void DFS_GraphAdjList(GraphAdjList* graphadjlist)
{
// 先标记
for (int i = 0; i < graphadjlist->numNodes; i++)
{
graphadjlist->adjList[i].color = 0;
graphadjlist->adjList[i].predecessor = NULL;
}
int time;
time = 0; // 时间戳

for (int i = 0; i < graphadjlist->numNodes; i++)
{
if (graphadjlist->adjList[i].color == 0)
{
DFS_Visit(graphadjlist, &(graphadjlist->adjList[i]), &time);
}
}
}

// 打印深度优先树
void Print_DFTree(GraphAdjList* graphadjlist)
{
printf("深度优先遍历树:\n");
for (int i = 0; i < graphadjlist->numNodes; i++)
{
printf("节点: %d 发现时间为: %d 终止时间为: %d ,深度优先树路径为: %d ", i, graphadjlist->adjList[i].d, graphadjlist->adjList[i].f, i);
VextexNode* p;
p = graphadjlist->adjList[i].predecessor;
if (graphadjlist->weighted == true)
{
printf(",路径权重为: %d ", graphadjlist->adjList[i].key);
}

printf("%d ", graphadjlist->adjList[i].data);
while (p != NULL)
{
printf("<-- %d ", p->data);
p = p->predecessor;
}
printf("\n");
}
}

// 最小生成树
// 不相交集合森林表示
typedef struct Set_Tree_Node
{
VextexNode* x;
struct Set_Tree_Node* p;
int rank;
}Set_Tree_Node;

// 建立新的树
Set_Tree_Node* Make_Set_Tree(VextexNode* x)
{
Set_Tree_Node* t;
t = (Set_Tree_Node*)malloc(sizeof(Set_Tree_Node));
t->rank = 0;
t->x = x;
t->p = t;

return t;
}

// 路径压缩
Set_Tree_Node* Find_Set_Tree(Set_Tree_Node* t)
{
if (t->p != t)
{
t->p = Find_Set_Tree(t->p);
}
return t->p;
}

// 按秩合并
void Union_Set_Tree(Set_Tree_Node* x, Set_Tree_Node* y)
{
Set_Tree_Node* t1, *t2;
t1 = Find_Set_Tree(x);
t2 = Find_Set_Tree(y);

if (t1 != t2)
{
if (t1->rank > t2->rank)
{
t2->p = t1;
}
else
{
t1->p = t2;
if (t1->rank == t2->rank)
{
t2->rank ++;
}
}
}
}

// 边表示
typedef struct Edge
{
int pre_data;
int afte_data;
EdgeType weight;
}Edge;

// 图邻接表转化边链表
Edge** nonDirected_GraphAdjList_Transfor_EdgeList(GraphAdjList* G)
{
Edge** edgelist;

edgelist = (Edge**)malloc(sizeof(Edge*) * GraphAdjList_Edge_Count(G)/2);
int j = 0;
for (int i = 0; i < G->numNodes; i++)
{
EdgeNode* node;
node = (&G->adjList[i])->next;
while (node != NULL)
{
if (node->adjvex > i)
{
edgelist[j] = (Edge*)malloc(sizeof(Edge));
edgelist[j]->pre_data = i;
edgelist[j]->afte_data = node->adjvex;
edgelist[j]->weight = node->weight;
j++;
}
node = node->next;
}
}

return edgelist;
}

//(随机)快速排序
void Swap_Array_ij(Edge** a, int num, int i, int j)
{
int t1, t2;
t1 = a[i]->pre_data;
a[i]->pre_data = a[j]->pre_data;
a[j]->pre_data = t1;

t2 = a[i]->afte_data;
a[i]->afte_data = a[j]->afte_data;
a[j]->afte_data = t2;

EdgeType t;
t = a[i]->weight;
a[i]->weight = a[j]->weight;
a[j]->weight = t;
}

int Random_Partition(Edge** a, int p, int r)
{
//随机主元
int random_num = rand() % (r - p + 1) + p;
EdgeType x = a[random_num]->weight;
Swap_Array_ij(a, r - p + 1, random_num, r);

int i = p - 1;
for (int j = p; j < r; j++)
{
if (a[j]->weight <= x)
{
i++;
//交换i与j
Swap_Array_ij(a, r - p + 1, i, j);
}
}
//交换i+1与r
Swap_Array_ij(a, r - p + 1, i + 1, r);

return i + 1;
}

void Random_Quick_Sort(Edge** a, int p, int r)
{
if (p < r)
{
int q;
q = Random_Partition(a, p, r);
Random_Quick_Sort(a, p, q - 1);
Random_Quick_Sort(a, q + 1, r);
}
}

// Kruskal算法
GraphAdjList* MST_Kruskal(GraphAdjList* G)
{
GraphAdjList* A;
A = (GraphAdjList*)malloc(sizeof(GraphAdjList));
A->numNodes = G->numNodes;
A->weighted = G->weighted;
A->adjList = (VextexNode*)malloc(sizeof(VextexNode) * G->numNodes);
for (int i = 0; i < A->numNodes; i++)
{
A->adjList[i].data = G->adjList[i].data; // 设置顶点数据
A->adjList[i].next = NULL; // 初始化为空
}

Set_Tree_Node** set_list;
set_list = (Set_Tree_Node**)malloc(sizeof(Set_Tree_Node*) * A->numNodes);

for (int i = 0; i < A->numNodes; i++)
{
set_list[i] = Make_Set_Tree(&G->adjList[i]);
}

Edge** edgelist;
edgelist = nonDirected_GraphAdjList_Transfor_EdgeList(G);
int edge_count = GraphAdjList_Edge_Count(G)/2;
Random_Quick_Sort(edgelist, 0, edge_count - 1);

int finish_count = 0;
for (int i = 0; i < edge_count; i++)
{
if ((Find_Set_Tree(set_list[edgelist[i]->pre_data]) != Find_Set_Tree(set_list[edgelist[i]->afte_data])) && finish_count < A->numNodes - 1)
{
// 将边添加入A中
AddEdge_GraphAdjList(A, edgelist[i]->pre_data, edgelist[i]->afte_data, edgelist[i]->weight);
AddEdge_GraphAdjList(A, edgelist[i]->afte_data, edgelist[i]->pre_data, edgelist[i]->weight);

// 合并
Union_Set_Tree(set_list[edgelist[i]->pre_data], set_list[edgelist[i]->afte_data]);
finish_count++;
}
}

// 删除边链表
for (int i = 0; i < edge_count; i++)
{
free(edgelist[i]);
}
free(edgelist);
free(set_list);
return A;
}

// 斐波那契堆节点
typedef struct Fib_Heap_Node
{
VextexNode* x;
Fib_Heap_Node* p; // 父节点
Fib_Heap_Node* child; // 某一个孩子节点
Fib_Heap_Node* left; // 左兄弟
Fib_Heap_Node* right; // 右兄弟
int degree; // 孩子数目(度)
bool mark; // 标记
}Fib_Heap_Node;

// 斐波那契堆
typedef struct Fib_Heap
{
int size;
Fib_Heap_Node* min_node;
}Fib_Heap;

// 创建斐波那契堆
Fib_Heap* Create_Fib_Heap()
{
Fib_Heap* H;
H = (Fib_Heap*)malloc(sizeof(Fib_Heap));
H->min_node = NULL;
H->size = 0;
return H;
}

// 递归删除斐波那契堆
void Free_Fib_Heap_Node(Fib_Heap_Node* node)
{
if (node != NULL)
{
Fib_Heap_Node* start = node;
do {
Fib_Heap_Node* child = node->child;
Fib_Heap_Node* next = node->right;

// 递归释放子节点
Free_Fib_Heap_Node(child);

// 释放当前节点
free(node);

node = next;
} while (node != start);
}
}

void Free_Fib_Heap(Fib_Heap* H)
{
if (H != NULL) {
// 释放所有根节点
Free_Fib_Heap_Node(H->min_node);

// 释放堆结构本身
free(H);
}
}

// 斐波那契堆树根的个数
int Fib_Heap_Root_Count(Fib_Heap* H)
{
if (H->min_node == NULL)
{
return 0;
}
else
{
int count = 1;
Fib_Heap_Node* p;
p = H->min_node->right;
while (p != H->min_node)
{
count++;
p = p->right;
}
return count;
}
}

// 插入根节点 插在最小节点与根节点右边节点之间
void Fib_Heap_Insert_Root(Fib_Heap* H, Fib_Heap_Node* node)
{
// 只有一个根
if (H->min_node->right == H->min_node)
{
H->min_node->right = node;
H->min_node->left = node;
node->left = H->min_node;
node->right = H->min_node;
}
else
{
node->p = NULL;
Fib_Heap_Node* p;
p = H->min_node->right;

H->min_node->right = node;
p->left = node;
node->left = H->min_node;
node->right = p;
}
node->p = NULL;
}

// 插入节点
Fib_Heap_Node* Fib_Heap_Insert(Fib_Heap* H, VextexNode* x)
{
Fib_Heap_Node* node;
node = (Fib_Heap_Node*)malloc(sizeof(Fib_Heap_Node));
node->x = x;
node->child = NULL;
node->degree = 0;
node->mark = 0;

// 堆空
if (H->min_node == NULL)
{
H->min_node = node;
node->p = NULL;
node->left = node;
node->right = node;
}
else
{
// 插入节点为根
Fib_Heap_Insert_Root(H, node);

if (x->key < H->min_node->x->key)
{
H->min_node = node; // 改变min指针
}
}
H->size ++;

return node;
}

// 合并堆
Fib_Heap* Fib_Heap_Union(Fib_Heap* H1, Fib_Heap* H2)
{
Fib_Heap* H;
H = Create_Fib_Heap();
H->min_node = H1->min_node;
H->size = H1->size + H2->size;
// 根合并
if (H1->min_node == NULL)
{
// 跳过
}
else if (H->min_node->right == H->min_node) // H1只有一棵树
{
if (H2->min_node->right == H2->min_node) // H2只有一棵树
{
H->min_node->right = H2->min_node;
H->min_node->left = H2->min_node;
H2->min_node->right = H->min_node;
H2->min_node->left = H->min_node;
}
else
{
Fib_Heap_Insert_Root(H2, H->min_node);
}
}
else // H1有多棵树
{
if (H2->min_node->right == H2->min_node) // H2只有一棵树
{
Fib_Heap_Insert_Root(H, H2->min_node);
}
else
{
Fib_Heap_Node* p,* q;
p = H->min_node->right;
q = H2->min_node->left;

H->min_node->right = H2->min_node;
H2->min_node->left = H->min_node;
q->right = p;
p->left = q;
}
}

if ((H1->min_node == NULL) || (H2->min_node && H2->min_node->x->key < H1->min_node->x->key))
{
H->min_node = H2->min_node;
}
free(H1);
free(H2);
return H;
}

// 链接根节点 把y接为x的儿子
void Fib_Heap_Link(Fib_Heap* H, Fib_Heap_Node* y, Fib_Heap_Node* x)
{
// 把y从根节点中移除
y->left->right = y->right;
y->right->left = y->left;

// 把y填入x的孩子中
y->p = x;
Fib_Heap_Node* p;
p = x->child;
if (p == NULL) // x原来没孩子
{
x->child = y;
y->left = y;
y->right = y;
}
else
{
p->right->left = y;
y->right = p->right;
p->right = y;
y->left = p;
}
x->degree ++;

y->mark = 0;
}

// 合并根链表
void Fib_Heap_Consolidate(Fib_Heap* H)
{
Fib_Heap_Node** A;
A = (Fib_Heap_Node**)malloc(sizeof(Fib_Heap_Node*) * H->size);
for (int i = 0; i < H->size; i++)
{
A[i] = NULL;
}
Fib_Heap_Node** root_list;
int count;
count = Fib_Heap_Root_Count(H);
Fib_Heap_Node* p;
p = H->min_node;
Fib_Heap_Node* x, *y;
int d;
for (int i = 0; i < count; i++)
{
x = p;
p = p->right;
d = x->degree; // 当前节点的度数
while (A[d] != NULL)
{
y = A[d];
// 比谁小,然后交换
if (x->x->key > y->x->key)
{
Fib_Heap_Node* t;
t = x;
x = y;
y = t;
}
Fib_Heap_Link(H, y, x);
A[d] = NULL; //清空
d++; //存入下一个
}
A[d] = x;
}

// 根据 A 重建 H
H->min_node = NULL;
for (int i = 0; i < H->size; i++)
{
if (A[i] != NULL)
{
if (H->min_node == NULL)
{
H->min_node = A[i];
H->min_node->right = H->min_node;
H->min_node->left = H->min_node;
}
else
{
Fib_Heap_Insert_Root(H, A[i]);
if (A[i]->x->key < H->min_node->x->key)
{
H->min_node = A[i];
}
}
}
}

free(A);
}

// 提取最小节点
VextexNode* Fib_Heap_Extract_Min(Fib_Heap* H)
{
Fib_Heap_Node* z;
z = H->min_node;
if (z != NULL)
{
if (z->child != NULL)
{
// 令z的孩子都为根
Fib_Heap_Node* child, *next;
child = z->child;
for (int i = 0; i < z->degree; i++)
{
next = child->right;
Fib_Heap_Insert_Root(H, child);
child = next;
}
}

VextexNode* element;
element = z->x;

// z为唯一节点
if (H->size == 1)
{
H->min_node = NULL;
}
else
{
// 从斐波那契堆移除z
z->left->right = z->right;
z->right->left = z->left;

H->min_node = z->right;
Fib_Heap_Consolidate(H);
}
H->size--;

free(z);
return element;
}
else
{
printf("空堆");
return NULL;
}
}

// 切断 切断x与父节点y,使x为根节点
void Fib_Heap_Cut(Fib_Heap* H, Fib_Heap_Node* x, Fib_Heap_Node* y)
{
// x为y的孩子指针
if (y->child == x)
{
// y只有一个孩子
if (x->right == x)
{
y->child = NULL;
Fib_Heap_Insert_Root(H, x);
}
else
{
y->child = x->right;
x->right->left = x->left;
x->left->right = x->right;
Fib_Heap_Insert_Root(H, x);
}
}
else
{
x->right->left = x->left;
x->left->right = x->right;
Fib_Heap_Insert_Root(H, x);
}
x->mark = 0;
}

// 级联切断
void Fib_Heap_Cascading_Cut(Fib_Heap* H, Fib_Heap_Node* y)
{
Fib_Heap_Node* z;
z = y->p;
// y非根节点
if (z != NULL)
{
if (y->mark == 0)
{
y->mark = 1;
}
else
{
Fib_Heap_Cut(H, y, z);
Fib_Heap_Cascading_Cut(H, z);
}
}
}

// 关键字减值
void Fib_Heap_Decrease_Node(Fib_Heap* H, Fib_Heap_Node* x, EdgeType k)
{
if (k > x->x->key)
{
printf("不能让值变大\n");
}
else
{
x->x->key = k;
Fib_Heap_Node *y;
y = x->p;

if (y != NULL && x->x->key < y->x->key)
{
Fib_Heap_Cut(H, x, y);
Fib_Heap_Cascading_Cut(H, y);
}
if (x->x->key < H->min_node->x->key)
{
H->min_node = x;
}
}
}

// Prim算法
GraphAdjList* MST_Prim(GraphAdjList* G, VextexNode* r)
{
GraphAdjList* A;
A = (GraphAdjList*)malloc(sizeof(GraphAdjList));
A->numNodes = G->numNodes;
A->weighted = G->weighted;
A->adjList = (VextexNode*)malloc(sizeof(VextexNode) * G->numNodes);

EdgeNode* node;
node = G->adjList[r->data].next;
if (node == NULL)
{
printf("图不连通\n");
Delete_GraphAdjList(A);
return NULL;
}
else
{
bool *vextexlist; // 标记已经发现的顶点
vextexlist = (bool*)calloc(G->numNodes, sizeof(bool));

Fib_Heap_Node** heap_node_list;
heap_node_list = (Fib_Heap_Node**)malloc(sizeof(Fib_Heap_Node*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
heap_node_list[i] = NULL;
(&G->adjList[i])->key = INT_MAX;
}

Fib_Heap* H;
H = Create_Fib_Heap();
r->key = 0;
heap_node_list[r->data] = Fib_Heap_Insert(H, r);

while (H->min_node != NULL)
{
VextexNode* u;
u = Fib_Heap_Extract_Min(H);
vextexlist[u->data] = 1;

EdgeNode* edge;
edge = u->next;
while (edge != NULL)
{
if (vextexlist[edge->adjvex] != 1 && edge->weight <= (&G->adjList[edge->adjvex])->key)
{
if (heap_node_list[edge->adjvex] == NULL)
{
(&G->adjList[edge->adjvex])->key = edge->weight;
heap_node_list[edge->adjvex] = Fib_Heap_Insert(H, &G->adjList[edge->adjvex]);
}
else
{
Fib_Heap_Decrease_Node(H, heap_node_list[edge->adjvex], edge->weight);
}
}
edge = edge->next;
}

if (u != r)
{
edge = u->next;
while (edge != NULL)
{
if (vextexlist[edge->adjvex] == 1 && edge->weight == u->key)
{
break;
}
edge = edge->next;
}
// 将边添加入A中
AddEdge_GraphAdjList(A, u->data, edge->adjvex, edge->weight);
AddEdge_GraphAdjList(A, edge->adjvex, u->data, edge->weight);
}
}
Free_Fib_Heap(H);
free(vextexlist);
free(heap_node_list);

return A;
}
}

// 单源最短路径
// 松弛边 u -> v
void Relax_Edge(VextexNode* u, VextexNode* v, EdgeType weight)
{
if (u->key != INT_MAX && v->key > u->key + weight)
{
v->key = u->key + weight;
v->predecessor = u;
}
}

// Bellman_Ford算法
bool Bellman_Ford(GraphAdjList* G, VextexNode* s)
{
// 初始化
for (int i = 0; i < G->numNodes; i++)
{
G->adjList[i].predecessor = NULL;
G->adjList[i].key = INT_MAX;
}
s->key = 0;

for (int k = 0; k < G->numNodes - 1; k++)
{
for (int i = 0; i < G->numNodes; i++)
{
EdgeNode* edge = G->adjList[i].next;
while (edge != NULL)
{
Relax_Edge(&G->adjList[i], &G->adjList[edge->adjvex], edge->weight);
edge = edge->next;
}
}
}

// 检查是否有负环路
for (int i = 0; i < G->numNodes; i++)
{
EdgeNode* edge;
edge = G->adjList[i].next;
while (edge != NULL)
{
if(G->adjList[edge->adjvex].key > G->adjList[i].key + edge->weight)
{
return false;
}
edge = edge->next;
}
}
return true;
}

// Dijkstra算法
void Dijkstra(GraphAdjList* G, VextexNode* s)
{
// 初始化
for (int i = 0; i < G->numNodes; i++)
{
G->adjList[i].predecessor = NULL;
G->adjList[i].key = INT_MAX;
}
s->key = 0;

Fib_Heap_Node** heap_node_list;
heap_node_list = (Fib_Heap_Node**)malloc(sizeof(Fib_Heap_Node*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
heap_node_list[i] = NULL;
}

// 与s连接的点集合
Fib_Heap* H;
H = Create_Fib_Heap();
heap_node_list[s->data] = Fib_Heap_Insert(H, s);

while (H->min_node != NULL)
{
VextexNode* u;
u = Fib_Heap_Extract_Min(H);

EdgeNode* edge;
edge = u->next;
while (edge != NULL)
{
if (heap_node_list[edge->adjvex] != NULL)
{
Relax_Edge(u, &G->adjList[edge->adjvex], edge->weight);
if ((&G->adjList[edge->adjvex])->predecessor == u)
{
Fib_Heap_Decrease_Node(H, heap_node_list[edge->adjvex], u->key + edge->weight);
}
}
else
{
G->adjList[edge->adjvex].key = u->key + edge->weight;
G->adjList[edge->adjvex].predecessor = u;
heap_node_list[edge->adjvex] = Fib_Heap_Insert(H, &G->adjList[edge->adjvex]);
}
edge = edge->next;
}
}
Free_Fib_Heap(H);
free(heap_node_list);
}

// 节点对最短路径
typedef struct Graph_Shortest_Paths
{
int numNodes; // 节点个数
EdgeType** D; // 路径权重
int** Pai; // 前驱图
}Graph_Shortest_Paths;

// 删除节点对最短路径
void Delete_Graph_Shortest_Paths(Graph_Shortest_Paths* P)
{
for (int i = 0; i < P->numNodes; i++)
{
free(P->D[i]);
free(P->Pai[i]);
}

free(P);
}

// 输出节点对最短路径
void Print_Graph_Shortest_Paths(Graph_Shortest_Paths* P)
{
printf("节点对最短路径权重\n");
for (int i = 0; i < P->numNodes; i++)
{
for (int j = 0; j < P->numNodes; j++)
{
printf("%d ", P->D[i][j]);
}
printf("\n");
}

printf("节点对最短路径前驱\n");
for (int i = 0; i < P->numNodes; i++)
{
for (int j = 0; j < P->numNodes; j++)
{
printf("%d ", P->Pai[i][j]);
}
printf("\n");
}
}

// 初始化节点对最短路径
Graph_Shortest_Paths* Init__Graph_Shortest_Paths(EdgeType** W, int numNodes)
{
Graph_Shortest_Paths* PP;
PP = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
PP->numNodes = numNodes;

EdgeType** L;
int** Pai;
L = (EdgeType**)malloc(sizeof(EdgeType*) * numNodes);
Pai = (int**)malloc(sizeof(int*) * numNodes);
for (int i = 0; i < numNodes; i++)
{
L[i] = (EdgeType*)malloc(sizeof(EdgeType) * numNodes);
Pai[i] = (int*)malloc(sizeof(int) * numNodes);
for (int j = 0; j < numNodes; j++)
{
L[i][j] = W[i][j];
if (i == j || W[i][j] == INT_MAX)
{
Pai[i][j] = -1;
}
else
{
Pai[i][j] = i;
}
}
}

PP->D = L;
PP->Pai = Pai;
return PP;
}

// 自底向上最短路径权重算法
Graph_Shortest_Paths* Extend_Shortest_Paths(EdgeType** W, Graph_Shortest_Paths* P)
{
EdgeType** LL;
int** PPai;
LL = (EdgeType**)malloc(sizeof(EdgeType*) * P->numNodes);
PPai = (int**)malloc(sizeof(int*) * P->numNodes);
for (int i = 0; i < P->numNodes; i++)
{
LL[i] = (EdgeType*)malloc(sizeof(EdgeType) * P->numNodes);
PPai[i] = (int*)malloc(sizeof(int) * P->numNodes);
for (int j = 0; j < P->numNodes; j++)
{
PPai[i][j] = P->Pai[i][j];
}
}

Graph_Shortest_Paths* PP;
PP = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
PP->numNodes = P->numNodes;
PP->D = LL;
PP->Pai = PPai;

for (int i = 0; i < P->numNodes; i++)
{
for (int j = 0; j < P->numNodes; j++)
{
if (i == j)
{
LL[i][j] = 0;
}
else
{
LL[i][j] = INT_MAX;
for (int k = 0; k < P->numNodes; k++)
{
if (i != j && k != j && P->D[i][k] != INT_MAX && W[k][j] != INT_MAX && P->D[i][k] + W[k][j] < LL[i][j])
{
LL[i][j] = P->D[i][k] + W[k][j];
// 快速算法
if (P->D == W)
{
PPai[i][j] = P->Pai[k][j];
}
else
{
PPai[i][j] = k;
}
}
}
}

}
}

return PP;
}

// 慢速算法
Graph_Shortest_Paths* Slow_All_Pairs_Shortest_Paths(GraphAdjList* G)
{
//构造边权重矩阵
EdgeType** W;
W = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
W[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
if (i == j)
{
W[i][j] = 0;
}
else
{
W[i][j] = INT_MAX;
}
}
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
W[i][edge->adjvex] = edge->weight;
edge = edge->next;
}
}

Graph_Shortest_Paths* P, *Pm, *t;
P = Init__Graph_Shortest_Paths(W, G->numNodes);

for (int m = 1; m < G->numNodes - 1; m++)
{
Pm = Extend_Shortest_Paths(W, P);
t = P;
P = Pm;
Delete_Graph_Shortest_Paths(t);
}

for (int i = 0; i < G->numNodes; i++)
{
free(W[i]);
}
free(W);

return P;
}

// 快速算法
Graph_Shortest_Paths* Faster_All_Pairs_Shortest_Paths(GraphAdjList* G)
{
//构造边权重矩阵
EdgeType** W;
W = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
W[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
if (i == j)
{
W[i][j] = 0;
}
else
{
W[i][j] = INT_MAX;
}
}
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
W[i][edge->adjvex] = edge->weight;
edge = edge->next;
}
}

Graph_Shortest_Paths* P, *Pm, *t;
P = Init__Graph_Shortest_Paths(W, G->numNodes);

int m = 1;
while (m < G->numNodes)
{
Pm = Extend_Shortest_Paths(P->D, P);
t = P;
P = Pm;
Delete_Graph_Shortest_Paths(t);
m = 2 * m;
}

for (int i = 0; i < G->numNodes; i++)
{
free(W[i]);
}
free(W);

return P;
}

// Floyd_Warshall算法
Graph_Shortest_Paths* Floyd_Warshall(GraphAdjList* G)
{
//构造边权重矩阵
EdgeType** D, **Dn;
int** Pai, ** PPai;
D = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
Dn = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
Pai = (int**)malloc(sizeof(int*) * G->numNodes);
PPai = (int**)malloc(sizeof(int*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
D[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
Dn[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
Pai[i] = (int*)malloc(sizeof(int) * G->numNodes);
PPai[i] = (int*)malloc(sizeof(int) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
if (i == j)
{
D[i][j] = 0;
}
else
{
D[i][j] = INT_MAX;
}
PPai[i][j] = -1;
}
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
D[i][edge->adjvex] = edge->weight;
PPai[i][edge->adjvex] = i;
edge = edge->next;
}
}

Graph_Shortest_Paths* PP;
PP = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
PP->numNodes = G->numNodes;
PP->D = D;
PP->Pai = Pai;

for (int k = 0; k < G->numNodes; k++)
{
for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
if (D[i][k] != INT_MAX && D[k][j] != INT_MAX && D[i][k] + D[k][j] < D[i][j])
{
D[i][j] = D[i][k] + D[k][j];
Pai[i][j] = PPai[k][j];
}
else
{
Pai[i][j] = PPai[i][j];
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
PPai[i][j] = Pai[i][j];
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
free(PPai[i]);
}
free(PPai);

return PP;
}

// 传递闭包
bool** Graph_Transitive_Closure(GraphAdjList* G)
{
// 初始化T
bool** T, ** TT;
T = (bool**)malloc(sizeof(bool*) * G->numNodes);
TT = (bool**)malloc(sizeof(bool*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
T[i] = (bool*)calloc(G->numNodes, sizeof(bool));
TT[i] = (bool*)calloc(G->numNodes, sizeof(bool));
T[i][i] = TT[i][i] = true;
}

EdgeNode* edge;
for (int i = 0; i < G->numNodes; i++)
{
edge = G->adjList[i].next;
while (edge != NULL)
{
T[i][edge->adjvex] = TT[i][edge->adjvex] = true;
edge = edge->next;
}
}

for (int k = 0; k < G->numNodes; k++)
{
for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
if (T[i][j] == 0 && (TT[i][k] == true && TT[k][j] == true))
{
T[i][j] = true;
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
TT[i][j] = T[i][j];
}
}
}

for (int i = 0; i < G->numNodes; i++)
{
free(TT[i]);
}
free(TT);

return T;
}

// Johnson算法
Graph_Shortest_Paths* Graph_Johnson(GraphAdjList* G)
{
// 增加点
GraphAdjList* GG;
GG = Create_GraphAdjList(G->numNodes + 1, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(GG, i, p->adjvex, p->weight);
p = p->next;
}
AddEdge_GraphAdjList(GG, GG->numNodes - 1, i, 0);
}

Graph_Shortest_Paths* P;
P = (Graph_Shortest_Paths*)malloc(sizeof(Graph_Shortest_Paths));
P->numNodes = G->numNodes;
P->D = NULL;
P->Pai = NULL;

if (Bellman_Ford(GG, &GG->adjList[GG->numNodes - 1]) == false)
{
printf("有负环路\n");
}
else
{
EdgeType* h;
h = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
h[i] = GG->adjList[i].key;
}
// 更新边
GraphAdjList* Gh;
Gh = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < Gh->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Gh, i, p->adjvex, p->weight + h[i] - h[p->adjvex]);
p = p->next;
}
}

P->D = (EdgeType**)malloc(sizeof(EdgeType*) * G->numNodes);
P->Pai = (int**)malloc(sizeof(int*) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
P->D[i] = (EdgeType*)malloc(sizeof(EdgeType) * G->numNodes);
P->Pai[i] = (int*)malloc(sizeof(int) * G->numNodes);
for (int j = 0; j < G->numNodes; j++)
{
P->D[i][j] = INT_MAX;
}
P->Pai[i][i] = -1;
}

for (int i = 0; i < Gh->numNodes; i++)
{
Dijkstra(Gh, &Gh->adjList[i]);
for (int j = 0; j < Gh->numNodes; j++)
{
P->D[i][j] = Gh->adjList[j].key - h[i] + h[j];
if (i != j)
{
P->Pai[i][j] = Gh->adjList[j].predecessor->data;
}
}
}

free(h);
Delete_GraphAdjList(Gh);
}
return P;
}

// 最大流算法
// 深度优先遍历寻找增广路径
bool DFS_FindAugmentingPath(GraphAdjList* G, int u, int t, bool* visited, int* path)
{
if (u == t)
{
return true; // 如果到达汇点,返回成功
}
visited[u] = 1; // 标记当前节点为已访问

EdgeNode* edge;
edge = G->adjList[u].next;
while (edge != NULL)
{
int v = edge->adjvex;
if (!visited[v] && edge->weight > 0) // 如果未访问且残存容量大于0
{
path[v] = u; // 记录路径
if (DFS_FindAugmentingPath(G, v, t, visited, path))
{
return true; // 如果找到一条路径,返回成功
}
}
edge = edge->next;
}
return false; // 未找到路径,返回失败
}

// 增广路径的残存容量
EdgeType Augmenting_Path_Capacity(GraphAdjList* G, int* path, int s, int t)
{
int path_flow = INT_MAX;
for (int v = t; v != s; v = path[v])
{
// 前驱节点
int u = path[v];
// 查找边(u, v)的残存容量
EdgeNode* edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v) //找到节点v
{
if (path_flow > edge->weight)
{
path_flow = edge->weight;
}
break;
}
edge = edge->next;
}
}
return path_flow;
}

// Ford_Fulkerson算法
GraphAdjList* Ford_Fulkerson(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

bool* visit_list;
visit_list = (bool*)malloc(sizeof(bool) * G->numNodes);
int* pre_path;
pre_path = (int*)malloc(sizeof(int) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
visit_list[i] = 0;
pre_path[i] = -1;
}

EdgeType path_capacity;
while (DFS_FindAugmentingPath(Ef, s, t, visit_list, pre_path))
{
path_capacity = Augmenting_Path_Capacity(Ef, pre_path, s, t);
// 更新流网络与残差网络
for (int v = t; pre_path[v] != -1; v = pre_path[v]) // 遍历path路径上的节点
{
int u = pre_path[v];
// 更新流
EdgeNode* edge;
edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
ChangeEdge_GraphAdjList(Flow, u, v, path_capacity);
break;
}
edge = edge->next;
}
if (edge == NULL)
{
ChangeEdge_GraphAdjList(Flow, v, u, -path_capacity);
}

// 更新残差网络
ChangeEdge_GraphAdjList(Ef, u, v, -path_capacity);
ChangeEdge_GraphAdjList(Ef, v, u, path_capacity);
}
for (int i = 0; i < G->numNodes; i++)
{
visit_list[i] = 0;
pre_path[i] = -1;
}
}

free(visit_list);
free(pre_path);
Delete_GraphAdjList(Ef); // 删除残差网络

return Flow;
}

// 广度优先遍历寻找增广路径
bool BFS_FindAugmentingPath(GraphAdjList* G, int s, int t, int* path)
{
LinkQueue* q = InitLinkQueue(); // 初始化队列
bool visited[G->numNodes]; // 访问标记数组
for (int i = 0; i < G->numNodes; i++)
{
visited[i] = false;
path[i] = -1; // 初始化父节点数组
}

visited[s] = true; // 标记源点为已访问
EnLinkQueue(q, &G->adjList[s]); // 源点入队

while (EmptyLinkQueue(q)) // 队列非空
{
VextexNode* uNode = DeLinkQueue(q); // 出队
int u = uNode->data; // 获取当前顶点编号

EdgeNode* e = uNode->next; // 获取邻接边
while (e != NULL)
{
int v = e->adjvex; // 邻接顶点编号
if (!visited[v] && e->weight > 0) // 如果未访问且边权重(容量)大于0
{
visited[v] = true;
path[v] = u; // 记录路径
EnLinkQueue(q, &G->adjList[v]); // 邻接顶点入队
if (v == t) // 如果到达汇点
{
free(q->front); // 清理队列
free(q);
return true; // 找到增广路径
}
}
e = e->next; // 移动到下一条边
}
}

free(q->front); // 清理队列
free(q);
return false; // 未找到增广路径
}

// Edmonds_Karp算法
GraphAdjList* Edmonds_Karp(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

int* pre_path;
pre_path = (int*)malloc(sizeof(int) * G->numNodes);
for (int i = 0; i < G->numNodes; i++)
{
pre_path[i] = -1;
}

EdgeType path_capacity;
while (BFS_FindAugmentingPath(Ef, s, t, pre_path))
{
path_capacity = Augmenting_Path_Capacity(Ef, pre_path, s, t);
// 更新流网络与残差网络
for (int v = t; pre_path[v] != -1; v = pre_path[v]) // 遍历path路径上的节点
{
int u = pre_path[v];
// 更新流
EdgeNode* edge;
edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
ChangeEdge_GraphAdjList(Flow, u, v, path_capacity);
break;
}
edge = edge->next;
}
if (edge == NULL)
{
ChangeEdge_GraphAdjList(Flow, v, u, -path_capacity);
}

// 更新残差网络
ChangeEdge_GraphAdjList(Ef, u, v, -path_capacity);
ChangeEdge_GraphAdjList(Ef, v, u, path_capacity);
}
for (int i = 0; i < G->numNodes; i++)
{
pre_path[i] = -1;
}
}

free(pre_path);
Delete_GraphAdjList(Ef); // 删除残差网络

return Flow;
}

// 溢出节点链表
typedef struct OverflowingNode{
int vertexIndex; // 存储顶点索引
OverflowingNode* next; // 指向下一个活跃节点
}OverflowingNode;

// 创建空头节点
OverflowingNode* Init_OverflowingList()
{
OverflowingNode* head = (OverflowingNode*)malloc(sizeof(OverflowingNode));
head->vertexIndex = -1; // 空头节点不存储有效的顶点索引
head->next = NULL;
return head;
}

// 判断空链表
bool Empty_OverflowingList(OverflowingNode* head)
{
if (head->next == NULL)
{
return true;
}
else
{
return false;
}
}

// 头插法添加节点
void Add_OverflowingNode(OverflowingNode* head, int vertexIndex)
{
OverflowingNode* newNode;
newNode = (OverflowingNode*)malloc(sizeof(OverflowingNode));
newNode->vertexIndex = vertexIndex;
// 插入到头节点之后
newNode->next = head->next;
head->next = newNode;
}

// 移除溢出节点
void Remove_OverflowingNode(OverflowingNode* head, int vertexIndex)
{
OverflowingNode* prev = head;
OverflowingNode* current = head->next;
while (current != NULL)
{
if (current->vertexIndex == vertexIndex)
{
prev->next = current->next;
free(current);
break;
}
prev = current;
current = current->next;
}
}

// 删除溢出节点链表
void Delete_OverflowingList(OverflowingNode* head)
{
OverflowingNode* current = head;
while (current != NULL)
{
OverflowingNode* temp = current;
current = current->next;
free(temp);
}
}

// Push算法
void Push(GraphAdjList* G, GraphAdjList* Ef, GraphAdjList* Flow, int u, int v)
{
EdgeNode* edge;
edge = Ef->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
break;
}
edge = edge->next;
}

EdgeType delta;
delta = (edge->weight < Ef->adjList[u].key) ? edge->weight : Ef->adjList[u].key;

edge = G->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
ChangeEdge_GraphAdjList(Flow, u, v, delta);
ChangeEdge_GraphAdjList(Ef, u, v, -delta);
ChangeEdge_GraphAdjList(Ef, v, u, delta);
break;
}
edge = edge->next;
}
if (edge == NULL)
{
ChangeEdge_GraphAdjList(Flow, v, u, -delta);
ChangeEdge_GraphAdjList(Ef, u, v, -delta);
ChangeEdge_GraphAdjList(Ef, v, u, delta);
}

Ef->adjList[u].key -= delta;
Ef->adjList[v].key += delta;
}

// Relabel算法
void Relabel(GraphAdjList* Ef, int u)
{
int min_height = INT_MAX; // 初始化为最大整数,以确保能找到最小的高度
for (EdgeNode* edge = Ef->adjList[u].next; edge != NULL; edge = edge->next) // 只考虑残余容量大于0的边
{
if (edge->weight > 0)
{
int neighbor_height = Ef->adjList[edge->adjvex].f;
if (neighbor_height < min_height)
{
min_height = neighbor_height;
}
}
}
// 只有当找到至少一个有效的邻接节点时,才更新高度
if (min_height != INT_MAX)
{
Ef->adjList[u].f = min_height + 1;
}
}

// 判断能否使用Push算法
bool exists_Push(GraphAdjList* Ef, int u, int v)
{
EdgeNode* edge;
edge = Ef->adjList[u].next;
while (edge != NULL)
{
if (edge->adjvex == v)
{
break;
}
edge = edge->next;
}

if (edge != NULL && edge->weight > 0)
{
if (Ef->adjList[u].f == Ef->adjList[v].f + 1)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

// 判断能否使用Relabel算法
bool exists_Relabel(GraphAdjList* Ef, int u)
{
for (EdgeNode* edge = Ef->adjList[u].next; edge != NULL; edge = edge->next)
{
if (Ef->adjList[edge->adjvex].f <= Ef->adjList[u].f && edge->weight > 0)
{
return false;
}
}
return true;
}

// Push_Relabel算法
GraphAdjList* Generic_Push_Relabel(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

// 创建残差网络
GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

// 初始化
for (int i = 0; i < Ef->numNodes; i++)
{
Ef->adjList[i].f = 0;
Ef->adjList[i].key = 0;
}
Ef->adjList[s].f = Ef->numNodes;

// 溢出节点链表
OverflowingNode* overflowinglist;
overflowinglist = Init_OverflowingList();

// 初始化预流
EdgeNode* edge;
edge = Ef->adjList[s].next;
for (EdgeNode* edge = Ef->adjList[s].next; edge != NULL; edge = edge->next)
{
EdgeType flow = edge->weight;
AddEdge_GraphAdjList(Flow, s, edge->adjvex, flow);
ChangeEdge_GraphAdjList(Ef, s, edge->adjvex, -flow);
ChangeEdge_GraphAdjList(Ef, edge->adjvex, s, flow);

Ef->adjList[edge->adjvex].key = flow;
Ef->adjList[s].key -= flow;
Add_OverflowingNode(overflowinglist, edge->adjvex);
}

while (!Empty_OverflowingList(overflowinglist))
{
OverflowingNode* currentNode;
currentNode = overflowinglist->next;
while (currentNode != NULL)
{
int u = currentNode->vertexIndex;

// 尝试对所有邻接节点v进行Push操作
bool pushed = false;
for (EdgeNode* edge = Ef->adjList[u].next; edge != NULL; edge = edge->next)
{
int v = edge->adjvex;
if (exists_Push(Ef, u, v))
{
Push(G, Ef, Flow, u, v);
pushed = true;

// 如果v变成了新的活跃节点,添加到活跃节点列表
if (Ef->adjList[v].key > 0 && v != s && v != t)
{
Add_OverflowingNode(overflowinglist, v);
}
break; // 只要成功进行一次Push,就跳出循环
}
}

// 如果没有成功进行Push操作,则尝试Relabel
if (!pushed)
{
Relabel(Ef, u);
}

// 移动到下一个活跃节点
currentNode = currentNode->next;

// 检查u是否仍然是活跃节点
if (Ef->adjList[u].key <= 0)
{
Remove_OverflowingNode(overflowinglist, u); // 移除不再活跃的节点u
}
}
}

Delete_GraphAdjList(Ef);
free(overflowinglist);
return Flow;
}

// 释放溢出节点
void Discharge(GraphAdjList* G, GraphAdjList* Ef, GraphAdjList* Flow, int u)
{
EdgeNode* currentNode;
currentNode = Ef->adjList[u].next;
while (Ef->adjList[u].key > 0)
{
if (currentNode == NULL) // 重贴标签
{
Ef->adjList[u].f += 1;
currentNode = Ef->adjList[u].next;
}
else if (Ef->adjList[u].f == Ef->adjList[currentNode->adjvex].f + 1 && currentNode->weight > 0) // push许可边
{
Push(G, Ef, Flow, u, currentNode->adjvex);
}
else
{
currentNode = currentNode->next;
}
}
}

// 前置重贴标签算法
GraphAdjList* Relabel_To_Front(GraphAdjList* G, int s, int t)
{
// 初始化流函数
GraphAdjList* Flow;
Flow = Create_GraphAdjList(G->numNodes, true);

// 创建残差网络
GraphAdjList * Ef;
Ef = Create_GraphAdjList(G->numNodes, true);
EdgeNode* p;
for (int i = 0; i < G->numNodes; i++)
{
p = G->adjList[i].next;
while (p != NULL)
{
AddEdge_GraphAdjList(Ef, i, p->adjvex, p->weight);
AddEdge_GraphAdjList(Ef, p->adjvex, i, 0);
p = p->next;
}
}

// 初始化
for (int i = 0; i < Ef->numNodes; i++)
{
Ef->adjList[i].f = 0;
Ef->adjList[i].key = 0;
}
Ef->adjList[s].f = Ef->numNodes;

// 溢出节点链表
OverflowingNode* OverflowingList;
OverflowingList = Init_OverflowingList();
for (int i = 0; i < G->numNodes; i++)
{
if (i != s && i != t)
{
Add_OverflowingNode(OverflowingList, i);
}
}

// 初始化预流
EdgeNode* edge;
edge = Ef->adjList[s].next;
for (EdgeNode* edge = Ef->adjList[s].next; edge != NULL; edge = edge->next)
{
EdgeType flow = edge->weight;
AddEdge_GraphAdjList(Flow, s, edge->adjvex, flow);
ChangeEdge_GraphAdjList(Ef, s, edge->adjvex, -flow);
ChangeEdge_GraphAdjList(Ef, edge->adjvex, s, flow);

Ef->adjList[edge->adjvex].key = flow;
Ef->adjList[s].key -= flow;
}

OverflowingNode* u,* pre;
u = OverflowingList->next;
pre = OverflowingList;
int old_height;
while (u != NULL)
{
if (Ef->adjList[u->vertexIndex].key > 0)
{
old_height = Ef->adjList[u->vertexIndex].f;
Discharge(G, Ef, Flow, u->vertexIndex);
int old_index = u->vertexIndex;

if (Ef->adjList[u->vertexIndex].f > old_height)
{
// 移动u到表头
pre->next = u->next;
u->next = OverflowingList->next;
OverflowingList->next = u;

pre = u;
u = u->next;
}
}
else
{
u = u->next;
pre = pre->next;
}
}

Delete_GraphAdjList(Ef);
Delete_OverflowingList(OverflowingList);

return Flow;
}

int main()
{
// 创建一个有向图的邻接表
GraphAdjList* graphadjlist = Create_GraphAdjList(8, 0);
int edges1[16][2] = {
{0, 1}, {0, 2},
{1, 3}, {1, 4},
{2, 3}, {2, 5},
{3, 4}, {3, 7},
{4, 5},
{5, 0}, {5, 6},
{6, 2}, {6, 7},
{7, 0}, {7, 6}
};
for (int i = 0; i < 16; i++) {
AddEdge_GraphAdjList(graphadjlist, edges1[i][0], edges1[i][1], 1);
}

// 将邻接表转换为邻接矩阵
GraphAdjMatrix* graphadjmatrix = GraphAdjList_Transfor_GraphMatrix(graphadjlist);

// 输出邻接表
Print_GraphAdjList(graphadjlist);
// 输出邻接矩阵
Print_GraphAdjMatrix(graphadjmatrix);

// 释放邻接矩阵空间
Delete_GraphAdjMatrix(graphadjmatrix);

// 执行广度优先遍历
BFS_GraphAdjList(graphadjlist, &graphadjlist->adjList[0]); // 假设从节点0开始遍历
// 打印广度优先树
Print_BFTree(graphadjlist, &graphadjlist->adjList[0]);

// 执行深度优先遍历
DFS_GraphAdjList(graphadjlist);
// 打印深度优先树
Print_DFTree(graphadjlist);

// 释放邻接表空间
Delete_GraphAdjList(graphadjlist);

// 定义加权无向连通图
GraphAdjList* G = Create_GraphAdjList(8, 1);

// 定义边的数组
int edges2[10][3] = {
{0, 1, 4}, {0, 2, 3},
{1, 2, 1}, {1, 3, 2},
{2, 3, 4},
{3, 4, 2},
{4, 5, 6},
{5, 6, 2}, {5, 7, 5},
{6, 7, 1}
};
for (int i = 0; i < 10; i++) {
int u = edges2[i][0];
int v = edges2[i][1];
int weight = edges2[i][2];
AddEdge_GraphAdjList(G, u, v, weight);
AddEdge_GraphAdjList(G, v, u, weight); // 由于是无向图,添加反向边
}

// Kruskal算法MST
Print_GraphAdjList(G);
GraphAdjList* MST1 = MST_Kruskal(G);
Print_GraphAdjList(MST1);
Delete_GraphAdjList(MST1);

// Prim算法
GraphAdjList* MST2 = MST_Prim(G, &G->adjList[0]);
Print_GraphAdjList(MST2);
Delete_GraphAdjList(MST2);

Delete_GraphAdjList(G);


// 创建一个包含8个顶点的图
G = Create_GraphAdjList(8, true);

// 定义边和权重
int edges3[][3] = {
{0, 1, 5}, {0, 2, 3},
{1, 3, 6}, {1, 2, 2}, {1, 7, 11},
{2, 4, 4}, {2, 5, 2},
{3, 4, 7}, {3, 0, 10}, {3, 2, 1}, {3, 5, 3}, {3, 6, 1},
{4, 5, 3},
{5, 6, 1},
{6, 7, 2},
{7, 5, 9}, {7, 3, 6}
};

// 添加边
int numEdges = sizeof(edges3) / sizeof(edges3[0]);
for (int i = 0; i < numEdges; i++) {
int u = edges3[i][0];
int v = edges3[i][1];
int weight = edges3[i][2];
AddEdge_GraphAdjList(G, u, v, weight);
}

Print_GraphAdjList(G);
if (Bellman_Ford(G, &G->adjList[0]))
{
Print_DFTree(G);
}
else
{
printf("存在负权重环路\n");
}

// 测试Dijkstra算法
Dijkstra(G, &G->adjList[0]);
Print_DFTree(G);

// 释放图内存
Delete_GraphAdjList(G);

G = Create_GraphAdjList(5, 1); // 5个顶点的图

// 定义边和权重
int edges4[][3] = {
{0, 1, 3}, {0, 2, 8}, {0, 4, -4},
{1, 3, 1}, {1, 4, 7},
{2, 1, 4},
{3, 0, 2}, {3, 2, -5},
{4, 3, 6}
};

// 添加边
numEdges = sizeof(edges4) / sizeof(edges4[0]);
for (int i = 0; i < numEdges; i++) {
int u = edges4[i][0];
int v = edges4[i][1];
int weight = edges4[i][2];
AddEdge_GraphAdjList(G, u, v, weight);
}

// 运行 Floyd-Warshall 算法
Graph_Shortest_Paths* result;
result = Slow_All_Pairs_Shortest_Paths(G);
Print_Graph_Shortest_Paths(result);
Delete_Graph_Shortest_Paths(result);

result = Faster_All_Pairs_Shortest_Paths(G);
Print_Graph_Shortest_Paths(result);
Delete_Graph_Shortest_Paths(result);

// 运行 Floyd-Warshall 算法
result = Floyd_Warshall(G);
Print_Graph_Shortest_Paths(result);
Delete_Graph_Shortest_Paths(result);

Delete_GraphAdjList(G);

G = Create_GraphAdjList(4, 1); // 4个顶点的图

// 定义边和权重
int edges5[][3] = {
{0, 1, 3},
{1, 2, 1},
{2, 3, 4},
{3, 1, 2}
};

// 添加边
numEdges = sizeof(edges5) / sizeof(edges5[0]);
for (int i = 0; i < numEdges; i++) {
int u = edges5[i][0];
int v = edges5[i][1];
int weight = edges5[i][2];
AddEdge_GraphAdjList(G, u, v, weight);
}

// 计算传递闭包
bool** T = Graph_Transitive_Closure(G);

Print_GraphAdjList(G);
printf("图的传递闭包\n");
for (int i = 0; i < G->numNodes; i++)
{
for (int j = 0; j < G->numNodes; j++)
{
printf("%d ", T[i][j]);
}
printf("\n");
}

// 清理资源
for (int i = 0; i < G->numNodes; i++) {
free(T[i]);
}
free(T);
Delete_GraphAdjList(G);

G = Create_GraphAdjList(5, 1); // 5个顶点的图

// 定义边和权重
int edges6[][3] = {
{0, 1, 3}, {0, 2, 8}, {0, 4, -4},
{1, 3, 1}, {1, 4, 7},
{2, 1, 4},
{3, 0, 2}, {3, 2, -5},
{4, 3, 6}
};

// 添加边
numEdges = sizeof(edges6) / sizeof(edges6[0]);
for (int i = 0; i < numEdges; i++) {
int u = edges6[i][0];
int v = edges6[i][1];
int weight = edges6[i][2];
AddEdge_GraphAdjList(G, u, v, weight);
}
Print_GraphAdjList(G);
result = Graph_Johnson(G);
Print_Graph_Shortest_Paths(result);
Delete_Graph_Shortest_Paths(result);
result = Floyd_Warshall(G);
Print_Graph_Shortest_Paths(result);
Delete_Graph_Shortest_Paths(result);
Delete_GraphAdjList(G);


G = Create_GraphAdjList(6, 1); // 6个顶点的图

// 定义边和权重
int edges7[][3] = {
{0, 1, 16}, {0, 2, 13},
{1, 3, 12},
{2, 1, 4}, {2, 4, 14},
{3, 2, 9}, {3, 5, 20},
{4, 3, 7}, {4, 5, 4}
};

// 添加边
numEdges = sizeof(edges7) / sizeof(edges7[0]);
for (int i = 0; i < numEdges; i++) {
int u = edges7[i][0];
int v = edges7[i][1];
int weight = edges7[i][2];
AddEdge_GraphAdjList(G, u, v, weight);
}
Print_GraphAdjList(G);

GraphAdjList* flow;
printf("Ford_Fulkerson 算法\n");
flow = Ford_Fulkerson(G, 0, 5);
Print_GraphAdjList(flow);
Delete_GraphAdjList(flow);
printf("Edmonds_Karp 算法\n");
flow = Edmonds_Karp(G, 0, 5);
Print_GraphAdjList(flow);
Delete_GraphAdjList(flow);
printf("重贴标签算法\n");
flow = Generic_Push_Relabel(G, 0, 5);
Print_GraphAdjList(flow);
Delete_GraphAdjList(flow);
printf("前置重贴标签算法\n");
flow = Relabel_To_Front(G, 0, 5);
Print_GraphAdjList(flow);
Delete_GraphAdjList(flow);
Delete_GraphAdjList(G);

return 0;
}