Shortest Path Algorithm

14
Shortest Path Algorithm By Weston Vu CS 146

description

Shortest Path Algorithm. By Weston Vu CS 146. What is Shortest Paths?. Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest way from the starting point to the destination by adding the weight of the edges together. Shortest Path. - PowerPoint PPT Presentation

Transcript of Shortest Path Algorithm

Page 1: Shortest Path Algorithm

Shortest Path Algorithm

By Weston Vu

CS 146

Page 2: Shortest Path Algorithm

What is Shortest Paths?

Shortest Paths is a part of the graph algorithm. It is used to calculate the

shortest way from the starting point to the destination by adding the weight of

the edges together.

Page 3: Shortest Path Algorithm

Shortest Path

• There are 2 types of shortest path:– Weight shortest path– Unweight shortest path: assign all edges a

weight of 1

V1 V2

V5V3 V4

V6 V7

Page 4: Shortest Path Algorithm

UnWeight Shortest Path

Vertex Known Dv PvV1 F ¥ 0V2 F ¥ 0V3 T 0 0V4 F ¥ 0V5 F ¥ 0V6 F ¥ 0V7 F ¥ 0

Vertex Known Dv PvV1 T 1 V3V2 F ¥ 0V3 T 0 0V4 F ¥ 0V5 F ¥ 0V6 F 1 V3V7 F ¥ 0

V1 V2

V5V3 V4

V6 V7

dw = dv + 1if dw = ¥

Page 5: Shortest Path Algorithm

UnWeight Shortest Path

V1 V2

V5V3 V4

V6 V7

Vertex Known Dv PvV1 T 1 V3V2 F 1 +1 V1V3 T 0 0V4 F 1 +1 V1V5 F ¥ 0V6 F 1 V3V7 F ¥ 0

Vertex Known Dv PvV1 T 1 V3V2 F 2 V1V3 T 0 0V4 F 2 V1V5 F ¥ 0V6 T 1 V3V7 F ¥ 0

Page 6: Shortest Path Algorithm

UnWeight Shortest Path

V1 V2

V5V4

V6 V7

Vertex Known Dv PvV1 T 1 V3V2 T 2 V1V3 T 0 0V4 T 2 V1V5 F 3 V2V6 T 1 V3V7 F 1 + 1 + 1 V4

V3

Vertex Known Dv PvV1 T 1 V3V2 T 2 V1V3 T 0 0V4 F 2 V1V5 F 1 + 1 +1 V2V6 T 1 V3V7 F ¥ 0

Page 7: Shortest Path Algorithm

UnWeight Shortest Path

V1 V2

V5V4

V6 V7

V3

Vertex Known Dv PvV1 T 1 V3V2 T 2 V1V3 T 0 0V4 T 2 V1V5 T 3 V2V6 T 1 V3V7 F 3 V4

Vertex Known Dv PvV1 T 1 V3V2 T 2 V1V3 T 0 0V4 T 2 V1V5 T 3 V2V6 T 1 V3V7 T 3 V4

Page 8: Shortest Path Algorithm

Dijkstra’s AlgorithmWeight Shortest Path

Vertex Known Dv PvV1 F 0 0V2 F ¥ 0V3 F ¥ 0V4 F ¥ 0V5 F ¥ 0V6 F ¥ 0V7 F ¥ 0

Vertex Known Dv PvV1 T 0 0V2 F 2 V1V3 F ¥ 0V4 F 1 V1V5 F ¥ 0V6 F ¥ 0V7 F ¥ 0

V1 V2

V5V3 V4

V6 V7

2

4 1 310

22

58

14 6

dw = dv + cv,w

if new value for dw would be an improve

Page 9: Shortest Path Algorithm

Dijkstra’s AlgorithmWeight Shortest Path

Vertex Known Dv PvV1 T 0 0V2 F 2 V1V3 F 2 + 1 V4V4 T 1 V1V5 F 2 + 1 V4V6 F 8 + 1 V4V7 F 4 + 1 V4

Vertex Known Dv PvV1 T 0 0V2 F 2 V1V3 F 3 V4V4 T 1 V1V5 F 3 V4V6 F 9 V4V7 F 5 V4

V1 V2

V5V3 V4

V6 V7

2

4 1 310

22

58

14 6

Page 10: Shortest Path Algorithm

Dijkstra’s AlgorithmWeight Shortest Path

Vertex Known Dv PvV1 T 0 0V2 T 2 V1V3 T 3 V4V4 T 1 V1V5 F 3 V4V6 F 9 V4V7 F 5 V4

Vertex Known Dv PvV1 T 0 0V2 T 2 V1V3 T 3 V4V4 T 1 V1V5 F 3 V4V6 F 1 + 2 + 5 V3V7 F 5 V4

Vertex Known Dv PvV1 T 0 0V2 T 2 V1V3 T 3 V4V4 T 1 V1V5 T 3 V4V6 F 8 V3V7 F 5 V4

Vertex Known Dv PvV1 T 0 0V2 T 2 V1V3 T 3 V4V4 T 1 V1V5 T 3 V4V6 F 1 + 4 + 1 V7V7 T 5 V4

Page 11: Shortest Path Algorithm

Dijkstra’s AlgorithmWeight Shortest Path

Vertex Known Dv PvV1 T 0 0V2 T 2 V1V3 T 3 V4V4 T 1 V1V5 T 3 V4V6 F 6 V7V7 T 5 V4

Vertex Known Dv PvV1 T 0 0V2 T 2 V1V3 T 3 V4V4 T 1 V1V5 T 3 V4V6 T 6 V7V7 T 5 V4

V1 V2

V5V3 V4

V6 V7

2

4 1 310

22

58

14 6

Page 12: Shortest Path Algorithm

Graphs with Negative Edge Cost

• If the graph has negative edge costs, then it makes the problem harder

• If negative-cost cycle are present, then the algorithm will loop indefinitely

V1 V2

V5V3 V4

V6 V7

4 1 3-10

22

58

14 6

2

Page 13: Shortest Path Algorithm

Acyclic Graph

• Changing the order in which vertices are declared known

• Set the vertices in topological order

Start

A-3

B-2

D-2

C-3

E-1

F-3

G-2

Finish

Page 14: Shortest Path Algorithm

The END