0% found this document useful (0 votes)
22 views

Lecture 23

Uploaded by

bluebirdrish666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lecture 23

Uploaded by

bluebirdrish666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 23: Minimum CSE 373: Data Structures and

Spanning Trees Algorithms

CSE 373 SP 18 - KASEY CHAMPION 1


Administriva

CSE 373 SP 18 - KASEY CHAMPION 2


Minimum Spanning Trees
It’s the 1920’s. Your friend at the electric company needs to choose where to build wires to
connect all these cities to the plant.
B
6
3
E
2
1 C
A 1
0 9
5
7
4
D
8
She knows how much it would cost to lay electric wires between any pair of locations, and wants the cheapest way to
make sure electricity from the plant to every city.

CSE 373 SP 18 – ROBBIE WEBBER 3


Minimum Spanning Trees
What do we need? A set of edges such that:
- Every vertex touches at least one of the edges. (the edges span the graph)
- The graph on just those edges is connected.
- The minimum weight set of edges that meet those conditions.
Notice we do not need a directed graph!
Assume all edge weights are positive. B
2
Claim: The set of edges we pick never has a cycle. Why? 3
E
MST is the exact number of edges to connect all vertices
- taking away 1 edge breaks connectiveness 1 C
- adding 1 edge makes a cycle A 7
- contains exactly V – 1 edges 5
4
D

CSE 373 19 WI – KASEY CHAMPION 4


Aside: Trees
B
2
Our BSTs had:
- A root 3
E
- Left and/or right children
- Connected and no cycles 1 C
A
Our heaps had:
- A root
4
- Varying numbers of children D
- Connected and no cycles

On graphs our tees:


- Don’t need a root (the vertices aren’t ordered, and we can start BFS from anywhere)
- Varying numbers of children
- Connected and no cycles Tree (when talking about graphs)
An undirected, connected acyclic graph.

CSE 373 SP 18 – ROBBIE WEBBER 5


MST Problem
What do we need? A set of edges such that:
- Every vertex touches at least one of the edges. (the edges span the graph)
- The graph on just those edges is connected.
- The minimum weight set of edges that meet those conditions.

Our goal is a tree!


Minimum Spanning Tree Problem
Given: an undirected, weighted graph G
Find: A minimum-weight set of edges such that you can
get from any vertex of G to any other on only those
edges.

We’ll go through two different algorithms for this problem today.

CSE 373 SP 18 – ROBBIE WEBBER 6


Example
Try to find an MST of this graph:

Graph Algorithm Toolbox B


BFS
1. Pick an arbitrary starting point
2. Queue up unprocessed neighbors E
3. Process next neighbor in queue
4. Repeat until all vertices in queue
have been processed C
A
Dijkstra’s
1. Start at source
2. Update distance from current to
unprocessed neighbors D F
3. Process optimal neighbor
4. Repeat until all vertices have been
marked processed

CSE 373 19 WI – KASEY CHAMPION 7


Example
Try to find an MST of this graph:

Graph Algorithm Toolbox B


BFS 6
1. Pick an arbitrary starting point 3
2. Queue up unprocessed neighbors E
3. Process next neighbor in queue 2
4. Repeat until all vertices in queue
have been processed 1 C
A 10
9
Dijkstra’s 5
1. Start at source 7
2. Update distance from current to 4
unprocessed neighbors D F
3. Process optimal neighbor 8
4. Repeat until all vertices have been
marked processed

CSE 373 19 WI – KASEY CHAMPION 8


Prim’s Algorithm
Dijkstra’s Algorithm idea: Prims(Graph G, Vertex source)
1. Start at source 1. choose an arbitrary initialize distances to ∞
2. Update distance from current to starting point mark source as distance 0
unprocessed neighbors 2. Investigate edges that mark all vertices unprocessed
3. Process optimal neighbor connect unprocessed while(there are unprocessed vertices){
4. Repeat until all vertices have been vertices let u be the closest unprocessed vertex
marked processed 3. Add the lightest edge to foreach(edge (u,v) leaving u){
solution (be greedy) if(weight(u,v) < v.dist){
4. Repeat until solution v.dist = u.dist+weight(u,v)
Dijkstra(Graph G, Vertex source)
connects all vertices v.predecessor = u
initialize distances to ∞
}
mark source as distance 0
}
mark all vertices unprocessed
mark u as processed
while(there are unprocessed vertices){
}
let u be the closest unprocessed vertex
foreach(edge (u,v) leaving u){
if(u.dist+weight(u,v) < v.dist){
v.dist = u.dist+weight(u,v)
v.predecessor = u
}
}
mark u as processed
}

