[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
Held: Friday, May 1, 1998
static boolean pathBetween(GraphNode source, GraphNode destination) {
// Base case
if (source.equals(destination)) return true;
// Recursive case: see if there's a path from
// a neighbor to the destination.
for(Iterator neighbors = source.getNeighbors());
neighbors.hasMoreElements(); ) {
GraphNode neighbor = neighbors.nextElement();
if pathBetween(neighbor,destination) return true;
}
// If we reach this point, we've effectively tried every
// possible way to reach the destination. It must not
// be possible.
return false;
} // reachable
+----+ | ^ v | A -> C -> D -> B
Is there a path from A to B?
Is B equal to A? No.
Pick some neighbor of A. C is the only one.
Is there a path from C to B?
Is B equal to C? No.
Pick some neighbor of C. Let's pick A.
Is there a path from A to B?
Is B equal to A? No.
Pick some neighbor of A. C is the only one.
Is there a path form C to B?
...
static boolean pathBetween(GraphNode source, GraphNode destination) {
// Note that we've seen the source.
mark(source);
// Base case: we're already there.
if (source.equals(destination)) return true;
// Recursive case: try all the neighbors.
for(Iterator neighbors = source.getNeighbors());
neighbors.hasMoreElements(); ) {
GraphNode neighbor = (Node) neighbors.nextElement();
if (!marked(neighbor) && pathBetween(neighbor,destination)) return true;
}
// Tried everything. Give up.
return false;
} // reachable
/**
* Determine if there is a path from source to destination in the
* "current" graph. nodes_to_check is used to govern the order
* in which nodes are checked. If it is a queue, we do a breadth-first
* search. If it is a stack, we do a depth-first search.
*/
public boolean pathBetween(GraphNode source,
GraphNode destination,
Linear nodes_to_check) {
// Clear the collection of nodes_to_check for safety's sake.
nodes_to_check.clear();
// Add the source.
mark(source);
nodes_to_check.add(source);
// Keep going until we reach the destination or run out of
// nodes.
while (!nodes_to_check.empty()) {
GraphNode next_node = nodes_to_check.remove();
// Have we found it?
if (next_node.equals(destination) {
return true;
}
// If we haven't found it, note that we should check all
// of its neighbors.
else {
Iterator neighbors = next_node.getNeighbors();
while (neighbors.hasMoreElements()) {
nodes_to_check.add(neighbor.nextElement());
}
} // else
} // while there are nodes left to check
// We've checked all the nodes reachable from the source
// node. None of them is the destination. Give up.
return false;
} // pathBetween
On to Dijkstra's Shortest Path Algorithm
Back to Introduction to Graphs
Outlines:
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
Current position in syllabus
[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/98S/home/rebelsky/public_html/Courses/CS152/98S/Outlines/outline.49.html
Source text last modified Tue Jan 12 11:56:52 1999.
This page generated on Mon Jan 25 09:50:06 1999 by SiteWeaver. Validate this page.
Contact our webmaster at rebelsky@math.grin.edu