-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathgraph_search.h
115 lines (104 loc) · 3 KB
/
graph_search.h
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
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* BREADTH FIRST SEARCH
*
* Features:
* In graph theory, breadth-first search (BFS) is a strategy for searching in
* a graph when search is limited to essentially two operations:
* (a) visit and inspect a node of a graph;
* (b) gain access to visit the nodes that neighbor the currently visited node.
* The BFS begins at a root node and inspects all the neighboring nodes. Then
* for each of those neighbor nodes in turn, it inspects their neighbor nodes
* which were unvisited, and so on. Compare it with the depth-first search.
*
* http://en.wikipedia.org/wiki/Breadth-first_search
*
******************************************************************************/
#ifndef ALGO_BREADTH_FIRST_SEARCH_H__
#define ALGO_BREADTH_FIRST_SEARCH_H__
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include "queue.h"
#include "stack.h"
#include "directed_graph.h"
#include "hash_table.h"
namespace alg {
/**
* BREADTH FIRST SEARCH
*/
static void BFS(const Graph & g, int32_t src_id) {
// mark all vertex color to WHITE
Graph::Adjacent * a;
list_for_each_entry(a, &g.list(), a_node) {
a->color = Graph::WHITE;
a->d = INT_MAX;
}
Graph::Adjacent * s = g[src_id];
s->d = 0;
Queue<uint32_t> Q(g.vertex_count());
Q.enqueue(s->v.id);
while(!Q.is_empty()) {
uint32_t id = Q.front();
printf("%d->", id); // output discovered id
Q.dequeue();
Graph::Vertex * _v;
Graph::Adjacent * u = g[id];
list_for_each_entry(_v, &u->v_head, v_node) {
Graph::Adjacent * v = g[_v->id]; // retrive the original adjacent list
if (v->color == Graph::WHITE) { // to change node color
v->color = Graph::GRAY;
v->d = u->d + 1;
Q.enqueue(v->v.id);
}
}
u->color = Graph::BLACK;
}
printf("\n");
}
static void _DFS_VISIT(Graph &g, Graph::Adjacent * u);
/**
* DEPTH FIRST SEARCH
*/
static void DFS(Graph & g) {
// mark all vertex color to WHITE
Graph::Adjacent * a;
list_for_each_entry(a, &g.list(), a_node) {
a->color = Graph::WHITE;
}
// for each vertex
g.graph_tick = 0;
list_for_each_entry(a, &g.list(), a_node) {
if (a->color == Graph::WHITE) {
printf("DFS from : %d\t",a->v.id);
_DFS_VISIT(g, a);
printf("\n");
}
}
printf("\n");
}
/**
* recursivly visit (Call Stack)
*/
static void _DFS_VISIT(Graph &g, Graph::Adjacent * u) {
// white vertex u has just benn discovered
u->d = ++g.graph_tick;
u->color = Graph::GRAY;
Graph::Vertex * _v;
list_for_each_entry(_v, &u->v_head, v_node) { // explore edge (u, v)
Graph::Adjacent * v = g[_v->id]; // retrive the original adjacent list
if (v->color == Graph::WHITE) {
_DFS_VISIT(g, v);
}
}
u->color = Graph::BLACK;
u->f = ++g.graph_tick;
printf("%d(d:%d, f:%d) -> ", u->v.id, u->d, u->f);
}
}
#endif //