Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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 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 5 4 3 3 6 1 6 9 8 3 7 1 2 7 עץ פורש מינימלי- הוא עץ פורש שסכום המשקולות על הצלעות שלו הוא מינימלי (MST)

4 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 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 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 1 4 9 3 2 4 נגדיר תת קבוצה של העץ הפורש, את A (A מכבדת את החתך). נוסיף ל-A קשתות: נסתכל על כל הקשתות החוצות את החתך ומתוכן ניקח את הקשת עם המשקל המינימלי ואותה נוסיף ל-A (זוהי הקשת הבטוחה). נשים לב ש-A עדיין תהייה תת קבוצה של העץ הפורש. ונוכיח זאת. הוכחה אינטואטיבית-(הוכחה פורמלית בהמשך המצגת) A היא הקבוצה עם הקשתות האדומות וניתן לראות כי היא מכבדת את החתך הירוק. נסתכל על כל הקשתות הכחולות שחוצות את החתך. מתוכן ניקח את הקשת עם המשקל הנמוך ביותר ונוסיף אותה ל-A. נמשיך כך עבור החתכים השונים ונפסיק את התהליך כשאנחנו מגיעים לעץ פורש של כל הגרף. נטען שעץ זה שהגענו אליו הוא הגרף הפורש המינימלי. נסתכל על A ועל G. אם הקשת המינימלית שנסמנה e נמצאת בעץ הפותר, אז סיימנו את ההוכחה. אחרת, יש מסלול אחר P שסוגר מעגל יחד עם הצלע המינימלית, באמצעות צלע f. כאשר f אינה ב-A כיון ש-A מכבדת את החתך. נבנה עץ פורש אחר בו נוריד את f ונוסיף את e שתסגור את זה לעץ חדש- T’ במקום העץ הקודם T. T’ יהיה העץ הפורש המינימלי.

7 7 4 9 12 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 8 e e f Tw(e) ≤ w(f) w(T') ≤ w(T) u v

9 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 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 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):12 2 1 1 12 8 5 3 9 4 3 2 5 1 6 a b c d e f g i h 2 1 1 8 5 3 9 4 3 2 5 1 6 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 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 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 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 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 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 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 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 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 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 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 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 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 24 Union Union is more complicated. Make one representative element point to the other, but which way? Does it matter?

25 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 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 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 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 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… מה יהיה זמן הריצה במצב זה? נשים לב שעדיין לא השגנו זמן ריצה טוב


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

Similar presentations


Ads by Google