Shortest Paths in Graphs CS16: Introduction to Data Structures - - PowerPoint PPT Presentation

shortest paths in graphs
SMART_READER_LITE
LIVE PREVIEW

Shortest Paths in Graphs CS16: Introduction to Data Structures - - PowerPoint PPT Presentation

Shortest Paths in Graphs CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline Shortest Paths Breadth First Search Dijkstras Algorithm 2 What is a Shortest Path? Given weighted graph G (weights on


slide-1
SLIDE 1

Shortest Paths in Graphs

CS16: Introduction to Data Structures & Algorithms Spring 2020

slide-2
SLIDE 2

Outline

  • Shortest Paths
  • Breadth First Search
  • Dijkstra’s Algorithm
2
slide-3
SLIDE 3

What is a Shortest Path?

  • Given weighted graph G (weights on edges)…
  • …what is shortest path from node u to v?
  • Applications
  • Google maps
  • Routing packets on the Internet
  • Social networks
3
slide-4
SLIDE 4

Single Source Shortest Paths (SSSP)

  • Given a graph and a source node
  • find the shortest paths to all other nodes
4

A B C D E

4 2 3 1 2 3 1 5 4

slide-5
SLIDE 5

Simpler Problem: Unit Edges

  • Let’s start with simpler problem
  • On graph where every edge has unit cost
5

A B C D E

1 1 1 1 1 1 1 1 1

slide-6
SLIDE 6

Simpler Problem: Unit Edges

  • What is shortest path from A to each node?
  • B:[A,B]
  • D:[A,B,D] or [A,C,D]
  • C:[A,C]
  • E:[A,B,E] or [A,C, E]
6

A B C D E

1 1 1 1 1 1 1 1 1

slide-7
SLIDE 7

Simpler Problem: Unit Edges

  • Is there an algorithm we’ve already seen that

solves this problem?

7

A B C D E

1 1 1 1 1 1 1 1 1

slide-8
SLIDE 8

numStops (with BFS)

8 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK

∞ ∞ 1 2 2 2 3 3 3 (HNL) (LAX) (LAX) (LAX) (ORD) (DFW) (DFW)

Have distance Want a path Follow ‘previous’ pointers to get a path

slide-9
SLIDE 9

Breadth-First Search

9

1 min

Activity #1

  • Use BFS to find shortest path from A to E.
  • Consider all steps of adding/removing nodes from queue …
  • …and updating each node’s ‘previous’ pointer.

A B C D E

1 1 1 1 1 1 1 1 1

slide-10
SLIDE 10

Breadth-First Search

10

1 min

Activity #1

  • Use BFS to find shortest path from A to E.
  • Consider all steps of adding/removing nodes from queue …
  • …and updating each node’s ‘previous’ pointer.

A B C D E

1 1 1 1 1 1 1 1 1

slide-11
SLIDE 11

Breadth-First Search

11

0 min

Activity #1

  • Use BFS to find shortest path from A to E.
  • Consider all steps of adding/removing nodes from queue …
  • …and updating each node’s ‘previous’ pointer.

A B C D E

1 1 1 1 1 1 1 1 1

slide-12
SLIDE 12

Breadth First Search

  • BFS always reaches target node in fewest steps
  • Let’s look at path from A to E
12

A B C D E

1 1 1 1 1 1 1 1 1

slide-13
SLIDE 13

Breadth First Search Simulation

  • Strategy
  • BFS uses queue to store nodes to visit
  • Enqueue start node
  • Decorate nodes w/ previous pointers to keep track of path
13

A B C D E

1 1 1 1 1 1 1 1 1

Queue

A

slide-14
SLIDE 14

Breadth First Search Simulation

  • Dequeue A
  • Decorate its neighbors w/ “prev: A”
  • Enqueue them
14

A B C D E

1 1 1 1 1 1 1 1 1

Queue

B C

prev: A prev: A
slide-15
SLIDE 15

Breadth First Search Simulation

  • Dequeue B and repeat…
  • …but ignoring nodes that have been decorated
15

A B C D E

1 1 1 1 1 1 1 1 1

Queue

C D E

prev: A prev: A prev: B prev: B
slide-16
SLIDE 16

Breadth First Search Simulation

  • Dequeuing C and D has no effect…
  • …since their neighbors have been decorated
16

A B C D E

1 1 1 1 1 1 1 1 1

Queue

E

prev: A prev: A prev: B prev: B
slide-17
SLIDE 17

Breadth First Search Simulation

  • When we dequeue E…
  • …we traverse the prev pointers to return paths
  • shortest path to E: [A,B,E]
17

A B C D E

1 1 1 1 1 1 1 1 1

Queue

E

prev: A prev: A prev: B prev: B
slide-18
SLIDE 18

Non-Unit Edge Weights

  • What if edge weights are not 1?
  • More complicated
18

A B C D E

4 2 3 1 2 3 1 5 4

slide-19
SLIDE 19

Shortest Path

19

2 min

Activity #2

  • Fill in missing spaces using graph below
  • Use A as source vertex

A B C D E

4 2 3 1 2 3 1 5 4
slide-20
SLIDE 20

Shortest Path

20

2 min

Activity #2

  • Fill in missing spaces using graph below
  • Use A as source vertex

A B C D E

4 2 3 1 2 3 1 5 4
slide-21
SLIDE 21

Shortest Path

21

1 min

Activity #2

  • Fill in missing spaces using graph below
  • Use A as source vertex

A B C D E

4 2 3 1 2 3 1 5 4
slide-22
SLIDE 22

Shortest Path

22

0 min

Activity #2

  • Fill in missing spaces using graph below
  • Use A as source vertex

A B C D E

4 2 3 1 2 3 1 5 4
slide-23
SLIDE 23

Non-unit Edge Weights

23 Goal Node Shortest Path Shortest Distance B [A, C, B] 3 C [A, C] 2 D [A, C, B, D] 5 E [A, C, B, E] 6

A B C D E

4 2 3 1 2 3 1 5 4

slide-24
SLIDE 24

Shortest Path Application

  • Road trip
  • Amy, Andy, Prakrit, & Stephanie want to get from

PVD to SF…

  • …following limited set of highways
  • Cities are nodes and highways are edges
  • Get to SF using shortest path
24
slide-25
SLIDE 25

Our Graph

25 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 5 10 20 15 15 10 10 Start End 20
slide-26
SLIDE 26

Our Graph

26 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 5 10 20 15 15 10 10 Start End 20

What is the cost of this path?

slide-27
SLIDE 27

Our Graph

27 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 5 10 20 15 15 10 10 Start End 20

What is the cost of this path? Is there a shorter path?

slide-28
SLIDE 28 5 10

Our Graph

28 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 10 20 15 15 10 Start End 20

What is the cost of this path? Is there a shorter path?

slide-29
SLIDE 29

Shortest Path

  • Why does BFS work with unit edges?
  • Nodes visited in order of total distance from source
  • We need way to do the same even when edges

have distinct weights!

  • How can we do this?
  • Hint: we’ll use a data structure we’ve already seen
29
slide-30
SLIDE 30

Shortest Path

  • Use a priority queue!
  • where priorities are total distances from source
  • By visiting nodes in order returned by

removeMin()…

  • …you visit nodes in order of how far they are from

source

  • You guarantee shortest path to node because…
  • …you don’t explore a node until all nodes closer to

source have already been explored

30
slide-31
SLIDE 31

Dijkstra’s Algorithm

  • The algorithm is as follows:
  • Decorate source with distance 0 & all other nodes with ∞
  • Add all nodes to priority queue w/ distance as priority
  • While the priority queue isn’t empty
  • Remove node from queue with minimal priority
  • Update distances of the removed node’s neighbors if distances

decreased

  • When algorithm terminates, every node is decorated

with minimal cost from source

31
slide-32
SLIDE 32

Dijkstra’s Algorithm Example

  • Step 1
  • Label source w/ dist. 0
  • Label other vertices w/ dist. ∞
  • Add all nodes to Q
  • Step 2
  • Remove node with min. priority

from Q (S in this example).

  • Calculate dist. from source to

removed node’s neighbors…

  • …by adding adjacent edge

weights to S’s dist.

32 S B A D C 7 8 2 3 2 1 5 5 4

∞ ∞ ∞ ∞

S B A D C 7 8 2 3 2 1 5 5 4 7 2

∞ ∞

slide-33
SLIDE 33

Dijkstra’s Algorithm Example

  • Step 3
  • While Q isn’t empty,
  • repeat previous step
  • removing A this time
  • Priorities of nodes in Q may have
to be updated
  • ex: B’ s priority
  • Step 4
  • Repeat again by removing vertex B
  • Update distances that are shorter
using this path than before
  • ex: C now has a distance 6 not 10
33 S B A D C 7 8 2 3 2 1 5 5 4 5 2 10 7 S B A D C 7 8 2 3 2 1 5 5 4 5 2 6 7
slide-34
SLIDE 34

Dijkstra’s Algorithm Example

  • Step 5
  • Repeat
  • this time removing C
  • Step 6
  • After removing D…
  • …every node has been

visited…

  • …and decorated w/

shortest dist. to source

34 S B A D C 7 8 2 3 2 1 5 5 4 5 2 6 7 S B A D C 7 8 2 3 2 1 5 5 4 5 2 6 7
slide-35
SLIDE 35

Dijkstra’s Algorithm Example

  • Previous example decorated nodes with

shortest distance but did not “create” paths

  • How could you enhance algorithm to return the

shortest path to a particular node?

  • Previous pointers!
  • Let’s do another example
35
slide-36
SLIDE 36

Dijkstra’s Example

36

A B C D E

4 2 3 1 2 3 1 5 4

A B C D E ∞ ∞ ∞ ∞
slide-37
SLIDE 37

Dijkstra’s Example

37

A B C D E

4 2 3 1 2 3 1 5 4

A B C D E 4 2 ∞ ∞
slide-38
SLIDE 38

Dijkstra’s Example

38

A B C D E

4 2 3 1 2 3 1 5 4

A B C D E 3 2 6 7
slide-39
SLIDE 39

Dijkstra’s Example

39

A B C D E

4 2 3 1 2 3 1 5 4

A B C D E 3 2 5 6
slide-40
SLIDE 40

Dijkstra’s Example

40

A B C D E

4 2 3 1 2 3 1 5 4

A B C D E 3 2 5 6
slide-41
SLIDE 41

Simulate Dijkstra’s

41

2 min

Activity #3

slide-42
SLIDE 42

Simulate Dijkstra’s

42

2 min

Activity #3

slide-43
SLIDE 43

Simulate Dijkstra’s

43

1 min

Activity #3

slide-44
SLIDE 44

Simulate Dijkstra’s

44

0 min

Activity #3

slide-45
SLIDE 45

Dijkstra’s Algorithm

45
  • Comes up with an optimal solution
  • shortest path to each node
  • Like many optimization algorithms, uses dynamic

programming

  • overlapping subproblems (distances to nodes)
  • solved in a particular order (closest first)
  • Dijkstra’s is greedy
  • at each step, considers next closest node
  • Greedy algorithms not always optimal, usually fast
slide-46
SLIDE 46

Dijkstra Pseudo-Code

46 function dijkstra(G, s): // Input: graph G with vertices V, and source s // Output: Nothing // Purpose: Decorate nodes with shortest distance from s for v in V: v.dist = infinity // Initialize distance decorations v.prev = null // Initialize previous pointers to null s.dist = 0 // Set distance to start to 0 PQ = PriorityQueue(V) // Use v.dist as priorities while PQ not empty: u = PQ.removeMin() for all edges (u, v): //each edge coming out of u if u.dist + cost(u, v) < v.dist: // cost() is weight v.dist = u.dist + cost(u,v) // Replace as necessary v.prev = u // Maintain pointers for path PQ.decreaseKey(v, v.dist)
slide-47
SLIDE 47

Runtime of Dijkstra w/ Heap

47

2 min

Activity #3

function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:
  • 7. O(__)
slide-48
SLIDE 48

Runtime of Dijkstra w/ Heap

48

2 min

Activity #3

function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:
  • 7. O(__)
slide-49
SLIDE 49

Runtime of Dijkstra w/ Heap

49

1 min

Activity #3

function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:
  • 7. O(__)
slide-50
SLIDE 50

Runtime of Dijkstra w/ Heap

50

0 min

Activity #3

function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:
  • 7. O(__)
slide-51
SLIDE 51

Dijkstra Runtime

51 function dijkstra(G, s): for v in V: v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) while PQ not empty: u = PQ.removeMin() for all edges (u, v): if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist)

O(|V|)

O(|V|)

depends
  • n PQ

O(|E|)

total depends
  • n PQ
depends
  • n PQ
slide-52
SLIDE 52

Dijkstra Runtime

  • Depends on priority queue implementation
  • If PQ implemented with Array or Linked List
  • insert() is O(1)
  • removeMin( ) is O(|V|)
  • you have to scan to find min-priority element
  • decreaseKey() is O(1)
  • you already have node when you change its key
52
slide-53
SLIDE 53

Dijkstra Runtime w/ Array or List

53 function dijkstra(G, s): for v in V: v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) while PQ not empty: u = PQ.removeMin() for all edges (u, v): if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist)

O(|V|)

O(|V|) O(|V|) O(1) O(|E|)

total

O(|V|)

slide-54
SLIDE 54

Dijkstra Runtime w/ Array or List

  • If PQ implemented with Array or Linked List
  • since |E|≤|V|2
54

O(|V | + |V | + |V |2 + |E|) = O(|V |2 + |E|) = O(|V |2)

<latexit sha1_base64="s/PpJtLJ9GM1Gg8qkhx28g4UfY=">ACInicbVDLSgMxFM3UV62vUZdugkVpEcpMEXyAUBTBnRXsA9paMmnahmYyQ5IRSt/ceOvuHhayX4MWamQ6mtB5J7Odeknscn1GpLOvbSCwsLi2vJFdTa+sbm1vm9k5ZeoHApIQ95omqgyRhlJOSoqRqi8Ich1GKk7vKvQrj0RI6vF71fdJw0UdTtsUI6Wlpnl+mxmWh/AITu6HfFivh1l4eAEjd6LU61NatmrZwVAc4TOyZpEKPYND/rLQ8HLuEKMyRlzbZ81RgoShmZJSqB5L4CPdQh9Q05cglsjGIdhzBA620YNsT+nAFI3V6YoBcKfuoztdpLpy1gvF/7xaoNqnjQHlfqAIx+OH2gGDyoNhYLBFBcGK9TVBWFD9V4i7SCsdKwpHYI9u/I8KeVzZzn7jhduIzTSI9sA8ywAYnoABuQBGUAZP4AW8gXfj2Xg1PoyvcWvCiGd2wR8YP7+84p79</latexit><latexit sha1_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DLSgMxFM3UV62vUZdugkVpEcpMEXyAUBTBnRXsA9paMmnahmYyQ5IRSt/ceOvuHhayX4MWamQ6mtB5J7Odeknscn1GpLOvbSCwsLi2vJFdTa+sbm1vm9k5ZeoHApIQ95omqgyRhlJOSoqRqi8Ich1GKk7vKvQrj0RI6vF71fdJw0UdTtsUI6Wlpnl+mxmWh/AITu6HfFivh1l4eAEjd6LU61NatmrZwVAc4TOyZpEKPYND/rLQ8HLuEKMyRlzbZ81RgoShmZJSqB5L4CPdQh9Q05cglsjGIdhzBA620YNsT+nAFI3V6YoBcKfuoztdpLpy1gvF/7xaoNqnjQHlfqAIx+OH2gGDyoNhYLBFBcGK9TVBWFD9V4i7SCsdKwpHYI9u/I8KeVzZzn7jhduIzTSI9sA8ywAYnoABuQBGUAZP4AW8gXfj2Xg1PoyvcWvCiGd2wR8YP7+84p79</latexit>
slide-55
SLIDE 55

