[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Introduction to Graphs. On to Reachability, Revisited; Shortest Path.
Held Wednesday, May 5
Summary
Contents
Notes
listField
method. However, since the database doesn't
rely on it, it's not a big problem.
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 each neighbor of source { 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. foreach neighbor of source if (!marked(neighbor) && pathBetween(neighbor,destination)) return true; } // Tried everything. Give up. return false; } // reachable
History
Back to Introduction to Graphs. On to Reachability, Revisited; Shortest Path.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [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/99S/Outlines/outline.51.html
Source text last modified Tue May 4 16:46:42 1999.
This page generated on Sun May 9 09:59:43 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu