1 of 91

CSE 373 SU23 Section 7

MSTs + Disjoint Sets

2 of 91

Agenda

  • Announcements

  • Disjoint Set ADT

  • Representing Disjoint Sets

  • MSTs

3 of 91

Announcements

Mon (7/31)

Tues (8/1)

Wed (8/2)

Thurs (8/3)

Fri (8/4)

Sat (8/5)

Mon (8/7)

Tues (8/8)

Wed (8/9)

Thurs (8/10)

Fri (8/11)

Sat (8/12)

EX5 due @ 11:59 pm

  • P4 is released
    • Last project, due Aug 18 at 11:59pm
    • Its is hard, so please start as soon as possible!!!
  • Final is on the last day of class, Friday August 18th
    • 1:10 - 2:10 PM in Smith 120

4 of 91

MicroTeach: Disjoint Sets

5 of 91

Disjoint Set ADT

  • makeSet(x): creates a new set within the disjoint set where the only member is x (also representative for set).�
  • findSet(x): looks up the set containing element x, returns representative of that set.�
  • union(x, y): looks up set containing x and set containing y, combines two sets into one. Picks new representative for resulting set.

� �

13

11

12

7

1

8

2

Representative

Representative

6 of 91

Disjoint Set Uses

  • Big Idea: find out if two elements are in the same set!�
  • Example below: findSet(1) = findSet(7) = 8. So 1 and 7 are in the same set!

� �

13

11

12

7

1

8

2

Representative

Representative

7 of 91

Example: union(1, 2)

findSet(1)

� �

findSet(2)

� �

13

11

12

7

1

8

2

13

11

12

7

1

8

2

Step 1: Use findSet twice to find the two representatives.

Step 2: Stick one representative under the other.

8 of 91

Optimization 1: Union By Size

Problem: Trees can be unbalanced!�

  • size(x) upper bounds the height of x (so size(x) >= height(x)).�
  • Keep track of size of all trees.�
  • When unioning make the tree with larger size the root.�
  • If it’s a tie, pick one to be the root arbitrarily.

size = 1

size = 4

9 of 91

Optimization 2: Path Compression

Clever idea: When we do findSet(15), tie all nodes seen to the root!

    • Additional cost is insignificant (same complexity class).

15

11

5

12

13

6

1

7

14

8

2

9

10

3

0

4

10 of 91

Optimization 2: Path Compression

Clever idea: When we do findSet(15), tie all nodes seen to the root!

    • Additional cost is insignificant (same complexity class).

15

11

5

12

13

6

1

7

14

8

2

9

0

4

10

3

11 of 91

Array Disjoint Set Implementation

  • Map stores item→index in our array representing disjoint sets

  • Array in which each index represents an item (from map)
    • Value at the index is either the index of that item's parent OR, if the item is the representative of the set, -1 * size

Map:

apple

0

banana

1

carrot

2

0

1

2

apple

carrot

banana

Array:

Disjoint Set:

0

0

-3

12 of 91

Problem 1B: Disjoint Sets

13 of 91

Problem 1B: Disjoint Sets

union(2, 13)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 6

Size 1

Size 5

Size 2

14 of 91

Problem 1B: Disjoint Sets

union(2, 13)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 6

Size 1

Size 5

Size 2

15 of 91

Problem 1B: Disjoint Sets

union(2, 13)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 6

Size 1

Size 5

Size 2

16 of 91

Problem 1B: Disjoint Sets

union(2, 13) ✅

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 3

Size 6

Size 5

17 of 91

Problem 1B: Disjoint Sets

union(4, 12)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 3

Size 6

Size 5

18 of 91

Problem 1B: Disjoint Sets

union(4, 12)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 3

Size 6

Size 5

Call findSet(12)

19 of 91

Problem 1B: Disjoint Sets

union(4, 12)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Path compression! 😎

Size 3

Size 6

Size 5

20 of 91

Problem 1B: Disjoint Sets

union(4, 12)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 3

Size 6

Size 5

21 of 91

Problem 1B: Disjoint Sets

union(4, 12) ✅

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 11

Size 3

22 of 91

Problem 1B: Disjoint Sets

union(2, 8)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 11

Size 3

23 of 91

Problem 1B: Disjoint Sets

union(2, 8)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 11

Size 3

24 of 91

Problem 1B: Disjoint Sets

union(2, 8)

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 3

Size 11

25 of 91

Problem 1B: Disjoint Sets

union(2, 8) ✅

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 14

26 of 91

Problem 1B: Disjoint Sets

