Dynamic Programming 3: Shortest Paths Flashcards

1
Q

Why do we like to have algorithms for directed graphs rather than undirected graphs?

A

Because we can encode any undirected graph as a directed graph using anti-parallel edges (directed edges from node A to B and node B to A)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the major constraint on Djikstra’s algorithm for finding shortest paths?

A

Edge weights can’t be negative!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a negative weight cycle?

A

A cycle in which the sum of weights is negative.If you take a negative weight cycle from node A back to A repeatedly, you make the distance shorter each time!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does it mean for the shortest path problem if the graph has a negative weight cycle?

A

It means the problem is no longer well-defined (you can keep following the NWC and get an ever larger negative distance)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe the algorithm for the single source shortest path DP algorithm (Bellman-Ford)

A

Given directed graph G with source s and assuming no negative weight cycles…

Subproblem:
D(i, z) = length of shortest path from s to z using <= i edges, where 0 <= i <= n-1 and z is a node in G.

Recurrence:
D(0, s) = 0, and D(0, z) = inf for all z != s
D(i, z) = min{D(i-1, z), min{D(i-1, y) + w(y, z)}} (double min!)

Pseudocode: (loosely)
Base cases
Iterate over all i = 1->(n-1)
Iterate over all nodes z
Iterate over all nodes with edges going to z
Then do our min calculations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we detect a negative weight cycle when computing single source shortest path DP (Bellman-Ford)?

A

As we fill out our table (nodes on the x axis and i on the y axis), we will eventually get a row where the values decrease, i.e., D(n, z) < D(n-1, z)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe the (good) algorithm for the all-pairs shortest path DP algorithm (Floyd-Warshall)

A

Given directed graph G with edge weights w(e) and assuming no negative weight cycles…

Subproblem:
Condition on a prefix of intermediate vertices. Let v_1, …, v_n be vertices.
for 0 <= i <= n and 1 <= s, t <= n, let d(i, s, t) be the shortest distance from s to t using only vertices v_1, …, v_i as intermediate vertices.

Recurrence:
D(0, s, t) = w(st) if there is an edge from s to t, inf otherwise
For i >= 1, D(i, s, t) = min(D(i-1, s, t), D(i-1, s, i) + D(i-1, i, t))

Pseudocode: (loosely)
For all pairs s and t, if st exists then D(0, s, t) = w(st), otherwise inf
for all i in 1->n, for all s in 1->n, and for all t in 1->n, D(i, s, t) = min(D(i-1, s, t), D(i-1, s, i) + D(i-1, i, t))
return D(n, s, t)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why is the O(n^3) Floyd-Warshall algorithm for all-pairs shortest paths better than the O(n^2m) naive reapplication of Bellman-Ford to all-pairs?

A

Because m, the number of total edges in the graph, can be as large as n^2, so O(n^2m) could be as bad as O(n^4).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between the basic DP idea of Bellman-Ford (single-source shortest path) and Floyd-Warshall (all-pairs)?

A

The input to the subproblem in Bellman-Ford is a prefix of the number of edges, while the input for the Floyd-Warshall subproblem is a prefix of the number of nodes/vertices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we detect a negative weight cycle when computing all-pairs shortest path DP (Floyd-Warshall)?

A

Check if any diagonal entry in our table is negative! i.e., D(n, y, y) < 0 for some y in V.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly