Find the shortest path from a to z

  • Slides: 20
Download presentation
Find the shortest path from a to z 3 b 3 3 2 g

Find the shortest path from a to z 3 b 3 3 2 g d 2 1 h 6 a 3 z e 6 1 3 c 8 8 f 1 9 i 3

Label all vertices as ∞ and put vertex a in the vertex list. b

Label all vertices as ∞ and put vertex a in the vertex list. b 3 ∞ ∞ g d 3 2 2 ∞ a 6 8 9 f ∞ Vertex List: 1 ∞ 3 ∞ 1 3 8 1 h e 6 c ∞ 3 i 3 ∞ ∞ a: 0 b: ∞ c: ∞ d: ∞ e: ∞ f: ∞ g: ∞ h: ∞ i: ∞ z

Re-label all vertices that can be reached from a b 3 2(a) ∞ g

Re-label all vertices that can be reached from a b 3 2(a) ∞ g d 3 2 2 6(a) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 ∞ 1 3 8 1 h e 6 c ∞ 3 i 3 ∞ ∞ a: 0 b: 2 c: 3 d: ∞ e: 6 f: ∞ g: ∞ h: ∞ i: ∞ z

Choose the unvisited vertex with the smallest label, in this case b. b 3

Choose the unvisited vertex with the smallest label, in this case b. b 3 2(a) ∞ g d 3 2 2 6(a) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 ∞ 1 3 8 1 h e 6 c ∞ 3 i 3 ∞ ∞ a: 0 b: 2 c: 3 d: ∞ e: 6 f: ∞ g: ∞ h: ∞ i: ∞ z

Re-label all vertices that can be reached from b now. b 3 2(a) 5(a,

Re-label all vertices that can be reached from b now. b 3 2(a) 5(a, b) g d 3 2 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 ∞ 1 3 8 1 h e 6 c ∞ 3 i 3 ∞ ∞ a: 0 b: 2 c: 3 d: 5 e: 6 5 f: ∞ g: ∞ h: ∞ i: ∞ z

Choose the unvisited vertex with the smallest label, now c. b 3 2(a) 5(a,

Choose the unvisited vertex with the smallest label, now c. b 3 2(a) 5(a, b) g d 3 2 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 ∞ 1 3 8 1 h e 6 c ∞ 3 i 3 ∞ ∞ a: 0 b: 2 c: 3 d: 5 e: 5 f: ∞ g: ∞ h: ∞ i: ∞ z

Re-label, now including c b 3 2(a) 5(a, b) g d 3 2 2

Re-label, now including c b 3 2(a) 5(a, b) g d 3 2 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 ∞ 1 3 8 1 h e 6 c ∞ 3 i 3 4(a, c) ∞ a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: ∞ h: ∞ i: ∞ z

Choose the smallest unvisited vertex, f b 3 2(a) 5(a, b) g d 3

Choose the smallest unvisited vertex, f b 3 2(a) 5(a, b) g d 3 2 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 ∞ 1 3 8 1 h e 6 c ∞ 3 i 3 4(a, c) ∞ a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: ∞ h: ∞ i: ∞ z

Re-label, now using f b 3 2(a) 5(a, b) g d 3 2 2

Re-label, now using f b 3 2(a) 5(a, b) g d 3 2 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 12(a, c, f) 1 3 8 1 h e 6 c ∞ 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: ∞ h: 12 i: 7 z: ∞ z

Smallest unvisited vertex is now d (alphabetical tie over e) b 3 2(a) 5(a,

Smallest unvisited vertex is now d (alphabetical tie over e) b 3 2(a) 5(a, b) g d 3 2 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 12(a, c, f) 1 3 8 1 h e 6 c ∞ 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: ∞ h: 12 i: 7 z: ∞ z

Re-label using d b 3 2(a) 5(a, b) g 2 5(a, b) a 6

Re-label using d b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 12(a, c, f) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 12 i: 7 z: ∞ z

Smallest unvisited vertex is now e b 3 2(a) 5(a, b) g 2 5(a,

Smallest unvisited vertex is now e b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 12(a, c, f) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 12 i: 7 z: ∞ z

Re-label using e, but no vertex gets updated b 3 2(a) 5(a, b) g

Re-label using e, but no vertex gets updated b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 12(a, c, f) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 12 i: 7 z: ∞ z

Smallest unvisited vertex is i b 3 2(a) 5(a, b) g 2 5(a, b)

Smallest unvisited vertex is i b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 9 f 3(a) Vertex List: 1 ∞ 3 12(a, c, f) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 12 i: 7 z: ∞ z

Re-label using i b 3 2(a) 5(a, b) g 2 5(a, b) a 6

Re-label using i b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 Vertex List: 1 16(a, c, f, i) z 9 f 3(a) 3 12(a, c, f) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 12 i: 7 z: 16

Now, choose g b 3 2(a) 5(a, b) g 2 5(a, b) a 6

Now, choose g b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 Vertex List: 1 16(a, c, f, i) z 9 f 3(a) 3 12(a, c, f) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 12 i: 7 z: 16

Re-label, using g b 3 2(a) 5(a, b) g 2 5(a, b) a 6

Re-label, using g b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 Vertex List: 1 16(a, c, f, i) z 9 f 3(a) 3 9(a, b, d, g) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 f: 4 g: 8 h: 12 9 i: 7 e: 5 z: 16

Now choose h b 3 2(a) 5(a, b) g 2 5(a, b) a 6

Now choose h b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 Vertex List: 1 16(a, c, f, i) z 9 f 3(a) 3 9(a, b, d, g) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 9 i: 7 z: 16

Re-label using h b 3 2(a) 5(a, b) g 2 5(a, b) a 6

Re-label using h b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 Vertex List: 1 12(a, b, d, g, h) z 9 f 3(a) 3 9(a, b, d, g) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 9 i: 7 z: 16 12

No vertices left to consider, so we have the shortest path b 3 2(a)

No vertices left to consider, so we have the shortest path b 3 2(a) 5(a, b) g 2 5(a, b) a 6 8 Vertex List: 1 12(a, b, d, g, h) z 9 f 3(a) 3 9(a, b, d, g) 1 3 8 1 h e 6 c 8(a, b, d) d 3 2 3 i 3 4(a, c) 7(a, c, f) a: 0 b: 2 c: 3 d: 5 e: 5 f: 4 g: 8 h: 9 i: 7 z: 16 12