5

1

3

4

0

6

2

12

11

10

9

8

13

7

Size 14

27 of 91

Problems 3-5: Disjoint Sets as Arrays

28 of 91

Q3: Disjoint Sets Array Representation

29 of 91

Q3: DJS Array Representation [PRESENT for Animations]

Given a mapping of items->their index,

fill out the pointers array representation for this disjoint set

8

11

7

50

4

3

2

1

Size 8

14

10

9

6

Size 4

Array Representation

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

Acts as “pointers”. Stores parent index or -size if element is set representative.

-8

-8

1

1

-4

1

1

Same parent as 50 and 7, so same index mapping!

1

1

2

2

Same parent!

5

5

5

5

7

7

Now try it yourself on the other set!

Click to next slide for the answer.

9

10

9

30 of 91

Q4: Disjoint Sets Array Find Set

31 of 91

Q4: DJS Array Find [PRESENT for Animations]

Call findSet(2) on the following disjoint sets. Give the return value of the call, and the updated array representation. Draw the resulting disjoint sets.

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

7

-4

9

10

8

11

7

50

4

3

2

1

Size 8

3

2

5

1

Return Value:

1

14

10

9

6

Size 4

32 of 91

Q5: Disjoint Sets Array Union

33 of 91

Q5: Disjoint Sets Array Union

union(3, 14) on the array representation of this Disjoint Set

8

11

7

50

4

3

2

1

Size 8

14

10

9

6

Size 4

34 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

8

11

7

50

4

3

2

1

Size 8

14

10

9

6

Size 4

Array Representation

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

5

7

-4

9

10

Acts as “pointers”. Stores parent index or -size if element is set representative.

35 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

5

7

-4

9

10

36 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3)

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

5

7

-4

9

10

37 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3)

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

5

7

-4

9

10

38 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3)

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

5

7

-4

9

10

39 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

5

7

-4

9

10

Found representative!

40 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

5

7

-4

9

10

Time to do path compression!

41 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

10

Time to do path compression!

42 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

10

43 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14)

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

10

44 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14)

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

10

45 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14)

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

10

46 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14) = 9

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

10

Found representative!

47 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14) = 9

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

10

Time to do path compression!

48 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14) = 9

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

9

Time to do path compression!

49 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14) = 9

Items:

Index:

Value:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

9

50 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1

findSet(14) = 9

findSet(3) == findSet(14)

No! 3 and 14 belong to different sets within our disjoint set.

50

4

7

8

9

11

1

2

3

6

10

14

Items:

Index:

Value:

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

9

?

¿

?

51 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1 → get set’s size

findSet(14) = 9 → get set’s size

Assign the smaller set’s rep. to point to the larger set’s rep. for the union by size optimization.

50

4

7

8

9

11

1

2

3

6

10

14

Items:

Index:

Value:

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

9

52 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1 → -1 * array.get(1) = 8

findSet(14) = 9 → -1 * array.get(9) = 4

Assign the smaller set’s rep. to point to the larger set’s rep. for the union by size optimization.

50

4

7

8

9

11

1

2

3

6

10

14

Items:

Index:

Value:

0

1

2

3

4

5

6

7

8

9

10

11

1

-8

1

2

9

1

5

1

1

-4

9

9

53 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

findSet(3) = 1 → -1 * array.get(1) = 8

findSet(14) = 9 → -1 * array.get(9) = 4

Assign the smaller set’s rep. to point to the larger set’s rep. for the union by size optimization.

50

4

7

8

9

11

1

2

3

6

10

14

Items:

Index:

Value:

0

1

2

3

4

5

6

7

8

9

10

11

1

-12

1

2

9

1

5

1

1

1

9

9

Update larger set’s size

Update smaller set’s parent

54 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

Final Result!

50

4

7

8

9

11

1

2

3

6

10

14

Items:

Index:

Value:

0

1

2

3

4

5

6

7

8

9

10

11

1

-12

1

2

9

1

5

1

1

1

9

9

55 of 91

Q5: Disjoint Sets Array Union

union(3, 14)

8

11

7

50

4

3

2

1

Size 12

14

10

9

6

Array Representation

Items:

50

4

7

8

9

11

1

2

3

6

10

14

0

1

2

3

4

5

6

7

8

9

10

11

1

-12

1

2

9

1

5

1

1

1

9

9

Index:

Value:

56 of 91

MicroTeach: MSTs

57 of 91