Dijkstra Runtime w/ Heap

  • If PQ implemented with Heap
  • insert( ) is O(log|V|)
  • you may need to upheap
  • removeMin( ) is O(log|V|)
  • you may need to downheap
  • decreaseKey() is O(log|V|)
  • assume we have dictionary that maps vertex to heap entry in

O(log|V|) time (so no need to scan heap to find entry)

  • you may need to upheap after decreasing the key
55
slide-56
SLIDE 56

Dijkstra Runtime w/ Heap

56 function dijkstra(G, s): for v in V: v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) while PQ not empty: u = PQ.removeMin() for all edges (u, v): if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist)

O(|V|)

O(|V|) O(log|V|) O(log|V|) O(|E|)

total

O(|V|log|V|)

slide-57
SLIDE 57

Dijkstra Runtime w/ Heap

  • If PQ implemented with Heap
  • Note
  • though the O(|E|) loop is nested in the O(|V|) loop
  • we visit each edge at most twice rather than |V| times
  • That’s why while loop is
57

O(|V | + |V | log |V |+|V | log |V | + |E| log |V |) = O(|V | + |V | log |V | + |E| log |V |) = O ✓ |V | + |E|

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CcH9/eS/oHlau6y5D2fl+nWRg6UwCGoABecgzq4Aw3QBAR8WEWrZO1Zn/auvW8fzKS2lXl2wI+yK18M5rhE</latexit>

