Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Greed is good. (Some of the time)
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
Chapter 23 Minimum Spanning Trees
Discussion #36 Spanning Trees
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Chapter 2 Graph Algorithms.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Union-find Algorithm Presented by Michael Cassarino.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Minimum Spanning Trees Featuring Disjoint Sets HKOI Training 2006 Liu Chi Man (cx) 25 Mar 2006.
Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Graph Search Applications, Minimum Spanning Tree
IOI/ACM ICPC Training 4 June 2005.
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
Graph theory Definitions Trees, cycles, directed graphs.
12. Graphs and Trees 2 Summary
Lecture 22 Minimum Spanning Tree
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
CS200: Algorithm Analysis
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Chapter 9: Graphs Spanning Trees
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Minimum Spanning Trees
Presentation transcript:

Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)

2 Prerequisites  Asymptotic complexity  Set theory  Elementary graph theory  Priority queues

3 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

4 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

5 Uh… a long way to go  What is a Tree?  Disjoint Sets  Minimum Spanning Trees  Various Tree Topics

6 What is a Tree? A tree in Pokfulam, Hong Kong. (Photographer: cx)

7 What is a Tree?  In graph theory, a tree is an acyclic, connected graph  Acyclic means “with no cycles”

8 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

9 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

10 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

11 ZzZZzzz… bored to death?  What is a Tree?  Disjoint Sets  Minimum Spanning Trees  Various Tree Topics

12 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

13 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}

14 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

15 Disjoint Sets  Implementation: Naïve arrays  Set[x] stores the representative of the set containing x, for 1 ≤ x ≤ N  :  Slight modifications give  U is the size of the union  Worst case is O(MN) for all M operations

16 Disjoint Sets  An example, with N = 4 x1234 Set[x]1234 Initial: x1234 Set[x]1214 Union 1 3: x1234 Set[x]1212 Union 2 4: x1234 Set[x]1111 Union 1 2: Find 4: Answer = 2 Find 4: Answer = 1

17 Disjoint Sets  Why is it slow?  Consider Union 1 2 on the table: x Set[x] x

18 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)

19 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}.

20 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

21 Disjoint Sets  An example, with N = 4 Initial: Union 1 3: Union 2 4: Find 4:

22 Disjoint Sets  An example, with N = 4 (cont.) Union 1 2: Find 4:

23 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

24 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

25 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

26 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

27 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

28 Disjoint Sets  An example, with N = 7  Now we Find The root is

29 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

30 When can I have my lunch?  What is a Tree?  Disjoint Sets  Minimum Spanning Trees  Various Tree Topics

31 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

32 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

33 Minimum Spanning Trees  Two algorithms  Kruskal’s algorithm  Prim’s algorithm

34 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

35 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

36 Kruskal’s Algorithm  How to check if a cycle is formed?  Depth-first search (DFS)  Time complexity is O(E 2 )  Alternatively, a disjoint set!  The sets are connected components

37 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

38 Kruskal’s Algorithm  The improved time complexity is O(ElgV)  In fact, the bottleneck is sorting!

39 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

40 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

41 Prim’s Algorithm  Time complexity: O(V 2 )  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

42 Minimum Spanning Trees  The fastest known algorithm runs in O(E  (V,E))

43 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

44 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

45 Are you with me?  What is a Tree?  Disjoint Sets  Minimum Spanning Trees  Various Tree Topics

46 Various Tree Topics  Center and diameter  Tree isomorphism  Canonical representation  Prüfer code  Lowest common ancestor (LCA)

47 Supplementary Readings  Advanced:  Disjoint set forest (Lecture slides) Disjoint set forest  Prim’s algorithm Prim’s algorithm  Kruskal’s algorithm Kruskal’s algorithm  Center and diameter Center and diameter  Post-advanced (so-called Beginners):  Lowest common ancestor Lowest common ancestor  Maximum branching Maximum branching