Download presentation

Presentation is loading. Please wait.

Published byRaven Follen Modified over 2 years ago

1
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009

2
Administrative Grading final grade extra credit TA issues/concerns HW6 shorter will be due Wed. 8/12 before class Errors in class slides and notes Anonymized scores posted

3
Shortest paths What is the shortest path from a to d? A B CE D

4
Shortest paths BFS A B CE D

5
Shortest paths What is the shortest path from a to d? A B CE D 1 1 3 2 2 3 4

6
Shortest paths We can still use BFS A B CE D 1 1 3 2 2 3 4

7
Shortest paths We can still use BFS A B CE D 1 1 3 2 2 3 4 A B CE D

8
Shortest paths We can still use BFS A B CE D

9
Shortest paths What is the problem? A B CE D

10
Shortest paths Running time is dependent on the weights A B C 4 1 2 A B C 200 50 100

11
Shortest paths A B C 200 50 100 A B C

12
Shortest paths A B C

13
A B C

14
A B C Nothing will change as we expand the frontier until we’ve gone out 100 levels

15
Dijkstra’s algorithm

17
prev keeps track of the shortest path

18
Dijkstra’s algorithm

21
Single source shortest paths All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms Why?

22
A B CE D 1 1 3 3 2 1 4

23
A B CE D 1 1 3 3 2 1 4

24
A B CE D 1 1 3 3 2 1 4 0 Heap A 0 B C D E

25
A B CE D 1 1 3 3 2 1 4 0 Heap B C D E

26
A B CE D 1 1 3 3 2 1 4 0 Heap B C D E

27
A B CE D 1 1 3 3 2 1 4 1 0 Heap C 1 B D E

28
A B CE D 1 1 3 3 2 1 4 1 0 Heap C 1 B D E

29
A B CE D 1 1 3 3 2 1 4 3 1 0 Heap C 1 B 3 D E

30
A B CE D 1 1 3 3 2 1 4 3 1 0 Heap C 1 B 3 D E

31
3 A B CE D 1 1 3 2 1 4 3 1 0 Heap B 3 D E

32
3 A B CE D 1 1 3 2 1 4 3 1 0 Heap B 3 D E

33
3 A B CE D 1 1 3 2 1 4 3 1 0 Heap B 3 D E

34
3 A B CE D 1 1 3 2 1 4 2 1 0 Heap B 2 D E

35
3 A B CE D 1 1 3 2 1 4 2 1 0 Heap B 2 D E

36
3 A B CE D 1 1 3 2 1 4 2 5 1 0 Heap B 2 E 5 D

37
3 A B CE D 1 1 3 2 1 4 2 5 1 0 Heap B 2 E 5 D Frontier?

38
3 A B CE D 1 1 3 2 1 4 2 5 1 0 Heap B 2 E 5 D All nodes reachable from starting node within a given distance

39
3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap E 3 D 5

40
3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap D 5

41
3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap

42
A B CE D 1 1 1 25 3 1 0 3

43
Is Dijkstra’s algorithm correct? Invariant:

44
Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v

45
Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v The only time a vertex gets visited is when the distance from s to that vertex is smaller than the distance to any remaining vertex Therefore, there cannot be any other path that hasn’t been visited already that would result in a shorter path

46
Running time?

47
1 call to MakeHeap

48
Running time? |V| iterations

49
Running time? |V| calls

50
Running time? O(|E|) calls

51
Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|)

52
Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|) Is this an improvement?If |E| < |V| 2 / log |V|

53
Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) Fib heapO(|V|)O(|V| log |V|) O(|E|) O(|V| log |V| + |E|) O(|E| log |V|)

54
What about Dijkstra’s on…? A B C E D 1 1 -10 5 10

55
What about Dijkstra’s on…? A B C E D 1 1 -10 5 10

56
What about Dijkstra’s on…? A B C E D 1 1 5 10 Dijkstra’s algorithm only works for positive edge weights

57
Bounding the distance Another invariant: For each vertex v, dist[v] is an upper bound on the actual shortest distance start of at only update the value if we find a shorter distance An update procedure

58
Can we ever go wrong applying this update rule? We can apply this rule as many times as we want and will never underestimate dist[v] When will dist[v] be right? If u is along the shortest path to v and dist[u] is correct

59
dist[v] will be right if u is along the shortest path to v and dist[u] is correct Consider the shortest path from s to v sp1p1 vp2p2 p3p3 pkpk

60
dist[v] will be right if u is along the shortest path to v and dist[u] is correct What happens if we update all of the vertices with the above update? sp1p1 vp2p2 p3p3 pkpk

61
dist[v] will be right if u is along the shortest path to v and dist[u] is correct What happens if we update all of the vertices with the above update? sp1p1 vp2p2 p3p3 pkpk correct

62
dist[v] will be right if u is along the shortest path to v and dist[u] is correct What happens if we update all of the vertices with the above update? sp1p1 vp2p2 p3p3 pkpk correct

63
dist[v] will be right if u is along the shortest path to v and dist[u] is correct Does the order that we update the vertices matter? sp1p1 vp2p2 p3p3 pkpk correct

64
dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk

65
dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct

66
dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct

67
dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct

68
dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct …

69
dist[v] will be right if u is along the shortest path to v and dist[u] is correct What is the longest (vetex-wise) the path from s to any node v can be? |V| - 1 edges/vertices sp1p1 vp2p2 p3p3 pkpk correct …

70
Bellman-Ford algorithm

71
Initialize all the distances

72
Bellman-Ford algorithm iterate over all edges/vertices and apply update rule

73
Bellman-Ford algorithm

74
check for negative cycles

75
Negative cycles A B C E D 1 1 -10 5 10 3 What is the shortest path from a to e?

76
Bellman-Ford algorithm

