Lecture 24
Lecture 24
Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora
Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...
1
Four classes of graph problem
.. that can be solved efficiently (in polynomial time)
A wide array of graph problems that can be solved in polynomial time are variants of these above
problems.
In this class, we’ll cover the first two problems – shortest path and minimum spanning tree
CSE 373 AU 18 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
9
5 0
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 AU 18 3
Minimum Spanning Trees
It’s the 1920’s.
1950’s Your boss
friend at the electric
phone company needs to choose where to build wires to
connect all these cities to theeach
plant.
other.
B
6
3
E
2
1 C
A 1
9
5 0
7
4
D F
8
phonewires between any pair of locations, and wants the cheapest way to
She knows how much it would cost to lay electric
make sure electricityEveryone
from thecan
plant to everyone
call every city.else.
CSE 373 AU 18 4
Minimum Spanning Trees
It’s the 1920’s. Your friend at the electric
today ISP company needs to choose where to build wires to
connect all these cities to the plant.
Internet with fiber optic cable
B
6
3
E
2
1 C
A 1
9
5 0
7
4
D
8
cable wires between any pair of locations, and wants the cheapest way to
She knows how much it would cost to lay electric
make sure electricityEveryone
from thecan
plant to every
reach city.
the server
CSE 373 AU 18 5
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.
CSE 373 AU 18 6
Aside: Trees
Our BSTs had:
- A root
- Left and/or right children
- Connected and no cycles
CSE 373 AU 18 7
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.
CSE 373 AU 18 8
Example
Try to find a MST of this graph:
B
6
3
E
2
1 C
A 1
9
5 0
7
4
D F
8
CSE 373 AU 18 9
Prim’s Algorithm
Algorithm idea: choose an arbitrary starting point. Add a new edge that:
- Will let you reach more vertices.
- Is as light as possible
We’d like each not-yet-connected vertex to be able to tell us the lightest edge we could add to
connect it.
CSE 373 AU 18 10
Code
PrimMST(Graph G)
initialize distances to
mark source as distance 0
mark all vertices unprocessed
foreach(edge (source, v) )
v.dist = w(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(w(u,v) < v.dist){
v.dist = w(u,v)
v.bestEdge = (u,v)
}
}
mark u as processed
}
CSE 373 AU 18 11
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
7 F
v.dist = w(source,v) D
while(there are unprocessed vertices){ 8
let u be the closest unprocessed Vertex Distance Best Edge Processed
vertex
A
add u.bestEdge to spanning tree
foreach(edge (u,v) leaving u){ B
if(w(u,v) < v.dist){ C
v.dist = w(u,v) D
v.bestEdge = (u,v)
E
}
} F
mark u as processed G
} CSE 373 AU 18 12
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
7 F
v.dist = w(source,v) D
while(there are unprocessed vertices){ 8
let u be the closest unprocessed Vertex Distance Best Edge Processed
vertex
A
add u.bestEdge to spanning tree
foreach(edge (u,v) leaving u){ B
if(w(u,v) < v.dist){ C
v.dist = w(u,v) D
v.bestEdge = (u,v)
E
}
} F
mark u as processed G
} CSE 373 AU 18 13
Does This Algorithm Always Work?
Prim’s Algorithm is a greedy algorithm. Once it decides to include an edge in the MST it never
reconsiders its decision.
Greedy algorithms rarely work.
There are special properties of MSTs that allow greedy algorithms to find them.
In fact MSTs are so magical that there’s more than one greedy algorithm that works.
CSE 373 AU 18 14
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)
CSE 373 AU 18 15
Kruskal’s Algorithm
KruskalMST(Graph G)
initialize each vertex to be a connected 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
}
}
CSE 373 AU 18 16
Try It Out
B
KruskalMST(Graph G)
initialize each vertex to be a connected component 6
3
sort the edges by weight E
foreach(edge (u, v) in sorted order){ 2
if(u and v are in different components){ 1 C
add (u,v) to the MST A 1
9
Update u and v to be in the same 5 0
component 7
4
} D F
} 8
CSE 373 AU 18 17
Kruskal’s Algorithm: Running Time
KruskalMST(Graph G)
initialize each vertex to be a connected 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
}
}
CSE 373 AU 18 18
Kruskal’s Algorithm: Running Time
Running a new [B/D]FS in the partial MST, at every step seems inefficient.
Do we have an ADT that will work here?
Not yet…
We will cover “Union-Find” next week.
CSE 373 AU 18 19
Try it Out
KruskalMST(Graph G)
initialize each vertex to be a connected component
sort the edges by weight
foreach(edge (u, v) in sorted order){
if(u and v are in different components){ G 50
add (u,v) to the MST 6
Update u and v to be in the same B
component 2 3 E
}
} 4 C 5
A 9
2
7
7 F
D
8
CSE 373 AU 18 20
Aside: A Graph of Trees
A tree is an undirected, connected, and acyclic graph.
How would we describe the graph Kruskal’s builds.
It’s not a tree until the end.
It’s a forest!
A forest is any undirected and acyclic graph
CSE 373 AU 18 21
Appendix: MST Properties,
Another MST Application
Some Extra Comments
Prim was the employee at Bell Labs in the 1950’s
The mathematician in the 1920’s was Boruvka
- He had a different also greedy algorithm for MSTs.
- Boruvka’s algorithm is trickier to implement, but is useful in some cases.
If all the edge weights are distinct, then the MST is unique.
If some edge weights are equal, there may be multiple spanning trees. Prim’s/Dijkstra’s are only
guaranteed to find you one of them.
CSE 373 AU 18 23
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 AU 18 24
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.
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 AU 18 26
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).
CSE 373 AU 18 27