cse 373: minimum spanning trees: prim and kruskal › ... › slides ›...

139
CSE 373: Minimum Spanning Trees: Prim and Kruskal Michael Lee Monday, Feb 26, 2018 1

Upload: others

Post on 05-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

CSE 373: Minimum Spanning Trees: Primand Kruskal

Michael LeeMonday, Feb 26, 2018

1

Page 2: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees

Punchline: a MST of a graph connects all the vertices togetherwhile minimizing the number of edges used (and their weights).

Minimum spanning treesGiven a connected, undirected graph G = (V ,E), a minimumspanning tree is a subgraph G ′ = (V ′,E ′) such that...

I V = V ′ (G ′ is spanning)I There exists a path from any vertex to any other oneI The sum of the edge weights in E ′ is minimized.

In order for a graph to have a MST, the graph must...

I ...be connected – there is a path from a vertex to any othervertex. (Note: this means |V | ≤ |E |).

I ...be undirected.

2

Page 3: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees

Punchline: a MST of a graph connects all the vertices togetherwhile minimizing the number of edges used (and their weights).Minimum spanning treesGiven a connected, undirected graph G = (V ,E), a minimumspanning tree is a subgraph G ′ = (V ′,E ′) such that...

I V = V ′ (G ′ is spanning)I There exists a path from any vertex to any other oneI The sum of the edge weights in E ′ is minimized.

In order for a graph to have a MST, the graph must...

I ...be connected – there is a path from a vertex to any othervertex. (Note: this means |V | ≤ |E |).

I ...be undirected.

2

Page 4: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees

Punchline: a MST of a graph connects all the vertices togetherwhile minimizing the number of edges used (and their weights).Minimum spanning treesGiven a connected, undirected graph G = (V ,E), a minimumspanning tree is a subgraph G ′ = (V ′,E ′) such that...

I V = V ′ (G ′ is spanning)

I There exists a path from any vertex to any other oneI The sum of the edge weights in E ′ is minimized.

In order for a graph to have a MST, the graph must...

I ...be connected – there is a path from a vertex to any othervertex. (Note: this means |V | ≤ |E |).

I ...be undirected.

2

Page 5: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees

Punchline: a MST of a graph connects all the vertices togetherwhile minimizing the number of edges used (and their weights).Minimum spanning treesGiven a connected, undirected graph G = (V ,E), a minimumspanning tree is a subgraph G ′ = (V ′,E ′) such that...

I V = V ′ (G ′ is spanning)I There exists a path from any vertex to any other one

I The sum of the edge weights in E ′ is minimized.

In order for a graph to have a MST, the graph must...

I ...be connected – there is a path from a vertex to any othervertex. (Note: this means |V | ≤ |E |).

I ...be undirected.

2

Page 6: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees

Punchline: a MST of a graph connects all the vertices togetherwhile minimizing the number of edges used (and their weights).Minimum spanning treesGiven a connected, undirected graph G = (V ,E), a minimumspanning tree is a subgraph G ′ = (V ′,E ′) such that...

I V = V ′ (G ′ is spanning)I There exists a path from any vertex to any other oneI The sum of the edge weights in E ′ is minimized.

In order for a graph to have a MST, the graph must...

I ...be connected – there is a path from a vertex to any othervertex. (Note: this means |V | ≤ |E |).

I ...be undirected.

2

Page 7: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees

Punchline: a MST of a graph connects all the vertices togetherwhile minimizing the number of edges used (and their weights).Minimum spanning treesGiven a connected, undirected graph G = (V ,E), a minimumspanning tree is a subgraph G ′ = (V ′,E ′) such that...

I V = V ′ (G ′ is spanning)I There exists a path from any vertex to any other oneI The sum of the edge weights in E ′ is minimized.

In order for a graph to have a MST, the graph must...

I ...be connected – there is a path from a vertex to any othervertex. (Note: this means |V | ≤ |E |).

I ...be undirected. 2

Page 8: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: example

An example of an minimum spanning tree (MST):

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

3

Page 9: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: Applications

Example questions:

I We want to connect phone lines to houses, but laying downcable is expensive. How can we minimize the amount of wirewe must install?

I We have items on a circuit we want to be “electricallyequivalent”. How can we connect them together using aminimum amount of wire?

