Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.

Similar presentations


Presentation on theme: "Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are."— Presentation transcript:

1 Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are based on “Algorithms in C” by R. Sedgewick 1

2 Weighted Graphs Each edge has a weight. Example: a transportation network (roads, railroads, subway). The weight of each road can be: – The length. – The expected time to traverse. – The expected cost to build. Example: in a computer network, the weight of each edge (direct link) can be: – Latency. – Expected cost to build. 2 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

3 Minimum-Cost Spanning Tree (MST) Important problem in weighted graphs: finding a minimum-cost spanning tree: A tree that: – Connects all vertices of the graph. – Has the smallest possible total weight of edges. 3 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25 0 1 7 2 5 3 4 6 10 20 30 20 15 10 15

4 Minimum-Cost Spanning Tree (MST) We will only consider algorithms that compute the MST for undirected graphs. We will allow edges to have negative weights. Warning: later in the course (when we discuss Dijkstra's algorithm) we will need to make opposite assumptions: – Allow directed graphs. – Not allow negative weights. 4 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25 0 1 7 2 5 3 4 6 10 20 30 20 15 10 15

5 Prim's Algorithm - Overview Prim's algorithm: – Start from an tree that contains a single vertex. – Keep growing that tree, by adding at each step the shortest edge connecting a vertex in the tree to a vertex outside the tree. As you see, it is a very simple algorithm, when stated abstractly. However, we have several choices regarding how to implement this algorithm. We will see three implementations, with significantly different properties from each other. 5

6 Prim's Algorithm - Simple Version Assume an adjacency matrix representation. – Each vertex is a number from 0 to V-1. – We have a V*V adjacency matrix ADJ, where: ADJ[v][w] is the weight of the edge connecting v and w. – If v and w are not connected, ADJ[v][w] = infinity. 6

7 Prim's Algorithm - Simple Version 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 7

8 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 8 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

9 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 9 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

10 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 10 0 1 7 2 5 3 4 6 20 30 20 15 30 10 20 15 25

11 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 11 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

12 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 12 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

13 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 13 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

14 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 14 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

15 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 15 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

16 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 16 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

17 Example 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. 17 0 1 7 2 5 3 4 6 10 20 30 20 15 10 15

18 Prim's Algorithm - Simple Version 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. Running time? 18

19 Prim's Algorithm - Simple Version 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. Most naive implementation: time ??? – Every time we add a new vertex and edge, go through all edges again, to identify the next edge (and vertex) to add. 19

20 Prim's Algorithm - Simple Version 1.Start by adding vertex 0 to the MST (minimum-cost spanning tree). 2.Repeat until all vertices have been added to the tree: 3.From all edges connecting vertices from the current tree to vertices outside the current tree, select the smallest edge. 4.Add that edge to the tree, and also add to the tree the non-tree vertex of that edge. Most naive implementation: time O(VE). – Every time we add a new vertex and edge, go through all edges again, to identify the next edge (and vertex) to add. 20

21 Prim's Algorithm - Dense Graphs A dense graph is nearly full, and thus has O(V 2 ) edges. – For example, think of a graph where each vertex has at least V/2 neighbors. Just reading the input (i.e., looking at each edge of the graph once) takes O(V 2 ) time. Thus, we cannot possibly compute a minimum-cost spanning tree for a dense graph in less than O(V 2 ) time. Prim's algorithm can be implemented so as to take O(V 2 ) time, which is optimal for dense graphs. 21

22 Prim's Algorithm - Dense Graphs Again, assume an adjacency matrix representation. Every time we add a vertex to the MST, we need to update, for each vertex W not in the tree: – The smallest edge wt[W] connecting it to the tree. – If no edge connects W to the tree, wt[W] = infinity. – The tree vertex fr[W] associated with the edge whose weight is wt[W]. These quantities can be updated in O(V) time when adding a new vertex to the tree. Then, the next vertex to add is the one with the smallest wt[W]. 22

23 Example Every time we add a vertex to the MST, we need to update, for each vertex W not in the tree: – The smallest edge wt[W] connecting it to the tree. – If no edge connects W to the tree, wt[W] = infinity. – The tree vertex fr[W] associated with the edge whose weight is wt[W]. When we add a vertex to the MST, we mark it as "processed" by setting: st[W] = fr[W]. 23 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25

24 Prim's Algorithm: Dense Graphs void GRAPHmstV(Graph G, int st[], double wt[]) { int v, w, min; for (v = 0; v V; v++) // initialize datastructures: st, wt, fr { st[v] = -1; fr[v] = v; wt[v] = maxWT; } st[0] = 0; // start with node 0 wt[G->V] = maxWT; // symbolic node G->V used as a sentinel to stop select nodes for (min = 0; min != G->V; ) { // min is the next node to add to the MST; start with 0 v = min; st[min] = fr[min]; // add min to the MST //loop will: update nodes outside of MST+v, find closest next node, min. for (w = 0, min = G->V; w V; w++) //for every node, w, in G; closest=sentinel if (st[w] == -1) // w is not in MST { if (G->adj[v][w] < wt[w]) // the added v node, brings w closer to the tree { wt[w] = G->adj[v][w]; fr[w] = v; } // update wt and fr for w if (wt[w] < wt[min]) min = w; // track the minimum weight edge from cut }}} 24

25 Prim's Algorithm: Dense Graphs // same code as above. Only comments were removed. void GRAPHmstV(Graph G, int st[], double wt[]) { int v, w, min; for (v = 0; v V; v++) { st[v] = -1; fr[v] = v; wt[v] = maxWT; } st[0] = 0; wt[G->V] = maxWT; for (min = 0; min != G->V; ) { v = min; st[min] = fr[min]; for (w = 0, min = G->V; w V; w++) if (st[w] == -1) { if (G->adj[v][w] < wt[w]) { wt[w] = G->adj[v][w]; fr[w] = v; } if (wt[w] < wt[min]) min = w; }}} 25

26 Prim's Algorithm - Dense Graphs Running time: ??? 26

27 Prim's Algorithm - Dense Graphs Running time: O(V 2 ) Optimal for dense graphs. 27

28 Prim's Algorithm for Sparse Graphs A sparse graph is one that is not dense. This is somewhat vague. If you want a specific example, think of a case where the number of edges is linear to the number of vertices. – For example, if each vertex can only have between 1 and 10 neighbors, than the number of edges can be at most ??? 28

29 Prim's Algorithm for Sparse Graphs A sparse graph is "one that is not dense". This is somewhat vague. If you want a specific example, think of a case where the number of edges is linear to the number of vertices. – For example, if each vertex can only have between 1 and 10 neighbors, than the number of edges can be at most (10*V)/2. 29

30 Prim's Algorithm for Sparse Graphs If we use an adjacency matrix representation, then we can never do better than O(V 2 ) time. Why? 30

31 Prim's Algorithm for Sparse Graphs If we use an adjacency matrix representation, then we can never do better than O(V 2 ) time. Why? – Because just scanning the adjacency matrix to figure out where the edges are takes O(V 2 ) time. – The adjacency matrix itself has size V*V. 31

32 Prim's Algorithm for Sparse Graphs If we use an adjacency matrix representation, then we can never do better than O(V 2 ) time. Why? – Because just scanning the adjacency matrix to figure out where the edges are takes O(V 2 ) time. – The adjacency matrix itself has size V*V. We have already seen an implementation of Prim's algorithm, using adjacency matrices, which achieves O(V 2 ) running time. For sparse graphs, if we want to achieve better running time than O(V 2 ), we have to switch to an adjacency lists representation. 32

33 Prim's Algorithm for Sparse Graphs Quick review: what exactly is an adjacency lists representation? 33

34 Prim's Algorithm for Sparse Graphs Quick review: what exactly is an adjacency lists representation? – Each vertex is a number between 0 and V (same as for adjacency matrices). – The adjacency information is stored in an array ADJ of lists. – ADJ[w] is a list containing all neighbors of vertex w. What is the sum of length of all lists in the ADJ array? 34

35 Prim's Algorithm for Sparse Graphs Quick review: what exactly is an adjacency lists representation? – Each vertex is a number between 0 and V (same as for adjacency matrices). – The adjacency information is stored in an array ADJ of lists. – ADJ[w] is a list containing all neighbors of vertex w. What is the sum of length of all lists in the ADJ array? – 2*E (each edge is included in two lists). 35

36 Prim's Algorithm for Sparse Graphs For sparse graphs, we will use an implementation of Prim's algorithm based on: – A graph representation using adjacency lists. – A priority queue (heap) containing the set of vertices on the fringe with the distance as the key. A vertex w will be included in this priority queue if there is a vertex v in the tree and (v,w) is the shortest edge connecting w to a vertex in the tree. The priority queue will hold pairs [weight,vertex] where the weight will be used as the key (the smaller the weight, the higher the priority) We will continue to use arrays fr and st. 36

37 Prim's Algorithm - PQ Version Initialize a priority queue, P, v = vertex 0 Add [0,v] to P While (P not empty) – [v_weight, v] = remove_minimum(P) – Add v to the spanning tree: st[v] = fr[v] – For each edge (v, w) adjacent to v: If w is not in P (i.e. fr[w] == -1), insert [weight of edge(v,w), w] to P. If [w_weight, w] is in P, we have cases: – w in the spanning tree => do nothing – w_weight ≥ weight of edge (v,w) => do nothing – w_weight ≤ weight of edge (v,w) => mark v as the closest vertex in the tree to w: set fr[w] = v and update [w_weight, w] to [weight of (v,w), w] in P 37

38 Prim's Algorithm - PQ Version runtime Initialize a priority queue, P, v = vertex 0 O(1) Add [0,v] to P O(1) While (P not empty) O(V) – [v_weight, v] = remove_minimum(P) O(lgV) O(VlgV) – Add v to the spanning tree: st[v] = fr[v] O(1) – For each edge (v, w) adjacent to v: O(E) If w is not in P, insert [weight of edge(v,w), w] to P. O(lg V) If [w_weight, w] is in P, we have cases: – w in the spanning tree => do nothing – w_weight ≥ weight of edge (v,w) => do nothing – w_weight ≤ weight of edge (v,w) => mark v as closest vertex in the tree to w: set fr[w] = v and update [w_weight, w] to [weight of (v,w), w] in P O(lgV) 38 O(E lgV)

39 Prim's Algorithm - PQ Version Running time??? 39

40 Prim's Algorithm - PQ Version Running time? O(E lg V). Why? 40

41 Prim's Algorithm - PQ Version Running time? O(E lg V). Why? Let's look at this piece of the pseudocode: For each edge (v, w) adjacent to v: If w is not in P (i.e. fr[w] == -1), insert [weight of edge(v,w), w] to P. If [w_weight, w] is in P, we have cases: – w in the spanning tree => do nothing – w_weight ≥ weight of edge (v,w) => do nothing – w_weight ≤ weight of edge (v,w) => mark v as the closest vertex in the tree to w: set fr[w] = v and update [w_weight, w] to [weight of (v,w), w] in P The number of iterations for this for loop will be 2*E (each edge is considered twice: ffor each vertex, iterate over all adjacent edges). At each iteration, we spend at most log(V) time. 41

42 Kruskal's Algorithm 42

43 Kruskal's Algorithm: Overview Prim's algorithm works with a single tree, such that: – First, the tree contains a single vertex. – The tree keeps growing, until it spans the whole tree. Kruskal's algorithm works with a forest (a set of trees). – Initially, each vertex in the graph is its own tree. – We keep merging trees together, until we end up with a single tree. 43

44 Kruskal's Algorithm: Overview 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F. As in Prim's algorithm, the abstract description is simple, but we need to think carefully about how exactly to implement these steps. 44

45 Kruskal's Algorithm: An Example 45 0 1 7 2 5 3 4 6 10 20 30 20 15 30 10 20 15 25 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F.

46 Kruskal's Algorithm: An Example 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F. 46 10 0 1 7 2 5 3 4 6 20 30 20 15 30 20 15 25

47 Kruskal's Algorithm: An Example 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F. 47 10 0 1 7 2 5 3 4 6 20 30 20 15 30 20 15 25

48 Kruskal's Algorithm: An Example 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F. 48 10 0 1 7 2 5 3 4 6 20 30 20 15 30 20 15 25

49 Kruskal's Algorithm: An Example 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F. 49 10 0 1 7 2 5 3 4 6 20 30 20 15 30 20 15 25

50 Kruskal's Algorithm: An Example 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F. 50 10 0 1 7 2 5 3 4 6 20 30 20 15

51 Kruskal's Algorithm: Simple Implementation Assume graphs are represented using adjacency lists. 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. – How? We will use the same representation for forests that we used for union-find. – We will have an id array, where each vertex will point to its parent. – The root of each tree will be the ID for that tree. Time it takes for this step ??? 51

52 Kruskal's Algorithm: Simple Implementation Assume graphs are represented using adjacency lists. 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. – How? We will use the same representation for forests that we used for union-find. – We will have an id array, where each vertex will point to its parent. – The root of each tree will be the ID for that tree. Time it takes for this step? O(V) 52

53 Kruskal's Algorithm: Simple Implementation 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. – Initialize F to some edge with infinite weight. – For each edge F' connecting any two vertices (v, w): Determine if v and w belong to two different trees in the forest. If so, update F to be the shortest of F and F'. 4.Connect those two trees into a single tree using edge F. 53

54 Kruskal's Algorithm: Simple Implementation 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. – Initialize F to some edge with infinite weight. – For each edge F' connecting (v, w): Determine if v and w belong to two different trees in the forest. HOW? If so, update F to be the shortest of F and F'. 4.Connect those two trees into a single tree using edge F. HOW? 54

55 Kruskal's Algorithm: Simple Implementation 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. – Initialize F to some edge with infinite weight. – For each edge F' connecting (v, w): Determine if v and w belong to two different trees in the forest. HOW? By comparing find(v) with find(w). Time: If so, update F to be the shortest of F and F'. 4.Connect those two trees into a single tree using edge F. HOW? By calling union(v, w). Time: 55

56 Kruskal's Algorithm: Simple Implementation 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. – Initialize F to some edge with infinite weight. – For each edge F' connecting (v, w): Determine if v and w belong to two different trees in the forest. HOW? By comparing find(v) with find(w). Time: O(lg V) If so, update F to be the shortest of F and F'. 4.Connect those two trees into a single tree using edge F. HOW? By calling union(v, w). Time: O(1) 56

57 Kruskal's Algorithm: Simple Implementation 2.Repeat until the forest contains a single tree: Total time for all iterations: 3.Find the shortest edge F connecting two trees in the forest. Time: – Initialize F to some edge with infinite weight. – For each edge F' connecting (v, w): Determine if v and w belong to two different trees in the forest. HOW? By comparing find(v) with find(w). Time: O(lg V) If so, update F to be the shortest of F and F'. 4.Connect those two trees into a single tree using edge F. HOW? By calling union(v, w). Time: O(1) 57

58 Kruskal's Algorithm: Simple Implementation 2.Repeat until the forest contains a single tree: Total time for all iterations: O(V*E*lg(V)) 3.Find the shortest edge F connecting two trees in the forest. Time: O(E*lg(V)) – Initialize F to some edge with infinite weight. – For each edge F' connecting (v, w): Determine if v and w belong to two different trees in the forest. HOW? By comparing find(v) with find(w). Time: O(lg V) If so, update F to be the shortest of F and F'. 4.Connect those two trees into a single tree using edge F. HOW? By calling union(v, w). Time: O(1) 58

59 Running Time for Simple Implementation 1.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 2.Repeat until the forest contains a single tree: 3.Find the shortest edge F connecting two trees in the forest. 4.Connect those two trees into a single tree using edge F. Running time for simple implementation: O(V*E*lg(V)). 59

60 Kruskal's Algorithm: Faster Version 60 1.Sort all edges, save result in array K. 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 3.For each edge F in K (in ascending order). 4.If F is connecting two trees in the forest: 5.Connect the two trees with F. 6.If the forest is left with a single tree, break (we are done).

61 Kruskal's Algorithm: Faster Version 61 1.Sort all edges, save result in array K. Time? 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. Time? 3.For each edge in K (in ascending order). Time? 4.If F is connecting two trees in the forest: Time? 5.Connect the two trees with F. Time? 6.If the forest is left with a single tree, break (we are done).

62 Kruskal's Algorithm: Faster Version 62 1.Sort all edges, save result in array K. Time: O(E lg E) 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. Time: O(V) 3.For each edge in K (in ascending order). Time: O(E lg V) 4.If F is connecting two trees in the forest: Time? O(lg V), two find operations 5.Connect the two trees with F. Time: O(1), union operation 6.If the forest is left with a single tree, break (we are done). Overall running time???

63 Kruskal's Algorithm: Faster Version 63 1.Sort all edges, save result in array K. Time: O(E lg E) 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. Time: O(V) 3.For each edge in K (in ascending order). Time: O(E lg V) 4.If F is connecting two trees in the forest: Time? O(lg V), two find operations 5.Connect the two trees with F. Time: O(1), union operation 6.If the forest is left with a single tree, break (we are done). Overall running time: O(E lg E) which is also O(E lgV) E < V 2, so lg E < 2 lg V, so O(lg E) = O(lg V). Note that the O(E lg E) comes from sorting and cannot be improved.

64 Kruskal's Algorithm - PQ Version In the previous implementation, we sort edges at the beginning. – This takes O(E lg E) time, which dominates the running time of the algorithm. – Thus, the entire algorithm takes O(E lg E) time. We can do better if, instead of sorting all edges at the beginning, we instead insert all edges into a priority queue. – How long does that take, if we use a heap? 64

65 Kruskal's Algorithm - PQ Version In the previous implementation, we sort edges at the beginning. – This takes O(E lg E) time, which dominates the running time of the algorithm. – Thus, the entire algorithm takes O(E lg E) time. We can do better if, instead of sorting all edges at the beginning, we instead insert all edges into a priority queue. – How long does that take, if we use a heap? – O(E) time. We can also do better if, for the find operation, we use the most efficient version discussed in the textbook. – That version flattens paths that it traverses. – Running time: O(lg* V). – lg* - iterated logarithm. lg*(V) is the number of times we need to apply lg to V to obtain 1. 65

66 Detour: lg* lg*(2) = ? lg*(4) = ? lg*(16) = ? 66

67 Detour: lg* lg*(2) = 1, because lg(2) = 1. lg*(4) = 2, because lg(lg(4)) = 1. lg*(16) = 3, because lg(lg(lg(16))) = 1. lg*(???) = 4 lg*(???) = 5 67

68 Detour: lg* lg*(2) = 1, because lg(2) = 1. lg*(4) = 2, because lg(lg(4)) = 1. lg*(16) = 3, because lg(lg(lg(16))) = 1. lg*(2 16 ) = 4, because lg(2 16 ) = 16. (2 16 = 65536) lg*(2 65536 ) = 5, because lg(2 65536 ) = 65536. I don't expect we will get to deal with data sizes larger than 2 65536 in our lifetime. Thus, lg* effectively has 5 as an upper bound, so for practical purposes we can treat it as a constant. 68

69 Kruskal's Algorithm - PQ Version 69 1.Initialize a heap with the edges (using weight as key). 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 3.While (true). 4.F = remove_mininum(heap). 5.If F is connecting two trees in the forest: 6.Connect the two trees with F. 7.If the forest is left with a single tree, break (we are done).

70 Kruskal's Algorithm - PQ Version 70 1.Initialize a heap with the edges (using weight as key). Time? 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. Time? 3.While (true). Time? 4.F = remove_mininum(heap). Time? 5.If F is connecting two trees in the forest: Time? 6.Connect the two trees with F. Time? 7.If the forest is left with a single tree, break (we are done). Overall running time?

71 Kruskal's Algorithm - PQ Version 71 1.Initialize a heap with the edges (using weight as key). Time? O(E) 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. Time? O(V) 3.While (true). Time? X lg V. X: number of iterations. 4.F = remove_mininum(heap). Time? O(lg E) 5.If F is connecting two trees in the forest: Time? O(lg* V), find operation 6.Connect the two trees with F. Time? O(1) 7.If the forest is left with a single tree, break (we are done). Overall running time? E + X lg V. E < V 2, so lg E < 2 lg V, so O(lg E) = O(lg V).

72 Kruskal's Algorithm - PQ Version 72 1.Initialize a heap with the edges (using weight as key). 2.Initialize a forest (a collection of trees), by defining each vertex to be its own separate tree. 3.While (true). Time? X lg V. X: number of iterations. 4.F = remove_mininum(heap). Time? O(lg E) 5.If F is connecting two trees in the forest: 6.Connect the two trees with F. Time? O(1) 7.If the forest is left with a single tree, break (we are done). Overall running time? E + X lg V. – X is the number of edges in the graph with weight <= the maximum weight of an edge in the final MST. – E < V 2, so lg E < 2 lg V, so O(lg E) = O(lg V).


Download ppt "Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are."

Similar presentations


Ads by Google