Section 7: Dijkstras Algorithm Slides adapted from Alex Mariakakis - - PowerPoint PPT Presentation

section 7
SMART_READER_LITE
LIVE PREVIEW

Section 7: Dijkstras Algorithm Slides adapted from Alex Mariakakis - - PowerPoint PPT Presentation

Section 7: Dijkstras Algorithm Slides adapted from Alex Mariakakis with material by Kellen Donohue, David Mailhot, Dan Grossman, Mike Ernst, Michael Hart, and Jacob Murphy Review: Shortest Paths with BFS 1 B From Node B Destination


slide-1
SLIDE 1

Slides adapted from Alex Mariakakis with material by Kellen Donohue, David Mailhot, Dan Grossman, Mike Ernst, Michael Hart, and Jacob Murphy

Section 7:

Dijkstra’s Algorithm

slide-2
SLIDE 2

Destination Path Cost A <B,A> 1 B <B> C <B,A,C> 2 D <B,D> 1 E <B,D,E> 2

From Node B

A B C D E

1 1 1 1 1 1 1

Review: Shortest Paths with BFS

slide-3
SLIDE 3

Review: Shortest Paths with BFS

Destination Path Cost A <B,A> 1 B <B> C <B,A,C> 2 D <B,D> 1 E <B,D,E> 2

From Node B

A B C D E

1 1 1 1 1 1 1

slide-4
SLIDE 4

A B C D E

2 100 2 6 2 3 100

Shortest Paths with Weights

B -> E = 106?

slide-5
SLIDE 5

A B C D E

2 100 2 6 2 3 100

How can we find the shortest path with weights?

slide-6
SLIDE 6

A B C D E Destination Path Cost A <B,A> 2 B <B> C <B,A,C> 5 D <B,A,C,D> 7 E <B,A,C,E> 7

From Node B 2 100 2 6 2 3 100

Paths are not the same!

Shortest Paths with Weights

slide-7
SLIDE 7

BFS vs. Dijkstra’s Algorithm

BFS can find the most direct path, but not necessarily the shortest path! Note that Dijkstra’s Algorithm only works if there is not a negative cycle 500 100 100 100 100

5

  • 10

1 1

slide-8
SLIDE 8

Dijkstra’s Algorithm

Named after its inventor Edsger Dijkstra (1930-2002)

  • Among his contributions to the growing CS community was his work on

Operating Systems, in which he motivated the design and structure of a large project, not just the code

The Algorithm: similar to BFS, but incorporating weights

  • Create a set of nodes to examine next, but instead of using the node that

was next in line, use the node with the shortest distance

  • How can you find the node with the shortest distance so far?

Priority Queues (explained later)

slide-9
SLIDE 9

Dijkstra’s Algorithm

Give a node two fields: cost and finished, cost gives an upper bound to the distance from the origin to that node, and finished describing if the cost of the node is the minimum cost of travelling to that node. 1.

For each node v, set v.cost = ∞ and v.finished = false

2.

Set source.cost = 0 (source is the starting node of our path)

3.

While there are unknown nodes in the graph a) Select the unknown node v with lowest cost b) Mark v as finalized c) For each edge (v,u) with weight w,

c1 = v.cost + w // cost of best path through v to u c2 = u.cost // cost of best path to u previously known if (c1 < c2) // if the new path through v is better,update u.cost = c1 u.path = v // add v to the nodes u has traversed

slide-10
SLIDE 10

A B D C F H E G 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5 Order Added to Known Set:

Example #1

vertex known? cost path A Y B ∞ C ∞ D ∞ E ∞ F ∞ G ∞ H ∞

Goal: Find the best paths from A to the other nodes

slide-11
SLIDE 11

A B D C F H E G 2 4 1 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A 3 10 1 11 1

Example #1

vertex known? cost path A Y B ≤ 2 A C ≤ 1 A D ≤ 4 A E ∞ F ∞ G ∞ H ∞

slide-12
SLIDE 12

A B D C F H E G 2 4 1 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C 3 10 1 11 1

Example #1

vertex known? cost path A Y B ≤ 2 A C Y 1 A D ≤ 4 A E ∞ F ∞ G ∞ H ∞

slide-13
SLIDE 13

A B D C F H E G 2 4 1 12 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C 3 10 1 11 1

Example #1

vertex known? cost path A Y B ≤ 2 A C Y 1 A D ≤ 4 A E ≤ 12 C F ∞ G ∞ H ∞

slide-14
SLIDE 14

A B D C F H E G 2 4 1 12 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D ≤ 4 A E ≤ 12 C F ∞ G ∞ H ∞

slide-15
SLIDE 15

