Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimum Spanning Tree.

Similar presentations


Presentation on theme: "Minimum Spanning Tree."— Presentation transcript:

1 Minimum Spanning Tree

2 Difference between Trees and Graphs

3 Difference between Trees and Graphs

4 Difference between Trees and Graphs

5 Graph Applications

6 Spanning Tree

7 Problem: Laying Telephone Wire
Central office

8 Wiring: Naïve Approach
Central office Expensive!

9 Wiring: Better Approach
Central office Minimize the total length of wire connecting the customers 9

10 Spanning Trees A spanning tree of an undirected graph G is a subgraph of G that is a tree containing all the vertices of G. If there are N vertices in a graph then there are n-1 edges in a spanning Tree In a weighted graph, the weight of a subgraph is the sum of the weights of the edges in the subgraph.

11 Spanning Trees Graph A Some Spanning Trees from Graph A or or or

12 An undirected graph and its minimum spanning tree.
A minimum spanning tree (MST) for a weighted undirected graph is a spanning tree with minimum weight. The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph. Graph Minimum Spanning Tree 7 2 2 5 3 3 4 1 1 An undirected graph and its minimum spanning tree.

13 Minimum Spanning Tree A tree (a connected graph with no cycles) which connects all the nodes together is called a Spanning Tree For any connected graph with n nodes, each spanning tree will have n - 1 arcs The Minimum Spanning Tree is the one with the Minimum weight

14 Algorithms for Obtaining the Minimum Spanning Tree
Kruskal's Algorithm Prim's Algorithm

15 Kruskal's Algorithm This algorithm creates a forest of trees.
Initially the forest consists of n single node trees (and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees together. If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.

16 Kruskal’s Algorithm Aim: To find a minimum spanning tree for a connected graph with n nodes: Step 1: . The forest is constructed - with each node in a separate tree Step 2: The edges are placed in a priority queue. Step 3: Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together. Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.

17 Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H 3 2 J

18 4 1 A B A D 4 4 B C B D 4 10 2 B C B J C E 4 2 1 1 5 C F D H A 4 E F 1 6 2 2 D J E G D 3 10 5 G 3 5 F G F I 5 6 3 4 3 4 I G I G J H 3 2 J 2 3 H J I J

19 Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize) 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

20 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

21 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

22 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

23 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

24 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

25 Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3
I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

26 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

27 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

28 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

29 Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3
I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

30 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

31 Minimum Spanning Tree Complete Graph 4 B C 4 B C 4 4 2 1 2 1 A E A 4 F
D 2 D 3 10 G 5 G 3 5 6 3 4 I I H H 3 2 3 J 2 J

32 Kruskal's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

33 Kruskal's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

34 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

35 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

36 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

37 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

38 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

39 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

40 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

41 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

42 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

43 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

44 Algorithm: Kruskal’s Lines 1–3 initialize the set A to the empty set and create |V| trees, one containing each vertex. For loop in lines 5–8 examines edges in order of weight, from lowest to highest. The loop checks, for each edge (u,v), whether the endpoints u and v belong to the same tree. If they do, then the edge (u,v) cannot be added to the forest without creating a cycle, and the edge is discarded. Otherwise, the two vertices belong to different trees. In this case, line 7 adds the edge (u,v) to A, and line 8 merges the vertices in the two trees.

45 Analysis: Kruskal’s Total Running Time = O(E log V)
O(V) O(E log E) O(E) Initializing the set A takes O(1) time. Sorting edges takes O(E log E) or O(n logn) FIND-SET and UNION operations take O(E) time. Along with |V| MAKE-SET operations, these take a total of O((V+E)α(V)) time, where α is the very slowly growing function. O((V+E)α(V)) O((E+E)α(V)) as |E| ≥ |V| -1 O(Eα(V)) O(E log V) as α (|V| ) =O( log V) Total Running Time = O(E log V)

46 Prim’s Algorithm This algorithm starts with one node.
It then, one by one, adds a node that is unconnected to the new graph, each time selecting the node whose connecting edge has the smallest weight out of the available nodes’ connecting edges.

47 Prim’s Algorithm Aim: To find a minimum spanning tree T
Step 1: Select any node to be the first node of T. Step 2: Consider the arcs which connect nodes in T to nodes outside T. Pick the one with minimum weight. Add this arc and the extra node to T. (If there are two or more arcs of minimum weight, choose any one of them.) Step 3: Repeat Step 2 until T contains every node of the graph.

48 Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H 3 2 J

49 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

50 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

51 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

52 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

53 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

54 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

55 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

56 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

57 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

58 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

59 Complete Graph Minimum Spanning Tree 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1
D 3 2 10 D 5 G G 5 6 3 3 4 I H I H 3 2 J 3 2 J

60 Prim's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11
the root vertex

61 Prim's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

62 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

63 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

64 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

65 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

66 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

67 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

68 a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

69 Algorithm: Prim’s Lines 1–5 set the key of each vertex to ∞ (except for the root r, whose key is set to 0 so that it will be the first vertex processed), set the parent of each vertex to NIL, and initialize the min priority queue Q to contain all the vertices. Line 6 runs a loops while Queue doesn’t get empty i.e. we extract all vertices from the queue to make a final solution tree. Line 7 extract-min(Q) extracts the minimum from the queue and stores in vertex u. Line 8 to ownward it checks all adjacent edges of vertex u, one by one, and see if it is in the queue and its edge with u and v vertices have weight less than v.key which means lowest weight edge from vertex v. If both conditions are true then make u then parent of vertex v and update v minimum weight edge to weight of u and v vertices edge.

70 Analysis: Prim’s Total Running Time = O(E log V) O(V) O(V) O(V log V)
O( log V) O(E) O(E log V) O(log V) Perform lines 1–5 in O(V) time The body of the while loop executes |V| times, and since each EXTRACT-MIN operation takes O(log V)time, the total time for all calls to EXTRACT-MIN is O(V log V). The for loop in lines 8–11 executes O(E)times, within the for loop, we can implement the test for membership in Q in line 9 in constant time by keeping a bit for each vertex that tells whether or not it is in Q, and updating the bit when the vertex is removed from Q. The assignment in line 11 involves an implicit DECREASE-KEY operation on the min-heap, which a binary min-heap supports in O(log V)time. Thus, the total time for Prim’s algorithm is O(V log V + E log V) = O(E log V) If implemented by binary min-heap then running time is same as Kruskal algorithm i.e. O(E log V).

71 Comparison Prim’s Algorithm is a quick way of finding the Minimum Spanning Tree (or minimum connector) This algorithm is said to be “greedy” since it picks the immediate best option available without taking into account the long-term consequences of the choices made. Kruskal’s Algorithm may also be used to find a minimum spanning tree, but this considers the weights themselves rather than the connecting points.


Download ppt "Minimum Spanning Tree."

Similar presentations


Ads by Google