Other applications:

I Implement efficient multiple constant multiplicationI Minimizing number of packets transmitted across a networkI Machine learning (e.g. real-time face verification)I Graphics (e.g. image segmentation)

4

Page 10: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: Applications

Example questions:

I We want to connect phone lines to houses, but laying downcable is expensive. How can we minimize the amount of wirewe must install?

I We have items on a circuit we want to be “electricallyequivalent”. How can we connect them together using aminimum amount of wire?

Other applications:

I Implement efficient multiple constant multiplicationI Minimizing number of packets transmitted across a networkI Machine learning (e.g. real-time face verification)I Graphics (e.g. image segmentation)

4

Page 11: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: Applications

Example questions:

I We want to connect phone lines to houses, but laying downcable is expensive. How can we minimize the amount of wirewe must install?

I We have items on a circuit we want to be “electricallyequivalent”. How can we connect them together using aminimum amount of wire?

Other applications:

I Implement efficient multiple constant multiplicationI Minimizing number of packets transmitted across a networkI Machine learning (e.g. real-time face verification)I Graphics (e.g. image segmentation)

4

Page 12: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: Applications

Example questions:

I We want to connect phone lines to houses, but laying downcable is expensive. How can we minimize the amount of wirewe must install?

I We have items on a circuit we want to be “electricallyequivalent”. How can we connect them together using aminimum amount of wire?

Other applications:

I Implement efficient multiple constant multiplicationI Minimizing number of packets transmitted across a networkI Machine learning (e.g. real-time face verification)I Graphics (e.g. image segmentation)

4

Page 13: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: properties

Important properties:

I A valid MST cannot contain a cycle

I If we add or remove an edge from an MST, it’s no longer avalid MST for that graph.Adding an edge introduces a cycle; removing an edge meansvertices are no longer connected.

I If there are |V | vertices, the MST contains exactly |V | − 1

edges.I An MST is always a tree.I If every edge has a unique weight, there exists a unique MST.

5

Page 14: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: properties

Important properties:

I A valid MST cannot contain a cycleI If we add or remove an edge from an MST, it’s no longer a

valid MST for that graph.Adding an edge introduces a cycle; removing an edge meansvertices are no longer connected.

I If there are |V | vertices, the MST contains exactly |V | − 1

edges.I An MST is always a tree.I If every edge has a unique weight, there exists a unique MST.

5

Page 15: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: properties

Important properties:

I A valid MST cannot contain a cycleI If we add or remove an edge from an MST, it’s no longer a

valid MST for that graph.Adding an edge introduces a cycle; removing an edge meansvertices are no longer connected.

I If there are |V | vertices, the MST contains exactly |V | − 1

edges.

I An MST is always a tree.I If every edge has a unique weight, there exists a unique MST.

5

Page 16: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: properties

Important properties:

I A valid MST cannot contain a cycleI If we add or remove an edge from an MST, it’s no longer a

valid MST for that graph.Adding an edge introduces a cycle; removing an edge meansvertices are no longer connected.

I If there are |V | vertices, the MST contains exactly |V | − 1

edges.I An MST is always a tree.

I If every edge has a unique weight, there exists a unique MST.

5

Page 17: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: properties

Important properties:

I A valid MST cannot contain a cycleI If we add or remove an edge from an MST, it’s no longer a

valid MST for that graph.Adding an edge introduces a cycle; removing an edge meansvertices are no longer connected.

I If there are |V | vertices, the MST contains exactly |V | − 1

edges.I An MST is always a tree.I If every edge has a unique weight, there exists a unique MST.

5

Page 18: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: algorithm

Design question: how would you implement an algorithm to findthe MST of some graph, assuming the edges all have the sameweight?

Hints:

I Try modifying DFS or BFS.I Try using an incremental approach: start with an empty

graph, and steadily add nodes and edges.

6

Page 19: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: algorithm

Design question: how would you implement an algorithm to findthe MST of some graph, assuming the edges all have the sameweight?

Hints:

I Try modifying DFS or BFS.

I Try using an incremental approach: start with an emptygraph, and steadily add nodes and edges.

6

Page 20: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: algorithm

Design question: how would you implement an algorithm to findthe MST of some graph, assuming the edges all have the sameweight?

Hints:

