minimum spanning trees
DESCRIPTION
Minimum Spanning Trees. CSCI 2720 Spring 2005. Extended Dijkstra. // extending Dijkstra’s algorithm to compute the edges of the shortest paths Algorithm Dijkstra ( G, s ) // same implementation choices (heap or not) as for Dijkstra - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/1.jpg)
MSTs 1
Minimum Spanning Trees
CSCI 2720
Spring 2005
![Page 2: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/2.jpg)
MSTs 2
Extended Dijkstra // extending Dijkstra’s algorithm to compute the edges of the shortest paths
Algorithm Dijkstra(G, s) // same implementation choices (heap or not) as for Dijkstra// initialize // same performance as for Dijkstrafor all v G.vertices() // same code except for the two red lines
if (v s) setDistance(v, 0) else setDistance(v, )
v is not in cloud setParent(v, NULL)
while there are nodes not in cloud
u node not in cloud with min. distance add u to cloud // update neighbors of u
for all e G.adjacentEdges(u) z G.getOpposite(e, u) d_new getDistance(u) weight(e)if d_new getDistance(z)
updateDistance(z, d_new) updateParent(z, u)
![Page 3: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/3.jpg)
MSTs 3
Example (same graph as before)
CB
A
E
D
F
0
327
5
848
7 1
2 5
2
3 9
A.parent = NULLB.parent = EC.parent = AD.parent = CE.parent = CF.parent = D
![Page 4: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/4.jpg)
MSTs 4
All-Pairs Shortest PathsFind the distance between every pair of vertices in a weighted directed graph G.
Create a matrix, i’th row = distances for vertex vi
(and another matrix if we want to store parent values)
We can make n calls to Dijkstra’s algorithm (if no negative edges), one for each vertex as source. Each call fills in one row of the matrix.
This takes O(nm log n) time with heap implementation (of sparse graph with adjacency lists), O(n3) otherwise.
![Page 5: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/5.jpg)
MSTs 5
DAGs and Topological Ordering
A directed acyclic graph (DAG) is a digraph that has no directed cycles
Halfway between trees and digraphs Used instead of expression trees, to
“fold together” common subexpressions
Often used to represent dependencies:
tasks/jobs -- (x,y) means x must be done before y
course requirements – (x,y) means x must be taken before y
preferences – (x,y) means that x is preferable to y
In each case, acyclicity is important
B
A
D
C
E
DAG G
![Page 6: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/6.jpg)
MSTs 6
Topological SortingNumber vertices, so that (u,v) in E implies u < v
Often needed in DAG applications tasks/jobs – the order in which to do jobs course requirements – the order in which to take courses preferences – the overall “rating
Theorem A digraph admits a topological ordering if and only if it is a DAG (why?)
B
A
D
C
E
3
4 5
1
2
![Page 7: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/7.jpg)
MSTs 7
Whenever there is a choice between multiple vertices with no outgoing edges, the output is not unique.
Naïve implementation is O(n2). Why?
Algorithm for Topological Sorting
Algorithm TopologicalSort(G) H G // Temporary copy of G n G.numVertices() while H is not empty do
v find vertex with no outgoing edgesLabel v nn n - 1Remove v from H
![Page 8: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/8.jpg)
MSTs 8
Topological sort can be implemented in O(n + m). (Is that always faster?)
This is done as a variant of depth-first search
We label every node at the end of visiting it (before returning).
The node labels are in descending order (from highest down).
Result is the same as with naïve version before.
Topological Sorting using DFS
![Page 9: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/9.jpg)
MSTs 9
Topological Sorting using DFS
Algorithm topologicalDFS(G, v)Input graph G and a start vertex v of G Output labeling of the vertices of G
in the connected component of v setLabel(v, VISITED)for all e G.incidentEdges(v)
if getLabel(e) UNEXPLORED
w opposite(v,e)if getLabel(w) UNVISITED
setLabel(e, DISCOVERY)topologicalDFS(G, w)
else{e is a forward or cross edge}
Set the topological order of v to n n n – 1 // end of forall loop
Algorithm topologicalDFS(G)Input dag GOutput topological ordering of G
n G.numVertices()for all u G.vertices()
setLabel(u, UNVISITED)for all e G.edges()
setLabel(e, UNEXPLORED)for all v G.vertices()
if getLabel(v) UNEXPLORED
topologicalDFS(G, v)
Except for blue lines, this is
the same DFS code as before.
![Page 10: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/10.jpg)
MSTs 10
Topological Sorting Example
![Page 11: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/11.jpg)
MSTs 11
Topological Sorting Example
9
![Page 12: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/12.jpg)
MSTs 12
Topological Sorting Example
8
9
![Page 13: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/13.jpg)
MSTs 13
Topological Sorting Example
78
9
![Page 14: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/14.jpg)
MSTs 14
Topological Sorting Example
78
6
9
![Page 15: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/15.jpg)
MSTs 15
Topological Sorting Example
78
56
9
![Page 16: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/16.jpg)
MSTs 16
Topological Sorting Example
7
4
8
56
9
![Page 17: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/17.jpg)
MSTs 17
Topological Sorting Example
7
4
8
56
3
9
![Page 18: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/18.jpg)
MSTs 18
Topological Sorting Example
2
7
4
8
56
3
9
![Page 19: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/19.jpg)
MSTs 19
Topological Sorting Example
2
7
4
8
56
1
3
9
![Page 20: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/20.jpg)
MSTs 20
Minimum Spanning TreeSpanning subgraph
Subgraph of a graph G containing all the vertices of G
Spanning tree Spanning subgraph that is
itself a (free) treeMinimum spanning tree (MST)
Spanning tree of a weighted graph with minimum total edge weight
Applications Communications networks:
Connect all servers, at least cost
Transportation networks:Connect all cities, at least cost
ORD
PIT
ATL
STL
DEN
DFW
DCA
101
9
8
6
3
25
7
4
![Page 21: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/21.jpg)
MSTs 21
U V
Partition PropertyPartition Property:
Consider a partition of the vertices of G into subsets U and V
Let e be an edge of minimum weight across the partition
There is a minimum spanning tree of G containing edge e
Proof (by contradiction): Suppose no MSTs of G contain e. Let T be an MST of G; T does not
contain e Consider the cycle C formed by e
with T and let f be an edge of C across the partition
By the cycle property,weight(f) weight(e)
By replacing f with e, we obtaina better spanning tree!
74
2 85
7
3
9
8 e
f
74
2 85
7
3
9
e
f
Replacing f with e yieldsanother MST
U V
![Page 22: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/22.jpg)
MSTs 22
MST algorithms
2 algorithms to compute MST of a given weighted graph
Prim-Jarnik (similar to Dijkstra’s) Kruskal (uses a new ADT, “union-find”)
Both are greedy greedy: An algorithm that always takes the best
immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems
Prim-Jarnik greedily chooses nodes for MST
Kruskal’s algorithm greedily chooses edges for MST.
![Page 23: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/23.jpg)
MSTs 23
Prim-Jarnik’s AlgorithmSimilar to Dijkstra’s algorithm; the only difference is what distance means (now it’s just the edge weight).
We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from s
We store with each vertex v a label d(v) = the smallest weight of an edge connecting v to a vertex in the cloud
At each step: We add to the cloud the vertex
u outside the cloud with the smallest distance label
We update the labels of the vertices adjacent to u
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
5 4
7
![Page 24: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/24.jpg)
MSTs 24
Example
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
8
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
5
7
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
5
7
BD
C
A
F
E
74
28
5
7
3
9
8
07
2
5 4
7
![Page 25: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/25.jpg)
MSTs 25
Example (contd.)
BD
C
A
F
E
74
28
5
7
3
9
8
03
2
5 4
7
BD
C
A
F
E
74
28
5
7
3
9
8
03
2
5 4
7
![Page 26: Minimum Spanning Trees](https://reader030.vdocuments.us/reader030/viewer/2022033106/5681375e550346895d9eecb4/html5/thumbnails/26.jpg)
MSTs 26
Pseudocode Algorithm Prim-Jarnik(G) // same implementation choices (heap or not) as for Dijkstra
// initialize // same performance as for Dijkstra s a vertex of G // same code except for the two red lines for all v G.vertices()
if (v s) setDistance(v, 0) else setDistance(v, )
v is not in cloud setParent(v, NULL)
while there are nodes not in cloud
u node not in cloud with min. distance add u to cloud // update neighbors of u
for all e G.adjacentEdges(u) z G.getOpposite(e, u) d_new weight(e)if d_new getDistance(z)
updateDistance(z, d_new) updateParent(z, u)