CSE 373 SP 18 - KASEY CHAMPION 9


Try it Out G 50
6
B
2 3 E
PrimMST(Graph G)
initialize distances to ∞
mark source as distance 0
4 C 5
A 9
mark all vertices unprocessed
2
foreach(edge (source, v) ) { 7
v.dist = weight(source,v) 7 F
v.bestEdge = (source,v) D
} 8
while(there are unprocessed vertices){ Vertex Distance Best Edge Processed
let u be the closest unprocessed vertex
A
add u.bestEdge to spanning tree
foreach(edge (u,v) leaving u){ B
if(weight(u,v) < v.dist && v unprocessed ){ C
v.dist = weight(u,v)
D
v.bestEdge = (u,v)
} E
} F
mark u as processed
} G
CSE 373 SP 18 - KASEY CHAMPION 10
Try it Out G 50
6
B
2 3 E
PrimMST(Graph G)
initialize distances to ∞
mark source as distance 0
4 C 5
A 9
mark all vertices unprocessed
2
foreach(edge (source, v) ) { 7
v.dist = weight(source,v) 7 F
v.bestEdge = (source,v) D
} 8
while(there are unprocessed vertices){ Vertex Distance Best Edge Processed
let u be the closest unprocessed vertex
A - X ✓
add u.bestEdge to spanning tree
foreach(edge (u,v) leaving u){ B 2 (A, B) ✓
if(weight(u,v) < v.dist && v unprocessed ){ C 4 (A, C) ✓
v.dist = weight(u,v)
D ---2
7 --------(C,
(A, D) D) ✓
v.bestEdge = (u,v)
} E 6
---5 (B, E) E)
--------(C, ✓
} F 3 (B, F) ✓
mark u as processed
} G 50 (B, G) ✓
CSE 373 SP 18 - KASEY CHAMPION 11
Prim’s Runtime
Runtime = VlogV + ElogV Runtime = VlogV + ElogV
Prims(Graph G, Vertex source) Dijkstra(Graph G, Vertex source)
initialize distances to ∞ initialize distances to ∞
mark source as distance 0 mark source as distance 0
mark all vertices unprocessed mark all vertices unprocessed
while(there are unprocessed vertices){ while(there are unprocessed vertices){
let u be the closest unprocessed vertex let u be the closest unprocessed vertex
foreach(edge (u,v) leaving u){ foreach(edge (u,v) leaving u){
if(weight(u,v) < v.dist){ if(u.dist+weight(u,v) < v.dist){
v.dist = u.dist+weight(u,v) v.dist = u.dist+weight(u,v)
v.predecessor = u v.predecessor = u
} }
} }
mark u as processed mark u as processed
} }

CSE 373 SP 18 - KASEY CHAMPION 12


A different Approach
Prim’s Algorithm started from a single vertex and reached more and more
other vertices.
Prim’s thinks vertex by vertex (add the closest vertex to the currently
reachable set).
What if you think edge by edge instead?
Start from the lightest edge; add it if it connects new things to each other
(don’t add it if it would create a cycle)

This is Kruskal’s Algorithm.


Example
Try to find an MST of this graph by adding edges in sorted order

G 50
6
B
2 3 E

4 C 5
A 9
2
7
7 F
D
8

CSE 373 19 WI – KASEY CHAMPION 14


Kruskal’s Algorithm
KruskalMST(Graph G)
initialize each vertex to be an independent component
sort the edges by weight
foreach(edge (u, v) in sorted order){
if(u and v are in different components){
add (u,v) to the MST
Update u and v to be in the same component
}
}
B
Try It Out
3 6
E
KruskalMST(Graph G)
initialize each vertex to be an independent component
2
sort the edges by weight 1 C
foreach(edge (u, v) in sorted order){ A 10
if(u and v are in different components){ 5 9
add (u,v) to the MST 7
Update u and v to be in the same component 4 D F
}
}
8

