Graph Algorithms Graphs Nodes/vertexes: Edges: (undirected) - - PowerPoint PPT Presentation

graph algorithms
SMART_READER_LITE
LIVE PREVIEW

Graph Algorithms Graphs Nodes/vertexes: Edges: (undirected) - - PowerPoint PPT Presentation

Graph Algorithms Graphs Nodes/vertexes: Edges: (undirected) (directed) b a Representations of graph G with vertices V and edges E c (u, v) E V x V adjacency-matrix A: A u, v = 1 a b c Size: |V| 2 a 0 1 0 b 1 0 1


slide-1
SLIDE 1

Graph Algorithms

slide-2
SLIDE 2

Graphs

Nodes/vertexes: Edges: (undirected) (directed)

slide-3
SLIDE 3

Representations of graph G with vertices V and edges E

  • V x V adjacency-matrix A: Au, v = 1 ⇔

(u, v) ∈ E Size: |V|2 Better for dense graphs, i.e., |E| = Ω(|V|2)

  • Adjaceny-list, e.g. (v1 , v5 ), (v1 , v17 ), (v2 , v3 ) …

Size: O(E) Better for sparse graphs, i.e., |E| = O(|V|)

b a

c

a b c a 1 b 1 1 c 1

Adj[a] = b Adj[b] = a,c Adj[c] = b

slide-4
SLIDE 4

Next we see several algorithms to compute shortest distance δ(u,v) := shortest distance from u to v ∞ if v is not reachable from u Variants include weighted/unweighted, single-source/all-pairs Algorithms will construct vector/matrix d; we want d = δ Back pointers π can be computed to reconstruct path

slide-5
SLIDE 5

Breadth-first search Input: Graph G= (V,E) as adjacency list, and s ∈ V. Output: Distance from s to any other vertex

  • Discover vertices at distance k before those at distance k+1

Algorithm colors each vertex: White : not discovered. Gray : discovered but its neighbors may not be. Black : discovered and all of its neighbors are too.

slide-6
SLIDE 6

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; // a vertex with min distance d[u]; // checks neighbors Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-7
SLIDE 7

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-8
SLIDE 8

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-9
SLIDE 9

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-10
SLIDE 10

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-11
SLIDE 11

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-12
SLIDE 12

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-13
SLIDE 13

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-14
SLIDE 14

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-15
SLIDE 15

BFS(G,s) For each vertex u ∈ V[G] – {s} color[u]:= White; d[u] := ∞; π[u] :=NIL; Q:= empty Queue; color[s] := Gray; d[s] :=0; π[s] :=NIL; Enqueue(Q,s) While (|Q| > 0) { u := Dequeue(Q) for each v ∈ adj[u] if color[v] = white { color[v] := gray; d[v]:= d[u]+1; π[v]:=u; Enqueue(Q,v) } color[u]:=Black; }

slide-16
SLIDE 16

Running time of BFS in adjacency-list representation Recall Enqueue and Dequeue take time ?

slide-17
SLIDE 17

Running time of BFS in adjacency-list representation Recall Enqueue and Dequeue take time O(1) Each edge visited O(1) times. Main loop costs O(E). Initialization step costs O(V) Running time O(V + E) What about space?

slide-18
SLIDE 18

Space of BFS Θ(V) to mark nodes Optimal to compute all of d What if we just want to know if u and v are connected?

slide-19
SLIDE 19

Theorem: Given a graph with n nodes, can decide if two nodes are connected in space O(log2 n) Proof: REACH(u, v, n) := \\ is v reachable from u in n steps? Enumerate all nodes w { If REACH(u, w, n/2) and REACH(w, v, n/2) return YES } Return NO S(n) := space for REACH(u, v, n). S(n) := O(log n) + S(n/2). Reuse space for 2 calls to REACH. S(n) = O(log2 n) ฀

slide-20
SLIDE 20

Next: weighted single-source shortest path Input: Output: Directed graph G= (V,E), s ∈ V, w: E → Z Shortest paths from s to all the other vertces

  • Note: Previous case was for w : E → {1}
  • Note: if weights can be negative, shortest paths exist ⇔

s cannot reach a cycle with negative weight

b a

c

15 7

slide-21
SLIDE 21

Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v) At the end of the algorithm, can detect negative cycles by: for each edge (u,v) ∈ E[G] if d[v] > d[u]+w(u,v) Return Negative cycle return No negative cycle

slide-22
SLIDE 22

Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v)

slide-23
SLIDE 23

Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v)

slide-24
SLIDE 24

Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v)