I Try modifying DFS or BFS.I Try using an incremental approach: start with an empty

graph, and steadily add nodes and edges.

6

Page 21: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We start with an “empty” MST, and steadily grow it.

Core algorithm:

1. Start with an arbitrary node.2. Run either DFS or BFS, storing edges in our stack or queue.3. As we visit nodes, add each edge we remove to our MST.

7

Page 22: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We start with an “empty” MST, and steadily grow it.

Core algorithm:

1. Start with an arbitrary node.

2. Run either DFS or BFS, storing edges in our stack or queue.3. As we visit nodes, add each edge we remove to our MST.

7

Page 23: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We start with an “empty” MST, and steadily grow it.

Core algorithm:

1. Start with an arbitrary node.2. Run either DFS or BFS, storing edges in our stack or queue.

3. As we visit nodes, add each edge we remove to our MST.

7

Page 24: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We start with an “empty” MST, and steadily grow it.

Core algorithm:

1. Start with an arbitrary node.2. Run either DFS or BFS, storing edges in our stack or queue.3. As we visit nodes, add each edge we remove to our MST.

7

Page 25: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack:

8

Page 26: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b), (a, d),

8

Page 27: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b), (d , e), (d , f ), (d , g),

8

Page 28: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b), (d , e), (d , f ), (g , h), (g , i),

8

Page 29: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b), (d , e), (d , f ), (g , h),

8

Page 30: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b), (d , e), (d , f ),

8

Page 31: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b), (d , e),

8

Page 32: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b), (e, c),

8

Page 33: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack: (a, b),

8

Page 34: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

An example using a modified version of DFS:

a

b

d

c

e

f

g

hi

Stack:

8

Page 35: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

What if the edges have different weights?

Observation:

We solved a similar problem earlier this quarter, when studyingshortest path algorithms!

9

Page 36: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

What if the edges have different weights?

Observation:

We solved a similar problem earlier this quarter, when studyingshortest path algorithms!

9

Page 37: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Interlude: finding the shortest path

Review: How do we find the shortest path between two vertices?

I If the graph is unweighted: run BFS

I If the graph is weighted: run Dijkstra’s

How does Dijkstra’s algorithm work?

1. Give each vertex v a “cost”: the cost of the shortest-knownpath so far between v and the start.(The cost of a path is the sum of the edge weights in that path)

2. Pick the node with the smallest cost, update adjacent nodecosts, repeat

10

Page 38: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Interlude: finding the shortest path

Review: How do we find the shortest path between two vertices?

I If the graph is unweighted: run BFSI If the graph is weighted: run Dijkstra’s

How does Dijkstra’s algorithm work?

1. Give each vertex v a “cost”: the cost of the shortest-knownpath so far between v and the start.(The cost of a path is the sum of the edge weights in that path)

2. Pick the node with the smallest cost, update adjacent nodecosts, repeat

10

Page 39: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Interlude: finding the shortest path

Review: How do we find the shortest path between two vertices?

I If the graph is unweighted: run BFSI If the graph is weighted: run Dijkstra’s

How does Dijkstra’s algorithm work?

1. Give each vertex v a “cost”: the cost of the shortest-knownpath so far between v and the start.(The cost of a path is the sum of the edge weights in that path)

2. Pick the node with the smallest cost, update adjacent nodecosts, repeat

10

Page 40: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Interlude: finding the shortest path

Review: How do we find the shortest path between two vertices?

I If the graph is unweighted: run BFSI If the graph is weighted: run Dijkstra’s

How does Dijkstra’s algorithm work?

1. Give each vertex v a “cost”: the cost of the shortest-knownpath so far between v and the start.(The cost of a path is the sum of the edge weights in that path)

2. Pick the node with the smallest cost, update adjacent nodecosts, repeat

10

Page 41: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We can use the same idea to find a MST!

Core idea: Use the exact same algorithm as Dijkstra’s algorithm,but redefine the cost:

I Previously, for Dijkstra’s:The cost of vertex v is the cost of the shortest-known path sofar between v and the start

I Now:The cost of vertex v is the cost of the shortest-known path sofar between v and any node we’ve visited so far

This algorithm is known as Prim’s algorithm.

11

Page 42: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We can use the same idea to find a MST!

