-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDFSSearch.java
More file actions
76 lines (66 loc) · 1.82 KB
/
Copy pathGraphDFSSearch.java
File metadata and controls
76 lines (66 loc) · 1.82 KB
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
package pkg;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class GraphDFSSearch {
private static String START;
private static String END;;
public static void main(String[] args) throws IOException {
UndirectGraph graph = new UndirectGraph();
BufferedReader br = new BufferedReader(new FileReader("input_1.txt"));
String sCurrentLine;
List<String> lines = new ArrayList<>();
while ((sCurrentLine = br.readLine()) != null) {
lines.add(sCurrentLine);
}
br.close();
String[] startnodes = lines.get(0).split(" ");
START = startnodes[0];
END = startnodes[1];
for (int i = 1; i < lines.size(); i++) {
String[] nodes = lines.get(i).split(":");
String fromEdge = nodes[0];
String[] toEdge = nodes[i].split(" ");
for (String str : toEdge)
graph.addEdge(fromEdge, str);
}
LinkedList<String> visited = new LinkedList<>();
visited.add(START);
new GraphDFSSearch().depthFirst(graph, visited);
}
private void depthFirst(UndirectGraph graph, LinkedList<String> visited) {
LinkedList<String> nodes = graph.adjacentNodes(visited.getLast());
for (String node : nodes) {
if (visited.contains(node)) {
continue;
}
if (node.equals(END)) {
visited.add(node);
printPath(visited);
visited.removeLast();
break;
}
}
for (String node : nodes) {
if (visited.contains(node) || node.equals(END)) {
continue;
}
visited.addLast(node);
depthFirst(graph, visited);
visited.removeLast();
}
}
private void printPath(LinkedList<String> visited) {
for (String node : visited) {
System.out.print(node);
System.out.print(" ");
}
System.out.println();
}
}