data structures and advanced programming · properties introduction 4 a connected graph can have...
TRANSCRIPT
CS062 DATA STRUCTURES AND ADVANCED PROGRAMMING
42: Minimum Spanning Trees
Alexandra PapoutsakiLECTURES
Mark KampeLABS
Some slides adopted from Princeton C0S226 course or Algorithms, 4th Edition
GRAPHS
TODAY’S LECTURE IN A NUTSHELL
Lecture 42: Minimum Spanning Trees
▸ Introduction
▸ Kruskal’s Algorithm
▸ Prim’s Algorithm
�2
Some slides adopted from Algorithms 4th Edition or COS226
Spanning Trees
�3INTRODUCTION
▸ Given an edge weighted graph � (not digraph!), a spanning tree of � is a subgraph � that is:
▸ A tree: connected and acyclic.
▸ Spanning: includes all of the vertices of � .
G G T
G
https://www.tutorialspoint.com/data_structures_algorithms/spanning_tree.htm
Properties
�4INTRODUCTION
▸ A connected graph � can have more than one spanning tree.
▸ All possible spanning trees of � , have the same number of vertices and edges.
▸ A spanning tree has � edges.
▸ A spanning tree by definition cannot have any cycle.
▸ Adding one edge to the spanning tree would create a cycle (i.e. spanning trees are maximally acyclic).
▸ Removing one edge from the spanning tree would make the graph disconnected (i.e. spanning trees are minimally connected).
G
G
|V | − 1
https://www.tutorialspoint.com/data_structures_algorithms/spanning_tree.htm
Minimum spanning tree problem
�5INTRODUCTION
▸ Given a connected edge-weighted undirected graph find a spanning tree of minimum weight.
Minimum spanning applications
�6INTRODUCTION
▸ Network design
▸ Cluster analysis
▸ Cancer imaging
▸ Cosmology
▸ Weather data interpretation
▸ Many others
▸ https://www.ics.uci.edu/~eppstein/gina/mst.html
▸ https://personal.utdallas.edu/~besp/teaching/mst-applications.pdf
TODAY’S LECTURE IN A NUTSHELL
Lecture 42: Minimum Spanning Trees
▸ Introduction
▸ Kruskal’s Algorithm
▸ Prim’s Algorithm
�7
KRUSKAL’S ALGORITHM
Kruskal’s algorithm
▸ Sort edges in ascending order of weight.
▸ Starting from the one with the smallest weight, add it to the MST � unless doing so would create a cycle.
▸ Uses a data structure called Union-Find (Chapter 1.5 in book).
▸ Running time of � in worst case.
T
|E | log |V |
�8
TEXT �9
ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
Algorithms
http://algs4.cs.princeton.edu
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
KRUSKAL'S ALGORITHM DEMO
TEXT �10
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
2
Kruskal's algorithm demo
5
4
7
13
0
2
6
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
graph edgessorted by weight
an edge-weighted graph
TEXT �11
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
3
Kruskal's algorithm demo
0-7 0.16
5
4
7
13
0
2
6
does not create a cycle
in MST
TEXT �12
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
4
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
5
4
7
13
0
2
6
in MST
does notcreate a cycle
TEXT �13
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
5
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
5
4
7
13
0
2
6
in MST
does not create a cycle
TEXT �14
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
6
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.265
4
7
13
0
2
6
in MST
does not create a cycle
TEXT �15
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
7
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.285
4
7
13
0
2
6
in MST
does not create a cycle
TEXT �16
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
8
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
5
4
7
13
0
2
6
creates a cycle
not in MST
TEXT �17
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
9
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
5
4
7
13
0
2
6
creates a cycle
not in MST
TEXT �18
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
10
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
5
4
7
13
0
2
6
creates a cycle
not in MST
TEXT �19
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
11
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
5
4
7
13
0
2
6
in MST
does not create a cycle
TEXT �20
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
12
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
5
4
7
13
0
2
6
creates a cycle
not inMST
TEXT �21
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
13
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
5
4
7
13
0
2
6
creates a cycle
not inMST
TEXT �22
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
14
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
5
4
7
13
0
2
6
creates a cycle not in MST
TEXT �23
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
15
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
5
4
7
13
0
2
6
in MSTdoes not create a cycle
TEXT �24
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
16
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
5
4
7
13
0
2
6
creates a cycle
not in MST
TEXT �25
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
17
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
5
4
7
13
0
2
6
creates a cycle
not in MST
TEXT �26
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
18
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
5
4
7
13
0
2
6
creates a cycle
not in MST
TEXT �27
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
19
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
5
4
7
13
0
2
6
a minimum spanning tree
KRUSKAL’S ALGORITHM
Practice Time
�28
KRUSKAL’S ALGORITHM
Answer
�29
TODAY’S LECTURE IN A NUTSHELL
Lecture 42: Minimum Spanning Trees
▸ Introduction
▸ Kruskal’s Algorithm
▸ Prim’s Algorithm
�30
PRIM’S ALGORITHM
Prim’s algorithm
▸ Start with a random vertex (here, 0) and greedily grow tree � .
▸ Add to � the min weight edge with exactly one endpoint in � .
▸ Repeat until � edges.
▸ Two versions, lazy and eager. We will see lazy, here…
▸ Uses min-priority queue.
▸ Running time of � in worst case, as well.
T
T T
|V | − 1
|E | log |V |
�31
TEXT �32
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
3
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 0.162-3 0.171-7 0.190-2 0.265-7 0.281-3 0.291-5 0.322-7 0.344-5 0.351-2 0.364-7 0.370-4 0.386-2 0.403-6 0.526-0 0.586-4 0.93
an edge-weighted graph
TEXT �33
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
4
Prim's algorithm demo
5
4
7
13
0
2
6
TEXT �34
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
5
Prim's algorithm demo
0
0-7 0.160-2 0.260-4 0.386-0 0.58
edges with exactlyone endpoint in T(sorted by weight)
5
4
7
13
2
6
in MST
min weight edge withexactly one endpoint in T
TEXT �35
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
6
Prim's algorithm demo
5
4
7
13
0
2
6
0-7
MST edges
TEXT �36
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
7
Prim's algorithm demo
5
4
7
13
0
2
6
1-7 0.190-2 0.265-7 0.282-7 0.344-7 0.370-4 0.386-0 0.58
in MST
edges with exactlyone endpoint in T(sorted by weight)
0-7
MST edges
min weight edge withexactly one endpoint in T
TEXT �37
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
8
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 1-7
MST edges
TEXT �38
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
9
Prim's algorithm demo
5
4
7
13
0
2
6
0-2 0.265-7 0.281-3 0.291-5 0.322-7 0.341-2 0.364-7 0.370-4 0.386-0 0.58
edges with exactlyone endpoint in T(sorted by weight)
in MST
0-7 1-7
MST edges
min weight edge withexactly one endpoint in T
TEXT �39
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
10
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 1-7 0-2
MST edges
TEXT �40
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
11
Prim's algorithm demo
5
4
7
13
0
2
6
2-3 0.175-7 0.281-3 0.291-5 0.324-7 0.370-4 0.386-2 0.406-0 0.58
edges with exactlyone endpoint in T(sorted by weight)
in MST
0-7 1-7 0-2
MST edges
min weight edge withexactly one endpoint in T
TEXT �41
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
12
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 1-7 0-2 2-3
MST edges
TEXT �42
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
13
Prim's algorithm demo
5
4
7
13
0
2
6
5-7 0.281-5 0.324-7 0.370-4 0.386-2 0.403-6 0.526-0 0.58
edges with exactlyone endpoint in T(sorted by weight)
in MST
0-7 1-7 0-2 2-3
MST edges
min weight edge withexactly one endpoint in T
TEXT �43
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
14
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 1-7 0-2 2-3 5-7
MST edges
TEXT �44
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
15
Prim's algorithm demo
5
4
7
13
0
2
6
4-5 0.354-7 0.370-4 0.386-2 0.403-6 0.526-0 0.58
edges with exactlyone endpoint in T(sorted by weight)
in MST
0-7 1-7 0-2 2-3 5-7
MST edges
min weight edge withexactly one endpoint in T
TEXT �45
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
16
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 1-7 0-2 2-3 5-7 4-5
MST edges
TEXT �46
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
17
Prim's algorithm demo
5
4
7
13
0
2
6
6-2 0.403-6 0.526-0 0.586-4 0.93
edges with exactlyone endpoint in T(sorted by weight)
in MST
0-7 1-7 0-2 2-3 5-7 4-5
MST edges
min weight edge withexactly one endpoint in T
TEXT �47
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
18
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 1-7 0-2 2-3 5-7 4-5 6-2
MST edges
PRIM’S ALGORITHM
Practice Time
�48
PRIM’S ALGORITHM
Answer…
�49
ASSIGNED READINGS AND PRACTICE PROBLEMS
Readings:
▸ Textbook: Chapter 4.3 (Pages 604-629)
▸ Website:
▸ https://algs4.cs.princeton.edu/43mst/
�50
Practice Problems:
https://visualgo.net/en/mst