cs 106b, lecture 24 dijkstra’s and kruskal’s · 7 dijkstra pseudocode dijkstra(v 1, v 2):...

68
CS 106B, Lecture 24 Dijkstra’s and Kruskal’s This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others

Upload: others

Post on 24-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.

CS 106B, Lecture 24Dijkstra’s and Kruskal’s

This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others

Page 2: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

2

Plan for Today• Real-world graph algorithms (with coding examples!)

– Dijkstra's Algorithm for finding the least-cost path (like Google Maps)– Kruskal's Algorithm for finding the minimum spanning tree

• Applications in civil engineering and biology

Page 3: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

3

Shortest Paths• Recall: BFS allows us to find the shortest path

• Sometimes, you want to find the least-cost path– Only applies to graphs with weighted edges

• Examples:– cheapest flight(s) from here to New York– fastest driving route (Google Maps)– the internet: fastest path to send information through the network of

routers

Page 4: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

4

Least-Cost Paths• BFS uses a queue to keep track of which nodes to use next• BFS pseudocode:

bfs from v1:add v1 to the queue.while queue is not empty:dequeue a node nenqueue n's unseen neighbors

• How could we modify this pseudocode to dequeue the least-costnodes instead of the closest nodes?

Page 5: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

5

Least-Cost Paths• BFS uses a queue to keep track of which nodes to use next• BFS pseudocode:

bfs from v1:add v1 to the queue.while queue is not empty:dequeue a node nenqueue n's unseen neighbors

• How could we modify this pseudocode to dequeue the least-costnodes instead of the closest nodes?– Use a priority queue instead of a queue

Page 6: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

6

Edsger Dijkstra (1930-2002)

• famous Dutch computer scientist and prof. at UT Austin– Turing Award winner (1972)

• Noteworthy algorithms and software:– THE multiprogramming system (OS)

• layers of abstraction– Compiler for a language that can do recursion– Dijkstra's algorithm– Dining Philosophers Problem: resource contention, deadlock

• famous papers:– "Go To considered harmful"– "On the cruelty of really teaching computer science"

Page 7: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

7

Dijkstra pseudocodedijkstra(v1, v2):

consider every vertex to have a cost of infinity, except v1 which has a cost of 0.create a priority queue of vertexes, ordered by cost, storing only v1 .

while the pqueue is not empty:dequeue a vertex v from the pqueue, and mark it as visited.for each unvisited neighbor, n, of v, we can reach nwith a total cost of (v's cost + the weight of the edge from v to n).

if this cost is cheaper than n's current cost,we should enqueue the neighbor n to the pqueue with this new cost,and remember v was its previous vertex.

when we are done, we can reconstruct the path from v2 back to v1by following the path of previous vertices.

Page 8: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

8

Dijkstra exampledijkstra(A, F);

• color key– white: unexamined– yellow: enqueued– green: visited

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 ¥

¥ ¥

¥

¥ ¥

v1's distance := 0. all other distances := ¥.

pqueue = {A:0}

H

13

I

6

¥

¥

Page 9: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

9

Dijkstra exampledijkstra(A, F);

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

¥ ¥

1

¥ ¥

pqueue = {D:1, B:2}

H

13

I

6

¥

¥

Page 10: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

10

Dijkstra exampledijkstra(A, F);

pqueue = {B:2, C:3, E:3, G:5, F:9}

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

33

1

9 5

H

13

I

6

¥

¥

Page 11: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

11

Dijkstra exampledijkstra(A, F);

pqueue = {C:3, E:3, G:5, F:9}

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

3 3

1

9 5

H

13

I

6

¥

¥

Page 12: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

12

Dijkstra exampledijkstra(A, F);

pqueue = {E:3, G:5, F:8, H:16}

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

3 3

1

8 5

13

I

6

¥

H16

Page 13: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

13

Dijkstra exampledijkstra(A, F);

pqueue = {G:5, F:8, H:16}

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

3 3

1

8 5

13

I

6

¥

H16

Page 14: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

14

Dijkstra exampledijkstra(A, F);

pqueue = {F:6, H:16}

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

3 3

1

6 5

13

I

6

¥

H16

Page 15: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

15

Dijkstra exampledijkstra(A, F);

pqueue = {H:16}

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

3 3

1

6 5

13

I

6

¥

H16

Page 16: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

16

Dijkstra exampledijkstra(A, F);

A

GF

B

EC D

4 1

2

103

64

22

85

1

0 2

3 3

1

6 5

13

I

6

¥

H16

Page 17: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

17

Algorithm properties• Dijkstra's algorithm is a greedy algorithm:

– Make choices that currently seem best

• It is correct because it maintains the following two properties:– 1) for every marked vertex, the current recorded cost is the lowest

cost to that vertex from the source vertex.– 2) for every unmarked vertex v, its recorded distance is shortest path

distance to v from source vertex, considering only currently known vertices and v.

Page 18: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

18

Dijkstra's runtime• For sparse graphs, (i.e. graphs with much less than V 2 edges)

Dijkstra's is implemented most efficiently with a priority queue.

– initialization: O(V)– while loop: O(V) times

