Download Minimum Spanning Trees: Algorithms and Implementations - Prof. Philip Hamilton Sweany and more Study notes Computer Science in PDF only on Docsity!
Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos 226
Minimum Spanning Tree
Reference: Chapter 20 , Algorithms in Java, 3 rd^ Edition, Robert Sedgewick. 2
Minimum Spanning Tree
MST. Given connected graph G with positive edge weights, find a min
weight set of edges that connects all of the vertices.
23 10 21 14 24 16 4 18 9 7 11 8 G 5 6 3
Minimum Spanning Tree
MST. Given connected graph G with positive edge weights, find a min
weight set of edges that connects all of the vertices.
Theorem. [Cayley 1 889] There are VV-^2 spanning trees on the complete
graph on V vertices.
can't solve by brute force 23 10 21 14 24 16 4 18 9 7 11 8 cost(T) = 50 5 6 4
MST Origin
Otakar Boruvka (1926).
! Electrical Power Company of Western Moravia in Brno.
! Most economical construction of electrical power network.
! Concrete engineering problem is now a cornerstone problem in
combinatorial optimization.
Otakar Boruvka
5
Applications
MST is fundamental problem with diverse applications. ! Network design.
- telephone, electrical, hydraulic, TV cable, computer, road ! Approximation algorithms for NP-hard problems.
- traveling salesperson problem, Steiner tree ! Indirect applications.
- max bottleneck paths
- LDPC codes for error correction
- image registration with Renyi entropy
- learning salient features for real-time face verification
- reducing data storage in sequencing amino acids in a protein
- model locality of particle interactions in turbulent fluid flows
- autoconfig protocol for Ethernet bridging to avoid cycles in a network ! Cluster analysis. 6
Medical Image Processing
MST describes arrangement of nuclei in the epithelium for cancer research http://www.bccrc.ca/ci/ta 01 _archlevel.html 8
Two Greedy Algorithms
Kruskal's algorithm. Consider edges in ascending order of cost.
Add the next edge to T unless doing so would create a cycle.
Prim's algorithm. Start with any vertex s and greedily grow a tree T
from s. At each step, add the cheapest edge to T that has exactly one
endpoint in T.
Theorem. Both greedy algorithms compute an MST.
Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit." - Gordon Gecko Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos 226
Weighted Graphs
14
Spanning Tree
MST. Given connected graph G with positive edge weights, find a min
weight set of edges that connects all of the vertices.
Def. A spanning tree of a graph G is a subgraph T that is connected
and acyclic.
Property. MST of G is always a spanning tree.
15
Greedy Algorithms
Simplifying assumption. All edge costs ce are distinct.
Cycle property. Let C be any cycle, and let f be the max cost edge
belonging to C. Then the MST does not contain f.
Cut property. Let S be any subset of vertices, and let e be the min
cost edge with exactly one endpoint in S. Then the MST contains e.
f
C
S
e is in the MST e f is not in the MST 16
Cut Property
Simplifying assumption. All edge costs ce are distinct.
Cut property. Let S be any subset of vertices, and let e be the min cost
edge with exactly one endpoint in S. Then the MST T* contains e.
Pf. [by contradiction]
! Suppose e does not belong to T*. Let's see what happens.
! Adding e to T* creates a (unique) cycle C in T*.
! Some other edge in C, say f, has exactly one endpoint in S.
! T = T*! { e } " { f } is also a spanning tree.
! Since ce < cf, cost(T) < cost(T*).
! This is a contradiction.!
f T* e
S
17
Cycle Property
Simplifying assumption. All edge costs ce are distinct.
Cycle property. Let C be any cycle in G, and let f be the max cost edge
belonging to C. Then the MST T* does not contain f.
Pf. [by contradiction]
! Suppose f belongs to T*. Let's see what happens.
! Deleting f from T* disconnects T*. Let S be one side of the cut.
! Some other edge in C, say e, has exactly one endpoint in S.
! T = T*! { e } " { f } is also a spanning tree.
! Since ce < cf, cost(T) < cost(T*).
! This is a contradiction.!
f T* e
S
Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos 226
Kruskal's Algorithm
19
Kruskal's algorithm. [Kruskal 1956] Consider edges in ascending order
of cost. Add the next edge to T unless doing so would create a cycle.
Kruskal's Algorithm: Example
3-5 1-7 6- 0-2 0-7 0-1 3-4 4-5 4- 20
Kruskal's Algorithm: Example
25% 50% 75% 100% 21 C e
Kruskal's Algorithm: Proof of Correctness
Theorem. Kruskal's algorithm computes the MST.
Pf (case 1). If adding e to T creates a cycle C, then e is the max weight
edge in C, so the cycle property asserts that e is not in the MST.
26
Kruskal's Algorithm: Running Time
Kruskal running time. O(E log V).
Remark. If edges already sorted: O(E log* V) time.
Operation sort union find Cost E log V log* V † log* V † Frequency 1 V - 1 E † amortized bound using weighted quick union with path compression recall: log* V # 5 in this universe E # V^2 so O(log E) is O(log V) Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos 226
Prim's Algorithm
28
Prim's Algorithm: Example
Prim's algorithm. [Jarník 1 930, Dijkstra 1957, Prim 1959]
Start with vertex 0 and greedily grow tree T. At each step, add
cheapest edge that has exactly one endpoint in T.
29
Prim's Algorithm: Example
25% 50% 75% 100%
30
Prim's Algorithm: Proof of Correctness
Theorem. Prim's algorithm computes the MST.
Pf.
! Let S be the subset of vertices in current tree T.
! Prim adds the cheapest edge e with exactly one endpoint in S.
! Cut property asserts that e is in the MST.!
S e 31
Prim's Algorithm: Implementation
Q. How to find cheapest edge with exactly one endpoint in S?
A1. Brute force: try all edges.
! O(E) time per spanning tree edge.
! O(E V) time overall.
32
Prim's Algorithm: Implementation
Q. How to find cheapest edge with exactly one endpoint in S?
A2. Maintain edges with (at least) one endpoint in S in a priority queue.
! Delete min to determine next edge e to add to T.
! Disregard e if both endpoints are in S.
! Upon adding e to T, add edges incident to one endpoint to PQ.
Running time.
! O(log V) time per edge (using a binary heap).
! O(E log V) time overall.
the one not already in S 33
Prim's Algorithm: Java Implementation
public class LazyPrim { private Sequence mst = new Sequence(); public LazyPrim(WeightedGraph G) { boolean[] marked = new boolean[G.V()]; MinPQ pq = new MinPQ(); marked[ 0 ] = true; for (Edge e : G.adj( 0 )) pq.insert(e); while (!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.v, w = e.w; if (!marked[v] || !marked[w]) mst.add(e); if (!marked[v]) for (Edge f : G.adj(v)) pq.insert(f); if (!marked[w]) for (Edge f : G.adj(w)) pq.insert(f); marked[v] = marked[w] = true; } } } these edges have exactly one endpoint in S disregard edge if both its endpoints are in S is v in S? add all edges incident to 0
38
Euclidean MST
Euclidean MST. Given N points in the plane, find MST connecting them.
! Distances between point pairs are Euclidean distances.
Brute force. Compute &(N^2 ) distances and run Prim's algorithm.
Ingenuity. Exploit geometry and do it in O(N log N).
39
Euclidean MST
Key geometric fact. Edges of the Euclidean MST are edges of the
Delaunay triangulation.
Euclidean MST algorithm.
! Compute Voronoi diagram to get Delaunay triangulation.
! Run Kruskal's MST algorithm on Delaunay edges.
Running time. O(N log N).
! Fact: # 3N Delaunay edges since it's planar.
! O(N log N) for Voronoi.
! O(N log N) for Kruskal.
Lower bound. Any comparison-based Euclidean
MST algorithm requires %(N log N) comparisons.
Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos 226
Clustering
O Reuftberreenack (^) eo: fN cihnaol eMriash dreaa,t HhsP Lianb Lsondon in 1850 s. 41
Clustering
Clustering. Given a set of objects classify into coherent groups.
Distance function. Numeric value specifying "closeness" of two objects.
Fundamental problem. Divide into clusters so that points in different
clusters are far apart.
! Routing in mobile ad hoc networks.
! Identify patterns in gene expression.
! Document categorization for web search.
! Similarity searching in medical image databases
! Skycat: cluster 109 sky objects into stars, quasars, galaxies.
photos, documents. micro-organisms number of corresponding pixels whose intensities differ by some threshold
42
Clustering of Maximum Spacing
k-clustering. Divide objects into k non-empty groups.
Distance function. Assume it satisfies several natural properties.
! c(v, w) = 0 iff v = w (identity of indiscernibles)
! c(v, w) ' 0 (nonnegativity)
! c(v, w) = c(w, v) (symmetry)
Spacing. Min distance between any pair of points in different clusters.
Clustering of maximum spacing. Given an integer k, find a k-clustering
of maximum spacing.
spacing k = 4 43
Single-Link Clustering Algorithm
Single-link k-clustering algorithm.
! Form V clusters of one object each.
! Find the closest pair of objects such that each object is in a
different cluster, and add an edge between them.
! Repeat until there are exactly k clusters.
Observation. This procedure is precisely Kruskal's algorithm
(except we stop when there are k connected components).
Property. Algorithm finds a k-clustering of maximum spacing.
44
Dendrogram
Dendrogram. Scientific visualization of hypothetical sequence of
evolutionary events.
! Leaves = genes.
! Internal nodes = hypothetical ancestors.
Reference: http://www.biostat.wisc.edu/bmi 576 /fall- 2003 /lecture 13 .pdf 45
Dendrogram of Cancers in Human
Tumors in similar tissues cluster together.
Reference: Botstein & Brown group Gene 1 Gene n gene expressed gene not expressed