Core idea: Use the exact same algorithm as Dijkstra’s algorithm,but redefine the cost:

I Previously, for Dijkstra’s:The cost of vertex v is the cost of the shortest-known path sofar between v and the start

I Now:The cost of vertex v is the cost of the shortest-known path sofar between v and any node we’ve visited so far

This algorithm is known as Prim’s algorithm.

11

Page 43: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We can use the same idea to find a MST!

Core idea: Use the exact same algorithm as Dijkstra’s algorithm,but redefine the cost:

I Previously, for Dijkstra’s:The cost of vertex v is the cost of the shortest-known path sofar between v and the start

I Now:The cost of vertex v is the cost of the shortest-known path sofar between v and any node we’ve visited so far

This algorithm is known as Prim’s algorithm.

11

Page 44: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees: approach 1, adding nodes

Intuition: We can use the same idea to find a MST!

Core idea: Use the exact same algorithm as Dijkstra’s algorithm,but redefine the cost:

I Previously, for Dijkstra’s:The cost of vertex v is the cost of the shortest-known path sofar between v and the start

I Now:The cost of vertex v is the cost of the shortest-known path sofar between v and any node we’ve visited so far

This algorithm is known as Prim’s algorithm.

11

Page 45: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Compare and contrast: Dijkstra vs Prim

Pseudocode for Dijkstra’s algorithm:

def dijkstra(start):

backpointers = new SomeDictionary<Vertex, Vertex>()

for (v : vertices):

set cost(v) to infinity

set cost(start) to 0

while (we still have unvisited nodes):

current = get next smallest node

for (edge : current.getOutEdges()):

newCost = min(cost(current) + edge.cost, cost(edge.dst))

update cost(edge.dst) to newCost

backpointers.put(edge.dst, edge.src)

return backpointers

12

Page 46: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Compare and contrast: Dijkstra vs Prim

Pseudocode for Prim’s algorithm:

def prim(start):

backpointers = new SomeDictionary<Vertex, Vertex>()

for (v : vertices):

set cost(v) to infinity

set cost(start) to 0

while (we still have unvisited nodes):

current = get next smallest node

for (edge : current.getOutEdges()):

newCost = min(edge.cost, cost(edge.dst))

update cost(edge.dst) to newCost

backpointers.put(edge.dst, edge.src)

return backpointers

13

Page 47: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

14

Page 48: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a∞

b∞

c∞

d∞

e∞

f∞

g∞

h∞

i∞

4

8

8

11

7

4

2 9

14

10

21

67

We initially set all costs to ∞, just like with Dijkstra.

14

Page 49: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b∞

c∞

d∞

e∞

f∞

g∞

h∞

i∞

4

8

8

11

7

4

2 9

14

10

21

67

We pick an arbitrary node to start.

14

Page 50: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c∞

d∞

e∞

f∞

g∞

h8

i∞

4

8

8

11

7

4

2 9

14

10

21

67

We update the adjacent nodes.

14

Page 51: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c∞

d∞

e∞

f∞

g∞

h8

i∞

4

8

8

11

7

4

2 9

14

10

21

67

We select the one with the smallest cost.

14

Page 52: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d∞

e∞

f∞

g∞

h8

i∞

4

8

8

11

7

4

2 9

14

10

21

67

We potentially need to update h and c, but only c changes.

14

Page 53: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d∞

e∞

f∞

g∞

h8

i∞

4

8

8

11

7

4

2 9

14

10

21

67

We (arbitrarily) pick c.

14

Page 54: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e∞

f4

g∞

h8

i2

4

8

8

11

7

4

2 9

14

10

21

67

...and update the adjacent nodes. Note that we don’t add thecumulative cost: the cost represents the shortest path to any greennode, not to the start.

14

Page 55: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e∞

f4

g∞

h8

i2

4

8

8

11

7

4

2 9

14

10

21

67

i has the smallest cost.

14

Page 56: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e∞

f4

g6

h7

i2

4

8

8

11

7

4

2 9

14

10

21

67

We update both unvisited nodes, and modify the edge to h sincewe now have a better option.

14

Page 57: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e∞

f4

g6

h7

i2

4

8

8

11

7

4

2 9

14

10

21

67

f has the smallest cost.

14

Page 58: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e10

f4

g2

h7

i2

4

8

8

11

7

4

2 9

14

10

21

67

Again, we update the adjacent unvisited nodes.

14

Page 59: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e10

f4

g2

h7

i2

4

8

8

11

7

4

2 9

14

10

21

67

g has the smallest cost.

14

Page 60: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e10

f4

g2

h1

i2

4

8

8

11

7

4

2 9

14

10

21

67

We update h again.

14

Page 61: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e10

f4

g2

h1

i2

4

8

8

11

7

4

2 9

14

10

21

67

h has the smallest cost. Note that there nothing to update here.

14

Page 62: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e10

f4

g2

h1

i2

4

8

8

11

7

4

2 9

14

10

21

67

d has the smallest cost.

14

Page 63: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e9

f4

g2

h1

i2

4

8

8

11

7

4

2 9

14

10

21

67

We can update e.

14

Page 64: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e9

f4

g2

h1

i2

4

8

8

11

7

4

2 9

14

10

21

67

e has the smallest cost.

14

Page 65: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: an example

a0

b4

c8

d7

e9

f4

g2

h1

i2

4

8

8

11

7

4

2 9

14

10

21

67

There are no more nodes left, so we’re done.

14

Page 66: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: another example

Now you try. Start on node a.

a b c

d e

4

1 2

2

43 1

5

15

Page 67: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Prim’s algorithm: another example

Now you try. Start on node a.

a0

b2

c2

d1

e1

4

1 2

2

43 1

5

15

Page 68: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Analyzing Prim’s algorithm

Question: What is the worst-case asymptotic runtime of Prim’salgorithm?

Answer: The same as Dijkstra’s: O (|V |ts + |E |tu) where...

I ts = time needed to get next smallest nodeI tu = time needed to update vertex costs

So, O (|V | log(|V |) + |E | log(|V |)) if we stick to data structureswe know how to implement; O (|V | log(|V |) + |E |) if we useFibonacci heaps.

16

Page 69: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Analyzing Prim’s algorithm

Question: What is the worst-case asymptotic runtime of Prim’salgorithm?

Answer: The same as Dijkstra’s: O (|V |ts + |E |tu) where...

I ts = time needed to get next smallest nodeI tu = time needed to update vertex costs

So, O (|V | log(|V |) + |E | log(|V |)) if we stick to data structureswe know how to implement; O (|V | log(|V |) + |E |) if we useFibonacci heaps.

16

Page 70: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Analyzing Prim’s algorithm

Question: What is the worst-case asymptotic runtime of Prim’salgorithm?

Answer: The same as Dijkstra’s: O (|V |ts + |E |tu) where...

I ts = time needed to get next smallest nodeI tu = time needed to update vertex costs

So, O (|V | log(|V |) + |E | log(|V |)) if we stick to data structureswe know how to implement; O (|V | log(|V |) + |E |) if we useFibonacci heaps.

16

Page 71: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees, approach 2

Recap: Prim’s algorithm works similarly to Dijkstra’s – we startwith a single node, and “grow” our MST.

A second approach: instead of “growing” our MST, we...

I Initially place each node into its own MST of size 1 – so, westart with |V | MSTs in total.

I Steadily combine together different MSTs until we have justone left

I How? Loop through every single edge, see if we can use it tojoin two different MSTs together.

This algorithm is called Kruskal’s algorithm

17

Page 72: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees, approach 2

Recap: Prim’s algorithm works similarly to Dijkstra’s – we startwith a single node, and “grow” our MST.

A second approach: instead of “growing” our MST, we...

I Initially place each node into its own MST of size 1 – so, westart with |V | MSTs in total.

I Steadily combine together different MSTs until we have justone left

I How? Loop through every single edge, see if we can use it tojoin two different MSTs together.

This algorithm is called Kruskal’s algorithm

17

Page 73: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees, approach 2

Recap: Prim’s algorithm works similarly to Dijkstra’s – we startwith a single node, and “grow” our MST.

A second approach: instead of “growing” our MST, we...

I Initially place each node into its own MST of size 1 – so, westart with |V | MSTs in total.

I Steadily combine together different MSTs until we have justone left

I How? Loop through every single edge, see if we can use it tojoin two different MSTs together.

This algorithm is called Kruskal’s algorithm

17

Page 74: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Minimum spanning trees, approach 2

