1 Graph Algorithms Minimum Spanning Trees (MST) Union - Find Dana Shapira נדבר השיעור על עצים פורשים. כל הגרפים יהיו לא מכוונים וקשירים, לכן בהכרח יש עץ.

Slides:



Advertisements
Similar presentations
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
Advertisements

Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
Disjoint-Set Operation
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
- אמיר רובינשטיין Union-Find 1. הגדרה: מבנה נתונים, אשר בהינתן אוסף איברים המחולקים לקבוצות זרות, מאפשר ביצוע הפעולות הבאות: Find(i) – החזר.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Trees (MST)
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
עקרון ההכלה וההדחה.
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.
1 מבוא למדעי המחשב סיבוכיות. 2 סיבוכיות - מוטיבציה סידרת פיבונאצ'י: long fibonacci (int n) { if (n == 1 || n == 2) return 1; else return (fibonacci(n-1)
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm Binary Search Trees1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
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 and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
CMSC 341 Disjoint Sets. 8/3/2007 UMBC CMSC 341 DisjointSets 2 Disjoint Set Definition Suppose we have an application involving N distinct items. We will.
CMSC 341 Disjoint Sets Textbook Chapter 8. Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b) with a,b  S,
Lecture X Disjoint Set Operations
Disjoint Sets Data Structure. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
Disjoint-Set Operation. p2. Disjoint Set Operations : MAKE-SET(x) : Create new set {x} with representative x. UNION(x,y) : x and y are elements of two.
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.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
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.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
CMSC 341 Disjoint Sets. 2 Disjoint Set Definition Suppose we have N distinct items. We want to partition the items into a collection of sets such that:
Data Structures Hanoch Levi and Uri Zwick March 2011 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Disjoint Sets Data Structure
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
תירגול 14: מבני נתונים דינאמיים
Marina Kogan Sadetsky –
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
CMSC 341 Disjoint Sets.
CMSC 341 Disjoint Sets.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Lecture 12 Algorithm Analysis
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

1 Graph Algorithms Minimum Spanning Trees (MST) Union - Find Dana Shapira נדבר השיעור על עצים פורשים. כל הגרפים יהיו לא מכוונים וקשירים, לכן בהכרח יש עץ פורש 3/7/2007

2 A spanning tree of G is a subset T  E of edges, such that the sub-graph G'=(V,T) is connected and acyclic. Spanning tree עץ פורש- מכיל את כל הקודקודים של הגרף, אבל לא בהכרח את כל צלעותיו (אין בו צלעות הסוגרות מעגל).

3 Minimum Spanning Tree Given a graph G = (V, E) and an assignment of weights w(e) to the edges of G, a minimum spanning tree T of G is a spanning tree with minimum total edge weight עץ פורש מינימלי- הוא עץ פורש שסכום המשקולות על הצלעות שלו הוא מינימלי (MST)

4 How To Build A Minimum Spanning Tree General strategy: 1. Maintain a set of edges A such that (V, A) is a spanning forest of G and such that there exists a MST (V, F) of G such that A  F. 2. As long as (V, A) is not a tree, find an edge that can be added to A while maintaining the above property. Generic-MST(G=(V,E)) 1. A=  ; 2. while (A is not a spanning tree of G) do 3. choose a safe edge e=(u,v)  E 4. A=A  {e} 5. return A כדי למצוא עץ פורש מינימלי, נתחיל בבחירת הקשת שהכי טובה לנו. נסמן ב-A את קבוצת הקשתות של G, שהיא מוכלת בקב' של הקשתות של העץ הפורש המינימלי. נוסיף לה כל הזמן קשתות עד שנגיע לעץ הפורש המינימלי. אנחנו מחפשים קשתות בטוחות- קשת ששייכת לעץ הפורש המינימלי ואפשר לצרף אותה ל-A נקרא לקשת כזו קשת קלה- קשת עם משקל מינימלי מבין קשתות המקיימות תכונה כלשהי שנראה בהמשך

5 Cuts A cut (X, Y) of a graph G = (V, E) is a partition of the vertex set V into two sets X and Y = V \ X. An edge (v, w) is said to cross the cut (X, Y) if v  X and w  Y. A cut (X, Y) respects a set A of edges if no edge in A crosses the cut. חתך cut(x,y)- חלוקה של קודקודים ל- 2 קבוצות זרות ומשלימות. קשת חוצה את החתך אם קודקוד אחד שייך לקבוצה אחת והקודקוד השני לקבוצה השנייה (באיור אלו הקשתות הכחולות). קבוצה A מכבדת את החתך אם אין בה קשתות חותכות

