graphs: msts and shortest paths david kauchak cs161 summer 2009
TRANSCRIPT
![Page 1: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/1.jpg)
Graphs: MSTsand Shortest Paths
David Kauchak
cs161
Summer 2009
![Page 2: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/2.jpg)
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
![Page 3: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/3.jpg)
Shortest paths
What is the shortest path from a to d?
A
B
C E
D
![Page 4: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/4.jpg)
Shortest paths
BFS
A
B
C E
D
![Page 5: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/5.jpg)
Shortest paths
What is the shortest path from a to d?
A
B
C E
D
1
1
3
2
23
4
![Page 6: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/6.jpg)
Shortest paths
We can still use BFS
A
B
C E
D
1
1
3
2
23
4
![Page 7: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/7.jpg)
Shortest paths
We can still use BFS
A
B
C E
D
1
1
3
2
23
4
A
B
C E
D
![Page 8: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/8.jpg)
Shortest paths
We can still use BFS
A
B
C E
D
![Page 9: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/9.jpg)
Shortest paths
What is the problem?
A
B
C E
D
![Page 10: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/10.jpg)
Shortest paths Running time is dependent on the weights
A
B
C4
1
2
A
B
C200
50
100
![Page 11: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/11.jpg)
Shortest paths
A
B
C200
50
100
A
B
C
![Page 12: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/12.jpg)
Shortest paths
A
B
C
![Page 13: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/13.jpg)
Shortest paths
A
B
C
![Page 14: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/14.jpg)
Shortest paths
A
B
C
Nothing will change as we expand the frontier until we’ve gone out 100 levels
![Page 15: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/15.jpg)
Dijkstra’s algorithm
![Page 16: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/16.jpg)
Dijkstra’s algorithm
![Page 17: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/17.jpg)
Dijkstra’s algorithm
prev keeps track of the shortest path
![Page 18: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/18.jpg)
Dijkstra’s algorithm
![Page 19: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/19.jpg)
Dijkstra’s algorithm
![Page 20: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/20.jpg)
Dijkstra’s algorithm
![Page 21: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/21.jpg)
Single source shortest paths
All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms
Why?
![Page 22: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/22.jpg)
A
B
C E
D
1
1
3
3
21
4
![Page 23: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/23.jpg)
A
B
C E
D
1
1
3
3
21
4
![Page 24: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/24.jpg)
A
B
C E
D
1
1
3
3
21
4
0
Heap
A 0B C D E
![Page 25: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/25.jpg)
A
B
C E
D
1
1
3
3
21
4
0
Heap
B C D E
![Page 26: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/26.jpg)
A
B
C E
D
1
1
3
3
21
4
0
Heap
B C D E
![Page 27: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/27.jpg)
A
B
C E
D
1
1
3
3
21
4
1
0
Heap
C 1B D E
![Page 28: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/28.jpg)
A
B
C E
D
1
1
3
3
21
4
1
0
Heap
C 1B D E
![Page 29: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/29.jpg)
A
B
C E
D
1
1
3
3
21
4
3
1
0
Heap
C 1B 3D E
![Page 30: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/30.jpg)
A
B
C E
D
1
1
3
3
21
4
3
1
0
Heap
C 1B 3D E
![Page 31: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/31.jpg)
3
A
B
C E
D
1
1
3
21
4
3
1
0
Heap
B 3D E
![Page 32: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/32.jpg)
3
A
B
C E
D
1
1
3
21
4
3
1
0
Heap
B 3D E
![Page 33: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/33.jpg)
3
A
B
C E
D
1
1
3
21
4
3
1
0
Heap
B 3D E
![Page 34: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/34.jpg)
3
A
B
C E
D
1
1
3
21
4
2
1
0
Heap
B 2D E
![Page 35: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/35.jpg)
3
A
B
C E
D
1
1
3
21
4
2
1
0
Heap
B 2D E
![Page 36: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/36.jpg)
3
A
B
C E
D
1
1
3
21
4
2
51
0
Heap
B 2E 5D
![Page 37: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/37.jpg)
3
A
B
C E
D
1
1
3
21
4
2
51
0
Heap
B 2E 5D
Frontier?
![Page 38: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/38.jpg)
3
A
B
C E
D
1
1
3
21
4
2
51
0
Heap
B 2E 5D
All nodes reachable from starting node within a given distance
![Page 39: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/39.jpg)
3
A
B
C E
D
1
1
3
21
4
2 5
31
0
Heap
E 3D 5
![Page 40: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/40.jpg)
3
A
B
C E
D
1
1
3
21
4
2 5
31
0
Heap
D 5
![Page 41: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/41.jpg)
3
A
B
C E
D
1
1
3
21
4
2 5
31
0
Heap
![Page 42: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/42.jpg)
A
B
C E
D
1
11
2 5
31
0
Heap
3
![Page 43: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/43.jpg)
Is Dijkstra’s algorithm correct?
Invariant:
![Page 44: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/44.jpg)
Is Dijkstra’s algorithm correct?
Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v
![Page 45: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/45.jpg)
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
![Page 46: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/46.jpg)
Running time?
![Page 47: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/47.jpg)
Running time?
1 call to MakeHeap
![Page 48: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/48.jpg)
Running time?
|V| iterations
![Page 49: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/49.jpg)
Running time?
|V| calls
![Page 50: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/50.jpg)
Running time?
O(|E|) calls
![Page 51: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/51.jpg)
Running time?
Depends on the heap implementation
1 MakeHeap |V| ExtractMin |E| DecreaseKey Total
Array O(|V|) O(|V|2) O(|E|) O(|V|2)
Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)
O(|E| log |V|)
![Page 52: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/52.jpg)
Running time?
Depends on the heap implementation
1 MakeHeap |V| ExtractMin |E| DecreaseKey Total
Array O(|V|) O(|V|2) O(|E|) O(|V|2)
Bin heap O(|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|
![Page 53: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/53.jpg)
Running time?
Depends on the heap implementation
1 MakeHeap |V| ExtractMin |E| DecreaseKey Total
Array O(|V|) O(|V|2) O(|E|) O(|V|2)
Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)
Fib heap O(|V|) O(|V| log |V|) O(|E|) O(|V| log |V| + |E|)
O(|E| log |V|)
![Page 54: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/54.jpg)
What about Dijkstra’s on…?
A
B
C E
D11
-10
5
10
![Page 55: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/55.jpg)
What about Dijkstra’s on…?
A
B
C E
D11
-10
5
10
![Page 56: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/56.jpg)
What about Dijkstra’s on…?
A
B
C E
D11
5
10
Dijkstra’s algorithm only works for positive edge weights
![Page 57: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/57.jpg)
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
)},(][],[min{][ vuwudistvdistvdist
![Page 58: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/58.jpg)
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
)},(][],[min{][ vuwudistvdistvdist
![Page 59: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/59.jpg)
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
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
![Page 60: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/60.jpg)
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?
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
![Page 61: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/61.jpg)
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?
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct
![Page 62: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/62.jpg)
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?
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct correct
![Page 63: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/63.jpg)
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?
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct correct
![Page 64: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/64.jpg)
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 pi to have the correct shortest path from s? i times
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
![Page 65: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/65.jpg)
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 pi to have the correct shortest path from s? i times
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct correct
![Page 66: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/66.jpg)
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 pi to have the correct shortest path from s? i times
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct correct correct
![Page 67: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/67.jpg)
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 pi to have the correct shortest path from s? i times
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct correct correct correct
![Page 68: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/68.jpg)
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 pi to have the correct shortest path from s? i times
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct correct correct correct …
![Page 69: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/69.jpg)
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
)},(][],[min{][ vuwudistvdistvdist
s p1 vp2 p3 pk
correct correct correct correct …
![Page 70: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/70.jpg)
Bellman-Ford algorithm
![Page 71: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/71.jpg)
Bellman-Ford algorithm
Initialize all the distances
![Page 72: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/72.jpg)
Bellman-Ford algorithm
iterate over all edges/vertices and apply update rule
![Page 73: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/73.jpg)
Bellman-Ford algorithm
![Page 74: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/74.jpg)
Bellman-Ford algorithm
check for negative cycles
![Page 75: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/75.jpg)
Negative cycles
A
B
C E
D11
-10
5
10
3
What is the shortest path from a to e?
![Page 76: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/76.jpg)
Bellman-Ford algorithm
![Page 77: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/77.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is the shortest path from s to:
A:
![Page 78: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/78.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is the shortest path from s to:
A: 3
![Page 79: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/79.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is the shortest path from s to:
A: 3
B:
![Page 80: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/80.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is the shortest path from s to:
A: 3
B: 5
![Page 81: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/81.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is the shortest path from s to:
A: 3
B: 5
D:
![Page 82: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/82.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is the shortest path from s to:
A: 3
B: 5
D: 7
![Page 83: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/83.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0
Iteration: 0
![Page 84: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/84.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 10
8
Iteration: 1
![Page 85: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/85.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 10
12
9
8
Iteration: 2
![Page 86: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/86.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
10
8
9
8
Iteration: 3
A has the correct distance and path
![Page 87: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/87.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
6
11
7
9
8
Iteration: 4
![Page 88: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/88.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
5
7
147
9
8
Iteration: 5
B has the correct distance and path
![Page 89: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/89.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
5
6
107
9
8
Iteration: 6
![Page 90: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/90.jpg)
Bellman-Ford algorithm
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
5
6
97
9
8
Iteration: 7
D (and all other nodes) have the correct distance and path
![Page 91: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/91.jpg)
Correctness of Bellman-Ford
Loop invariant:
![Page 92: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/92.jpg)
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
![Page 93: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/93.jpg)
Runtime of Bellman-Ford
O(|V| |E|)
![Page 94: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/94.jpg)
Runtime of Bellman-Ford
Can you modify the algorithm to run faster (in some circumstances)?
![Page 95: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/95.jpg)
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|)
![Page 96: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/96.jpg)
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
'
)(Ee
ewTweight
![Page 97: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/97.jpg)
MST example
A
B D
C
4
1
23
4F
E
54
6
4
A
B D
C
4
1
2
F
E
54
![Page 98: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/98.jpg)
MSTs
Can an MST have a cycle?
A
B D
C
4
1
2
F
E
54
4
![Page 99: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/99.jpg)
MSTs
Can an MST have a cycle?
A
B D
C
4
1
2
F
E
54
![Page 100: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/100.jpg)
Applications?
Connectivity Networks (e.g. communications) Circuit desing/wiring
hub/spoke models (e.g. flights, transportation)
Traveling salesman problem?
![Page 101: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/101.jpg)
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 uV and vV-S
A
B D
C
4
1
23
4F
E
54
6
4
![Page 102: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/102.jpg)
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.
S V-S
e’
e
Consider an MST with edge e’ that is not the minimum edge
![Page 103: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/103.jpg)
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.
S V-S
e’
e
Using e instead of e’, still connects the graph, but produces a tree with smaller weights
![Page 104: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/104.jpg)
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.
![Page 105: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/105.jpg)
Kruskal’s algorithm
A
B D
C
4
1
23
4F
E
54
6
4
G
MST
A
B D
C
F
E
Add smallest edge that connects two sets not already connected
![Page 106: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/106.jpg)
A
B D
C
4
1
23
4F
E
54
6
4
G
MST
A
B D
C1
F
E
Add smallest edge that connects two sets not already connected
Kruskal’s algorithm
![Page 107: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/107.jpg)
A
B D
C
4
1
23
4F
E
54
6
4
G
MST
A
B D
C1
2
F
E
Add smallest edge that connects two sets not already connected
Kruskal’s algorithm
![Page 108: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/108.jpg)
A
B D
C
4
1
23
4F
E
54
6
4
G
MST
A
B D
C
4
1
2
F
E
Kruskal’s algorithmAdd smallest edge that connects two sets not already connected
![Page 109: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/109.jpg)
A
B D
C
4
1
23
4F
E
54
6
4
G
MST
A
B D
C
4
1
2
F
E
4
Kruskal’s algorithmAdd smallest edge that connects two sets not already connected
![Page 110: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/110.jpg)
A
B D
C
4
1
23
4F
E
54
6
4
G
MST
A
B D
C
4
1
2
F
E
54
Kruskal’s algorithmAdd smallest edge that connects two sets not already connected
![Page 111: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/111.jpg)
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
![Page 112: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/112.jpg)
Running time of Kruskal’s
|V| calls to MakeSet
![Page 113: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/113.jpg)
Running time of Kruskal’s
O(|E| log |E|)
![Page 114: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/114.jpg)
Running time of Kruskal’s
2 |E| calls to FindSet
![Page 115: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/115.jpg)
Running time of Kruskal’s
|V| calls to Union
![Page 116: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/116.jpg)
Running time of Kruskal’s
Disjoint set data structure
O(|E| log |E|) +
MakeSet FindSet|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| )
![Page 117: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/117.jpg)
Prim’s algorithm
![Page 118: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/118.jpg)
Prim’s algorithm
![Page 119: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/119.jpg)
Prim’s algorithm
![Page 120: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/120.jpg)
Prim’s algorithm Start at some root node and build out the MST by
adding the lowest weighted edge at the frontier
![Page 121: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/121.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
![Page 122: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/122.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
0
![Page 123: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/123.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
4 5
6 0
![Page 124: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/124.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
4 5
6 0
![Page 125: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/125.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 126: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/126.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 127: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/127.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 128: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/128.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 129: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/129.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 130: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/130.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 131: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/131.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 132: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/132.jpg)
Prim’s
A
B D
C
4
1
23
4F
E
54
6
4 MST
A
B D
C
F
E
1 4 5
4 2 0
![Page 133: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/133.jpg)
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
![Page 134: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/134.jpg)
Running time of Prim’s
Θ(|V|)
![Page 135: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/135.jpg)
Running time of Prim’s
Θ(|V|)
![Page 136: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/136.jpg)
Running time of Prim’s
|V| calls to Extract-Min
![Page 137: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/137.jpg)
Running time of Prim’s
|E| calls to Decrease-Key
![Page 138: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cae5503460f949710e2/html5/thumbnails/138.jpg)
Running time of Prim’s
Same as Dijksta’s algorithm
1 MakeHeap |V| ExtractMin |E| DecreaseKey Total
Array O(|V|) O(|V|2) O(|E|) O(|V|2)
Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)
Fib heap O(|V|) O(|V| log |V|) O(|E|) O(|V| log |V| + |E|)
O(|E| log |V|)
Kruskal’s: O(|E| log |E| )