31
CS350: Data Structures © James Moscola James Moscola Department of Engineering & Computer Science York College of Pennsylvania CS350: Data Structures Dijkstra’s Shortest Path Alg.

CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures © James Moscola

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATALO

G 2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania

CS350: Data StructuresDijkstra’s Shortest Path Alg.

Page 2: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Shortest Path Algorithms

• Several different shortest path algorithms exist - Dijkstra’s Algorithm - suitable for finding shortest path in a graph

when all edge weights are positive values- Bellman-Ford Algorithm - slower than Dijkstra’s algorithm, but can be

used when negative edge weights exist- Floyd-Warshall Algorithm - finds the length of the shortest path for all

pairs of vertices in a graph; can be used with both positive and negative edge weights

2

Page 3: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Uses for Dijkstra’s Algorithm

• Finding the shortest route between two locations for a GPS systems

• Determining the optimal path for packets through a network -- that is, the path with the shortest delay

• Determining the fewest number of moves in which one might solve a Rubik’s Cube

• Many other uses ...

3

Page 4: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Dijkstra’s Algorithm

• Algorithm to find the shortest weighted path from some source vertex s in a graph to all other vertices in the graph (i.e. one-to-all shortest path problem)

- The node s is the start vertex, from which the shortest path to all other nodes in the graph will be determined

• Algorithm works as follows: - Starts by assigning some initial distance value for each node in the graph

• Distance from node s to itself is 0• Distances from node s to all other nodes in graph are initialized to INFINITY

- Operates in steps, where at each step the algorithm improves the distance values for nodes in the graph

- At each step the shortest distance from node s to another node in the graph is determined

4

Page 5: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Detailed Description of Dijkstra’s Algorithm

• Let the node at which we are starting be called the start node. Let the distance of node Y be the distance from the start node to Y. Dijkstra's algorithm will assign some initial distance values and will try to improve them step by step.

(1) Assign to every node a tentative distance value: set it to zero for our start node and to infinity for all other nodes.

(2) Mark all nodes except the start node as unvisited. Set the start node as current. Create a set of the unvisited nodes called the unvisited set consisting of all the nodes except the start node.

(3) For the start node, consider all of its unvisited adjacent nodes and calculate their tentative distances. For example, if the current node A is marked with a distance of 6, and the edge connecting it with a neighbor B has length 2, then the distance to B (through A) will be 6+2=8. If this distance is less than the previously recorded distance, then overwrite that distance. Even though a neighbor has been examined, it is not marked as visited at this time, and it remains in the unvisited set.

(4) When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again; its distance recorded now is final and minimal.

(5) If the unvisited set is empty, then stop. The algorithm has finished.(6) Set the unvisited node marked with the smallest tentative distance as the next "current node" and

go back to step 3.

5

Detailed description from Wikipedia

Page 6: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Implementation Ideas

• Note that the last step in the description states the following:

• A simple way to keep track of the node with the smallest tentative distance is to maintain a minHeap as a priority queue; the element at the root of the minHeap is the node with the smallest tentative distance

6

(1) Set the unvisited node marked with the smallest tentative distance as the next "current node" and go back to step 3.

Page 7: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

7

V1

V2

V6

V3 V4

V596

112

149

15

7

10

Select V1 as the start node

Page 8: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

8

V1

V2

V6

V3 V4

V596

112

149

15

7

10

Initialize the distances from V1 to all other nodes in the graph

d5 = ∞

d4 = ∞d3 = ∞

d6 = ∞

d1 = 0

d2 = ∞

Page 9: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

9

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = ∞

d6 = ∞

d1 = 0

d2 = ∞

V6(∞)

V5(∞)

V4(∞)

V2(∞)

V3(∞)

V1(0)

- Insert all vertices into a minHeap to keep track of minimum distances

- Sort the minHeap based on the distance from the source node

minHeap

Page 10: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

10

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = ∞

d6 = ∞

d1 = 0

d2 = ∞

V5(∞)

V4(∞)

V2(∞)

V3(∞)

V6(∞)

- After initialization, pull the minimum node from the heap and begin processing by visiting the node’s neighbors and updating their distances

V1(0)

minHeap

Page 11: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

11

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = ∞

d6 = ∞

d1 = 0

d2 = ∞

V5(∞)

V4(∞)

V6(∞)

V3(∞)

V2(7)

- When the distance of a node is updated, change the distance value in the minHeap and adjust the heap; in this case, V2 becomes the new minimum node

Current distance from V1 is ∞, since 7 < ∞, d2 = 7

d2 = 7

V1(0)

minHeap

Page 12: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

12

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = ∞

d6 = ∞

d1 = 0

d2 = ∞

V5(∞)

V4(∞)

V6(∞)

V3(9)

V2(7)

- When the distance of a node is updated, change the distance value in the minHeap and adjust the heap

Current distance from V1 is ∞, since 9 < ∞, d3 = 9

d2 = 7

d3 = 9

V1(0)

minHeap