Edge Include? Reason Edge (cont.) Inc? Reason


(A,C) (B,F)
(C,E) (D,E)
(A,B) (D,F)
(A,D) (E,F)
(C,D) (C,F)
B
Try It Out
3 6
E
KruskalMST(Graph G) 2
initialize each vertex to be an independent component 1 C
sort the edges by weight A 10
foreach(edge (u, v) in sorted order){
5 9
if(u and v are in different components){ 7
add (u,v) to the MST 4 D F
Update u and v to be in the same component
} 8
}

Edge Include? Reason Edge (cont.) Inc? Reason


(A,C) Yes (B,F) Yes
(C,E) Yes (D,E) No Cycle A,C,E,D,A
(A,B) Yes (D,F) No Cycle A,D,F,B,A
(A,D) Yes (E,F) No Cycle A,C,E,F,D,A
(C,D) No Cycle A,C,D,A (C,F) No Cycle C,A,B,F,C
Kruskal’s Algorithm Implementation
KruskalMST(Graph G)
initialize each vertex to be an independent component
sort the edges by weight
foreach(edge (u, v) in sorted order){
if(u and v are in different components){
add (u,v) to the MST
update u and v to be in the same component
}
}

KruskalMST(Graph G)
foreach (V : vertices) {
makeMST(v); +? +V(makeMST)
}
sort edges in ascending order by weight +ElogE How many times will we call union?
foreach(edge (u, v)){
V–1
if(findMST(v) is not in findMST(u)){+?
union(u, v) +? +E(2findMST + union) -> +Vunion + EfindMST
}
}
Appendix: MST Properties, Another
MST Application

CSE 373 SP 18 - KASEY CHAMPION 19


Why do all of these MST Algorithms Work?
MSTs satisfy two very useful properties:
Cycle Property: The heaviest edge along a cycle is NEVER part of an MST.
Cut Property: Split the vertices of the graph any way you want into two sets A and B. The lightest
edge with one endpoint in A and the other in B is ALWAYS part of an MST.

Whenever you add an edge to a tree you create exactly one cycle, you can then remove any edge
from that cycle and get another tree out.
This observation, combined with the cycle and cut properties form the basis of all of the greedy
algorithms for MSTs.

CSE 373 SP 18 - KASEY CHAMPION 20


One More MST application
Let’s say you’re building a new building.
There are very important building donors coming to visit TOMORROW,
- and the hallways are not finished.

You have n rooms you need to show them, connected by the unfinished hallways.
Thanks to your generous donors you have n-1 construction crews, so you can assign one to each
of that many hallways.
- Sadly the hallways are narrow and you can’t have multiple crews working on the same hallway.

Can you finish enough hallways in time to give them a tour?


Minimum Bottleneck Spanning Tree Problem
Given: an undirected, weighted graph G
Find: A spanning tree such that the weight of the
maximum edge is minimized.

CSE 373 SP 18 - KASEY CHAMPION 21


MSTs and MBSTs
Minimum Spanning Tree Problem Minimum Bottleneck Spanning Tree Problem
Given: an undirected, weighted graph G Given: an undirected, weighted graph G
Find: A minimum-weight set of edges such that you Find: A spanning tree such that the weight of the
can get from any vertex of G to any other on only maximum edge is minimized.
those edges.

B B
2 2

3 3 C
C A
A
1 1
2 2
4 4
D D

Graph on the right is a minimum bottleneck spanning tree, but not a minimum
spanning tree.
CSE 373 SP 18 - KASEY CHAMPION 22
Finding MBSTs
Algorithm Idea: want to use smallest edges. Just start with the smallest edge and add it if it
connects previously unrelated things (and don’t if it makes a cycle).
Hey wait…that’s Kruskal’s Algorithm!

Every MST is an MBST (because Kruskal’s can find any MST when looking for MBSTs)
but not vice versa (see the example on the last slide).

If you need an MBST, any MST algorithm will work.


There are also some specially designed MBST algorithms that are faster (see Wikipedia)
Takeaway: When you’re modeling a problem, be careful to really understand what you’re looking
for. There may be a better algorithm out there.

CSE 373 SP 18 - KASEY CHAMPION 23

You might also like