slide-25
SLIDE 25

Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v)

slide-26
SLIDE 26

Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v)

slide-27
SLIDE 27

Running time of Bellman-Ford Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v) Time = ??

slide-28
SLIDE 28

Running time of Bellman-Ford Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v) Time = O(|V|.|E|)

slide-29
SLIDE 29

Analysis of Bellman-Ford(G,w, s) d[s] :=0; Set the others to ∞ Repeat |V| stages: for each edge (u,v) ∈ E[G] d[v] := min{ d[v], d[u]+w(u,v); } //relax(u,v)

  • Claim: d = δ if no negative-weight cycle exists.
  • Proof: Consider a shortest path s → u1 → u2 → … → uk

k ≤ n by assumtion. We claim at stage i = 1..|V|, d[ui ] = δ(s, ui) This holds by induction, because: d[ui ] = δ(s, ui) and relax ui → ui+1 ⇔ d[ui+1 ] = δ(s, ui+1). d is never increased d is never set below δ ฀

slide-30
SLIDE 30

Fact: Consider an algorithm that starts with d[s] = 0 and ∞ otherwise, and only does edge relaxations. Then d ≥ δ throughout

slide-31
SLIDE 31

Analysis of negative-cycle detection at the end of algorithm: for each edge (u,v) ∈ E[G] if d[v] > d[u]+w(u,v) Return Negative cycle return No negative cycle

  • Proof of correctness:

If not ∃ neg-cycle, d = δ, tests pass (triangle inequality). O.w. let u1 → u2 → … → uk = u1 so that ∑i < k w(ui , ui+1) < 0 We know ∀ i<k : d[ui+1] ≤ d[ui] + w(ui , ui+1) now what?

slide-32
SLIDE 32

Analysis of negative-cycle detection at the end of algorithm: for each edge (u,v) ∈ E[G] if d[v] > d[u]+w(u,v) Return Negative cycle return No negative cycle

  • Proof of correctness:

If not ∃ neg-cycle, d = δ, tests pass (triangle inequality). O.w. let u1 → u2 → … → uk = u1 so that ∑i < k w(ui , ui+1) < 0 We know ∀ i<k : d[ui+1] ≤ d[ui] + w(ui , ui+1) ∑i<k d[ui+1] ≤ ∑i<k d[ui] + ∑i<k w(ui , ui+1) ⇒ 0 < 0 ฀

slide-33
SLIDE 33

Dijkstra's algorithm Input: Directed graph G=(V,E), s ∈ V, non-negative w: E → N Output: Shortest paths from s to all the other vertices.

slide-34
SLIDE 34

Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) }

slide-35
SLIDE 35

Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Gray: the extracted u. Black: not in Q

slide-36
SLIDE 36

Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Gray: the extracted u. Black: not in Q

slide-37
SLIDE 37

Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Gray: the extracted u. Black: not in Q

slide-38
SLIDE 38

Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Gray: the extracted u. Black: not in Q

slide-39
SLIDE 39

Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Gray: the extracted u. Black: not in Q

slide-40
SLIDE 40

Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Gray: the extracted u. Black: not in Q

slide-41
SLIDE 41

Running time of Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Running time depends on data structure for Q Naive implementation, array: Extract-min in time ?

slide-42
SLIDE 42

Running time of Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Running time depends on data structure for Q Naive implementation, array: Extract-min in time |V| ⇒ running time = ?

slide-43
SLIDE 43

Running time of Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Running time depends on data structure for Q Naive implementation, array: Extract-min in time |V| ⇒ running time = O(V2 + E) Can we do better?

slide-44
SLIDE 44

Running time of Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Running time depends on data structure for Q Naive implementation, array: Extract-min in time |V| ⇒ running time = O(V2 + E) Implement Q with min-heap. Extract-min in time ?

slide-45
SLIDE 45

Running time of Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Running time depends on data structure for Q Naive implementation, array: Extract-min in time |V| ⇒ running time = O(V2 + E) Implement Q with min-heap. Extract-min in time O(log V) ⇒ running time = ?

slide-46
SLIDE 46

Running time of Dijkstra(G,w,s) d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Running time depends on data structure for Q Naive implementation, array: Extract-min in time |V| ⇒ running time = O(V2 + E) Implement Q with min-heap. Extract-min in time O(log V) ⇒ running time = O((V+E) log V) Note: Can be improved to V log V + E

slide-47
SLIDE 47

Analysis of Dijkstra d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Claim: When u is extracted, d[u] = δ(s,u)

slide-48
SLIDE 48

