-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdfs-demo.c
89 lines (65 loc) · 2.12 KB
/
dfs-demo.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
/* dfs-demo.c
Driver program demonstrating depth-first search numbering and
edge labeling.
by: Steven Skiena
begun: March 27, 2002
*/
/*
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 "bool.h"
#include "bfs-dfs.h"
#include "queue.h"
extern bool processed[]; /* which vertices have been processed */
extern bool discovered[]; /* which vertices have been found */
extern int parent[]; /* 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 */
void process_vertex_early(int v) {
time = time + 1;
entry_time[v] = time;
printf("entered vertex %d at time %d\n",v, entry_time[v]);
}
void process_vertex_late(int v) {
time = time + 1;
exit_time[v] = time;
printf("exit vertex %d at time %d\n",v, exit_time[v]);
}
void process_edge(int x, int y) {
int class; /* edge class */
class = edge_classification(x, y);
if (class == BACK) {
printf("back edge (%d,%d)\n", x, y);
} else if (class == TREE) {
printf("tree edge (%d,%d)\n", x, y);
} else if (class == FORWARD) {
printf("forward edge (%d,%d)\n", x, y);
} else if (class == CROSS) {
printf("cross edge (%d,%d)\n", x, y);
} else {
printf("edge (%d,%d)\n not in valid class=%d", x, y, class);
}
}
int main(void) {
graph g;
int i;
read_graph(&g, FALSE);
print_graph(&g);
initialize_search(&g);
dfs(&g, 1);
for (i = 1; i <= g.nvertices; i++) {
find_path(1, i, parent);
}
printf("\n");
return 0;
}