Page 13: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

13

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞

d1 = 0

d2 = ∞

V5(∞)

V4(∞)

V6 (14)

V3(9)

V2(7)

- When the distance of a node is updated, change the distance value in the minHeap and adjust the heap

Current distance from V1 is ∞, since 14 < ∞, d6 = 14

d2 = 7

d3 = ∞d3 = 9

d6 = ∞d6 = 14

V1(0)

minHeap

Page 14: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

14

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = 9

d6 = 14

d1 = 0

d2 = 7

V5(∞)

V4(∞)

V6 (14)

V3(9)

V2(7)

- Done processing node V1

minHeap

Page 15: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

15

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = 9

d6 = 14

d1 = 0 V4(∞)

V6 (14)

V5(∞)

V3(9)

- Pull the next minimum node from the minHeap and process it

d2 = 7

V2(7)

minHeap

Page 16: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

16

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = 9

d6 = 14

d1 = 0 V4(∞)

V6 (14)

V5(∞)

V3(9)

d2 = 7

Current distance from V1 is 9, since 9 < 7+10, d3 = 9

(no change)

V2(7)

minHeap

Page 17: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

17

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d4 = ∞d3 = 9

d6 = 14

d1 = 0 V4(22)

V6 (14)

V5(∞)

V3(9)

d2 = 7

Current distance from V1 is ∞, since 7+15 < ∞, d4 = 22

d4 = 22

V2(7)

minHeap

Page 18: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

18

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d3 = 9

d6 = 14

d1 = 0 V4(22)

V6 (14)

V5(∞)

V3(9)

d2 = 7

d4 = 22

- Done processing node V2

minHeap

Page 19: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

19

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d3 = 9

d6 = 14

d1 = 0

V4(22)

V5(∞)

V6 (14)

d2 = 7

d4 = 22

- Pull the next minimum node from the minHeap and process it

V3(9)

minHeap

Page 20: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

20

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d3 = 9

d6 = 14

d1 = 0

V4(20)

V5(∞)

V6 (14)

d2 = 7

d4 = 22

Current distance from V1 is 22, since 9+11 < 22, d4 = 20

d4 = 20

V3(9)

minHeap

Page 21: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

21

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d3 = 9

d6 = 14

d1 = 0

V4(20)

V5(∞)

V6 (11)

d2 = 7

d4 = 22d4 = 20

Current distance from V1 is 14, since 9+2 < 14, d6 = 11

d6 = 11

V3(9)

minHeap

Page 22: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

22

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d3 = 9

d1 = 0

V4(20)

V5(∞)

V6 (11)

d2 = 7

d4 = 20

d6 = 11- Done processing node V3

minHeap

Page 23: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

23

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d3 = 9

d1 = 0

V5(∞)

V4(20)

d2 = 7

d4 = 20

d6 = 11

- Pull the next minimum node from the minHeap and process it

V6 (11)

minHeap

Page 24: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

24

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d5 = ∞

d3 = 9

d1 = 0

V5(20)

V4(20)

d2 = 7

d4 = 20

d6 = 11

Current distance from V1 is ∞, since 11+9 < ∞, d5 = 20

d5 = 20

V6 (11)

minHeap

Page 25: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

25

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d3 = 9

d1 = 0

V5(20)

V4(20)

d2 = 7

d4 = 20

d6 = 11d5 = 20 - Done processing node V6

minHeap

Page 26: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

26

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d3 = 9

d1 = 0

V5(20)

d2 = 7

d4 = 20

d6 = 11d5 = 20

- Pull the next minimum node from the minHeap and process it

V4(20)

minHeap

Page 27: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

27

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d3 = 9

d1 = 0

V5(20)

d2 = 7

d4 = 20

d6 = 11d5 = 20 Current distance from V1 is

20, since 20 < 20+6, d5 = 20 (no change)

V4(20)

minHeap

Page 28: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

28

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d3 = 9

d1 = 0

V5(20)

d2 = 7

d4 = 20

d6 = 11d5 = 20 - Done processing node V4

minHeap

Page 29: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

29

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d3 = 9

d1 = 0

d2 = 7

d4 = 20

d6 = 11d5 = 20

- Pull the next minimum node from the minHeap and process it

V5(20)

minHeap

Page 30: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

30

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d3 = 9

d1 = 0

d2 = 7

d4 = 20

d6 = 11d5 = 20 - Node V5 has no adjacent

nodes and is therefore complete

minHeap

Page 31: CS350: Data Structures Dijkstra’s Shortest Path Alg. · Dijkstra’s Algorithm • Algorithm to find the shortest weighted path from some source vertex s in a graph to all other

CS350: Data Structures

Example of Dijkstra’s Algorithm

31

V1

V2

V6

V3 V4

V596

112

149

15

7

10

d3 = 9

d1 = 0

d2 = 7

d4 = 20

d6 = 11d5 = 20

- No more nodes exist in the minHeap

- The distance value at each node now represents its minimum distance from the source node