Analysis of Dijkstra d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Claim: When u is extracted, d[u] = δ(s,u) Proof: Let u ≠ s be first violation, and Q right before extract Let s → … → x → y → … → u be a shortest path, where s ∉ Q and y is first ∈Q Note d[x] = ?

slide-49
SLIDE 49

Analysis of Dijkstra d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Claim: When u is extracted, d[u] = δ(s,u) Proof: Let u ≠ s be first violation, and Q right before extract Let s → … → x → y → … → u be a shortest path, where s ∉ Q and y is first ∈Q Note d[x] = δ(s,x) (since u is first violation) d[y] = ?

slide-50
SLIDE 50

Analysis of Dijkstra d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Claim: When u is extracted, d[u] = δ(s,u) Proof: Let u ≠ s be first violation, and Q right before extract Let s → … → x → y → … → u be a shortest path, where s ∉ Q and y is first ∈Q Note d[x] = δ(s,x) d[y] = δ(s,y) (since u is first violation) (since x → y was relaxed) Then d[u] ? d[y] How do they compare?

slide-51
SLIDE 51

Analysis of Dijkstra d[s] :=0; Set others d to ∞; Q := V While (|Q|> 0) { u:= extract-remove-min(Q) // vertex with min distance d[u]; for each v ∈ adj[u] d[v] := min{ d[v], d[u]+w(u,v)} //relax(u,v) } Claim: When u is extracted, d[u] = δ(s,u) Proof: Let u ≠ s be first violation, and Q right before extract Let s → … → x → y → … → u be a shortest path, where s ∉ Q and y is first ∈Q Note d[x] = δ(s,x) d[y] = δ(s,y) (since u is first violation) (since x → y was relaxed) (because d[u] is minimum) Then d[u] ≤ d[y] = δ(s,y) ≤ δ(s,u) ฀

slide-52
SLIDE 52

All-pairs shortest paths Input:

  • Directed graph G= (V,E), and w: E → R

Output:

  • The shortest paths between all pairs of vertices.

if w ≥ 0

  • Run Dijkstra |V| times: O(V2 log V + E V)
  • Run Bellman-Ford |V| times: O(V2 E)
  • Next, simple algorithms achieving time about |V|3 for any w
slide-53
SLIDE 53

All-pairs shortest paths Dynamic programming approach: di,j(m) = shortest paths of lengths ≤ m di,j(m) = mink { di,k(m-1) + w(k,j) } (Includes k = j, w(j,j) = 0) Compute |V| x |V| matrix d(m) from d(m-1) in time |V|3. ⇒ d|V| computables in time |V|4 How to speed up?

slide-54
SLIDE 54

All-pairs shortest paths Note: di,j(m) = mink { di,k(m-1) + w(k,j) } Is just like matrix multiplication: d(m) = d(m-1) W, except + → min x → + Like matrix multiplication, this is associative. So, instead of doing d|V| = (...)W)W)W can do ?

slide-55
SLIDE 55

All-pairs shortest paths Note: di,j(m) = mink { di,k(m-1) + w(k,j) } Is just like matrix multiplication: d(m) = d(m-1) W, except + → min x → + Like matrix multiplication, this is associative. So, instead of doing d|V| = (...)W)W)W can do repeated squaring: Compute d(2) = W2 d(4) = ?

slide-56
SLIDE 56

All-pairs shortest paths Note: di,j(m) = mink { di,k(m-1) + w(k,j) } Is just like matrix multiplication: d(m) = d(m-1) W, except + → min x → + Like matrix multiplication, this is associative. So, instead of doing d|V| = (...)W)W)W can do repeated squaring: Compute d(2) = W2 d(4) = d(2) x d(2) = W2 x W2 d(8) = ?

slide-57
SLIDE 57

All-pairs shortest paths Note: di,j(m) = mink { di,k(m-1) + w(k,j) } Is just like matrix multiplication: d(m) = d(m-1) W, except + → min x → + Like matrix multiplication, this is associative. So, instead of doing d|V| = (...)W)W)W can do repeated squaring: Compute d(2) = W2 d(4) = d(2) x d(2) = W2 x W2 d(8) = d(4) x d(4) ... T

  • get d|V| need ?
slide-58
SLIDE 58

All-pairs shortest paths Note: di,j(m) = mink { di,k(m-1) + w(k,j) } Is just like matrix multiplication: d(m) = d(m-1) W, except + → min x → + Like matrix multiplication, this is associative. So, instead of doing d|V| = (...)W)W)W can do repeated squaring: Compute d(2) = W2 d(4) = d(2) x d(2) = W2 x W2 d(8) = d(4) x d(4) ... T

  • get d|V| need log |V| multiplications only ⇒ ? time
