Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest path algorithm

Similar presentations


Presentation on theme: "Shortest path algorithm"— Presentation transcript:

1 Shortest path algorithm
Zhiqi Chen The slides are referenced from Dr. Parberry and Dr. Roden’s notes.

2 Single source shortest path problem
Given a labeled, directed (or undirected) graph, with nonnegative edge costs, G = (V, E) and a vertex, s Є V, find the shortest path from s to every other vertex in the graph. Create a data structure that allows us to compute the vertices on a shortest path from s to w for each w Є V. Vertex s is the source let Δ(v, w) denote the cost of the shortest path from v to w let C(v, w) be the cost of the edge from v to w (∞ if don’t exist, 0 if v=w)

3 Dijkstra’s algorithm Keep a set S of vertices whose minimum distance from the source s is known. Initially S = {s} keep adding vertices to S eventually, S = V (V is all vertices in graph) Maintain an array D (distance) with If w Є S, then D[w] is the cost of the shortest path to w If w Є S, then D[w] is the cost of the shortest path from s to w Δ(s, w)

4 The algorithm S = {s} for each v Є V D[v] = C(s, v) for i = 1 to n-1
choose w Є V – S with smallest D[w] S = S υ {w} for each vertex v Є V D[v] = min { D[v], D[w] + C(w, v) }

5 Example A 3 10 B E 2 4 C 5 2 7 F 2 3 D

6 Step 1: S = {A} D[A] = 0 D[B] = 3 D[C] = ∞ D[D] = 5 D[E] = 10 D[F] = ∞
S = {s} for each v Є V D[v] = C(s, v) D[A] = 0 D[B] = 3 D[C] = ∞ D[D] = 5 D[E] = 10 D[F] = ∞ A 3 10 B E 2 4 C 5 2 7 F 2 3 D

7 Step 2: S = {A} D[A] = 0 D[B] = 3 D[C] = ∞ D[D] = 5 D[E] = 10 D[F] = ∞
Pick Smallest D[w] for i = 1 to n-1 choose w Є V – S with smallest D[w] D[A] = 0 D[B] = 3 D[C] = ∞ D[D] = 5 D[E] = 10 D[F] = ∞ A 3 10 B E 2 4 C 5 2 7 F 2 3 D

8 Step 3: S = {A, B} S = S υ {w} for each vertex v Є V
D[v] = min { D[v], D[w] + C(w, v) } W = B D[A] = min {D[A], D[B] + C(B, A)} D[B] = min {D[B], D[B] + C(B, B)} D[C] = min {D[C], D[B] + C(B, C)} D[D] = min {D[D], D[B] + C(B, D)} D[E] = min {D[E], D[B] + C(B, E)} D[F] = min {D[F], D[B] + C(B, F)} A 3 3 3 10 3 3 3 B E 2 5 3 2 4 C 5 2 5 5 3 7 7 F 2 10 10 3 3 D 3

9 Step 4: S = {A, B} D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 10
Pick Smallest D[w] for i = 1 to n-1 choose w Є V – S with smallest D[w] D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 10 D[F] = ∞ A 3 10 B E 2 4 C 5 2 7 F 2 3 D

10 Step 5: S = {A, B, C} S = S υ {w} for each vertex v Є V
D[v] = min { D[v], D[w] + C(w, v) } W = C D[A] = min {D[A], D[C] + C(C, A)} D[B] = min {D[B], D[C] + C(C, B)} D[C] = min {D[C], D[C] + C(C, C)} D[D] = min {D[D], D[C] + C(C, D)} D[E] = min {D[E], D[C] + C(C, E)} D[F] = min {D[F], D[C] + C(C, F)} A 5 3 10 3 3 5 2 B E 2 5 5 5 4 C 5 2 5 5 5 3 7 F 2 9 10 5 4 3 D 7 5 2

11 Step 6: S = {A, B, C} D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 9
Pick Smallest D[w] for i = 1 to n-1 choose w Є V – S with smallest D[w] D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 9 D[F] = 7 A 3 10 B E 2 4 C 5 2 7 F 2 3 D

12 Step 7: S = {A, B, C, D} S = S υ {w} for each vertex v Є V
D[v] = min { D[v], D[w] + C(w, v) } W = D D[A] = min {D[A], D[D]+ C(D, A)} D[B] = min {D[B], D[D] + C(D, B)} D[C] = min {D[C], D[D] + C(D, C)} D[D] = min {D[D], D[D] + C(D, D)} D[E] = min {D[E], D[D] + C(D, E)} D[F] = min {D[F], D[D] + C(D, F)} A 5 5 3 10 3 3 5 7 B E 2 5 5 5 3 4 C 5 2 5 5 5 7 F 2 9 9 5 3 D 7 7 5

13 Step 8: S = {A, B, C, D} D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 9
Pick Smallest D[w] for i = 1 to n-1 choose w Є V – S with smallest D[w] D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 9 D[F] = 7 A 3 10 B E 2 4 C 5 2 7 F 2 3 D

14 Step 9: S = {A, B, C, D, F} D[A] = min {D[A], D[F]+ C(F, A)}
S = S υ {w} for each vertex v Є V D[v] = min { D[v], D[w] + C(w, v) } W = F D[A] = min {D[A], D[F]+ C(F, A)} D[B] = min {D[B], D[F] + C(F, B)} D[C] = min {D[C], D[F] + C(F, C)} D[D] = min {D[D], D[F] + C(F, D)} D[E] = min {D[E], D[F] + C(F, E)} D[F] = min {D[F], D[F] + C(F, F)} A 7 3 10 3 3 7 B E 2 5 5 7 2 4 C 5 2 5 5 7 7 F 2 9 9 7 2 3 D 7 7 7

15 Step 10: S = {A, B, C, D} D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 9
Pick Smallest D[w] for i = 1 to n-1 choose w Є V – S with smallest D[w] D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 9 D[F] = 7 A 3 10 B E 2 4 C 5 2 7 F 2 3 D

16 Step 11: S = {A, B, C, D, F, E} S = S υ {w} for each vertex v Є V
D[v] = min { D[v], D[w] + C(w, v) } W = E D[A] = min {D[A], D[E] + C(E, A)} D[B] = min {D[B], D[E] + C(E, B)} D[C] = min {D[C], D[E] + C(E, C)} D[D] = min {D[D], D[E] + C(E, D)} D[E] = min {D[E], D[E] + C(E, E)} D[F] = min {D[F], D[E] + C(E, F)} A 9 10 3 10 3 3 9 B E 2 5 5 9 4 4 C 5 2 5 5 9 7 F 2 9 9 9 3 D 7 7 9 2

17 S = {A, B, C, D, F, E} D[A] = 0 D[B] = 3 D[C] = 5 D[D] = 5 D[E] = 9
10 B E 2 4 C 5 2 7 F 2 3 D

18 Implementation using heap
Store S’ = V – S as a (minimum) heap with minimum D value, vertex at top of heap (where each entry in the heap is a vertex and its corresponding D value)

19 Implementation using heap
makeNull (S’) // S’ = V - s For each v Є V except s D[v] = C(s, v) insert (S’, v) // build the min heap for i = 1 to n-1 w = S’.deleteMin() // always pick the smallest D for each v connected by an edge to w D[v] = min { D[v], D[w] + C(w, v) } move v up the heap

20 Exercise A 2 5 B C 1 3 10 D

21 Answer: S = {A, B, C, D} D[A] = 0 D[B] = 2 D[C] = 3 D[D] = 6 3 10 A 2
5 B C 1 3 10 D


Download ppt "Shortest path algorithm"

Similar presentations


Ads by Google