Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 : Graphs Part II (Minimum Spanning Trees)

Similar presentations


Presentation on theme: "Chapter 9 : Graphs Part II (Minimum Spanning Trees)"— Presentation transcript:

1 Chapter 9 : Graphs Part II (Minimum Spanning Trees)
CE 221 Data Structures and Algorithms Chapter 9 : Graphs Part II (Minimum Spanning Trees) Text: Read Weiss, §9.5 Izmir University of Economics 1

2 Izmir University of Economics
Minimum Spanning Tree A minimum spanning tree of an undirected graph G is a tree formed from graph edges that connects all the vertices of G at a lowest cost. # of edges in a MST is |V|-1. If an edge e not in a MST T is added, acycle is created. The removal of an edge from a cycle reinstates the spanning tree property. As MST is created, if minimum cost edge not creating cycles is selected, then it’s a MST. So, greed works for MST problem. Izmir University of Economics 2

3 Izmir University of Economics
Prim’s Algorithm Proceed in stages. In each stage, find a new vertex to add to the tree already formed by choosing an edge (u, v) such that the cost of (u, v) is the smallest among all the edges where u is in the tree and v is not. Prim’s algorithm for MSTs is essentially identical to Dijkstra’s for shortest paths. Update rule is even simpler: after a vertex is picked, for each unknown w adjacent to v, dw=min(dw, cv,w). Izmir University of Economics 3

4 Prim’s Example By Joe James
Minimum Spanning Tree Prim’s Example By Joe James

5 Prim’s Algoritim for MST

6 Time Complexity of Prim’s Algoritim

7 Prim’s Algoritim for MST

8 Prim’s Algoritim for MST

9 Prim’s Algoritim for MST

10 Prim’s Algoritim for MST

11 Prim’s Algoritim for MST

12 Prim’s Algoritim for MST

13 Prim’s Algoritim for MST

14 Prim’s Algoritim for MST

15 Prim’s Algoritim for MST

16 Prim’s Algoritim for MST

17 Prim’s Algoritim for MST
Take away all unused edges, So the final MST will be obtained

18 Prim’s Algorithm – Example I
Izmir University of Economics 18

19 Prim’s Algorithm – Example II
Izmir University of Economics 19

20 Prim’s Algorithm – Example III
Izmir University of Economics 20

21 Prim’s Algorithm – Example IV
Izmir University of Economics 21

22 Prim’s Algorithm – Time Complexity
The running time is O(|V|2) (|V| iterations in each of which a sequential scan is performed to find the minimum cost unknown edge) without heaps, which is optimal for dense graphs. O(|E|*log|V|) (an underlying binary heap with |V| elements is used, and |V| deleteMin (for picking min cost vertex) and at most |E| decreaseKey (for updates) are performed each taking O(log|V|) time) using binary heaps,which is good for sparse graphs Izmir University of Economics 22

23 Izmir University of Economics
Kruskal’s Algorithm A second greedy strategy is continually to select the edges in order of smallest weight and accept an edge if it does not cause a cycle.Formally, Kruskal's algorithm maintains a forest - a collection of trees. Initially, there are |V| single-node trees. Adding an edge merges two trees into one. When the algorithm terminates, there is only one tree, and this is the minimum spanning tree. Izmir University of Economics 23

24 Kruskal’s Example By Joe James
Minimum Spanning Tree Kruskal’s Example By Joe James

25 Kruskal’s Algorithm for MST

26 Time Complexity of Kruskal’s Algoritim

27 First: Sort all edge costs

28 2nd: Chose the Edge of Lowest Cost

29 Chose Other Edges of Lowest Costs and join them

30 Chose Edges of Lowest Costs and join them

31 Now Finalize the MST

32 An example run of Kruskal’s Algorithm
Izmir University of Economics 32

33 Kruskal’s Algorithm: Pseudo Code I
// Create the list of all edges E solution = { } // will include the list of MST edges while ( more edges in E) do // Selection select minimum weight(cost) edge remove edge from E // Feasibility if (edge closes a cycle with solution so far) then reject edge else add edge to solution // Solution check if |solution| = |V | - 1 return solution Izmir University of Economics 33

34 Kruskal’s Algorithm: Pseudo Code II
The worst-case running time of this algorithm is O(|E|log|E|), which is dominated by the heap operations. Notice that since |E| = O(|V|2), this running time is actually O(|E| log |V|). In practice, the algorithm is much faster. Izmir University of Economics 34

35 Izmir University of Economics
Homework Assignments 9.15, 9.16 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics 35


Download ppt "Chapter 9 : Graphs Part II (Minimum Spanning Trees)"

Similar presentations


Ads by Google