Docsity

Prim's Algorithm for Minimum Spanning Trees in Weighted Connected Graphs - Prof. Edward Bo, Study notes of Computer Science

Toggle sidebar
bg1
Chapter 9 – Greedy
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.
1) One could give 93 pennies, for a total of 93 coins, or
2) One could give 3 quarters, 1 dime, 1 nickel, and 3 pennies for a total 8 coins.
The greedy algorithm for making change for amounts less than a dollar is simply stated. Let
C be the amount to be returned in change, with 1 C 99. Then:
1) Give the maximum number of quarters whose total value does not exceed C.
Subtract that amount from C.
2) Give the maximum number of dimes whose total value does not exceed the new
value of C. Subtract that amount from C.
3) Give the maximum number of nickels whose total value does not exceed the new
value of C. Subtract that amount from C.
4) We now have 0 C 4. Give that number of pennies.
It can be proven that this algorithm produces a solution with the fewest number of coins.
This proof can be applied to any number of coin sets, including the U.S. coin set with or
without the 50-cent piece. This author cannot reproduce the proof, but believes that it has
something to do with the following fact. Let d1 = 50, d2 = 25, d3 = 10, d4 = 5, and d5 = 1, and
note that d4 > d5, d3 > (d4 + d5), d2 > (d3 + d4 + d5), and d1 > (d2 + d3 + d4 + d5).
For an example of a coin set in which greedy does not produce the optimal number of coins,
consider making change for 7 with d1 = 10, d2 = 5, d3 = 4, d4 = 3, and d5 = 1. The greedy
solution is to give one of d2 and two of d5 – a total of three coins. The optimal solution is to
give one of d3 and one of d4 – a total of two coins.
Chapter 9 Version of 11/29/2020 Page 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Prim's Algorithm for Minimum Spanning Trees in Weighted Connected Graphs - Prof. Edward Bo and more Study notes Computer Science in PDF only on Docsity!

Chapter 9 – Greedy

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.

  1. One could give 93 pennies, for a total of 93 coins, or
  2. One could give 3 quarters, 1 dime, 1 nickel, and 3 pennies for a total 8 coins. The greedy algorithm for making change for amounts less than a dollar is simply stated. Let C be the amount to be returned in change, with 1  C  99. Then:
  3. Give the maximum number of quarters whose total value does not exceed C. Subtract that amount from C.
  4. Give the maximum number of dimes whose total value does not exceed the new value of C. Subtract that amount from C.
  5. Give the maximum number of nickels whose total value does not exceed the new value of C. Subtract that amount from C.
  6. We now have 0  C  4. Give that number of pennies. It can be proven that this algorithm produces a solution with the fewest number of coins. This proof can be applied to any number of coin sets, including the U.S. coin set with or without the 50-cent piece. This author cannot reproduce the proof, but believes that it has something to do with the following fact. Let d 1 = 50, d 2 = 25, d 3 = 10, d 4 = 5, and d 5 = 1, and note that d 4 > d 5 , d 3 > (d 4 + d 5 ), d 2 > (d 3 + d 4 + d 5 ), and d 1 > (d 2 + d 3 + d 4 + d 5 ). For an example of a coin set in which greedy does not produce the optimal number of coins, consider making change for 7 with d 1 = 10, d 2 = 5, d 3 = 4, d 4 = 3, and d 5 = 1. The greedy solution is to give one of d 2 and two of d 5 – a total of three coins. The optimal solution is to give one of d 3 and one of d 4 – a total of two coins.

We now must consider the characteristics of an optimization problem, subject to constraints. We have two important terms:

  1. a feasible solution is one that satisfies the problem’s constraints, and
  2. an optimal solution is a feasible solution that optimizes some function, often called the objective function. At each state in generating a solution to an optimization problem, we must make choices that satisfy the feasibility constraints. Under the greedy criterion, an algorithm examines all of the partial solutions to the problem that satisfy the feasibility constraints and selects the one that is locally optimal – i.e., looks best at the time. We now discuss a number of algorithms designed according to the Greedy strategy. Prim’s Algorithm The first algorithm to be considered in this class of greedy algorithms is Prim’s algorithm for creating a spanning tree of a connected graph or the connected component of a graph. We first define a spanning tree and then define the algorithm. Let G be an (N, M)-graph ; thus G has N vertices and M edges. It can be proven that G can be a connected graph only if M  (N – 1). A spanning tree of a connected graph is a tree containing all N vertices of the graph. There are a number of ways to define a tree on N vertices. Two definitions are:
  3. It is a connected graph on N vertices, having (N – 1) edges, and
  4. It is a connected acyclic graph that contains all N vertices. It is an easy proof to show that the above two definitions are equivalent in that each implies the other, but we shall leave this for another time. A minimum spanning tree of a weighted connected graph G is that spanning tree with smallest total weight of its edges, each edge in the tree having the same weight as the same edge in the graph. As in all optimization problems, a graph may have more than one distinct minimum spanning tree. In the extreme case of an unweighted graph (in which each edge has weight 1), any spanning tree is a minimum spanning tree. In the above example we show the minimum spanning tree of a weighted (4, 4)-graph. Any tree spanning this graph will have three edges, so we have 4 choices of the edge to remove. Removal of (A, B) will disconnect the graph, so we examine the other three choices and conclude that the first, with a total weight of 6, is optimal.

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

