













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














We now discuss another of the major strategies used to design algorithms. This is the greedy strategy. Greedy is best summarized as follows: “Make the choice that looks best at present and never change your mind”. Algorithms designed according to the greedy strategy are applied to problems in which the solution can be viewed as a sequence of decisions. In a greedy algorithm, once a decision has been made it is never changed. Other algorithms use backtracking to reconsider some decisions already made and explore alternate solutions. There is a class of problems for which the algorithm designed according to the greedy strategy can be shown to produce an optimal answer. We shall investigate these problems and close the chapter with an example in which the greedy algorithm produces a good answer that occasionally is optimal. The change making problem is one of the simplest for which a good greedy algorithm exists and can be shown to produce optimal results. The goal is to make change for a given amount using the minimum number of coins. For example, consider the problem of giving change for 93 cents, using the standard U.S. coins.
We now must consider the characteristics of an optimization problem, subject to constraints. We have two important terms:
We now illustrate the execution of Prim’s algorithm on this example. The graph with no edges is at left. The graph G has four edges, each with weight as follows. Weight ([A, B]) = 1 Weight ([A, C]) = 5 Weight ([A, D]) = 2 Weight ([C, D]) = 3 Start the algorithm with VT = {A}. The shortest edge connecting A to a vertex not in VT is the edge [A, B] with edge weight 1. At this point we have the following VT = {A, B} ET = { [A, B] } WT = 1 Now, the shortest edge connecting a vertex in set {A, B} to a vertex in V – {A, B} = { C, D} is the edge [A, D]. We add this to the subtree with the following results. VT = {A, B, D} ET = { [A, B], [A, D] } WT = 3 Finally, the shortest edge connecting vertex C to a vertex in the set {A, B, D} is the edge [C, D] with weight 3. We add this edge to the subtree to get the following spanning tree. VT = {A, B, C, D} ET = { [A, B], [A, D], [C, D] } WT = 6 This is our spanning tree.
Does Prim’s Algorithm Work? We now ask the simple question –given a connected weighted (N, M)-graph, does Prim’s algorithm always yield a minimum spanning tree? Before beginning a proof by induction we note that we shall assume that the graph has M N, causing the graph to contain a cycle. If the graph is a connected (N, N – 1) graph it is already a tree and Prim’s algorithm will return the graph itself, it being the only possible spanning tree of itself. If M < (N – 1), it is a trivial proof to show that the graph cannot be connected. We also assume that N 3, as the only connected graph on two vertices is already a tree and no graph on one vertex can really be called connected. Certainly, the spanning tree of a one-vertex graph is unique and trivial. Prim’s algorithm begins with a tree T 0 having one vertex and no edges, and repeatedly adds an edge to generate a sequence of trees T 1 , T 2 , …, TN-1, TN. We prove by induction that each of the subtrees TI, 1 I (N – 1), is a subgraph of some minimum spanning tree, and thus that the final tree TN-1 is itself a minimum spanning tree. The basis for induction is that the single vertex tree T 0 must be a part of any minimum spanning tree as any minimum spanning tree must contain all of the vertices. Now assume that the tree TI-1 on (I – 1) vertices is a part of some minimum spanning tree. We need to prove that the tree TI formed from TI-1 by Prim’s algorithm is also a part of a minimum spanning tree. We prove this by contradiction – assuming that no minimum spanning tree can contain TI and arriving at an impasse. Let ( v , u ) be a minimum weight edge from a vertex in TI-1 to a vertex not in TI-1 used by Prim’s algorithm to expand TI-1 to TI. By our assumption this edge cannot be in any spanning tree, because we have stated in the inductive hypothesis that TI-1 is a part of a minimum spanning tree and are arguing that TI-1 {( v , u )} is not. Therefore, if we add ( v , u ) to a spanning tree, we must form a cycle, as adding an edge to any tree forms a cycle. Since T {( v , u )} contains a cycle there must be another edge ( v ’, u ’) connecting a vertex
not less than ( v , u ), so removing the edge ( v ’, u ’) from T {( v , u )} produces another spanning tree of equal or lesser total weight, thus contradicting the assumption that TI is not a part of a minimum spanning tree. Thus Prim’s algorithm produces a minimum spanning tree.
Testing the Growing Tree for Cycles Up to now, we have been ignoring a very important part of these two algorithms. At each stage of both algorithms, we consider adding an edge and ask whether or not the addition of this edge will cause a cycle. This has begged the question of detecting cycles. Recall that the following code fragment is essential in the construction of the spanning tree.
EdgeCount = EdgeCount + 1 End If We now attempt to address this problem, beginning with a standard theorem on trees and an obvious corollary of that theorem. Definition: A tree is a connected acyclic graph. Theorem: The following statements are equivalent.
Here are two algorithms for use in detecting cycles while using Kruskal’s Algorithm. Algorithm ConnectedComp ( e = ( u , v )) -- Add edge e = ( u , v ) to T If e = ( u , v ) is the first edge added, create component C 1 = { u , v }. Otherwise scan all of the existing component linked lists for vertices u and v. If one component contains both u and v , reject the edge as forming a cycle. If component CJ contains one vertex (call it u ) and vertex v is not contained in any component, add vertex v to component CJ. If vertex u belongs to component CJ and vertex v belongs to component CK, then merge the two components into one component. If neither u nor v are in a component, create a new component CN and set CN = ( u , v ). Here is a slightly better algorithm. Like the above, it relies on the use of sets to represent the connected components in the graph. This algorithm begins with a graph T = (V(G), ), an (N, 0)–graph with the vertex set of the graph for which a spanning tree is to be constructed. Let V(G) = {1, 2, 3, …., (N – 1), N }. Construct the connected components for graph T. It is a set of sets, best denoted as K(G) = { {1}, {2}, {3}, …., {N – 1}, {N} }. Each set in the collection is a singleton set containing only one vertex, reflecting the fact that the graph T has no edges at this point. Denote the set representing the component containing vertex J as S(J). At the beginning, we have S(J) = {J} for 1 J N. Algorithm ConnectedComp ( e = (I, J)) -- Add edge e = (I, J) to T // WLOG (Without Loss of Generality), assume that I < J. If I S(J) Then // J S(I) also Reject Edge e = (I, J). Else Accept Edge e = (I, J) as not adding a cycle. Set both S(I) to S(I) S(J) and remove S(J). End If Spanning Trees for Unweighted Graphs It should be obvious that either Prim’s Algorithm or Kruskal’s Algorithm can be adopted to create spanning trees in unweighted graphs. For each, the modification is simple. In Prim’s, one replaces the statement “Select an edge of minimum weight between a tree node t and a fringe node u ” with the statement “Select any edge between a tree node t and a fringe node u ”. In Kruskal’s algorithm, the only requirement is to omit the sorting of edges by weight, as they all have the same weight in an unweighted graph, and begin with any listing of edges.
K = 0, EdgeCount = 0; K < 8 and EdgeCount < 6
7 S(7) and 8 S(7), so it is OK to add. ET = { (7, 8, 1) } and WT = 1. S(1) = {1}, S(2) = {2}, S(3) = {3}, S(4) = {4}, S(5) = {5}, S(6) = {6}, S(7) = {7, 8}, S(9) = {9}. K = 1, EdgeCount = 1; K < 8 and EdgeCount < 6
4 S(4) and 5 S(4), so it is OK to add. ET = { (7, 8, 1), (4, 5, 2) } and WT = 3. S(1) = {1}, S(2) = {2}, S(3) = {3}, S(4) = {4, 5}, S(6) = {6}, S(7) = {7, 8}, S(9) = {9}. K = 2, EdgeCount = 2; K < 8 and EdgeCount < 6
1 S(1) and 2 S(1), so it is OK to add. ET = { (7, 8, 1), (4, 5, 2), (1, 2, 3) } and WT = 6. S(1) = {1, 2}, S(3) = {3}, S(4) = {4, 5}, S(6) = {6}, S(7) = {7, 8}, S(9) = {9}. K = 3, EdgeCount = 3; K < 8 and EdgeCount < 6
5 S(4) and 6 S(4), so it is OK to add. ET = { (7, 8, 1), (4, 5, 2), (1, 2, 3), (5, 6, 4) } and WT = 10. S(1) = {1, 2}, S(3) = {3}, S(4) = {4, 5, 6}, S(7) = {7, 8}, S(9) = {9}. K = 4, EdgeCount = 4; K < 8 and EdgeCount < 6
1 S(1) and 3 S(1), so it is OK to add. ET = { (7, 8, 1), (4, 5, 2), (1, 2, 3), (5, 6, 4), (1, 3, 5) } and WT = 15. S(1) = {1, 2, 3}, S(4) = {4, 5, 6}, S(7) = {7, 8}, S(9) = {9}. K = 5, EdgeCount = 5; K < 8 and EdgeCount < 6
8 S(7) and 9 S(7), so it is OK to add. ET = { (7, 8, 1), (4, 5, 2), (1, 2, 3), (5, 6, 4), (1, 3, 5), (8, 9, 6) } and WT = 21. S(1) = {1, 2, 3}, S(4) = {4, 5, 6}, S(7) = {7, 8, 9}. EdgeCount = 6, so the algorithm terminates with the three sets representing components.
Another Example, This Time Connected We examine a more realistic example and apply both Prim’s and Kruskal’s algorithms. The graph is a (5, 8)–graph with numerous cycles. The spanning tree will have four edges. Prim’s Algorithm Applied to This Graph We first apply Prim’s Algorithm. To avoid the boredom of repetition, this author elects to begin the spanning tree at vertex 2. VT = {2} V – VT = {1, 3, 4, 5} Edges ( x , y ) with x VT and y V – VT are (1, 2, 4), (2, 3, 3), and (2, 4, 3). The algorithm allows us to select either of the two edges with weight 3. We arbitrarily select (2, 3, 3). We now show the spanning tree as it grows. VT = {2, 3} V – VT = {1, 4, 5} Edges ( x , y ) with x VT and y V – VT are (1, 2, 4), (1, 3, 2), (2, 4, 3), (3, 4, 2) & (3, 5, 1). The obvious choice is (3, 5, 1). The total tree weight is now 4.
Kruskal’s Algorithm Again we show the original graph. In terms that we shall use for the algorithm, we have V(G) = {1, 2, 3, 4, 5} E(G) = { (3, 5, 1), (4, 5, 1), (1, 3, 2), (1, 4, 2), (3, 4, 2), (2, 3, 3), (2, 4, 3), & (1, 2, 4). The set of components. S(1) = {1}, S(2) = {2}, S(3) = {3}, S(4) = {4}, and S(5) = {5}. The code section of interest (with the problem values supplied) is: While ( (EdgeCount < 4) And (K < 8) ) Do // |V| = 5, |E| = 8 K = K + 1
EdgeCount = EdgeCount + 1 End If End Do K = 0, EdgeCount = 0; K < 8 and EdgeCount < 4
3 S(3) and 5 S(3), so it is OK to add. ET = { (3, 5, 1) } and WT = 1. S(1) = {1}, S(2) = {2}, S(3) = {3, 5}, and S(4) = {4}.. K = 1, EdgeCount = 1; K < 8 and EdgeCount < 4
4 S(4) and 5 S(4), so it is OK to add. ET = { (3, 5, 1), (4, 5, 1) } and WT = 2. S(1) = {1}, S(2) = {2}, and S(3) = {3, 4, 5}.
K = 2, EdgeCount = 2; K < 8 and EdgeCount < 4
1 S(1) and 3 S(1), so it is OK to add. ET = { (3, 5, 1), (4, 5, 1), (1, 3, 2) } and WT = 4. S(1) = {1, 3, 4, 5} and S(2) = {2}. K = 3, EdgeCount = 3; K < 8 and EdgeCount < 4
1 S(1) and 4 S(1), so reject this edge. Nothing changes. ET = { (3, 5, 1), (4, 5, 1), (1, 3, 2) } and WT = 4. S(1) = {1, 3, 4, 5} and S(2) = {2}. K = 4, EdgeCount = 3; K < 8 and EdgeCount < 4
3 S(1) and 4 S(1), so reject this edge. Nothing changes. ET = { (3, 5, 1), (4, 5, 1), (1, 3, 2) } and WT = 4. S(1) = {1, 3, 4, 5} and S(2) = {2}. K = 5, EdgeCount = 3; K < 8 and EdgeCount < 4
2 S(2) and 3 S(2), so it is OK to add. ET = { (3, 5, 1), (4, 5, 1), (1, 3, 2), (2, 3, 3) } and WT = 7. S(1) = {1, 2, 3, 4, 5} K = 6, EdgeCount = 4. Algorithm terminates. This is one of the two solutions from Prim’s Algorithm.
K K for 1 K N. Since we have specified that v K > 0.0 and w K > 0.0 for
increasing sequence for all values of K. This ordering is essential for a solution to the fractional knapsack problem, but has no effect on the solution for the discrete problem. Solving the Fractional Knapsack Problem Here we present a solution based on the greedy procedure and prove its optimality.
Algorithm FractionalKnapsack // // Input: a set of values and weights as described above.
// Value – the total value placed into the knapsack. For K = 1 to N Do
End Do // Value = 0. Capacity = W // Start with all of the knapsack capacity // available and gradually decrease it. K = 1 While (K N) And (Capacity > 0.0) Do
Else // Fill up the knapsack.
Capacity = 0. End If K = K + 1 End Do // We first illustrate this algorithm by applying it to the sample problem on page 116 of the textbook and then we prove its optimality by showing that any other solution must have a total value not exceeding the value produced by this algorithm.
Here is the problem from figure 3.8 on page 116 of the textbook. W = 10.0 N = 4 w 1 = 7.0 v 1 = 42.0 1 = 6. w 2 = 3.0 v 2 = 12.0 2 = 4. w 3 = 4.0 v 3 = 40.0 3 = 10. w 4 = 5.0 v 4 = 25.0 4 = 5. Note here that we have 3 1 4 2. In order to apply the algorithm discussed above, I would have to renumber the four items according to the permutation 3 1, 1 2, 4 3, and 2 4, or 1 2, 2 4, 3 1, 4 3 in more conventional order. To be consistent with the book, I shall keep the present indices and reorder the entries. w 3 = 4.0 v 3 = 40.0 3 = 10. w 1 = 7.0 v 1 = 42.0 1 = 6. w 4 = 5.0 v 4 = 25.0 4 = 5. w 2 = 3.0 v 2 = 12.0 2 = 4. We now solve this with greedy, taking note of a specific partial solution. K = 3 Capacity = 10. w 3 = 4.0 x 3 = 1.0 Value = 0.0 + 1.0 40.0 = 40. K = 1 Capacity = 6. w 1 = 7.0 x 1 = (6.0 / 7.0) Value = 40.0 + (6.0 / 7.0) 42.0 = 76. The optimal solution to the fractional knapsack problem is x 1 = 6.0/7.0 x 2 = 0.0 x 3 = 1.0 x 4 = 0.0 Value = 76.0. The two solutions of interest here have values of 40.0 and 76.0 respectively. The value 76. is the optimal value for any variant of this problem and is the solution to the fractional knapsack problem. We shall see later that the solution to the 0-1 knapsack problem is bounded by these two values. Indeed, we shall obtain an easy lower bound that is better.
Insights into Discrete Knapsack This variant of the algorithm is an application of greedy to the 0-1 knapsack problem. This algorithm obviously produces a solution, but can be shown not always to produce an optimal
Algorithm DiscreteKnapsackLowerBound // // Input: a set of values and weights as described above.
// Value – a lower bound on the optimal solution. For K = 1 to N Do
End Do // Value = 0. Capacity = W // Start with all of the knapsack capacity // available and gradually decrease it. K = 1 While (K N) And (Capacity > 0.0) Do
End If K = K + 1 End Do // We argue that the optimal solution to the discrete knapsack problem is bounded by the results of the above algorithm as a lower bound and the results of the greedy algorithm as an upper bound. The arguments are simple.
We now apply the lower bound algorithm to the problem from the book. As before we rearrange the items according to the greedy criteria, but keep the original numbering. w 3 = 4.0 v 3 = 40.0 3 = 10. w 1 = 7.0 v 1 = 42.0 1 = 6. w 4 = 5.0 v 4 = 25.0 4 = 5. w 2 = 3.0 v 2 = 12.0 2 = 4. We now attempt to solve the discrete knapsack problem with greedy. K = 3 Capacity = 10. w 3 = 4.0 x 3 = 1.0 Value = 0.0 + 40.0 = 40. K = 1 Capacity = 6. w 1 > 6.0, so skip. K = 4 Capacity = 6. w 4 = 5.0 x 4 = 1.0 Value = 40.0 + 25.0 = 65. K = 2 Capacity = 1. w 1 > 1.0, so skip. We have now run out of items in the list. We now have two values for the optimal solution to the discrete knapsack problem. Upper bound 76.0, and Lower bound 65.0. In this case, it turns out that 65.0 is the solution to the discrete knapsack problem, but this is an accident as it is quite easy to create examples in which the above algorithm does not produce the optimal solution. One should note that there is one condition under which the optimal solution to the discrete knapsack problem can easily be produced. Should the greedy algorithm, as applied to the fractional knapsack problem, produce a solution in which there is no value of x K such that 0. < x K < 1.0, with the inequalities strict (that is for either x K = 0.0 or x K = 1.0 for all K), then the solution to the fractional knapsack problem is the exact optimal solution to the discrete knapsack problem. To illustrate this claim, consider the same problem as before, except that W = 11.