-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbfs-dfs.c
169 lines (131 loc) · 3.8 KB
/
bfs-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
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
/* bfs-dfs.c
A generic implementation of graph traversal: breadth-first
and depth-first search
begun: March 27, 2002
by: Steven Skiena
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include "bfs-dfs.h"
#include "bool.h"
#include "queue.h"
/* [[[ graph_arrays_cut */
bool processed[MAXV+1]; /* which vertices have been processed */
bool discovered[MAXV+1]; /* which vertices have been found */
int parent[MAXV+1]; /* discovery relation */
/* ]]] */
int entry_time[MAXV+1]; /* time of vertex entry */
int exit_time[MAXV+1]; /* time of vertex exit */
int time; /* current event time */
bool finished = FALSE; /* if true, cut off search immediately */
/* [[[ init_search_cut */
void initialize_search(graph *g) {
int i; /* counter */
time = 0;
for (i = 0; i <= g->nvertices; i++) {
processed[i] = FALSE;
discovered[i] = FALSE;
parent[i] = -1;
}
}
/* ]]] */
/* [[[ bfs_cut */
void bfs(graph *g, int start) {
queue q; /* queue of vertices to visit */
int v; /* current vertex */
int y; /* successor vertex */
edgenode *p; /* temporary pointer */
init_queue(&q);
enqueue(&q, start);
discovered[start] = TRUE;
while (empty_queue(&q) == FALSE) {
v = dequeue(&q);
process_vertex_early(v);
processed[v] = TRUE;
p = g->edges[v];
while (p != NULL) {
y = p->y;
if ((processed[y] == FALSE) || g->directed) {
process_edge(v, y);
}
if (discovered[y] == FALSE) {
enqueue(&q,y);
discovered[y] = TRUE;
parent[y] = v;
}
p = p->next;
}
process_vertex_late(v);
}
}
/* ]]] */
/* [[[ eclass_cut */
int edge_classification(int x, int y) {
if (parent[y] == x) {
return(TREE);
}
if (discovered[y] && !processed[y]) {
return(BACK);
}
if (processed[y] && (entry_time[y]>entry_time[x])) {
return(FORWARD);
}
if (processed[y] && (entry_time[y]<entry_time[x])) {
return(CROSS);
}
printf("Warning: self loop (%d,%d)\n", x, y);
return -1;
}
/* ]]] */
/* [[[ dfs_cut */
void dfs(graph *g, int v) {
edgenode *p; /* temporary pointer */
int y; /* successor vertex */
if (finished == TRUE) {
return; /* allow for search termination */
}
discovered[v] = TRUE;
time = time + 1;
entry_time[v] = time;
process_vertex_early(v);
p = g->edges[v];
while (p != NULL) {
y = p->y;
if (discovered[y] == FALSE) {
parent[y] = v;
process_edge(v, y);
dfs(g, y);
} else if ((!processed[y]) || (g->directed)) {
process_edge(v, y);
}
if (finished == TRUE) {
return;
}
p = p->next;
}
process_vertex_late(v);
time = time + 1;
exit_time[v] = time;
processed[v] = TRUE;
}
/* ]]] */
/* [[[ find_path_cut */
void find_path(int start, int end, int parents[]) {
if ((start == end) || (end == -1)) {
printf("\n%d", start);
} else {
find_path(start, parents[end], parents);
printf(" %d", end);
}
}
/* ]]] */