-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPath.java
More file actions
84 lines (59 loc) · 2.87 KB
/
FindPath.java
File metadata and controls
84 lines (59 loc) · 2.87 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
// Implementation of graph search/traversal methods
// package GraphGame;
import java.util.Iterator;
import java.util.Stack;
public class FindPath<V> {
/* METHODS */
// Returns true if there is a ”frozen” path between vertices v and u in graph g
public boolean markedPathExists(Graph<V> g, Vertex<V> v, Vertex<V> u) {
// Iterate through graph's vertices and reset all to 'unvisited'
Iterator<Vertex<V>> iter = g.vertices();
while(iter.hasNext()) { iter.next().setMarker(false); }
// Create stack to store path between start and end vertices
Stack<Vertex<V>> s = new Stack<Vertex<V>>();
Stack<Vertex<V>> stack = pathDFS(g, v, u, s, true);
// If path exists, return true; otherwise false
if (stack != null)
return true;
return false;
}
// If there is a path between vertices u and v in graph g, returns an iterator of path; otherwise, returns null
public Iterator<Vertex<V>> givePath(Graph<V> g, Vertex<V> v, Vertex<V> u) {
// Iterate through graph's vertices and reset all to 'unvisited'
Iterator<Vertex<V>> iter = g.vertices();
while(iter.hasNext()) { iter.next().setMarker(false); }
// Create stack to store path between start and end vertices
Stack<Vertex<V>> s = new Stack<Vertex<V>>();
Stack<Vertex<V>> stack = pathDFS(g, v, u, s, false);
// If path exists, return its iterator; otherwise null
if (stack != null)
return stack.iterator();
return null;
}
/* HELPER METHODS */
// Traverse graph via depth-first search to determine a path between vertices v and u; return stack holding path or empty stack if no path was found
// Condition parameter determines whether edge status is relevant (ie. true if path must only contain frozen edges; false if irrelevant)
private Stack<Vertex<V>> pathDFS(Graph<V> g, Vertex<V> v, Vertex<V> u, Stack<Vertex<V>> stack, boolean condition) {
v.setMarker(true); // Mark current vertex as 'visited'
stack.push(v); // Store vertex in path stack
if (v == u) // If destination vertex has been reached, return current stack
return stack;
Edge<V> edge;
Vertex<V> w;
Stack<Vertex<V>> result;
Iterator<Edge<V>> iter = v.incidentEdges();
// Iterate through edges in adjacency list of current vertex
while(iter.hasNext()) {
edge = iter.next();
w = g.giveOpposite(v, edge); // Get opposite endpoint
// Build path via opposite endpoint if this vertex is unvisited (and potentially, if connecting edge is frozen)
if (w.getMarker() == false && (edge.getMarker() || !condition)) {
result = pathDFS(g, w, u, stack, condition); // Recursively build path
if (result != null) // until base case (detination vertex u) is found
return result;
}
}
stack.pop(); // If destination vertex u could not be found by following this path, move to previous vertex
return null; // and pursue a new path via another adjacent vertex
}
} // End of class