A B D C F H E G 2 4 4 1 12 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D ≤ 4 A E ≤ 12 C F ≤ 4 B G ∞ H ∞

slide-16
SLIDE 16

A B D C F H E G 2 4 4 1 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B, D 12 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E ≤ 12 C F ≤ 4 B G ∞ H ∞

slide-17
SLIDE 17

A B D C F H E G 2 4 4 1 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B, D, F 12 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E ≤ 12 C F Y 4 B G ∞ H ∞

slide-18
SLIDE 18

A B D C F H E G 2 4 7 4 1 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B, D, F 12 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E ≤ 12 C F Y 4 B G ∞ H ≤ 7 F

slide-19
SLIDE 19

A B D C F H E G 2 4 7 4 1 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B, D, F, H 12 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E ≤ 12 C F Y 4 B G ∞ H Y 7 F

slide-20
SLIDE 20

A B D C F H E G 2 4 7 4 1 8 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B, D, F, H 12 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E ≤ 12 C F Y 4 B G ≤ 8 H H Y 7 F

slide-21
SLIDE 21

A B D C F H E G 2 4 7 4 1 8 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B, D, F, H, G 12 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E ≤ 12 C F Y 4 B G Y 8 H H Y 7 F

slide-22
SLIDE 22

A B D C F H E G 2 4 7 4 1 8 2 2 1 2 3 7 9 2 4 5 Order Added to Known Set: A, C, B, D, F, H, G 11 3 10 1 11 1

Example #1

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E ≤ 11 G F Y 4 B G Y 8 H H Y 7 F

slide-23
SLIDE 23

A B D C F H E G 2 4 7 4 1 11 8 2 2 1 2 3 7 9 2 4

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

5 Order Added to Known Set: A, C, B, D, F, H, G, E 3 10 1 11 1

Example #1

slide-24
SLIDE 24

A B D C F H E G 2 4 7 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5

Interpreting the Results

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

slide-25
SLIDE 25

A B D C F H E G 2 4 7 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5

Interpreting the Results

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

A

slide-26
SLIDE 26

A B D C F H E G 2 4 7 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5

Interpreting the Results

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

A B D C 2 1 4

slide-27
SLIDE 27

A B D C F H E G 2 4 7 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5

Interpreting the Results

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

A B D C F 2 2 1 4

slide-28
SLIDE 28

A B D C F H E G 2 4 7 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5

Interpreting the Results

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

A B D C F H 2 2 3 1 4

slide-29
SLIDE 29

A B D C F H E G 2 4 7 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5

Interpreting the Results

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

A B D C F H G 2 2 3 1 1 4

slide-30
SLIDE 30

A B D C F H E G 2 4 7 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 5

Interpreting the Results

vertex known? cost path A Y B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F

A B D C F H E G 2 2 3 1 3 1 4

slide-31
SLIDE 31

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Order Added to Known Set:

Example #2

vertex known? cost path A Y B ∞ C ∞ D ∞ E ∞ F ∞ G ∞

slide-32
SLIDE 32

A B C D F E G 3 4 2 1 2 6 2 1 2 5 1 1 1 2 6 5 3 10 Order Added to Known Set: A, D, C, E, B, F, G

Example #2

vertex known? cost path A Y B Y 3 E C Y 2 A D Y 1 A E Y 2 D F Y 4 C G Y 6 D

slide-33
SLIDE 33

// pre-condition: start is the node to start at // initialize things active = new empty priority queue of paths from start to a given node // A path's “priority” in the queue is the total // cost of that path. finished = new empty set of nodes // Holds nodes for which we know the // minimum-cost path from start. // We know path start->start has cost 0 Add a path from start to itself to active

Pseudocode

slide-34
SLIDE 34

while active is non-empty: minPath = active.removeMin() minDest = destination node in minPath if minDest is in finished: continue for each edge e = ⟨minDest, child⟩: if child is not in finished: newPath = minPath + e add newPath to active add minDest to finished

Pseudocode (cont.)

slide-35
SLIDE 35

Priority Queue

Given a set of weighted paths, find the shortest path Increase efficiency by considering lowest cost unknown vertex with sorting instead of looking at all vertices PriorityQueue is like a queue, but returns elements by lowest value instead of FIFO

slide-36
SLIDE 36

Priority Queue

Increase efficiency by considering lowest cost unknown vertex with sorting instead of looking at all vertices PriorityQueue is like a queue, but returns elements by lowest value instead of FIFO Two ways to implement:

  • 1. Comparable

a) class Node implements Comparable<Node> b) public int compareTo(other)

  • 2. Comparator

a) class NodeComparator extends Comparator<Node> b) new PriorityQueue(new NodeComparator())