1 / 29

Graph Algorithms

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

eara
Download Presentation

Graph Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. כדי למצוא עץ פורש מינימלי, נתחיל בבחירת הקשת שהכי טובה לנו. How To Build A Minimum Spanning Tree • General strategy: • 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. • 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)) • A= ; • while (A is not a spanning tree of G) do • choose a safe edge e=(u,v)E • A=A{e} • return A נסמן ב-A את קבוצת הקשתות של G, שהיא מוכלת בקב' של הקשתות של העץ הפורש המינימלי. נוסיף לה כל הזמן קשתות עד שנגיע לעץ הפורש המינימלי. אנחנו מחפשים קשתות בטוחות- קשת ששייכת לעץ הפורש המינימלי ואפשר לצרף אותה ל-A נקרא לקשת כזו קשת קלה- קשת עם משקל מינימלי מבין קשתות המקיימות תכונה כלשהי שנראה בהמשך

  5. חתך cut(x,y)- חלוקה של קודקודים ל- 2 קבוצות זרות ומשלימות. קשת חוצה את החתך אם קודקוד אחד שייך לקבוצה אחת והקודקוד השני לקבוצה השנייה (באיור אלו הקשתות הכחולות). קבוצה A מכבדת את החתך אם אין בה קשתות חותכות 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) respectsa set A of edges if no edge in A crosses the cut.

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

  7. A Cut Theorem 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. 12 9 4

  8. A Cut Theorem u e e v f T w(e) ≤ w(f) w(e) ≤ w(f) w(T') ≤ w(T)

  9. Proof: • 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).

  10. A Cut Theorem Proof: The cut (VC, V–VC) 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=(VC,EC) be a tree in the forest GA=(V,A). If e is a light edge connecting C to some other component in GA, then e is safe for A.

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

  12. ei ei Correctness Proof Sorted edge sequence: e1, e2, e3, e4, e5, e6, …, ei, ei + 1, ei + 2, ei + 3, …, en Every edge ej that cross the cut have a weight w(ej) ≥ w(ei). Hence, edge ei is safe.

  13. כדי לדעת אם 2 קודקודים שייכים לאותו רכיב קשירות, נעזר במבנה הנתונים union-find Union-Find Data Structures • Given a set S of n elements, maintain a partition of S into subsets S1, S2, …, Sk • Support the following operations: • Union(x, y): Replace sets Si and Sj such that xSi and ySj with SiSj in the current partition • Find(x): Returns a member r(Si) of the set Si 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

  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)Edo if Find-Set(u) ≠ Find-Set(v) thenA  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 S0,S1,…,Sn-1 • If we were to call Union(S2,S4), these sets would be removed from the list, and the new set would now be called either S2 or S4 • 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(Si,Sj): • For every k such that arr[k]=j, set arr[k]=i נעזר במערך שיכיל את n האיברים בהתחלה כל איבר עומד בפני עצמו, ולכן arr[i] מכיל את האיבר עצמו. בהתחלה כל איבר נמצא בקבוצה שמכילה רק אותו. אם הוא שייך ל-Si הוא יהיה ב- arr[j]. פעולה אחת- O(1) Union- O(n) – כיון שעוברים על כל המערך

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

  20. נסיון שני, ננסה שלא להשתמש במשהו שלוקח O(n). 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(Si,Sj)? נעזר ברשימה מקושרת. כל איבר מצביע לבא בתור וגם לאיבר הראשון ברשימה, שהוא יהיה המייצג של הקבוצה. נניח שיש מצביע לאיבר הראשון ברשימה ולאיבר האחרון ברשימה. כדי לבצע 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(Si,Sj) takes (n) time (Why?) • A sequence of nUnions-Find will take (n2) time (Example?) כי צריך לעבור בממוצע על n/2 איברים עשינו n פעולות. Make-set עכשיו O(n) פעולות שלוקחות סה"כ Θ(n2) של union-find. נבצע את פעולות האיחוד עד הסוף: union(x2,x1) union(x3,x2)n-1 … פעולות union(xn,xn-1) זמן הריצה לשיעורין: n*O(1) + (n-1) Θ(n) =( O(n) + O(n2) ) / 2 = Θ(n2) Make-set union נחלק במס' הפעולות

  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 H X F A W B R H, A and W belong to the same set. H is the representative 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) עדיף שאיברי הקבוצה הקטנה יצביעו כולם לראש של הקבוצה הגדולה. זה יעסוק בזמן הריצה של find. H X F X points to H B, R and F are now deeper A W B R H X F H points to X A and W are now deeper A W B R

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

  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(Si,Sj) 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(n2) • This strategy doesn’t really help… נשים לב שעדיין לא השגנו זמן ריצה טוב

More Related