77
G S F E A D B C 10 8 1 3 1 1 2 -2 -4 How many edges is the shortest path from s to: A:

78
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 How many edges is the shortest path from s to: A:3

79
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 How many edges is the shortest path from s to: A:3 B:

80
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 How many edges is the shortest path from s to: A:3 B:5

81
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 How many edges is the shortest path from s to: A:3 B:5 D:

82
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 How many edges is the shortest path from s to: A:3 B:5 D:7

83
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 0 Iteration: 0

84
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 010 8 Iteration: 1

85
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 010 12 9 8 Iteration: 2

86
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 05 10 8 9 8 Iteration: 3 A has the correct distance and path

87
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 05 6 11 7 9 8 Iteration: 4

88
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 05 5 7 14 7 9 8 Iteration: 5 B has the correct distance and path

89
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 05 5 6 10 7 9 8 Iteration: 6

90
Bellman-Ford algorithm G S F E A D B C 10 8 1 3 1 1 2 -2 -4 05 5 6 9 7 9 8 Iteration: 7 D (and all other nodes) have the correct distance and path

91
Correctness of Bellman-Ford Loop invariant:

92
Correctness of Bellman-Ford Loop invariant: After iteration i, all vertices with shortest paths from s of length i edges or less have correct distances

93
Runtime of Bellman-Ford O(|V| |E|)

94
Runtime of Bellman-Ford Can you modify the algorithm to run faster (in some circumstances)?

95
All pairs shortest paths Simple approach Call Bellman-Ford |V| times O(|V| 2 |E|) Floyd-Warshall – Θ(|V| 3 ) Johnson’s algorithm – O(|V| 2 log |V| + |V| |E|)

96
Minimum spanning trees What is the lowest weight set of edges that connects all vertices of an undirected graph with positive weights Input: An undirected, positive weight graph, G=(V,E) Output: A tree T=(V,E’) where E’ E that minimizes

97
MST example A BD C 4 1 2 3 4 F E 5 4 6 4 A BD C 4 1 2 F E 5 4

98
MSTs Can an MST have a cycle? A BD C 4 1 2 F E 5 4 4

99
MSTs Can an MST have a cycle? A BD C 4 1 2 F E 5 4

100
Applications? Connectivity Networks (e.g. communications) Circuit desing/wiring hub/spoke models (e.g. flights, transportation) Traveling salesman problem?

101
Cuts A cut is a partitioning of the vertices into two sets S and V-S An edges “crosses” the cut if it connects a vertex u V and v V-S A BD C 4 1 2 3 4 F E 5 4 6 4

102
Minimum cut property Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e. SV-S e’ e Consider an MST with edge e’ that is not the minimum edge

103
Minimum cut property Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e. SV-S e’ e Using e instead of e’, still connects the graph, but produces a tree with smaller weights

104
Algorithm ideas? Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e.

105
Kruskal’s algorithm A BD C 4 1 2 3 4 F E 5 4 6 4 G MST A BD C F E Add smallest edge that connects two sets not already connected

106
A BD C 4 1 2 3 4 F E 5 4 6 4 G MST A BD C 1 F E Add smallest edge that connects two sets not already connected Kruskal’s algorithm

107
A BD C 4 1 2 3 4 F E 5 4 6 4 G MST A BD C 1 2 F E Add smallest edge that connects two sets not already connected Kruskal’s algorithm

108
A BD C 4 1 2 3 4 F E 5 4 6 4 G MST A BD C 4 1 2 F E Kruskal’s algorithm Add smallest edge that connects two sets not already connected

109
A BD C 4 1 2 3 4 F E 5 4 6 4 G MST A BD C 4 1 2 F E 4 Kruskal’s algorithm Add smallest edge that connects two sets not already connected

110
A BD C 4 1 2 3 4 F E 5 4 6 4 G MST A BD C 4 1 2 F E 5 4 Kruskal’s algorithm Add smallest edge that connects two sets not already connected

111
Correctness of Kruskal’s Never adds an edge that connects already connected vertices Always adds lowest cost edge to connect two sets. By min cut property, that edge must be part of the MST

112
Running time of Kruskal’s |V| calls to MakeSet

113
Running time of Kruskal’s O(|E| log |E|)

114
Running time of Kruskal’s 2 |E| calls to FindSet

115
Running time of Kruskal’s |V| calls to Union

116
Running time of Kruskal’s Disjoint set data structure O(|E| log |E|) + MakeSetFindSet |E| calls Union |V| calls Total Linked lists |V| O(|V| |E|) |V|O(|V||E| + |E| log |E|) O(|V| |E|) Linked lists + heuristics |V| O(|E| log |V|) |V|O(|E| log |V|+ |E| log |E|) O(|E| log |E| )

117
Prim’s algorithm

120
Start at some root node and build out the MST by adding the lowest weighted edge at the frontier

121
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E

122
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 0

123
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 4 5 60

124
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 4 5 60

125
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

126
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

127
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

128
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

129
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

130
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

131
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

132
Prim’s A BD C 4 1 2 3 4 F E 5 4 6 4 MST A BD C F E 14 5 420

133
Correctness of Prim’s? Can we use the min-cut property? Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e. Let S be the set of vertices visited so far The only time we add a new edge is if it’s the lowest weight edge from S to V-S

134
Running time of Prim’s Θ(|V|)

135
Running time of Prim’s Θ(|V|)

136
Running time of Prim’s |V| calls to Extract-Min

137
Running time of Prim’s |E| calls to Decrease-Key

138
Running time of Prim’s Same as Dijksta’s algorithm 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) Fib heapO(|V|)O(|V| log |V|) O(|E|) O(|V| log |V| + |E|) O(|E| log |V|) Kruskal’s: O(|E| log |E| )

Similar presentations

OK

© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.

© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google