MSTs

  • Tree: (when talking about graphs) An connected acyclic graph.�
  • Spanning Tree: A set of edges connecting all nodes of the graph in a Tree.�
  • Minimum Spanning Tree: The Spanning Tree of lowest possible total weight.�
  • Multiple algorithms that are used to find MSTs!

� �

1

A

B

C

D

3

4

2

5

1

A

B

C

D

3

4

2

5

1

A

B

C

D

3

4

2

5

58 of 91

MSTs vs. SPTs: A Key Difference

A shortest paths tree depends on the source vertex. There is no source vertex for a minimum spanning tree. The SPT for every other vertex is different from the MST!

Minimizes each node’s distance from source

Minimizes tree’s total edge weight

A

2

4

3

1

D

B

C

SPT from A

A

2

4

3

1

D

B

C

MST

59 of 91

BFS

Dijkstra’s

Prim’s & Kruskal’s

Both find Shortest Paths Trees (SPTs)!

Finds MST!

  • Unweighted
  • Shortest path (number of edges) from S to every other node
  • Weighted
  • Shortest path (sum of edge weights) from S to every other node
  • Weighted, undirected
  • Lowest cumulative weight for whole tree

Y

S

Z

T

X

Y

S

Z

T

X

6

7

8

9

7

3

2

5

4

Y

S

Z

T

X

6

7

8

9

7

3

2

5

4

60 of 91

MST Finding Algorithms

Prim’s

Start a tree from a single node.��Each iteration, connect a node in the tree to a node not in the tree with the cheapest edge possible. ��(Slowly builds up one, connected tree).

Kruskal’s

Repeatedly add the smallest edge that doesn’t cause a cycle in the tree you are building. ��(Can build a few disconnected trees before they all merge).

[Thinks about nodes!]

[Thinks about edges!]

61 of 91

Prim’s Algorithm Pseudocode

PrimMST(Graph G)

initialize distances to

mark source as distance 0

mark all vertices unprocessed

foreach (edge (source, v) ) {

v.dist = weight(source,v)

v.bestEdge = (source,v)

}

while (there are unprocessed vertices) {

let u be the closest unprocessed vertex

add u.bestEdge to spanning tree

foreach (edge (u,v) leaving u) {

if (weight(u,v) < v.dist && v unprocessed ) {

V.dist = weight(u,v)

v.bestEdge = (u,v)

}

}

mark u as processed

}

Prim’s

Creates a tree starting from a single node. During each iteration, connect a node in the tree to a node not in the tree with the lowest edge weight. Iterate until we have a spanning tree.��Thinks about what nodes we are adding and why.

62 of 91

Kruskal’s Algorithm Pseudocode

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v

Add edge(u,v) to mst

Kruskal’s

Start by adding the smallest edge.

During each iteration add the lowest weight edge that does not create a cycle to build the tree.

It will not be a connected tree until we finish�

Thinks about what edges we are adding and why.

63 of 91

Q6A + Q6C + Q7:

MST Practice

64 of 91

Problem 6A: Prim’s

65 of 91

Problem 6A: Prim’s

A

C

B

D

2

7

1

4

3

E

5

G

F

9

10

0

6

8

dist

edge

processed

A

0

none

yes

B

2

(AB)

C

7

(AC)

D

infinity

E

8

(AE)

F

infinity

G

infinity

66 of 91

Problem 6A: Prim’s

A

C

B

D

2

7

1

4

3

E

5

G

F

9

10

0

6

8

dist

edge

processed

A

0

none

yes

B

2

(AB)

yes

C

3

(BC)

D

4

(BD)

E

8

(AE)

F

infinity

G

infinity

67 of 91

Problem 6A: Prim’s

A

C

B

D

2

7

1

4

3

E

5

G

F

9

10

0

6

8

dist

edge

processed

A

0

none

yes

B

2

(AB)

yes

C

3

(BC)

yes

D

1

(CD)

E

5

(CE)

F

infinity

G

infinity

68 of 91

Problem 6A: Prim’s

A

C

B

D

2

7

1

4

3

E

5

G

F

9

10

0

6

8

dist

edge

processed

A

0

none

yes

B

2

(AB)

yes

C

3

(BC)

yes

D

1

(CD)

yes

E

5

(CE)

F

6

(DF)

G

10

(DG)

69 of 91

Problem 6A: Prim’s

A

C

B

D

2

7

1

4

3

E

5

G

F

9

10

0

6

8

dist

edge

processed

A

0

none

yes

B

2

(AB)

yes

C

3

(BC)

yes

D

1

(CD)

yes

E

5

