[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]

Back to Simple Graph Algorithms. On to Graphs, Concluded.

**Held** Friday, December 3, 1999

**Overview**

Today, we consider a variety of techniques for solving the
*shortest path problem*, including an adapation of
the breadth-first reachability solution and a brute-force method.

**Notes**

- Any final questions on exam 3?
- A: Since you'll need a comparator for the individual elements,
you can make it a parameter to the constructor for the class.
__public____class__ListComparator__implements__Comparator { ...**/** * Create a new list comparator that compares individual * list elements using compare. */**__public__ListComparator(Comparator compare) { ... } // ListComparator(Comparator) ... } - A: Some of you have asked how we ensure that the two things to
be compared are both
`CursoredList`

s.**/** * Determine if two lists are equal. */**__public____void__equals(Object a, Object b)__throws__IncomparableException { CursoredList listA; CursoredList listB;__if__(a__instanceof__CursoredList) { listA = (CursoredList) a; }__else__{__throw____new__IncomparableException("*Not a cursored list*"); }__if__(b__instanceof__CursoredList) { listB = (CursoredList) b; }__else__{__throw____new__IncomparableException("*Not a cursored list*"); } ... } // equals(Object,Object) - Alternatively
__public____void__equals(Object a, Object b)__throws__IncomparableException {__try__{ CursoredList listA = (CursoredList) a; CursoredList listB = (CursoredList) b;**// ...**__return__...; }__catch__(ClassCastException e) {__throw____new__IncomparableException("*Not a cursored list*"); } } // equals(Object,Object)

- A: Since you'll need a comparator for the individual elements,
you can make it a parameter to the constructor for the class.

**Contents**

**Summary**

- Reachability, revisited
- The shortest path problem
- A greedy solution

- Last class, we saw that it is possible to write a recursive, depth-first, connectivity algorithm.
- Is it possible to write a breadth-first connectivity algorithm?
- Hint: Think about the "how to find the best solution to a puzzle" discussion.

- The
*shortest path*algorithm finds the shortest path from A to B in a graph. The shortest path from A to B is a path from A to B whose cost (determined by some cost function) is no greater than any other path from A to B. - The algorithm generally applies to directed, weighted graphs.
- The cost function is most often the sum of the weights of the edges on
the path, but it might involve other combinations, such as
- the average of the weights
- the product of the weights
- some function of weights and number of edges
- ...

- Typically, the weights on the graph are nonnegative.
- If the graph includes negative weights and cycles, there may be no shortest path, as there may be a cycle which decreases the cost each time it is taken.

- Depending on the algorithm we develop, there may also be other restrictions on weights, structure, or cost function to ensure that there is a clear shortest path.

- If the graph is unweighted, a slight variant of
the breadth-first connectivity algorithm should work fine.
- Instead of enqueing just labels, we enqueue labels and the distance to that label.

- However, this won't work for weighted graphs (sometimes six edges have less total weight than two).

- In general, shortest paths don't involve cycles. If we can guarantee
that this is the case (e.g., if all weights are positive), then
there is a very simple method for finding shortest path.
- List all acyclic paths.
- Compute the cost of each path.
- Pick the smallest such cost.

- This guarantees that we get a correct answer. Why? Because it explicitly matches the definition of shortest path.
- Are there disadvantages? Possibly.
- What is the running time of this algortihm?
- It is clearly proportional to the number of paths in the graph times the cost of determing the cost of each path.
- We've already seen that the number of paths is O(
*n*!) - Note that for graph algorithms, we tend to use
*n*for the number of nodes in the graph and*m*for the number of edges.

Tuesday, 10 August 1999

- Created as a blank outline.

Thursday, 2 December 1999

- Filled in introductory material.
- Moved discussion of shortest path from previous outline.
- Added further discussion from outline 53 of CS152 99S.
- Reformatted.

Monday, 6 December 1999

- Moved Dijkstra's algorithm to the next outline.

Back to Simple Graph Algorithms. On to Graphs, Concluded.

[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [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/99F/Outlines/outline.52.html

Source text last modified Mon Dec 6 09:45:14 1999.

This page generated on Mon Dec 6 09:56:46 1999 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu