Repository navigation
Expand file tree
/
Copy pathGraphSearch.java
More file actions
183 lines (166 loc) · 6.1 KB
/
Copy pathGraphSearch.java
File metadata and controls
183 lines (166 loc) · 6.1 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class GraphSearch {
static ArrayList<GraphNode> DFSRec(final GraphNode start, final GraphNode end)
{
ArrayList<GraphNode> path = new ArrayList<>();
Stack<GraphNode> stack = new Stack<>();
stack = DFSRecHelper(start, end, path, stack);
//Add everything from stack to "path" in reverse
while(!stack.isEmpty())
path.add(0, stack.pop());
return path;
}
static Stack<GraphNode> DFSRecHelper(GraphNode start, GraphNode end, ArrayList<GraphNode> path, Stack<GraphNode> stack)
{
stack.push(start);
//Once the end is found, return the path
if (!stack.isEmpty() && stack.peek() == end) {
return stack;
}
//If node has already been visited, keep going
if (start.visited) {
stack.pop();
return stack;
}
//Set visited to true and check neighbors
start.visited = true;
for (Edge edge : start.neighbors) {
DFSRecHelper(edge.destination, end, path, stack);
if (stack.peek() == end)
break;
}
return stack;
}
static ArrayList<GraphNode> DFSIter(Graph graph, final GraphNode start, final GraphNode end)
{
Stack<GraphNode> stack = new Stack<>();
ArrayList<GraphNode> path = new ArrayList<>();
GraphNode curr = start;
// Keep going until you find the end node
while (curr != end)
{
path.add(curr);
// If already visited, continue
if (curr.visited)
{
path.remove(path.size()-1);
continue;
}
// If disconnect from graph, remove from path
if (curr.neighbors.isEmpty())
{
path.remove(path.size()-1);
}
curr.visited = true;
int count=0;
// Add each edge to the stack
for (Edge edge : curr.neighbors)
{
if (!edge.destination.visited) {
stack.push(edge.destination);
count++;
}
}
// If all the neighbors have been visited, remove the curr node from the path (not in the path)
if (count==0)
path.remove(path.size()-1);
if (stack.isEmpty())
return null;
curr = stack.pop();
}
path.add(curr);
return path;
}
static ArrayList<GraphNode> BFTRec(Graph graph)
{
ArrayList<GraphNode> path = new ArrayList<>();
Queue<GraphNode> queue = new LinkedList<>();
ArrayList<Boolean> visited = new ArrayList<>();
// Keep track of how many unvisited nodes there are
for (GraphNode node : graph.getAllNodes())
visited.add(false);
graph.get(0).visited = true;
visited.remove(0);
queue.add(graph.get(0));
return BFTRecHelper(graph, queue, path, visited);
}
static ArrayList<GraphNode> BFTRecHelper(Graph graph,Queue<GraphNode> queue, ArrayList<GraphNode> path, ArrayList<Boolean> visited)
{
// If all nodes have been visited and nothing left to check, finished
if (queue.isEmpty() && visited.isEmpty())
return path;
// Check nodes that aren't connected to graph
if (queue.isEmpty() && !visited.isEmpty())
{
for (int i = 0; i < graph.getAllNodes().size(); i++)
{
if (!graph.get(i).visited)
{
queue.add(graph.get(i));
}
}
}
GraphNode curr = queue.poll();
path.add(curr);
// Add all the neighbors to the queue if they've not been visited
if (!curr.neighbors.isEmpty()) {
for (Edge edge : curr.neighbors) {
if (!edge.destination.visited) {
edge.destination.visited = true;
visited.remove(0);
queue.add(edge.destination);
}
}
BFTRecHelper(graph, queue, path, visited);
}
return path;
}
static ArrayList<GraphNode> BFTIter(final Graph graph)
{
ArrayList<GraphNode> path = new ArrayList<>();
ArrayList<Boolean> visited = new ArrayList<>();
Queue<GraphNode> queue = new LinkedList<>();
for (int i = 0; i < graph.getAllNodes().size(); i++)
visited.add(false);
graph.get(0).visited = true;
visited.remove(0);
GraphNode curr = graph.get(0);
path.add(curr);
while (visited.size() != 0) //All nodes not visited
{
// Add neighbors to the queue if they haven't been visited
if (!curr.neighbors.isEmpty()) {
for (Edge edge : curr.neighbors) {
if (edge.destination.visited) {
continue;
}
edge.destination.visited = true;
visited.remove(0);
queue.add(edge.destination);
path.add(edge.destination);
}
}
if (queue.peek() != null) {
curr = queue.poll();
continue;
}
// If there are no more connected nodes in that part of the graph, look for another one
// Look for a node that hasn't been visited and add it to the queue to BFS again
for (int p = 0; p < graph.getAllNodes().size(); p++)
{
if (!graph.get(p).visited)
{
curr = graph.get(p);
path.add(graph.get(p));
graph.get(p).visited = true;
visited.remove(0);
break;
}
}
}
return path;
}
}