# Class 52: Shortest Path

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)
```

Contents

Summary

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

## Connectivity, Revisited

• 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.

## Shortest Path

• 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.

### Using The Breadth-First Connectivity Algorithm

• 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).

### A Brute-Force Shortest Path Algorithm

• 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.
• 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.

### History

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.

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.