u ’  TI-1 to a vertex v’  TI-1. By the assumption of Prim’s algorithm this edge has weight

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.

If ET  {eK} is acyclic Then

ET = ET  {eK}

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.

  1. G is a tree with n vertices and m edges.
  2. Every two distinct vertices of G are connected by a unique path.
  3. G is connected and m = n – 1.
  4. G is acyclic and m = n – 1.
  5. G is acyclic and if any two nonadjacent vertices of G are joined by an edge e , then G + e , the graph with one edge added, has exactly one cycle. The definition is standard, and the proof of the theorem is found in every book on graph theory. We now extend this by two corollaries that are easy to prove, but just stated here. Corollary: Let G be an ( n , m )–graph with m < ( n – 1). Then G is disconnected. Corollary: Let G be an ( n , m )–graph with mn. Then G contains a cycle. The key feature in our detection of cycles in the growing tree is the concept of connected components. Recall that two vertices are in the same connected component if there is a path from one vertex to the other. We make the following observation, stated as a lemma. Lemma: Let J and K be two non–adjacent vertices in a graph G. Then: a) If J and K are in the same connected component, addition of the edge (J, K) to the graph will cause a cycle to be formed. b) If J and K are not in the same component, but in distinct components ( call them C(J) and C(K) ), then addition of the edge (J, K) will not add a cycle to the graph, but will cause every vertex in both C(J) and C(K) to be in the same component. Specifically, it causes J and K to be in the same component. Proof: Suppose J and K are in the same component, but that (J, K) is not an edge in the graph G. Then there exists a path from J to K, not involving that edge. Addition of the edge (J, K) to that path forms a cycle. Suppose that J and K are not in the same connected component. Then there is no path from J to K and addition of edge (J, K) to the graph will generate a unique path from J to K. As vertex J is connected to all vertices in its component, the addition of this edge, forms a path from every vertex in C(J) to vertex K, hence to every vertex in C(K).

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

Set K = 1. Consider e 1 = (7, 8, 1).

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

Set K = 2. Consider e 2 = (4, 5, 2).

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

Set K = 3. Consider e 3 = (1, 2, 3).

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

Set K = 4. Consider e 4 = (5, 6, 4).

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

Set K = 5. Consider e 5 = (1, 3, 5).

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

Set K = 6. Consider e 6 = (8, 9, 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

If ET  {eK} is acyclic Then

ET = ET  {eK}

EdgeCount = EdgeCount + 1 End If End Do K = 0, EdgeCount = 0; K < 8 and EdgeCount < 4

Set K = 1. Consider e 1 = (3, 5, 1).

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

Set K = 2. Consider e 2 = (4, 5, 1).

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

Set K = 3. Consider e 3 = (1, 3, 2).

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

Set K = 4. Consider e 4 = (1, 4, 2).

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

Set K = 5. Consider e 5 = (3, 4, 2).

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

Set K = 6. Consider e 6 = (2, 3, 3).

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.

Define K =

w

v

K K for 1  K  N. Since we have specified that v K > 0.0 and w K > 0.0 for

all K, 1  K  N, it follows that K > 0.0 and is a finite number for every K, 1  K  N.

Without loss of generality, we order the items such that  1   2  …  K  …  N; thus

item 1 is the item with the highest value to weight ratio and the sequence of K forms a non-

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.

Remember that this assumes the ordering  1   2  …  K  …  N, with K as defined.

Algorithm FractionalKnapsack // // Input: a set of values and weights as described above.

// Output: a set of x K – the fraction of item K to include.

// Value – the total value placed into the knapsack. For K = 1 to N Do

x K = 0.0 // Initialize the output array.

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

If ( w K  Capacity) Then // The item all fits in.

Value = Value + v K

Capacity = Capacity – w K

x K = 1.

Else // Fill up the knapsack.

x K = Capacity / w K

Value = Value + x K v K

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

solution. As before, this assumes the ordering of items so that  1   2  …  K  … 

N.

Algorithm DiscreteKnapsackLowerBound // // Input: a set of values and weights as described above.

// Output: a set of x K – the fraction of item K to include.

// Value – a lower bound on the optimal solution. For K = 1 to N Do

x K = 0.0 // Initialize the output array.

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

If ( w K  Capacity)Then

Value = Value + v K

Capacity = Capacity – w K

x K = 1.

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.

  1. The above algorithm produces an answer that meets all of the constraints of the discrete (0-1) knapsack problem. Any optimal answer must be at least as good, thus this answer is a lower bound on the optimal answer.
  2. The greedy algorithm has just been proven to provide an optimal solution to the knapsack problem under the constraints that 0.0  x K  1.0 for all items. The additional constraint of the discrete knapsack problem is that x K = 0.0 or x K = 1.0 – obviously a special case of the fractional knapsack. Thus no solution to the discrete knapsack can have value exceeding that of the solution to the associated fractional knapsack, thus the greedy solution to the fractional knapsack gives an upper bound on the optimal solution of the discrete knapsack.

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.