- Design and Analysis of Algorithms
- Home

- Basics of Algorithms
- DAA - Introduction
- DAA - Analysis of Algorithms
- DAA - Methodology of Analysis
- Asymptotic Notations & Apriori Analysis
- DAA - Space Complexities

- Design Strategies
- DAA - Divide & Conquer
- DAA - Max-Min Problem
- DAA - Merge Sort
- DAA - Binary Search
- Strassen’s Matrix Multiplication
- DAA - Greedy Method
- DAA - Fractional Knapsack
- DAA - Job Sequencing with Deadline
- DAA - Optimal Merge Pattern
- DAA - Dynamic Programming
- DAA - 0-1 Knapsack
- Longest Common Subsequence

- Graph Theory
- DAA - Spanning Tree
- DAA - Shortest Paths
- DAA - Multistage Graph
- Travelling Salesman Problem
- Optimal Cost Binary Search Trees

- Heap Algorithms
- DAA - Binary Heap
- DAA - Insert Method
- DAA - Heapify Method
- DAA - Extract Method

- Sorting Methods
- DAA - Bubble Sort
- DAA - Insertion Sort
- DAA - Selection Sort
- DAA - Quick Sort
- DAA - Radix Sort

- Complexity Theory
- Deterministic vs. Nondeterministic Computations
- DAA - Max Cliques
- DAA - Vertex Cover
- DAA - P and NP Class
- DAA - Cook’s Theorem
- NP Hard & NP-Complete Classes
- DAA - Hill Climbing Algorithm

- DAA Useful Resources
- DAA - Quick Guide
- DAA - Useful Resources
- DAA - Discussion

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Design and Analysis 0-1 Knapsack

In this tutorial, earlier we have discussed Fractional Knapsack problem using Greedy approach. We have shown that Greedy approach gives an optimal solution for Fractional Knapsack. However, this chapter will cover 0-1 Knapsack problem and its analysis.

In 0-1 Knapsack, items cannot be broken which means the thief should take the item as a whole or should leave it. This is reason behind calling it as 0-1 Knapsack.

Hence, in case of 0-1 Knapsack, the value of ** x_{i}** can be either

**or**

*0***, where other constraints remain the same.**

*1*0-1 Knapsack cannot be solved by Greedy approach. Greedy approach does not ensure an optimal solution. In many instances, Greedy approach may give an optimal solution.

The following examples will establish our statement.

### Example-1

Let us consider that the capacity of the knapsack is W = 25 and the items are as shown in the following table.

Item | A | B | C | D |
---|---|---|---|---|

Profit | 24 | 18 | 18 | 10 |

Weight | 24 | 10 | 10 | 7 |

Without considering the profit per unit weight (** p_{i}/w_{i}**), if we apply Greedy approach to solve this problem, first item

**will be selected as it will contribute max**

*A*_{i}mum profit among all the elements.

After selecting item ** A**, no more item will be selected. Hence, for this given set of items total profit is

**. Whereas, the optimal solution can be achieved by selecting items,**

*24***and C, where the total profit is 18 + 18 = 36.**

*B*### Example-2

Instead of selecting the items based on the overall benefit, in this example the items are selected based on ratio *p _{i}/w_{i}*. Let us consider that the capacity of the knapsack is

*W*= 60 and the items are as shown in the following table.

Item | A | B | C |
---|---|---|---|

Price | 100 | 280 | 120 |

Weight | 10 | 40 | 20 |

Ratio | 10 | 7 | 6 |

Using the Greedy approach, first item ** A** is selected. Then, the next item

**is chosen. Hence, the total profit is**

*B***100 + 280 = 380**. However, the optimal solution of this instance can be achieved by selecting items,

**and**

*B***, where the total profit is**

*C***280 + 120 = 400**.

Hence, it can be concluded that Greedy approach may not give an optimal solution.

To solve 0-1 Knapsack, Dynamic Programming approach is required.

### Problem Statement

A thief is robbing a store and can carry a max_{i}mal weight of ** W** into his knapsack. There are

**items and weight of**

*n***i**item is

^{th}**and the profit of selecting this item is**

*w*_{i}**. What items should the thief take?**

*p*_{i}## Dynamic-Programming Approach

Let ** i** be the highest-numbered item in an optimal solution

**S**for

**W**dollars. Then

**is an optimal solution for**

*S*^{'}= S - {i}**dollars and the value to the solution**

*W - w*_{i}**is**

*S***plus the value of the sub-problem.**

*V*_{i}We can express this fact in the following formula: define **c[i, w]** to be the solution for items **1,2, … , i** and the max_{i}mum weight **w**.

The algorithm takes the following inputs

The max

_{i}mum weight**W**The number of items

**n**The two sequences

**v = <v**and_{1}, v_{2}, …, v_{n}>**w = <w**_{1}, w_{2}, …, w_{n}>

Dynamic-0-1-knapsack (v, w, n, W)for w = 0 to W do c[0, w] = 0 for i = 1 to n do c[i, 0] = 0 for w = 1 to W do if w_{i}≤ w then if v_{i}+ c[i-1, w-w_{i}] then c[i, w] = v_{i}+ c[i-1, w-w_{i}] else c[i, w] = c[i-1, w] else c[i, w] = c[i-1, w]

The set of items to take can be deduced from the table, starting at **c[n, w]** and tracing backwards where the optimal values came from.

If *c[i, w] = c[i-1, w]*, then item ** i** is not part of the solution, and we continue tracing with

**c[i-1, w]**. Otherwise, item

**is part of the solution, and we continue tracing with**

*i***c[i-1, w-W]**.

### Analysis

This algorithm takes θ(*n*, *w*) times as table *c* has (*n* + 1).(*w* + 1) entries, where each entry requires θ(1) time to compute.