slide-59
SLIDE 59

All-pairs shortest paths Note: di,j(m) = mink { di,k(m-1) + w(k,j) } Is just like matrix multiplication: d(m) = d(m-1) W, except + → min x → + Like matrix multiplication, this is associative. So, instead of doing d|V| = (...)W)W)W can do repeated squaring: Compute d(2) = W2 d(4) = d(2) x d(2) = W2 x W2 d(8) = d(4) x d(4) ... T

  • get d|V| need log |V| multiplications only ⇒ |V|3 log |V| time
slide-60
SLIDE 60

The Floyd-Warshall algorithm A more clever dynamic programming algorithm Before, di,j(m) = shortest paths of lengths ≤ m Next: di,j(m) = shortest paths from i to j such that all INTERMEDIATE vertices are ≤ m d(0) = W d(m) = ???

slide-61
SLIDE 61

The Floyd-Warshall algorithm A more clever dynamic programming algorithm Before, di,j(m) = shortest paths of lengths ≤ m Next: di,j(m) = shortest paths from i to j such that all INTERMEDIATE vertices are ≤ m d(0) = W d(m)i,j = w(i,j) min (d(m-1)i,j , d(m-1)i,m + d(m-1)m,j ) if m ≥ 1.

slide-62
SLIDE 62

Floyd-Warshall(W) D(0) := W; , d(m-1)

i,m + d(m-1) m,j )

for m = 1 to n for every i,j : d(m)

(m-1)

i,j = min (d i,j

Return D(n) Time Θ(|V|3)

slide-63
SLIDE 63

The Floyd-Warshall Example d(0) = adjacency matrix with diagonal 0

slide-64
SLIDE 64

The Floyd-Warshall Example Entries d(4,2) and d(4,5) updated

slide-65
SLIDE 65

The Floyd-Warshall Example

slide-66
SLIDE 66

The Floyd-Warshall Example

slide-67
SLIDE 67

The Floyd-Warshall Example

slide-68
SLIDE 68

The Floyd-Warshall Example

slide-69
SLIDE 69

Note: Matrix multiplication/ Floyd Warshall allow for w < 0 If w ≥ 0, can repeat Dijkstra. Time: O(V2 log V + VE) = O(|V|3) Floyd Warshall is easier and has better constants Johnson algorithm matches Dijkstra but allows for w < 0.

slide-70
SLIDE 70

Johnson: Idea: Reweigh so that shortest paths don't change, but w ≥ 0 Add new node s, with zero weight edges to all previous nodes Run Bellman-Ford to get minimum distances from s (only) Use Bellman-Ford distances bf(s,x) to reweigh: w'(u,v) := w(u,v) + bf(u) – bf(v) (Can show this preserves shortest paths, and w' ≥ 0) Now run Dijkstra |V| times Time: O(V E + V2 log V + VE) = O(V2 log V + VE).

slide-71
SLIDE 71

Jonhson's Algorithm Example

slide-72
SLIDE 72

Jonhson's Algorithm Example Add new node s, with weight-0 edges to all previous nodes. Compute Bellman-Ford distance bf(s,x) from s to all nodes x (distance shown inside the nodes)

slide-73
SLIDE 73

Jonhson's Algorithm Example Use Bellman-Ford distances bf(s,x) to reweight: w'(u,v) = w(u,v) + bf(u) – bf(v)

slide-74
SLIDE 74

Jonhson's Algorithm Example bf = -5 Run Dijkstra algorithm from each node. Inside each node are minimum distances d'/d w.r.t. w' and w d(u,v) = d'(u,v) + bf(v) – bf(u) ≥ 0 bf = -1 bf = 0 bf = 0 bf = -4

2/1

slide-75
SLIDE 75

bf = -5 Jonhson's Algorithm Example Run Dijkstra algorithm on each node. bf = -1 bf = 0 bf = 0 bf = -4

slide-76
SLIDE 76

bf = -5 Jonhson's Algorithm Example Run Dijkstra algorithm on each node. bf = -1 bf = 0 bf = 0 bf = -4

slide-77
SLIDE 77

bf = -5 Jonhson's Algorithm Example Run Dijkstra algorithm on each node. bf = -1 bf = 0 bf = 0 bf = -4

slide-78
SLIDE 78

bf = -5 Jonhson's Algorithm Example Run Dijkstra algorithm on each node. bf = -1 bf = 0 bf = 0 bf = -4