Recap: Prim’s algorithm works similarly to Dijkstra’s – we startwith a single node, and “grow” our MST.

A second approach: instead of “growing” our MST, we...

I Initially place each node into its own MST of size 1 – so, westart with |V | MSTs in total.

I Steadily combine together different MSTs until we have justone left

I How? Loop through every single edge, see if we can use it tojoin two different MSTs together.

This algorithm is called Kruskal’s algorithm

17

Page 75: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 76: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 77: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 78: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 79: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 80: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 81: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 82: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 83: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 84: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 85: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 86: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 87: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 88: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 89: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 90: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 91: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 92: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 93: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 94: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 95: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 96: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 97: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 98: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 99: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 100: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 101: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 102: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm

An example, for unweighted graphs. Note: each MST has adifferent color.

a

b

d

c

e

f

g

hi

18

Page 103: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: weighted graphs

Question: How do we handle edge weights?

Answer: Consider edges sorted in ascending order by weight.

So, we look at the edge with the smallest weight first, the edgewith the second smallest weight next, etc.

19

Page 104: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: weighted graphs

Question: How do we handle edge weights?

Answer: Consider edges sorted in ascending order by weight.

So, we look at the edge with the smallest weight first, the edgewith the second smallest weight next, etc.

19

Page 105: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: pseudocode

Pseudocode for Kruskal’s algorithm:def kruskal():

mst = new SomeSet<Edge>()

for (v : vertices):

makeMST(v)

sort edges in ascending order by their weight

for (edge : edges):

if findMST(edge.src) != findMST(edge.dst):

union(edge.src, edge.dst)

mst.add(edge)

return mst

I makeMST(v): stores v as a MST containing just one nodeI findMST(v): finds the MST that vertex is a part ofI union(u, v): combines the two MSTs of the two given

vertices, using the edge (u, v) 20

Page 106: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 107: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 108: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 109: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 110: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 111: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 112: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 113: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 114: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 115: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 116: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 117: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 118: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 119: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 120: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 121: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 122: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 123: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 124: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 125: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 126: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 127: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 128: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 129: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 130: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 131: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 132: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 133: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 134: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: example with a weighted graph

Now you try:

a

b c d

e

fgh

i

4

8

8

11

7

4

2 9

14

10

21

67

21

Page 135: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: analysis

What is the worst-case runtime?def kruskal():

mst = new SomeSet<Edge>()

for (v : vertices):

makeMST(v)

sort edges in ascending order by their weight

for (edge : edges):

if findMST(edge.src) != findMST(edge.dst):

union(edge.src, edge.dst)

mst.add(edge)

return mst

Note: assume that...

I makeMST(v) takes O (tm) timeI findMST(v): takes O (tf ) timeI union(u, v): takes O (tu) time 22

Page 136: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: analysis

I Making the |V | MSTs takes O (|V |·tm) timeI Sorting the edges takes O (|E |·log(|E |)) time, assuming we

use a general-purpose comparison sortI The final loop takes O (|E |·tf + |V |·tu) time

Putting it all together:

O (|V | · tm + |E |·log(|E |) + |E |·tf + |V |·tu)

23

Page 137: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

Kruskal’s algorithm: analysis

I Making the |V | MSTs takes O (|V |·tm) timeI Sorting the edges takes O (|E |·log(|E |)) time, assuming we

use a general-purpose comparison sortI The final loop takes O (|E |·tf + |V |·tu) time

Putting it all together:

O (|V | · tm + |E |·log(|E |) + |E |·tf + |V |·tu)

23

Page 138: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

The DisjointSet ADT

But wait, what exactly is tm, tf , and tu? How exactly do weimplement makeMST(v), findMST(v), and union(u, v)?

We can do so using a new ADT called the DisjointSet ADT!

24

Page 139: CSE 373: Minimum Spanning Trees: Prim and Kruskal › ... › slides › lecture-21.pdfMinimumspanningtrees Punchline:aMSTofagraphconnectsalltheverticestogether whileminimizingthenumberofedgesused(andtheirweights)

The DisjointSet ADT

But wait, what exactly is tm, tf , and tu? How exactly do weimplement makeMST(v), findMST(v), and union(u, v)?

We can do so using a new ADT called the DisjointSet ADT!

24