(CE)

yes

F

6

(DF)

G

9

(EG)

70 of 91

Problem 6A: Prim’s

A

C

B

D

2

7

1

4

3

E

5

G

F

9

10

0

6

8

dist

edge

processed

A

0

none

yes

B

2

(AB)

yes

C

3

(BC)

yes

D

1

(CD)

yes

E

5

(CE)

yes

F

6

(DF)

yes

G

0

(FG)

71 of 91

Problem 6A: Prim’s

A

C

B

D

2

7

1

4

3

E

5

G

F

9

10

0

6

8

dist

edge

processed

A

0

none

yes

B

2

(AB)

yes

C

3

(BC)

yes

D

1

(CD)

yes

E

5

(CE)

yes

F

6

(DF)

yes

G

0

(FG)

yes

72 of 91

Problem 6A: Prim’s

A

C

B

D

2

1

3

E

5

G

F

0

6

73 of 91

Problem 6C: Unique MSTs

74 of 91

Problem 6C: Prim’s

A

C

B

D

2

1

3

E

5

G

F

0

6

75 of 91

Problem 6C: Prim’s

Only 1 MST!

A

C

B

D

2

1

3

E

5

G

F

0

6

76 of 91

Problem 6C: Prim’s

Only 1 MST!

(All edge weights are unique.)

A

C

B

D

2

1

3

E

5

G

F

0

6

77 of 91

Problem 6C: Prim’s

Only 1 MST!

(All edge weights are unique.)

Intuition: Doing a swap like this would make MST more expensive!

A

C

B

D

2

1

3

E

5

G

F

0

6

8

78 of 91

Problem 7: Kruskal’s

79 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

B

C

D

E

F

G

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

4

80 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{A}

B

{B}

C

{C}

D

{D}

E

{E}

F

{F}

G

{G}

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

4

81 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{A}

B

{B}

C

{C, F}

D

{D}

E

{E}

F

{C, F}

G

{G}

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

4

82 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{A}

B

{B}

C

{C, F}

D

{D, G}

E

{E}

F

{C, F}

G

{D, G}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

83 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{A, E}

B

{B}

C

{C, F}

D

{D, G}

E

{E, A}

F

{C, F}

G

{D, G}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

84 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{E, C, F, A}

B

{B}

C

{E, C, F, A}

D

{D, G}

E

{E, C, F, A}

F

{E, C, F, A}

G

{D, G}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

85 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{E, C, F, A}

B

{D, G, B}

C

{E, C, F, A}

D

{D, G, B}

E

{E, C, F, A}

F

{E, C, F, A}

G

{D, G, B}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

86 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{E, C, F, A}

B

{D, G, B}

C

{E, C, F, A}

D

{D, G, B}

E

{E, C, F, A}

F

{E, C, F, A}

G

{D, G, B}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

87 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{E, C, F, A, D, G, B}

B

{E, C, F, A, D, G, B}

C

{E, C, F, A, D, G, B}

D

{E, C, F, A, D, G, B}

E

{E, C, F, A, D, G, B}

F

{E, C, F, A, D, G, B}

G

{E, C, F, A, D, G, B}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

88 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{E, C, F, A, D, G, B}

B

{E, C, F, A, D, G, B}

C

{E, C, F, A, D, G, B}

D

{E, C, F, A, D, G, B}

E

{E, C, F, A, D, G, B}

F

{E, C, F, A, D, G, B}

G

{E, C, F, A, D, G, B}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

89 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{E, C, F, A, D, G, B}

B

{E, C, F, A, D, G, B}

C

{E, C, F, A, D, G, B}

D

{E, C, F, A, D, G, B}

E

{E, C, F, A, D, G, B}

F

{E, C, F, A, D, G, B}

G

{E, C, F, A, D, G, B}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

90 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

5

1

6

9

3

2

Disjoint set components

A

{E, C, F, A, D, G, B}

B

{E, C, F, A, D, G, B}

C

{E, C, F, A, D, G, B}

D

{E, C, F, A, D, G, B}

E

{E, C, F, A, D, G, B}

F

{E, C, F, A, D, G, B}

G

{E, C, F, A, D, G, B}

4

Kruskal’s pseudocode(Graph G):

Create disjoint sets for each vertex in G

Sort the edges by weight

for each edge(u,v) in sorted order:

If u is not in the same set as v:

Join set u and v)

Add edge(u,v) to mst

91 of 91

Ex. Kruskal’s

A

B

E

C

D

G

F

4

2

3

1

3

2

Tada!