Chapter 5: Graphs (part II)

Single-Source Shortest paths
18:16

Problem

Given a directed graph with nonnegative weights on the edges and a source vertex , find a shortest path from to any .

Remark

  • We might only care about one pair of vertices, but these problems have the same time complexity.
  • If the weights are all one, this was solved by .
  • Later: negative weights with Bellman-Ford.

Animation
18:16

Dijkstra's algorithm: idea
18:16

  • Start at
  • Keep a heap with a good guess of the distance
  • Go to the next vertex in the queue
  • Update distances for the neighbours of that vertex

Dijkstra's algorithm
18:16

Loading

Dijkstra: correctness and complexity
18:16

Proposition (Dijkstra: correctness)

After a vertex leaves the queue,

Sketch proof

  • Assume we're about to add
  • Shortest path:

Proposition

The time complexity is

A*
18:16

Exercise

Implement the algorithm, which brings the following changes to Dijkstra:

  • Stop as soon as we've reached our goal
  • Instead of pushing the distance to the heap, use

Apply A* to the graph below:

Exercises: A*
18:16

Exercise

How would you find the actual shortest path?

Define a heuristic to avoid a particular airport

Exercise

Why doesn't the algorithm work for negative weights?

Exercises
18:16

Dijkstra: recap
18:16

Bellman-Ford
18:16

Dijkstra is an efficient algorithm if the weights associated with the edges are nonnegative.

Example

Apply Dijkstra to this graph (we'll apply Bellman-Ford afterwards) with as a starting point

Bellman-Ford: implementation
18:16

Exercise

Implement Bellman-Ford to find the length of the shortest path. What's its time complexity?

Loading

Exercise

How would you get the actual path as well?

Bellman-Ford: correctness
18:16

Theorem (Correctness of Bellman-Ford)

Assume has no negative-weight cycles that are reachable from .

At the end of the algorithm,

for all vertices that are reachable from .

Sketch proof

  • Let be a shortest path.
  • Loop invariant: at the beginning of each iteration

Detecting negative-weight cycles
18:16

Proposition

A graph contains a negative cycle reachable from if and only if one edge can be relaxed at the end of Bellman-Ford.

Sketch proof

  • : previous correctness result
  • :

Detecting negative-weight cycles: implementation
18:16

Exercise

Change your Bellman-Ford code so that it detects negative cycle reachable from the source

Loading

Exercises
18:16

All Pairs Shortest Paths
18:16

Problem

Find the shortest path between all pairs of vertices

Question

What would the time complexities be if we applied Bellman-Ford or Dijkstra and iterate through all vertices for the source node?

We'll try to use dynamic programming to get a better time complexity.

1st attempt: guess next-to-last vertex
18:16

Subproblem
DP(u, v, k): length of shortest path using at most edges
Guess
Next-to-last vertex
Base cases
Recurrence

Python implementation of the first attempt
18:16

Loading

Exercise

What is the time complexity?

Combining DP and D&C
18:16

Question

What if we guess the middle vertex instead of the penultimate? What would the time complexity be?

Floyd-Warshall
18:16

Before, APSP(u, v, k) added the constraint that the SP used at most edges. The guessing part is .

Idea

FW(u, v, k): minimum weight of a path via first vertices.

Subproblem
FW(u, v, k): minimum weight of path via first vertices
Guess
Should we include the th vertex?
Base cases
Recursion
Time complexity

Python implementation of Floyd-Warshall
18:16

Loading