6 Theorem: Let A be a subset of the edges of some minimum spanning tree of G; let (X, Y) be a cut that respects A; and let e be a minimum weight edge that crosses (X, Y). Then A  {e} is also a subset of the edges of a minimum spanning tree of G; edge e is safe. A Cut Theorem נגדיר תת קבוצה של העץ הפורש, את A (A מכבדת את החתך). נוסיף ל-A קשתות: נסתכל על כל הקשתות החוצות את החתך ומתוכן ניקח את הקשת עם המשקל המינימלי ואותה נוסיף ל-A (זוהי הקשת הבטוחה). נשים לב ש-A עדיין תהייה תת קבוצה של העץ הפורש. ונוכיח זאת. הוכחה אינטואטיבית-(הוכחה פורמלית בהמשך המצגת) A היא הקבוצה עם הקשתות האדומות וניתן לראות כי היא מכבדת את החתך הירוק. נסתכל על כל הקשתות הכחולות שחוצות את החתך. מתוכן ניקח את הקשת עם המשקל הנמוך ביותר ונוסיף אותה ל-A. נמשיך כך עבור החתכים השונים ונפסיק את התהליך כשאנחנו מגיעים לעץ פורש של כל הגרף. נטען שעץ זה שהגענו אליו הוא הגרף הפורש המינימלי. נסתכל על A ועל G. אם הקשת המינימלית שנסמנה e נמצאת בעץ הפותר, אז סיימנו את ההוכחה. אחרת, יש מסלול אחר P שסוגר מעגל יחד עם הצלע המינימלית, באמצעות צלע f. כאשר f אינה ב-A כיון ש-A מכבדת את החתך. נבנה עץ פורש אחר בו נוריד את f ונוסיף את e שתסגור את זה לעץ חדש- T’ במקום העץ הקודם T. T’ יהיה העץ הפורש המינימלי.

Theorem: Let A be a subset of the edges of some minimum spanning tree of G; let (X, Y) be a cut that respects A; and let e be a minimum weight edge that crosses (X, Y). Then A  {e} is also a subset of the edges of a minimum spanning tree of G; edge e is safe. A Cut Theorem

8 e e f Tw(e) ≤ w(f) w(T') ≤ w(T) u v

9 Let T be a MST such that A  T. If e = (u,v)  T, add e to T. The edge e = (u,v) forms a cycle with edges on the path p from u to v in T. Since u and v are on opposite sides of the cut, there is at least one edge f = (x,y) in T on the path p that also crosses the cut. f  A since the cut respects A. Since f is on the unique path from u to v in T, removing it breaks T into two components. w(e) ≤ w(f) (why?) Let T ' = T – {f}  {e}  w(T ') ≤ w(T). Proof:

10 Proof: The cut (V C, V–V C ) respects A, and e is a light edge for this cut. Therefore, e is safe. Corollary: Let G=(V,E) be a connected undirected graph and A a subset of E included in a minimum spanning tree T for G, and let C=(V C,E C ) be a tree in the forest G A =(V,A). If e is a light edge connecting C to some other component in G A, then e is safe for A. A Cut Theorem

