-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSearch.java
More file actions
47 lines (37 loc) · 1.43 KB
/
DFSearch.java
File metadata and controls
47 lines (37 loc) · 1.43 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
//package stjohns_javarefresher;
import java.util.*;
public class DFSearch implements GraphSearchOperator {
// recursive dfs
private void dfs(GraphAL g, int vIdx, boolean[] visited, ArrayList traversedVertex) {
visited[vIdx] = true;
System.out.println("visiting " + g.adjLists[vIdx].name);
traversedVertex.add(g.adjLists[vIdx]);
for (AdjacentNode curr=g.adjLists[vIdx].adjList; curr != null; curr=curr.next ) {
if (!visited[curr.vertexNum]) {
System.out.println("\n" + g.adjLists[vIdx].name + "--" + g.adjLists[curr.vertexNum].name);
dfs(g, curr.vertexNum, visited, traversedVertex);
}
}
}
//read here then go up
public void dfs(GraphAL g, ArrayList traversedVertex) {
boolean[] visited = new boolean[g.currentNumberVertices];
for (int vIdx=0; vIdx < visited.length; vIdx++) {
if (!visited[vIdx]) {
System.out.println("\nSTARTING AT " + g.adjLists[vIdx].name);
dfs(g,vIdx, visited,traversedVertex);
}
}
}
@Override
public ArrayList<Vertex> traverse(GraphAL g) {
ArrayList<Vertex> traversedOrder = new ArrayList<Vertex>();
dfs(g,traversedOrder);
return traversedOrder;
}
@Override
public Queue<Vertex> traverse1(GraphAL g) {
// TODO Auto-generated method stub
return null;
}
}