• remove min-cost vertex from pq: O(log V)• potentially perform E updates on cost/previous• update costs in pq: O(log V)

– reconstruct path: O(E)

– Total runtime: O(V log V + E log V)• For more in depth calculation: http://www-

inst.eecs.berkeley.edu/~cs61bl/r//cur/graphs/dijkstra-algorithm-runtime.html?topic=lab24.topic&step=4&course=

Page 19: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

19

Announcements• Assn. 6 due today

• Assn. 7 (the last one!) comes out today

Page 20: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

20

Minimum Spanning Trees• Sometimes, you want to find a way to connect every node in a

graph in the least-cost way possible– Utility (road, water, or power) connectivity– Tracing virus evolution

source: https://www.researchgate.net/figure/Position-of-the-eleven-Dutch-strains-on-the-B-anthracis-phylogenetic-tree-based-on_fig2_274406206

Page 21: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

21

Spanning trees• A spanning tree of a graph is a set of edges that connects all

vertices in the graph with no cycles.– What is a spanning tree for the graph below?

XU

V

W

Z

Y

a

c

b

e

d

f

g

hXU

V

W

Z

Y

c

b

eg

h

Page 22: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

22

Minimum spanning tree• minimum spanning tree (MST): A spanning tree that has the lowest

combined edge weight (cost).

XU

V

W

Z

Y

8

4

3

5

6

6

2

9XU

V

W

Z

Y

4

3

52

9

Page 23: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

23

MST examples• Q: How many minimum spanning trees does this graph have?

A. 0-1B. 2-3C. 4-5D. 6-7E. > 7

(question courtesy Cynthia Lee)

B D

ECA

F

3

3

1

3

3

7

Page 24: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

24

Kruskal's algorithm• Kruskal's algorithm: Finds a MST in a given graph.

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

• Runtime: O(E log E) = O(E log V)

Page 25: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

25

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {a:1, b:2, c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 26: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

26

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {b:2, c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 27: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

27

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {b:2, c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 28: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

28

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 29: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

29

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 30: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

30

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {d:4, e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 31: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

31

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 32: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

32

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 33: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

33

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 34: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

34

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

e:5

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 35: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

35

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 36: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

36

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 37: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

37

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 38: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

38

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 39: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

39

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

g:7

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 40: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

40

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {h:8, i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 41: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

41

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 42: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

42

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {i:9, j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 43: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

43

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 44: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

44

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {j:10, k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 45: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

45

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 46: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

46

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9j:10

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 47: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

47

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {k:11, l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 48: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

48

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 49: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

49

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {l:12, m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 50: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

50

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 51: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

51

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

l:12

m:13

n:14

o:15

p:16q:17

r:18

Page 52: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

52

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {m:13, n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

m:13

n:14

o:15

p:16q:17

r:18

Page 53: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

53

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

m:13

n:14

o:15

p:16q:17

r:18

Page 54: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

54

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {n:14, o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

n:14

o:15

p:16q:17

r:18

Page 55: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

55

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

n:14

o:15

p:16q:17

r:18

Page 56: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

56

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {o:15, p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

o:15

p:16q:17

r:18

Page 57: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

57

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

o:15

p:16q:17

r:18

Page 58: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

58

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {p:16, q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16q:17

r:18

Page 59: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

59

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16q:17

r:18

Page 60: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

60

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {q:17, r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16q:17

r:18

Page 61: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

61

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16q:17

r:18

Page 62: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

62

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = {r:18}

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16

r:18

Page 63: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

63

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = { }

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16

r:18

Page 64: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

64

Kruskal example• In what order would Kruskal's algorithm visit the edges

in the graph below? What MST would it produce?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

– pq = { }

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16

Page 65: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

65

Kruskal example• Kruskal's algorithm would output the following MST:

– {a, b, c, d, f, h, i, k, p}

• The MST's total cost is:1+2+3+4+6+8+9+11+16 = 60

– Can you find any spanning trees of lower cost?Of equal cost?

a:1 b:2c:3

d:4

f:6

h:8

i:9

k:11

p:16

Page 66: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

66

Implementing Kruskal• What data structures should we use to implement this algorithm?

function kruskal(graph):Start with an empty structure for the MSTPlace all edges into a priority queue

based on their weight (cost).While the priority queue is not empty:

Dequeue an edge e from the priority queue.If e's endpoints aren't already connected,

add that edge into the MST.Otherwise, skip the edge.

Page 67: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

67

Vertex clusters• Need some way to identify which vertexes are "connected" to

which other ones– we call these "clusters" of vertices

• Also need an efficient wayto figure out which clustera given vertex is in.

• Also need to merge clusterswhen adding an edge.

Page 68: CS 106B, Lecture 24 Dijkstra’s and Kruskal’s · 7 Dijkstra pseudocode dijkstra(v 1, v 2): consider every vertex to have a cost of infinity, except v 1which has a cost of 0. create

68

Kruskal's Code• How would we code Kruskal's algorithm to find a minimum

spanning tree?• What type of graph (adjacency list, adjacency matrix, or edge list)

should we use?