-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathdfs.c
101 lines (65 loc) · 2.1 KB
/
dfs.c
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
//
// dfs.c
// Algorithms - Graph depth-first search
//
// Created by YourtionGuo on 08/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <stdlib.h>
#include "dfs.h"
#include "graph.h"
#include "list.h"
#pragma mark - Private
/**
实际执行深度优先搜索
@param graph 图
@param adjlist 邻接表
@param ordered 顶点链表
@return 成功返回0;否则返回-1
*/
static int dfs_main(Graph *graph, AdjList *adjlist, List *ordered)
{
AdjList *clr_adjlist;
DfsVertex *clr_vertex, *adj_vertex;
ListElmt *member;
/// 将顶点着色为灰色并遍历它的邻接表
((DfsVertex *)adjlist->vertex)->color = gray;
for (member = list_head(&adjlist->adjacent); member != NULL; member = list_next(member)) {
/// 确定下一个邻接顶点的颜色
adj_vertex = list_data(member);
if (graph_adjlist(graph, adj_vertex, &clr_adjlist) != 0) return -1;
clr_vertex = clr_adjlist->vertex;
/// 深入邻接顶点如果为白色
if (clr_vertex->color == white) {
if (dfs_main(graph, clr_adjlist, ordered) != 0)return -1;
}
}
/// 将当前顶点着色为黑色并将它放在列表头
((DfsVertex *)adjlist->vertex)->color = black;
if (list_ins_next(ordered, NULL, (DfsVertex *)adjlist->vertex) != 0) return -1;
return 0;
}
#pragma mark - Public
int dfs(Graph *graph, List *ordered)
{
DfsVertex *vertex;
ListElmt *element;
/// 初始化图的所有顶点
for (element = list_head(&graph_adjlists(graph)); element != NULL; element = list_next(element)) {
vertex = ((AdjList *)list_data(element))->vertex;
vertex->color = white;
}
/// 执行深度优先搜索
list_init(ordered, NULL);
for (element = list_head(&graph_adjlists(graph)); element != NULL; element = list_next(element)) {
/// 确保每个未连接顶点被搜索到
vertex = ((AdjList *)list_data(element))->vertex;
if (vertex->color == white) {
if (dfs_main(graph, (AdjList *)list_data(element), ordered) != 0) {
list_destroy(ordered);
return -1;
}
}
}
return 0;
}