# Class 52: Reachability, Revisited; Shortest Path

Back to Reachability. On to Shortest Path, Revisited; Graphs, Concluded.

Held Friday, May 7

Summary

• Testing our algorithm
• The shortest-path problem (revising reachability): You can get there from here. But how quickly?

Contents

Notes

• We're demonstrating the projects on Monday, to a large audience, so make sure to have things working.
• At minimum, we need one GUI, the network interface (and related parts) and the database working.
• Barring unforseen circumstances, I should be in my office most of the weekend, working hard on a variety of projects. Feel free to interrupt me with questions.
• Attendance is required for all classes remaining this semester.
• If you are sick, you need a note from a doctor (health services doesn't count).
• No sports events allowed during 14th week, sorry. (Exceptions made for playoffs.)
• The picnic is now officially scheduled for Friday, May 14, from 5pm to whenever. Please put a check next to your name if you still plan to come, and cross of your name if you don't.

In today's class, we continued the student-led discussion of reachability and shortest-path. Some post-class notes have been added to the discussion.

## Reachability, Revisited

By the end of yesterday's class, you'd come up with the following for your algorithm for determining whether Y is reachable from X (there is a path from X to Y).

``` 0: boolean reachable(NodeLabel X, nodeLabel Y)
1:   if there is an edge from X to Y then
2:     return true
3:   else
4:     mark X
5:     while there are unmarked neighbors of X, N
6:       if reachable(N, Y) then
7:         return true
08:       end if
09:     end for
11:     // At this point, we've looked at every reasonable path
12:     // from X and haven't found a way to Y.  There is none.
13:     return false
14:   end if
13: end reachable
```

Evan suggested that we consider it in some more depth on something like the following example

``` +----------------------A8
|                       |
|    A0-----A1-----A2   A9
|     |\    |
A6-A7 | \   A10
|    A4--A3
|        |
A5-------+
```
• What does our algorithm do when given `reachable(A0,A9)`?
• Are you sure that the while loop terminates?
• In running your code, you're likely to report that A9 is not reachable from A2.
• Is that really the case?
• What does this say about the way that we should describe the algorithm?

### Tracing the execution of reachable

```reachable(A0,A9)
1: There is not an edge from A0 to A9
4: A0 is marked
5: A1 is an unmarked neighbor
6: Call reachable(A1,A9) and wait for result
reachable(A1,A9)
1. There is not an edge from A1 to A9
4: A1 is now marked
5: A2 is an unmarked neighbor of A1
6: Call reachable(A2,A9) and wait for result
reachable(A2,A9)
1. There is not an edge from A2 to A9
4. A2 is now marked
5. There are no unmarked neighbors of A2
13. Returns false
6. The recursive call failed.
9. Go back to the top of the loop
5. A10 is an unmarked neighbor of A1
6: Call reachable(A10,A9) and wait for result
reachable(A10,A9)
1. There is not an edge from A10 to A9
4. A10 is now marked
5. There are no unmarked neighbors of A10
13. Returns false
6. The recursive call failed.
9. Go back to the top of the loop
5. There are no unmarked neighbors of A1
13. Return false
6. The recursive call failed.
9. Go back to the top of the loop
5. A3 is an unmarked neighbor of A0.
6. Call reachable(A3,A9) and wait for result
reachable(A3,A9)
1. There is not an edge from A3 to A9
5. A4 is an unmarked neighbor of A3
6: Call reachable(A4,A9) and wait for result
reachable(A4,A9)
1. There is not an edge from A4 to A9
4. A4 is now marked
5. There are no unmarked neighbors of A4
13. Returns false
6. The recursive call failed.
9. Go back to the top of the loop
5. A5 is an unmarked neighbor of A3
6: Call reachable(A5,A9) and wait for result
reachable(A5,A9)
1. There is not an edge from A5 to A9
4. A5 is now marked
5. A6 is an unmarked neighbor of A5
6. Call reachable(A6,A9) and wait for result
reachable(A6,A9)
1. There is not an edge from A6 to A9
4. A6 is now marked
5. A7 is an unmarked neighbor of A5
6. Call reachable(A7,A9) and wait for result
reachable(A7,A9)
1. There is not an edge from A7 to A9
4. A7 is now marked
5. There are no unmarked neighbors of A7
13. Returns false
6. The recursive call failed.
9. Go back to the top of the loop
5. A8 is an unmarked neighbor of A5
6. Call reachable(A8,A9) and wait for result
reachable(A8,A9)
1. There is an edge from A8 to A9
2. Returns true
6. The recusive call succeeded
7. Returns true
6. The recusive call succeeded
7. Returns true
6. The recusive call succeeded
7. Returns true
6. The recusive call succeeded
7. Returns true
```

### Termination

The loop terminates because it scans through unmarked nodes, and recursive calls either mark nodes or return true (in which case the loop also terminates). Since there are only a finite number of nodes, we'll eventually mark them all or return true.

### Reachable, Redefined

Our particular algorithm determines whether there is a path from X to Y using only unmarked nodes.

## Shortest Path

• The shortest path algorithm finds the shortest path from X to Y in a graph. The shortest path from X to Y is a path from X to Y whose cost (determined by some cost function) is no greater than any other path from X to Y.
• 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.

### Strategies

• Assume you could list all the paths in the graph, what solution does that give you?
• Find the length of each path and take the smallest
• In the worst case, there are n! paths
• What restrictions do you need on the graph?
• No negative edges
• Think about better strategies for the next class.

History

• Created Monday, January 11, 1999.
• Added short summary on Friday, January 22, 1999.
• Filled in notes on Friday, May 7, 1999.
• Filled in approximate record of in-class discussions on Saturday, May 9, 1999.

Back to Reachability. On to Shortest Path, Revisited; 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.