11 Kruskal’s Algorithm Kruskal(G) 1A ← ∅ 2for every edge e = (v, w) of G, sorted by weight 3do if v and w belong to different connected components of (V, A) 4then add edge e to A (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f): a b c d e f g i h a b c d e f g i h איך בוחרים את החתך שמכבד את A? נסתכל על רכיב קשיר שנמצא ב- A ונסמנו C. נחבר בין תת העץ הקשיר הזה לרכיב קשירות אחר של A וכך נגדיל את A. החיבור יעשה באמצעות e. נרצה לראות ש-A מכבדת את החתך. לכן: חתך שמפריד בין קבוצת הקשתות של C לבין שאר הקשתות, יהיה חתך המכבד את A. e קשת קלה של החתך. והחתך מכבד את A. לכן e קשת בטוחה שניתן להוסיף אותה ל-A כך ש-A עדיין תהייה תת קבוצה שמוכלת- שווה לעץ הפורש המינימלי. ובכך סיימנו את הוכחת המשפט. בצעד הראשון לוקחים את הקשת הקלה ביותר (כי A=קב' ריקה בהתחלה) האלגוריתם הבא מראה את הדברים (כאשר כל קודקוד הוא רכיב קשירות בפני עצמו)

12 Correctness Proof eiei eiei Sorted edge sequence: e 1, e 2, e 3, e 4, e 5, e 6, …, e i, e i + 1, e i + 2, e i + 3, …, e n Every edge e j that cross the cut have a weight w(e j ) ≥ w(e i ). Hence, edge e i is safe.

13 Union-Find Data Structures Given a set S of n elements, maintain a partition of S into subsets S 1, S 2, …, S k Support the following operations: Union(x, y): Replace sets S i and S j such that x  S i and y  S j with S i  S j in the current partition Find(x): Returns a member r(S i ) of the set S i that contains x In particular, Find(x) and Find(y) return the same element if and only if x and y belong to the same set. It is possible to create a data structure that supports the above operations in O(α(n)) amortized time, where α is the inverse Ackermann function. הערכים מאוד מאוד קטנים לכן α(n) תחזיר מקסימום 5 כדי לדעת אם 2 קודקודים שייכים לאותו רכיב קשירות, נעזר במבנה הנתונים union-find

14 Kruskal’s Algorithm Using Union- Find Data Structure Kruskal(G,w) A   for each vertex v  V do Make-Set(v) sort the edges in E in non-decreasing weight order w for each edge (u,v)  E do if Find-Set(u) ≠ Find-Set(v) then A  A  {(u,v)} Union(u,v) return A

15 Kruskal’s Algorithm Using Union- Find Data Structure Analysis: O(|E| log |E|) time for everything except the operations on S Cost of operations on S: O( α (|E|,|V|)) amortized time per operation on S |V| – 1 Union operations |E| Find operations Total: O((|V| + |E|) α (|E|,|V|)) running time Total running time: O(|E| lg |E|). בהנחה שאנו יודעים לממש את האלגוריתם, מהו זמן הריצה שלו? זמן מינימלי למיון קשתות סה"כ לאחר מיון הקשתות לא כולל כיון ש- α מתנהגת כמעט כמו קבוע נקבל שבסה"כ:

16 Union/Find Assumptions: The Sets are disjoint. Each set is identified by a representative of the set. Initial state: A union/find structure begins with n elements, each considered to be a one element set. Functions: Make-Set(x): Creates a new set with element x in it. Union(x,y): Make one set out of the sets containing x and y. Find-Set(x): Returns a pointer to the representative of the set containing x. נסתכל על הפעולות על מבנה הנתונים ונבין מה זמן הריצה שלהן תומך בפעולות מחזירים נציג של הקבוצה

17 Basic Notation The elements in the structure will be numbered 0 to n-1 Each set will be referred to by the number of one of the element it contains Initially we have sets S 0,S 1,…,S n-1 If we were to call Union(S 2,S 4 ), these sets would be removed from the list, and the new set would now be called either S 2 or S 4 Notations: n Make-Set operations m total operations n  m נמספר את האיברים נסמן עברנו לקבוצות כי הרעיון הזה עוזר לפתור עוד בעיות מלבד זו שראינו בשיעור הקודם

18 First Attempt Represent the Union/Find structure as an array arr of n elements arr[i] contains the set number of element i Initially, arr[i]=i (Make-Set(i)) Find-Set(i) just returns the value of arr[i] To perform Union(S i,S j ): For every k such that arr[k]=j, set arr[k]=i נסיון ראשון לביצוע הפעולות בכמה שפחות זמן פעולה אחת- O(1) נעזר במערך שיכיל את n האיברים בהתחלה כל איבר עומד בפני עצמו, ולכן arr[i] מכיל את האיבר עצמו. בהתחלה כל איבר נמצא בקבוצה שמכילה רק אותו. אם הוא שייך ל-Si הוא יהיה ב- arr[j]. Union- O(n) – כיון שעוברים על כל המערך

19 Analysis The worst-case analysis: Find(i) takes O(1) time Union(S i,S j ) takes  (n) time A sequence of n Unions will take  (n 2 ) time מה יהיה זמן הריצה במצב זה? עוברים על כל איברי המערך לוקחים סדרה כדי לקבל סיבוכיות לשיעורין

20 Second Attempt Represent the Union/Find structure using linked lists. Each element points to another element of the set. The representative is the first element of the set. Each element points to the representative. How do we perform Union(S i,S j )? נסיון שני, ננסה שלא להשתמש במשהו שלוקח O(n). נעזר ברשימה מקושרת. כל איבר מצביע לבא בתור וגם לאיבר הראשון ברשימה, שהוא יהיה המייצג של הקבוצה. נניח שיש מצביע לאיבר הראשון ברשימה ולאיבר האחרון ברשימה. כדי לבצע union, נוסיף רשימה אחת לסופה של האחרת. ונדאג שכל האיברים ברשימה האחרת יצביעו לאיבר הראשון (המייצג של הרשימה) החדש. יהיה מערך שמצביע לכל האיברים, ככה יהיה ניתן לעשות find בזמן של O(1). Find לאיבר שאנחנו רוצים, מוביל לתא שלו במערך, שמוביל אליו, שמוביל לאיבר הראש הרשימה (לנציג הרשימה).

21 Analysis The worst-case analysis: Find(i) takes O(1) time Make-Set(i) takes O(1) time Union(S i,S j ) takes  (n) time (Why?) A sequence of n Unions-Find will take  (n 2 ) time (Example?) מה יהיה זמן הריצה במצב זה? כי צריך לעבור בממוצע על n/2 איברים עשינו n פעולות. Make-set עכשיו O(n) פעולות שלוקחות סה"כ Θ(n 2 ) של union-find. נבצע את פעולות האיחוד עד הסוף : union(x 2,x 1 ) union(x 3,x 2 ) n-1 … פעולות union(x n,x n-1 ) זמן הריצה לשיעורין : n*O(1) + (n-1) Θ(n) =( O(n) + O(n 2 ) ) / 2 = Θ(n 2 ) נחלק במס' הפעולותunionMake-set

22 Up-Trees A simple data structure for implementing disjoint sets is the up- tree. We visualize each element as a node A set will be visualized as a directed tree Arrows will point from child to parent The set will be referred to by its root A H W H, A and W belong to the same set. H is the representative B X R F X, B, R and F are in the same set. X is the representative מבנה נתונים נוסף שאולי יעזור לנו כי עדיין לא השגנו זמן ריצה טוב הוא עץ שבו כל שורש הוא הנציג של הקבוצה

23 Operations in Up-Trees Follow pointer to representative element. find(x) { if (x≠p(x))// not the representative then p(x)  find(p(x)); return p(x); } נראה את הפעולות על העץ P(x)- האב של x

24 Union Union is more complicated. Make one representative element point to the other, but which way? Does it matter?

25 Union(H, X) A H W B X R F A H W B X R F X points to H B, R and F are now deeper H points to X A and W are now deeper דוגמא: עדיף שאיברי הקבוצה הקטנה יצביעו כולם לראש של הקבוצה הגדולה. זה יעסוק בזמן הריצה של find.

26 A worst case for Union Union can be done in O(1), but may cause find to become O(n) A BCDE Consider the result of the following sequence of operations: Union (A, B) Union (C, A) Union (D, C) Union (E, D) נקבל עץ מנוון: וזה לא עזר לנו לגבי זמן הריצה A B C D E

27 Array Representation of Up-tree Assume each element is associated with an integer i=0…n-1. From now on, we deal only with i. Create an integer array, A[n] An array entry is the element’s parent A -1 entry signifies that element i is the representative element. נבחן ייצוג של עץ הפוך באמצעות מערך כל איבר נמצא במקום שלו. במקום של הנציג נשים -1.

28 Array Representation of Up-tree Now the union algorithm might be: Union(x,y) { A[y] = x;// attaches y to x } The find algorithm would be find(x) { if (A[x] < 0) return(x); else return(find(A[x])); } Performance: ??? איך מתבצעות הפעולות בייצוג הזה? הגענו לשורש נחפש את מי שנמצא באותו מקום במערך (בתא שמור האינדקס של האבא) דוגמא למימוש יש בהמשך

29 Analysis Worst case: Union(S i,S j ) take O(1) time Find(i) takes O(n) time Can we do better in an amortized analysis? What is the maximum amount of time n operations could take us? Suppose we perform n/2 unions followed by n/2 finds The n/2 unions could give us one tree of height n/2-1 Thus the total time would be n/2 + (n/2)(n/2) = O(n 2 ) This strategy doesn’t really help… מה יהיה זמן הריצה במצב זה? נשים לב שעדיין לא השגנו זמן ריצה טוב