Given a directed graph with nonnegative weights on the edges and a source vertex s∈V, find a shortest path from s to any v∈V.
After a vertex v leaves the queue,
distances[v]=δ(s,v)Sketch proof
The time complexity is O((V+E)logV)
Implement the A⋆ algorithm, which brings the following changes to Dijkstra:
Apply A* to the graph below:
How would you find the actual shortest path?
Define a heuristic to avoid a particular airport
Why doesn't the algorithm work for negative weights?
Dijkstra is an efficient algorithm if the weights associated with the edges are nonnegative.
Apply Dijkstra to this graph (we'll apply Bellman-Ford afterwards) with A as a starting point
Implement Bellman-Ford to find the length of the shortest path. What's its time complexity?
How would you get the actual path as well?
Assume G has no negative-weight cycles that are reachable from s.
At the end of the algorithm,
d[v]=δ(s,v).for all vertices v that are reachable from s.
A graph contains a negative cycle reachable from s if and only if one edge can be relaxed at the end of Bellman-Ford.
Change your Bellman-Ford code so that it detects negative cycle reachable from the source
Find the shortest path between all pairs of vertices
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.
DP(u, v, k)
: length of shortest path u⇝v using at most k edges
What is the time complexity?
What if we guess the middle vertex instead of the penultimate? What would the time complexity be?
Before, APSP(u, v, k)
added the constraint that the SP used at most k edges. The guessing part is O(V).
FW(u, v, k)
: minimum weight of a path via first k vertices.
FW(u, v, k)
: minimum weight of path via first k vertices