1.0 - Shortest Path

1.1 - Types of Shortest Paths

  1. Single-Pair Given a pair (u,v)(u, v), find a shortest path between them
  2. Single-Source Given a vertex vv, find a shortest path to every other vertex
  3. Single-Destination Given a vertex vv, find a shortest path from every other vertex
  4. All-Pairs Given a graph, find a shortest path between every pair of vertices

1.1.1 - Single-Source Shortest Path

Given a graph GG, with weight function ww and a source vertex ss, calculate for each vertex:

The steps to this algorithm are:

  1. Initialise s.d=0s.d=0 and v.d=0v.d=0 for all vertices vG.V{s}v\in G.V-\{s\}
    • We have the invariant that distance(s,v)v.d\text{distance}(s,v)\le v.d\le \infty
  2. Initialise v.π=NULLv.\pi=\text{NULL} for all vertices vG.Vv\in G.V
    • Invariant v.πv.\pi is v’s predecessor on the shortest path found so far
  3. Improve upper bound estimates until they reach a solution

Can we improve our estimate distance(s,v)v.d\text{distance}(s,v)\le \color{lightgreen}v.d based on:

We know that:

distance(s,v)distance(s,u)+w(u,v)u.d+w(u,v) \def\dist{\text{distance}} \dist(s,v)\le\dist(s, u)+{\color{pink}w(u,v)}\le {\color{pink}u.d}+{\color{pink}w(u,v)}

And thus, we know that

distance(s,v)min(v.d,u.d+w(u,v)) \def\dist{\text{distance}} \dist(s,v)\le\min({\color{lightgreen}v.d}, {\color{pink}u.d + w(u,v)})

1.1.2 - Edge Relaxation Algorithm

relax(u, v, w): improves estimate distance(s,v) ≤ v.d if possible, based on: our estimate distance(s, u) ≤ u.d and the existence of edge (u,v) with weight w(u,v)
relax(u, v, w) if v.d > u.d + w(u,v) v.d = u.d + w(u,v) v.π = u

1.1.3 - Edge Relaxation

Let

pn=v1v2v3vn p_n=v_1\rightarrow v_2\rightarrow v_3\rightarrow\cdots\rightarrow v_n

be a shortest path from v1v_1 to vnv_n

If edge (vi1,vi)(v_{i-1},v_i) is relaxed after a shortest path to vi1v_{i-1} is found, we will find a shortest path to viv_i

We can relax edges in a wa y that guarantees that we find shortest paths from our source vertex ss to every other vertex vG.Vv\in G.V (and is efficient)?

1.2 - Dijkstra’s Algorithm

🌱 Dijkstra’s algorithm is a generalisation of Breadth First Search, but for weighted graphs.

1.2.1 - Shortest Path Algorithm - Steps

1.2.2 - Efficient Implementation of Dijkstra

🌱 How do we (efficiently) find the next vertex to visit?

1.2.3 - Why does Dijkstra’s Algorithm Work?

1.2.4 - Dijkstra’s Algorithm

Dijkstra(G, w, s) // (G) Graph, (w) weight function, (s) source vertex init_single_source(G, s) S = ∅ // S is the set of visited vertices Q = G.V // Q is a priority queue, maintaining G.V - S while Q != ∅ u = extract_min(Q) S = S ∪ {u} for each vertex in G.adj[u] relax(u, v, w) init_single_source(G, s) for each vertex v in G.V v.d = ∞ v.pi = NIL s.d = 0 relax(u, v, w) if v.d > u.d + w(u,v) v.d = u.d + w(u,v) v.pi = u

1.2.5 - Analysis of Dijkstra’s Algorithm