-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathDepthFirstDemo.java
108 lines (84 loc) · 2.32 KB
/
DepthFirstDemo.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
package ds_013_network_algorithms;
import java.util.LinkedList;
import java.util.List;
/*
* J --- 10 --- D --- 10 --- A --- 10 --- B --- 10 --- E
* | | |
* | | |
* | | |
* I C F
* / \
* / \
* / \
* H G
*
*/
public class DepthFirstDemo {
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");
// Links
Link linkAB = new Link(10, nodeB);
Link linkAC = new Link(10, nodeC);
Link linkAD = new Link(10, nodeD);
Link linkBE = new Link(10, nodeE);
Link linkBF = new Link(10, nodeF);
Link linkCH = new Link(10, nodeH);
Link linkCG = new Link(10, nodeG);
Link linkDJ = new Link(10, nodeJ);
Link linkDI = new Link(10, nodeI);
// Connection
nodeA.links.add(linkAB);
nodeA.links.add(linkAC);
nodeA.links.add(linkAD);
nodeB.links.add(linkBE);
nodeB.links.add(linkBF);
nodeC.links.add(linkCG);
nodeC.links.add(linkCH);
nodeD.links.add(linkDI);
nodeD.links.add(linkDJ);
depthFirstTraversal(nodeA);
}
private static class Node {
private final String label;
private List<Link> links;
public Node(final String label) {
this.label = label;
this.links = new LinkedList<>();
}
@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(final int cost) {
this.cost = cost;
}
public Link(final int cost, Node toNode) {
this.cost = cost;
this.toNode = toNode;
}
}
private static void depthFirstTraversal(Node node) {
visitNode(node);
for(Link link : node.links) {
depthFirstTraversal(link.toNode);
}
}
private static void visitNode(Node node) {
System.out.printf("%s ", node);
}
}