(W7) Dynamic Programming Flashcards

(10 cards)

1
Q

What does the Bellman-Ford algorithm do that Dijkstras doesn’t?

A

can calculate the shortest path even if there are negative edge weights

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

What are the 2 key ideas of Bellman-Ford?

A

Cycles with positive weights won’t be part of the shortest path

Any simple path has at most |V|-1 edges

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

What is the Bellman-Ford Algorithm?

A

Create a distance and previous array
Mark the starting distance as 0

Iterate over |V|-1 times:
- for each edge (in the whole graph)
- get the updated distance = dist[u] + w(u,v)
- if the distance < distance[v] -> update distance[v] and prev[v] = u

Then - do a final loop check
for each edge in the entire graph
- if distance[u] + w(u,v) < distance[v]
- return an error

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

What is the complexity (time/space) of Bellman-Ford algorithm

A

Time: O(VE)
Space: O(V)

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

What problem does the Floyd-Warshall problem try and solve?

A

Compute the shortest distance between any 2 pairs of vertices

Is able to work with negative edge weights

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

What is the Floyd-Warshall Algorithm?

A

Create an adjacency matrix called dist[][] and preload each edge (u,v) with weight(u,v)

For each vertex k in the graph
- for each pair of vertices (u,v)
if distance[u][k] + distance[k][v] is less than distance[u][v]
- then we have found a shortcut
- distance[u][v] = distance[u][k] + distance[k][v]

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

What is the complexity of Floyd-Warshall?

A

Time O(V^3) - this should be obvious from the 3x internal loop
Space (V^2) - creating the distance matrix

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

What is the key Floyd-Warshall invariant?

A

For a graph without negative cycles, after the k-th iteration, the distance[i][j] contains the weight of the shortest path that only uses the intermediate nodes from the set {1,…,k}

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

What properties can you determine about a graph from the Floyd-Warshall distances matrix?

A

Connectivity - if the distance between u,v is None or Inf - then there is no connecting path

Negative Cycles - if there is any distance[i][i] < 0, then there is a negative cycle

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

What is transitive closure?

A

It’s a graph G that only contains an edge between (u,v) if there is a path between (u,v) in the main graph

it can be found using the floyd-warshall algorithm

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