המצגת נטענת. אנא המתן

המצגת נטענת. אנא המתן

יסודות מבני נתונים תרגול 13: עץ פורש מינימלי - Minimum Spanning Tree (MST)

מצגות קשורות


מצגת בנושא: "יסודות מבני נתונים תרגול 13: עץ פורש מינימלי - Minimum Spanning Tree (MST)"— תמליל מצגת:

1 יסודות מבני נתונים תרגול 13: עץ פורש מינימלי - Minimum Spanning Tree (MST)

2 Minimum Spanning Tree (MST)
Kruskal’s algorithm Select the shortest edge in a network Select the next shortest edge which does not create a cycle Repeat step 2 until all vertices have been connected Prim’s algorithm Select any vertex Select the shortest edge connected to that vertex Select the shortest edge connected to any vertex already connected Repeat step 3 until all vertices have been connected

3 Example Kruskal’s Algorithm B C 3 6 8 A D F 7 5 4 2 E
List the edges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 A F B C D E 2 7 4 5 8 6 3

4 Kruskal’s Algorithm Select the shortest edge in the network B C 3 6 8
F B C D E 2 7 4 5 8 6 3

5 Kruskal’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the next shortest
edge which does not create a cycle ED 2 AB 3 A F B C D E 2 7 4 5 8 6 3

6 Kruskal’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the next shortest
edge which does not create a cycle ED 2 AB 3 CD 4 (or AE 4) A F B C D E 2 7 4 5 8 6 3

7 Kruskal’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the next shortest
edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 A F B C D E 2 7 4 5 8 6 3

8 Kruskal’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the next shortest
edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 A F B C D E 2 7 4 5 8 6 3

9 Kruskal’s Algorithm B C 3 6 8 A D F 7 5 4 2 E All vertices have been
connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 A F B C D E 2 7 4 5 8 6 3

10 Example Prim’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select any vertex A
Select the shortest edge connected to that vertex AB 3 A F B C D E 2 7 4 5 8 6 3

11 Prim’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the shortest
edge connected to any vertex already connected. AE 4 A F B C D E 2 7 4 5 8 6 3

12 Prim’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the shortest
edge connected to any vertex already connected. ED 2 A F B C D E 2 7 4 5 8 6 3

13 Prim’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the shortest
edge connected to any vertex already connected. DC 4 A F B C D E 2 7 4 5 8 6 3

14 Prim’s Algorithm B C 3 6 8 A D F 7 5 4 2 E Select the shortest
edge connected to any vertex already connected. EF 5 A F B C D E 2 7 4 5 8 6 3

15 Prim’s Algorithm B C 3 6 8 A D F 7 5 4 2 E All vertices have been
connected. The solution is AB 3 AE 4 ED 2 DC 4 EF 5 Total weight of tree: 18 A F B C D E 2 7 4 5 8 6 3

16 Some points to note Both algorithms will always give solutions with the same length. They will usually select edges in a different order. Occasionally they will use different edges – this may happen when you have to choose between edges with the same length. In this case there is more than one minimum connector for the network.

17 Kruskal Prim מתחילים בכך שכל קדקוד גרף הוא עץ
כל פעם נוסיף קשת בעלת משקל מינימלי המחברת בין שני עצים שונים מסיימים כאשר הוספנו |V|-1 צלעות זמן ריצה O(E logV) Prim מתחילים מקדקוד רנדומלי R שהוא יהיה שורש של MST כל פעם מוסיפים קדקוד חדש v לעץ פורש כך שהקשת המחברת את v עם MST היא בעלת משקל מינימלי מסיימים כאשר הוספנו |V|-1 צלעות זמן ריצה O(E logV) שאלה: האם נכון להגיד שאם בגרף יש שתי צלעות לפחות עם משקל זהה שמשתתפות באותו מעגל, אז יש לגרף הזה יותר מעץ פורש מינימלי אחד? שאלה: האם נכון להגיד שאם בגרף יש שתי צלעות לפחות עם משקל זהה, אז יש לגרף הזה יותר מעץ פורש מינימלי אחד? תשובה: לא. לדוגמא נקח גרף שהוא מעגל עם שלוש צלעות עם משקלות 1,1,ו 2. ברור שלגרף הזה יש MST יחיד, שבמקרה הזה גם מכיל את שתי הצלעות עם משקל זהה. תשובה: לא. אם גרף הוא עץ כאשר לכל הצלעות אותו משקל (דוגמא קיצונית), הרי ברור שיש לגרף הזה עץ פורש יחיד

18 שאלה 1


הורד את "ppt "יסודות מבני נתונים תרגול 13: עץ פורש מינימלי - Minimum Spanning Tree (MST)

מצגות קשורות


מודעות Google