-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBreadthFirstDemo.java
166 lines (133 loc) · 3.55 KB
/
BreadthFirstDemo.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
156
157
158
159
160
161
162
163
164
165
166
package ds_013_network_algorithms;
import java.util.LinkedList;
import java.util.List;
/*
* A - 10 - B - 10 - E - 10 - J
* | \ | | |
* | \____ | | |
* | \| | |
* D - 10 - C - 10 - F - 10 - K
* | | \ | |
* | | \____ | |
* | | \| |
* I - 10 - H - 10 - G - 10 - L
* | | | |
* | | | |
* | | | |
* P - 10 - O - 10 - N - 10 - M
*
*/
public class BreadthFirstDemo {
public static void main(String[] args) {
// Nodes
Node nodeA = new Node("A");
Node nodeB = new Node("B");
Node nodeC = new Node("C");
Node nodeD = new Node("D");
Node nodeE = new Node("E");
Node nodeF = new Node("F");
Node nodeG = new Node("G");
Node nodeH = new Node("H");
Node nodeI = new Node("I");
Node nodeJ = new Node("J");
Node nodeK = new Node("K");
Node nodeL = new Node("L");
Node nodeM = new Node("M");
Node nodeN = new Node("N");
Node nodeO = new Node("O");
Node nodeP = new Node("P");
// connections
nodeA.connectNode(nodeB);
nodeA.connectNode(nodeC);
nodeA.connectNode(nodeD);
nodeB.connectNode(nodeE);
nodeB.connectNode(nodeC);
nodeC.connectNode(nodeF);
nodeC.connectNode(nodeG);
nodeC.connectNode(nodeH);
nodeD.connectNode(nodeI);
nodeE.connectNode(nodeJ);
nodeE.connectNode(nodeF);
nodeF.connectNode(nodeK);
nodeF.connectNode(nodeG);
nodeG.connectNode(nodeL);
nodeG.connectNode(nodeN);
nodeH.connectNode(nodeO);
nodeH.connectNode(nodeI);
nodeI.connectNode(nodeP);
nodeJ.connectNode(nodeK);
nodeK.connectNode(nodeL);
nodeL.connectNode(nodeM);
nodeM.connectNode(nodeN);
nodeN.connectNode(nodeO);
nodeO.connectNode(nodeP);
// depthFirstTraversal(nodeA);
breadthFirstTraversal(nodeA);
}
private static class Node {
private final String label;
private List<Link> links;
private boolean isVisisted;
public Node(final String label) {
this.label = label;
this.links = new LinkedList<>();
isVisisted = false;
}
public void connectNode(Node other) {
this.connectNode(other, 0);
}
public void connectNode(Node other, int cost) {
links.add(new Link(this, other, cost));
other.links.add(new Link(other, this, cost));
}
@Override
public String toString() {
return String.format("Node %s", label);
}
}
private static class Link {
private final int cost;
private Node fromNode;
private Node toNode;
public Link(Node fromNode, Node toNode) {
this.cost = 0;
this.fromNode = fromNode;
this.toNode = toNode;
}
public Link(Node fromNode, Node toNode, final int cost) {
this.cost = cost;
this.fromNode = fromNode;
this.toNode = toNode;
}
}
private static void breadthFirstTraversal(Node startNode) {
SQueue<Node> queue = new SQueue<>();
queue.enqueue(startNode);
while(!queue.isEmpty()) {
Node node = queue.dequeue();
visitNode(node);
for(Link link : node.links) {
if(!link.toNode.isVisisted) {
queue.enqueue(link.toNode);
link.toNode.isVisisted = true;
}
}
}
}
private static void depthFirstTraversal(Node node) {
if(node.isVisisted) {
return;
}
visitNode(node);
for(Link link : node.links) {
depthFirstTraversal(link.toNode);
}
}
private static void visitNode(Node node) {
// if(node.isVisisted) {
// return;
// }
System.out.printf("%s ", node);
node.isVisisted = true;
}
}