470 likes | 489 Views
Trees Featuring Minimum Spanning Trees. HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx). Prerequisites. Asymptotic complexity Set theory Elementary graph theory Priority queues. Assumptions. In the coming slides, we only talk about simple graphs
E N D
TreesFeaturing Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)
Prerequisites • Asymptotic complexity • Set theory • Elementary graph theory • Priority queues
Assumptions • In the coming slides, we only talk about simple graphs • Extensions to multigraphs and pseudographs should be straightforward if you understand the materials well
Notations • A graph G = (V, E) • V is the set of vertices • E is the set of edges • |V| is the number of vertices in V • Sometimes we use V for convenience • |E| is the number of edges in E • Sometimes we use E for convenience
Uh… a long way to go • What is a Tree? • Disjoint Sets • Minimum Spanning Trees • Various Tree Topics
What is a Tree? A tree in Pokfulam, Hong Kong. (Photographer: cx)
What is a Tree? • In graph theory, a tree is an acyclic, connected graph • Acyclic means “with no cycles”
What is a Tree? • Properties of a tree: • |E| = |V| - 1 • |E| = (|V|) • Adding an edge between a pair of non-adjacent vertices creates exactly one cycle • Removing any one edge from the tree makes it disconnected
What is a Tree? • The following four conditions are equivalent: • G is connected and acyclic • G is connected and |E| = |V| - 1 • G is acyclic and |E| = |V| - 1 • Between any pair of vertices in G, there exists a unique path • G is a tree if at least one of the above conditions is satisfied
Other Properties of Trees • A tree is bipartite • A tree with at least two vertices has at least two leaves (vertices of degree 1) • A tree is planar
ZzZZzzz… bored to death? • What is a Tree? • Disjoint Sets • Minimum Spanning Trees • Various Tree Topics
The Union-Find Problem • Initially there are N objects, each in its own singleton set • Let’s label the objects by 1, 2, …, N • So the sets are {1}, {2}, …, {N} • There are two kinds of operations: • Merge two sets (Union) • Find the set to which an object belongs • Let there be a sequence of M operations
The Union-Find Problem • An example, with N = 4, M = 5 • Initial: {1}, {2}, {3}, {4} • Union {1}, {3} {1, 3}, {2}, {4} • Find 3. Answer: {1, 3} • Union {4}, {1,3} {1, 3, 4}, {2} • Find 2. Answer: {2} • Find 1. Answer {1, 3, 4}
Disjoint Sets • We usually use disjoint-set data structures to solve the union-find problem • A disjoint set is simply a data structure that supports the Union and Find operations • How should a set be named? • Each set has its own representative element
Disjoint Sets • Implementation: Naïve arrays • Set[x] stores the representative of the set containing x, for 1 ≤ x ≤ N • <per Union, per Find>: <O(N), O(1)> • Slight modifications give <O(U), O(1)> • U is the size of the union • Worst case is O(MN) for all M operations
Disjoint Sets • An example, with N = 4 Initial: Union 1 3: Union 2 4: Answer = 2 Find 4: Union 1 2: Answer = 1 Find 4:
Disjoint Sets • Why is it slow? • Consider Union 1 2 on the table:
Disjoint Sets • Idea for improvement: • When doing a Union, always update the set of smaller size • The overall running time can be shown to be O(M lgN)
6 1 3 5 4 7 2 Disjoint Sets • Implementation: Forest • A forest is a set of trees • Each set is represented by a rooted tree, with the root being the representative element Two sets: {1, 3, 5} and {2, 4, 6, 7}.
Disjoint Sets • Ideas • Find • Traverses from the vertex up to the root • Union • Merges two trees in the forest • That is, the root of one tree becomes a child of the other tree
1 2 3 4 1 2 4 3 1 2 3 4 1 2 3 4 Disjoint Sets • An example, with N = 4 Initial: Union 1 3: Union 2 4: Find 4:
1 2 3 4 1 2 3 4 Disjoint Sets • An example, with N = 4 (cont.) Union 1 2: Find 4:
Disjoint Sets • How to represent the trees? • Leftmost-Child-Right-Sibling (LCRS)? • Too complex! • We just need to traverse from bottom to top – parent-finding should be supported • An array Parent[] is enough • Parent[x] stores the parent vertex of x if x is not the root of a tree • Parent[x] stores x if x is the root of a tree
Disjoint Sets • The worst case is still O(MN ) overall • To improve the worst case time complexity, we employ two techniques • Union-by-rank • Path compression
Disjoint Sets • Union-by-rank • Recall that in our previous naïve array implementation, the worst case time bound can be improved by always updating the smaller set in a Union • Here, we make the “smaller” tree a subtree of the “larger” tree • To keep the resulting tree “small” • To compare the “sizes” of trees, we assign a rank to every tree in the forest
Disjoint Sets • Union-by-rank (cont.) • The rank of the tree is the height of the tree when it is created • A tree is created either at the beginning, or after a Union • That means we need to keep track of the trees’ ranks during the Unions
Disjoint Sets • Path compression • See also the solution for Symbolic Links (HKOI2005 Senior Final) • When we carry out a Find, we traverse from a vertex to the root • Why don’t we move the vertices closer to the root at the same time? • Closer to the root means shorter query time
The root is 3 3 3 3 5 5 1 5 1 6 1 4 6 6 The root is 3 7 2 4 4 The root is 3 7 7 2 2 Disjoint Sets • An example, with N = 7 • Now we Find 4
Disjoint Sets • We ignore the effect of path compression on tree ranks • Union-by-rank alone gives a running time of O(M lgN) • Together with path compression, the improved running time is O(M(N)) • is the inverse of the Ackermann's function • (n) is almost constant
When can I have my lunch? • What is a Tree? • Disjoint Sets • Minimum Spanning Trees • Various Tree Topics
Minimum Spanning Trees • Given a connected graph G = (V, E), a spanning tree of G is a graph T such that • T is a subgraph of G • T is a tree • T contains every vertex of G • A connected graph must have at least one spanning tree
Minimum Spanning Trees • Given a weighted connected graph G, a minimum spanning tree T* of G is a spanning tree of G with minimum total edge weight • Application: Minimizing the total length of wires needed to connect up a collection of computers
Minimum Spanning Trees • Two algorithms • Kruskal’s algorithm • Prim’s algorithm
Kruskal’s Algorithm • Algorithm • Initially T is an empty set • While T has less than |V|-1 edges Do • Choose an edge with minimum weight such that it does not form a cycle with the edges in T, and add it to T • The edges in T form a MST
Kruskal’s Algorithm • Algorithm (Detailed) • T is an empty set • Sort the edges in G by their weights • For (in ascending weight) each edge e Do • If T {e} does not contain a cycle Then • Add e to T • Return T
Kruskal’s Algorithm • How to check if a cycle is formed? • Depth-first search (DFS) • Time complexity is O(E2) • Alternatively, a disjoint set! • The sets are connected components
Kruskal’s Algorithm • Algorithm (disjoint-set) • T is an empty set • Create sets {1}, {2}, …, {V} • Sort the edges in G by their weights • For (in ascending weight) each edge e Do • Let e connect vertices x and y • If Find(x) Find(y) Then • Add e to T, then Union(Find(x), Find(y)) • Return T
Kruskal’s Algorithm • The improved time complexity is O(ElgV) • In fact, the bottleneck is sorting!
Prim’s Algorithm • Algorithm • Initially T is a set containing any one vertex in V • While T has less than |V|-1 edges Do • Among those edges connecting a vertex in T and a vertex in V-T, choose one with minimum weight, and add it (with its two end vertices) to T • T is a minimum spanning tree
Prim’s Algorithm • Algorithm (Detailed) • T • Cost[x] for all x V • Marked[x] false for all x V • Pred[x] for all x V • Pick any u V, and set Cost[u] to 0 • Repeat |V| times • Let w be a vertex of minimum Cost among all unMarked vertices • Marked[w] true • T T Pred[x] • For each unMarked vertex v such that (w, v) E Do • If weight(w, v) < Cost[v] Then • Cost[v] weight(w, v) • Pred[x] { (w, v) } • Return T
Prim’s Algorithm • Time complexity: O(V2) • If a (binary) heap is used to accelerate the Find-Min operations, the time complexity is reduced to O(ElgV) • O(VlgV+E) if a Fibonacci heap is used
Minimum Spanning Trees • The fastest known algorithm runs in O(E(V,E))
MST Extensions • Second-best MST • We don’t want the best! • Online MST • See IOI2003 Path Maintenance • Minimum Bottleneck Spanning Tree • The bottleneck of a spanning tree is the weight of its maximum weight edge • An algorithm that runs in O(V+E) exists
MST Extensions (NP-Hard) • Minimum Steiner Tree • No need to connect all vertices, but at least a given subset B V • Degree-bounded MST • Every vertex of the spanning tree must have degree not greater than a given value K
Are you with me? • What is a Tree? • Disjoint Sets • Minimum Spanning Trees • Various Tree Topics
Various Tree Topics • Center and diameter • Tree isomorphism • Canonical representation • Prüfer code • Lowest common ancestor (LCA)
Supplementary Readings • Advanced: • Disjoint set forest (Lecture slides) • Prim’s algorithm • Kruskal’s algorithm • Center and diameter • Post-advanced (so-called Beginners): • Lowest common ancestor • Maximum branching