O ✓ V log |V |

  • +
  • |E| log |V |

slide-58
SLIDE 58

Dijkstra’s on Graph with Negative Edges

58

1 min

Activity #5

D A B C 2
  • 7
5 8 Start End
slide-59
SLIDE 59

Dijkstra’s on Graph with Negative Edges

59

1 min

Activity #5

D A B C 2
  • 7
5 8 Start End
slide-60
SLIDE 60

Dijkstra’s on Graph with Negative Edges

60

0 min

Activity #5

D A B C 2
  • 7
5 8 Start End
slide-61
SLIDE 61

Dijkstra isn’ t perfect!

  • We can find shortest path on weighted graph in
  • O((|V|+|E|)×log|V|)
  • or can we…
  • Dijkstra fails with negative edge weights
  • Returns [A,C,D] when it should return [A,B,C,D]
61 D A B C 2
  • 7
5 8 Start End
slide-62
SLIDE 62

Negative Edge Weights

  • Negative edge weights are problem for Dijkstra
  • But negative cycles are even worse!
  • because there is no true shortest path!
62 D A B C
  • 10
5 15 3 Start End
slide-63
SLIDE 63

Bellman-Ford Algorithm

  • Algorithm that handles graphs w/ neg. edge

weights

  • Similar to Dijkstra’s but more robust
  • Returns same output as Dijkstra’s for any graph w/
  • nly positive edge weights (but runs slower)
  • Returns correct shortest paths for graphs w/ neg.

edge weights

  • How: not greedy!
63