-
Notifications
You must be signed in to change notification settings - Fork 2
/
BreadFirstSearch.java
156 lines (130 loc) · 3.94 KB
/
BreadFirstSearch.java
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
package algo.graph;
import org.junit.Assert;
import org.junit.Test;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BreadFirstSearch
{
/**
* 1
* |
* 5 - 2 - 6
* |
* 3
* / \
* 4 7
*/
@Test
public void testWithoutCycle() {
Graph<GNode> g = Graph.Builder.buildWithoutCycle();
g.init();
for(GNode node: g.nodes)
{
Assert.assertEquals(GNode.Status.UNDISCOVERED,node.status);
}
List<GNode> nodeSeq = new LinkedList<>();
List<String> edgeSeq = new LinkedList<>();
BFS bfs = new BFS(nodeSeq,edgeSeq);
bfs.search(g,g.nodes.get(0));
Assert.assertEquals(1, nodeSeq.get(0).value);
Assert.assertEquals(2, nodeSeq.get(1).value);
Assert.assertEquals(5, nodeSeq.get(2).value);
Assert.assertEquals(3, nodeSeq.get(3).value);
Assert.assertEquals(6, nodeSeq.get(4).value);
Assert.assertEquals(4, nodeSeq.get(5).value);
Assert.assertEquals(7, nodeSeq.get(6).value);
Assert.assertEquals(6, edgeSeq.size());
Assert.assertEquals("1-2", edgeSeq.get(0));
Assert.assertEquals("2-5", edgeSeq.get(1));
Assert.assertEquals("2-3", edgeSeq.get(2));
Assert.assertEquals("2-6", edgeSeq.get(3));
Assert.assertEquals("3-4", edgeSeq.get(4));
Assert.assertEquals("3-7", edgeSeq.get(5));
for(GNode node: g.nodes)
{
Assert.assertEquals(GNode.Status.PROCESSED,node.status);
}
}
/**
* 1
* |
* |-5 - 2 - 6
* | |
* | 3
* | / \
* |---4 7
*/
@Test
public void testWithCycle()
{
Graph<GNode> g = Graph.Builder.buildWithCycle();
g.init();
for(GNode node: g.nodes)
{
Assert.assertEquals(GNode.Status.UNDISCOVERED,node.status);
}
List<GNode> nodeSeq = new LinkedList<>();
List<String> edgeSeq = new LinkedList<>();
BFS bfs = new BFS(nodeSeq,edgeSeq);
bfs.search(g,g.nodes.get(0));
Assert.assertEquals(1, nodeSeq.get(0).value);
Assert.assertEquals(2, nodeSeq.get(1).value);
Assert.assertEquals(5, nodeSeq.get(2).value);
Assert.assertEquals(3, nodeSeq.get(3).value);
Assert.assertEquals(6, nodeSeq.get(4).value);
Assert.assertEquals(4, nodeSeq.get(5).value);
Assert.assertEquals(7, nodeSeq.get(6).value);
Assert.assertEquals(7, edgeSeq.size());
Assert.assertEquals("1-2", edgeSeq.get(0));
Assert.assertEquals("2-5", edgeSeq.get(1));
Assert.assertEquals("2-3", edgeSeq.get(2));
Assert.assertEquals("2-6", edgeSeq.get(3));
Assert.assertEquals("5-4", edgeSeq.get(4));
Assert.assertEquals("3-4", edgeSeq.get(5));
Assert.assertEquals("3-7", edgeSeq.get(6));
for(GNode node: g.nodes)
{
Assert.assertEquals(GNode.Status.PROCESSED,node.status);
}
}
}
class BFS extends AbstractGS {
public BFS(List<GNode> nodeSeq, List<String> edgeSeq) {
super(nodeSeq,edgeSeq);
}
@Override
public <T> void search(Graph g, GNode<T> head)
{
// init queue
Queue<GNode> queue = new LinkedList<>();
// add root to the queue (at the end)
queue.add(head);
head.status= GNode.Status.DISCOVERED;
// while the queue is not empty
while (!queue.isEmpty()) {
// extract first from the queue
GNode<T> actual = queue.remove();
// process early
processEarly(actual);
actual.status=GNode.Status.PROCESSED;
// loop through all the other one
for(GNode n: actual.adj)
{
// add to the queue if not visited
if(n.status==GNode.Status.UNDISCOVERED)
{
queue.add(n);
n.parent=actual;
n.status=GNode.Status.DISCOVERED;
}
if(n.status!= GNode.Status.PROCESSED || g.isDirected)
{
processEdge(actual, n);
}
}
// process late
processLate(actual);
}
}
}