Knapsack problem ==>>
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
This presentation discusses the knapsack problem and its two main versions: 0/1 and fractional. The 0/1 knapsack problem involves indivisible items that are either fully included or not included, and is solved using dynamic programming. The fractional knapsack problem allows items to be partially included, and is solved using a greedy algorithm. Examples are provided of solving each version using their respective algorithms. The time complexity of these algorithms is also presented. Real-world applications of the knapsack problem include cutting raw materials and selecting investments.
The document discusses the 0/1 knapsack problem and the greedy algorithm approach. It describes the knapsack problem as selecting a subset of items with weights and values that fit within a knapsack capacity while maximizing the total value. The greedy algorithm works by selecting the highest value item at each step that fits within remaining capacity. The document provides an example problem of selecting boxes to fill a knapsack of 15kg capacity to maximize profit. It outlines the recurrence relation and time/space complexity of the greedy knapsack algorithm.
The document discusses the knapsack problem, which involves selecting a subset of items that fit within a knapsack of limited capacity to maximize the total value. There are two versions - the 0-1 knapsack problem where items can only be selected entirely or not at all, and the fractional knapsack problem where items can be partially selected. Solutions include brute force, greedy algorithms, and dynamic programming. Dynamic programming builds up the optimal solution by considering all sub-problems.
Knapsack problem using dynamic programmingkhush_boo31
The document describes the 0-1 knapsack problem and provides an example of solving it using dynamic programming. Specifically, it defines the 0-1 knapsack problem, provides the formula for solving it dynamically using a 2D array C, walks through populating the C array and backtracking to find the optimal solution for a sample problem instance, and analyzes the time complexity of the dynamic programming algorithm.
The document discusses the 0/1 knapsack problem and dynamic programming algorithm to solve it. The 0/1 knapsack problem involves selecting a subset of items to pack in a knapsack that maximizes the total value without exceeding the knapsack's weight capacity. The dynamic programming algorithm solves this by building up a table where each entry represents the maximum value for a given weight. It iterates through items, checking if including each item increases the maximum value for that weight.
This document discusses the greedy algorithm approach and the knapsack problem. It defines greedy algorithms as choosing locally optimal solutions at each step in hopes of reaching a global optimum. The knapsack problem is described as packing items into a knapsack to maximize total value without exceeding weight capacity. An optimal knapsack algorithm is presented that sorts by value-to-weight ratio and fills highest ratios first. An example applies this to maximize profit of 440 by selecting full quantities of items B and A, and half of item C for a knapsack with capacity of 60.
This Presentation will Use to develop your knowledge and doubts in Knapsack problem. This Slide also include Memory function part. Use this Slides to Develop your knowledge on Knapsack and Memory function
This presentation discusses the knapsack problem and its two main versions: 0/1 and fractional. The 0/1 knapsack problem involves indivisible items that are either fully included or not included, and is solved using dynamic programming. The fractional knapsack problem allows items to be partially included, and is solved using a greedy algorithm. Examples are provided of solving each version using their respective algorithms. The time complexity of these algorithms is also presented. Real-world applications of the knapsack problem include cutting raw materials and selecting investments.
The document discusses the 0/1 knapsack problem and the greedy algorithm approach. It describes the knapsack problem as selecting a subset of items with weights and values that fit within a knapsack capacity while maximizing the total value. The greedy algorithm works by selecting the highest value item at each step that fits within remaining capacity. The document provides an example problem of selecting boxes to fill a knapsack of 15kg capacity to maximize profit. It outlines the recurrence relation and time/space complexity of the greedy knapsack algorithm.
The document discusses the knapsack problem, which involves selecting a subset of items that fit within a knapsack of limited capacity to maximize the total value. There are two versions - the 0-1 knapsack problem where items can only be selected entirely or not at all, and the fractional knapsack problem where items can be partially selected. Solutions include brute force, greedy algorithms, and dynamic programming. Dynamic programming builds up the optimal solution by considering all sub-problems.
Knapsack problem using dynamic programmingkhush_boo31
The document describes the 0-1 knapsack problem and provides an example of solving it using dynamic programming. Specifically, it defines the 0-1 knapsack problem, provides the formula for solving it dynamically using a 2D array C, walks through populating the C array and backtracking to find the optimal solution for a sample problem instance, and analyzes the time complexity of the dynamic programming algorithm.
The document discusses the 0/1 knapsack problem and dynamic programming algorithm to solve it. The 0/1 knapsack problem involves selecting a subset of items to pack in a knapsack that maximizes the total value without exceeding the knapsack's weight capacity. The dynamic programming algorithm solves this by building up a table where each entry represents the maximum value for a given weight. It iterates through items, checking if including each item increases the maximum value for that weight.
This document discusses the greedy algorithm approach and the knapsack problem. It defines greedy algorithms as choosing locally optimal solutions at each step in hopes of reaching a global optimum. The knapsack problem is described as packing items into a knapsack to maximize total value without exceeding weight capacity. An optimal knapsack algorithm is presented that sorts by value-to-weight ratio and fills highest ratios first. An example applies this to maximize profit of 440 by selecting full quantities of items B and A, and half of item C for a knapsack with capacity of 60.
This Presentation will Use to develop your knowledge and doubts in Knapsack problem. This Slide also include Memory function part. Use this Slides to Develop your knowledge on Knapsack and Memory function
Knapsack problem algorithm, greedy algorithmHoneyChintal
The document discusses the knapsack problem and algorithms to solve it. It describes the 0-1 knapsack problem, which does not allow breaking items, and the fractional knapsack problem, which does. It provides an example comparing the two. The document then explains the greedy algorithm approach to solve the fractional knapsack problem by calculating value to weight ratios and filling the knapsack with the highest ratio items first. Pseudocode for the greedy fractional knapsack algorithm is provided along with analysis of its time complexity.
The document discusses various backtracking techniques including bounding functions, promising functions, and pruning to avoid exploring unnecessary paths. It provides examples of problems that can be solved using backtracking including n-queens, graph coloring, Hamiltonian circuits, sum-of-subsets, 0-1 knapsack. Search techniques for backtracking problems include depth-first search (DFS), breadth-first search (BFS), and best-first search combined with branch-and-bound pruning.
The 0-1 knapsack problem involves selecting items with given values and weights to maximize the total value without exceeding a weight capacity. It can be solved using a brute force approach in O(2^n) time or dynamic programming in O(n*c) time. Dynamic programming constructs a value matrix where each cell represents the maximum value for a given item and weight. The cell value is either from the item above or adding the item's value if the weight is less than remaining capacity. The last cell provides the maximum value solution.
The document presents an overview of fractional and 0/1 knapsack problems. It defines the fractional knapsack problem as choosing items with maximum total benefit but fractional amounts allowed, with total weight at most W. An algorithm is provided that takes the item with highest benefit-to-weight ratio in each step. The 0/1 knapsack problem requires choosing whole items. Solutions like greedy and dynamic programming are discussed. An example illustrates applying dynamic programming to a sample 0/1 knapsack problem.
The document discusses the knapsack problem and greedy algorithms. It defines the knapsack problem as an optimization problem where given constraints and an objective function, the goal is to find the feasible solution that maximizes or minimizes the objective. It describes the knapsack problem has having two versions: 0-1 where items are indivisible, and fractional where items can be divided. The fractional knapsack problem can be solved using a greedy approach by sorting items by value to weight ratio and filling the knapsack accordingly until full.
The document discusses the greedy method algorithmic approach. It provides an overview of greedy algorithms including that they make locally optimal choices at each step to find a global optimal solution. The document also provides examples of problems that can be solved using greedy methods like job sequencing, the knapsack problem, finding minimum spanning trees, and single source shortest paths. It summarizes control flow and applications of greedy algorithms.
The document discusses the 0-1 knapsack problem and how it can be solved using dynamic programming. It first defines the 0-1 knapsack problem and provides an example. It then explains how a brute force solution would work in exponential time. Next, it describes how to define the problem as subproblems and derive a recursive formula to solve the subproblems in a bottom-up manner using dynamic programming. This builds up the solutions in a table and solves the problem in polynomial time. Finally, it walks through an example applying the dynamic programming algorithm to a sample problem instance.
The document provides an introduction to the knapsack problem. It defines the knapsack problem as determining the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible, given a set of items each with a mass and value. It notes there are two versions: the 0-1 knapsack problem, where items are indivisible and you either take an item or not, and the fractional knapsack problem, where items are divisible and you can take fractions of items.
This document discusses NP-complete problems and their properties. Some key points:
- NP-complete problems have an exponential upper bound on runtime but only a polynomial lower bound, making them appear intractable. However, their intractability cannot be proven.
- NP-complete problems are reducible to each other in polynomial time. Solving one would solve all NP-complete problems.
- NP refers to problems that can be verified in polynomial time. P refers to problems that can be solved in polynomial time.
- A problem is NP-complete if it is in NP and all other NP problems can be reduced to it in polynomial time. Proving a problem is NP-complete involves showing
The document discusses two types of knapsack problems - the 0-1 knapsack problem and the fractional knapsack problem. The 0-1 knapsack problem uses dynamic programming to determine how to fill a knapsack to maximize the total value of items without exceeding the knapsack's weight limit, where each item is either fully included or not included. The fractional knapsack problem allows partial inclusion of items and can be solved greedily by always including a fraction of the highest value per unit weight item until the knapsack is full.
This document provides an introduction to greedy algorithms. It defines greedy algorithms as algorithms that make locally optimal choices at each step in the hope of finding a global optimum. The document then provides examples of problems that can be solved using greedy algorithms, including counting money, scheduling jobs, finding minimum spanning trees, and the traveling salesman problem. It also provides pseudocode for a general greedy algorithm and discusses some properties of greedy algorithms.
The document discusses optimal binary search trees (OBST) and describes the process of creating one. It begins by introducing OBST and noting that the method can minimize average number of comparisons in a successful search. It then shows the step-by-step process of calculating the costs for different partitions to arrive at the optimal binary search tree for a given sample dataset with keys and frequencies. The process involves calculating Catalan numbers for each partition and choosing the minimum cost at each step as the optimal is determined.
The document discusses recursion, including:
1) Recursion involves breaking a problem down into smaller subproblems until a base case is reached, then building up the solution to the overall problem from the solutions to the subproblems.
2) A recursive function is one that calls itself, with each call typically moving closer to a base case where the problem can be solved without recursion.
3) Recursion can be linear, involving one recursive call, or binary, involving two recursive calls to solve similar subproblems.
This is a short presentation on Vertex Cover Problem for beginners in the field of Graph Theory...
Download the presentation for a better experience...
This document discusses greedy algorithms and dynamic programming techniques for solving optimization problems. It covers the activity selection problem, which can be solved greedily by always selecting the shortest remaining activity. It also discusses the knapsack problem and how the fractional version can be solved greedily while the 0-1 version requires dynamic programming due to its optimal substructure but non-greedy nature. Dynamic programming builds up solutions by combining optimal solutions to overlapping subproblems.
Greedy algorithms work by making locally optimal choices at each step to arrive at a global optimal solution. They require that the problem exhibits the greedy choice property and optimal substructure. Examples that can be solved with greedy algorithms include fractional knapsack problem, minimum spanning tree, and activity selection. The fractional knapsack problem is solved greedily by sorting items by value/weight ratio and filling the knapsack completely. The 0/1 knapsack problem differs in that items are indivisible.
The document discusses minimum spanning trees (MST) and two algorithms for finding them: Prim's algorithm and Kruskal's algorithm. Prim's algorithm operates by building the MST one vertex at a time, starting from an arbitrary root vertex and at each step adding the cheapest connection to another vertex not yet included. Kruskal's algorithm finds the MST by sorting the edges by weight and sequentially adding edges that connect different components without creating cycles.
Given two integer arrays val[0...n-1] and wt[0...n-1] that represents values and weights associated with n items respectively. Find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to knapsack capacity W. Here the BRANCH AND BOUND ALGORITHM is discussed .
The document discusses the knapsack problem and provides examples to illustrate the recursive solution approach. It describes the knapsack problem as selecting items to place in a knapsack with a weight capacity to maximize the total benefit without exceeding the weight limit. It shows that the optimal solution can be found using a recursive function f(w) that returns the maximum benefit for a knapsack of weight w. The examples demonstrate computing f(w) values to find the optimal solutions for sample knapsack problems.
Problem: Given, number of items each with a weight and value. The aim is to find each item to be put in a knapsack so that the total weight of included items is less than or equal to the capacity of the knapsack simultaneous total value of the included items should be maximum. It’s a problem that belongs to the NP class of problems. The decision problem form of the knapsack problem is NP-complete whereas optimization problem is NP-hard..
Knapsack problem algorithm, greedy algorithmHoneyChintal
The document discusses the knapsack problem and algorithms to solve it. It describes the 0-1 knapsack problem, which does not allow breaking items, and the fractional knapsack problem, which does. It provides an example comparing the two. The document then explains the greedy algorithm approach to solve the fractional knapsack problem by calculating value to weight ratios and filling the knapsack with the highest ratio items first. Pseudocode for the greedy fractional knapsack algorithm is provided along with analysis of its time complexity.
The document discusses various backtracking techniques including bounding functions, promising functions, and pruning to avoid exploring unnecessary paths. It provides examples of problems that can be solved using backtracking including n-queens, graph coloring, Hamiltonian circuits, sum-of-subsets, 0-1 knapsack. Search techniques for backtracking problems include depth-first search (DFS), breadth-first search (BFS), and best-first search combined with branch-and-bound pruning.
The 0-1 knapsack problem involves selecting items with given values and weights to maximize the total value without exceeding a weight capacity. It can be solved using a brute force approach in O(2^n) time or dynamic programming in O(n*c) time. Dynamic programming constructs a value matrix where each cell represents the maximum value for a given item and weight. The cell value is either from the item above or adding the item's value if the weight is less than remaining capacity. The last cell provides the maximum value solution.
The document presents an overview of fractional and 0/1 knapsack problems. It defines the fractional knapsack problem as choosing items with maximum total benefit but fractional amounts allowed, with total weight at most W. An algorithm is provided that takes the item with highest benefit-to-weight ratio in each step. The 0/1 knapsack problem requires choosing whole items. Solutions like greedy and dynamic programming are discussed. An example illustrates applying dynamic programming to a sample 0/1 knapsack problem.
The document discusses the knapsack problem and greedy algorithms. It defines the knapsack problem as an optimization problem where given constraints and an objective function, the goal is to find the feasible solution that maximizes or minimizes the objective. It describes the knapsack problem has having two versions: 0-1 where items are indivisible, and fractional where items can be divided. The fractional knapsack problem can be solved using a greedy approach by sorting items by value to weight ratio and filling the knapsack accordingly until full.
The document discusses the greedy method algorithmic approach. It provides an overview of greedy algorithms including that they make locally optimal choices at each step to find a global optimal solution. The document also provides examples of problems that can be solved using greedy methods like job sequencing, the knapsack problem, finding minimum spanning trees, and single source shortest paths. It summarizes control flow and applications of greedy algorithms.
The document discusses the 0-1 knapsack problem and how it can be solved using dynamic programming. It first defines the 0-1 knapsack problem and provides an example. It then explains how a brute force solution would work in exponential time. Next, it describes how to define the problem as subproblems and derive a recursive formula to solve the subproblems in a bottom-up manner using dynamic programming. This builds up the solutions in a table and solves the problem in polynomial time. Finally, it walks through an example applying the dynamic programming algorithm to a sample problem instance.
The document provides an introduction to the knapsack problem. It defines the knapsack problem as determining the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible, given a set of items each with a mass and value. It notes there are two versions: the 0-1 knapsack problem, where items are indivisible and you either take an item or not, and the fractional knapsack problem, where items are divisible and you can take fractions of items.
This document discusses NP-complete problems and their properties. Some key points:
- NP-complete problems have an exponential upper bound on runtime but only a polynomial lower bound, making them appear intractable. However, their intractability cannot be proven.
- NP-complete problems are reducible to each other in polynomial time. Solving one would solve all NP-complete problems.
- NP refers to problems that can be verified in polynomial time. P refers to problems that can be solved in polynomial time.
- A problem is NP-complete if it is in NP and all other NP problems can be reduced to it in polynomial time. Proving a problem is NP-complete involves showing
The document discusses two types of knapsack problems - the 0-1 knapsack problem and the fractional knapsack problem. The 0-1 knapsack problem uses dynamic programming to determine how to fill a knapsack to maximize the total value of items without exceeding the knapsack's weight limit, where each item is either fully included or not included. The fractional knapsack problem allows partial inclusion of items and can be solved greedily by always including a fraction of the highest value per unit weight item until the knapsack is full.
This document provides an introduction to greedy algorithms. It defines greedy algorithms as algorithms that make locally optimal choices at each step in the hope of finding a global optimum. The document then provides examples of problems that can be solved using greedy algorithms, including counting money, scheduling jobs, finding minimum spanning trees, and the traveling salesman problem. It also provides pseudocode for a general greedy algorithm and discusses some properties of greedy algorithms.
The document discusses optimal binary search trees (OBST) and describes the process of creating one. It begins by introducing OBST and noting that the method can minimize average number of comparisons in a successful search. It then shows the step-by-step process of calculating the costs for different partitions to arrive at the optimal binary search tree for a given sample dataset with keys and frequencies. The process involves calculating Catalan numbers for each partition and choosing the minimum cost at each step as the optimal is determined.
The document discusses recursion, including:
1) Recursion involves breaking a problem down into smaller subproblems until a base case is reached, then building up the solution to the overall problem from the solutions to the subproblems.
2) A recursive function is one that calls itself, with each call typically moving closer to a base case where the problem can be solved without recursion.
3) Recursion can be linear, involving one recursive call, or binary, involving two recursive calls to solve similar subproblems.
This is a short presentation on Vertex Cover Problem for beginners in the field of Graph Theory...
Download the presentation for a better experience...
This document discusses greedy algorithms and dynamic programming techniques for solving optimization problems. It covers the activity selection problem, which can be solved greedily by always selecting the shortest remaining activity. It also discusses the knapsack problem and how the fractional version can be solved greedily while the 0-1 version requires dynamic programming due to its optimal substructure but non-greedy nature. Dynamic programming builds up solutions by combining optimal solutions to overlapping subproblems.
Greedy algorithms work by making locally optimal choices at each step to arrive at a global optimal solution. They require that the problem exhibits the greedy choice property and optimal substructure. Examples that can be solved with greedy algorithms include fractional knapsack problem, minimum spanning tree, and activity selection. The fractional knapsack problem is solved greedily by sorting items by value/weight ratio and filling the knapsack completely. The 0/1 knapsack problem differs in that items are indivisible.
The document discusses minimum spanning trees (MST) and two algorithms for finding them: Prim's algorithm and Kruskal's algorithm. Prim's algorithm operates by building the MST one vertex at a time, starting from an arbitrary root vertex and at each step adding the cheapest connection to another vertex not yet included. Kruskal's algorithm finds the MST by sorting the edges by weight and sequentially adding edges that connect different components without creating cycles.
Given two integer arrays val[0...n-1] and wt[0...n-1] that represents values and weights associated with n items respectively. Find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to knapsack capacity W. Here the BRANCH AND BOUND ALGORITHM is discussed .
The document discusses the knapsack problem and provides examples to illustrate the recursive solution approach. It describes the knapsack problem as selecting items to place in a knapsack with a weight capacity to maximize the total benefit without exceeding the weight limit. It shows that the optimal solution can be found using a recursive function f(w) that returns the maximum benefit for a knapsack of weight w. The examples demonstrate computing f(w) values to find the optimal solutions for sample knapsack problems.
Problem: Given, number of items each with a weight and value. The aim is to find each item to be put in a knapsack so that the total weight of included items is less than or equal to the capacity of the knapsack simultaneous total value of the included items should be maximum. It’s a problem that belongs to the NP class of problems. The decision problem form of the knapsack problem is NP-complete whereas optimization problem is NP-hard..
The Kruskal algorithm is used to find the minimum spanning tree (MST) of a connected undirected weighted graph. It works by sorting the edges in non-decreasing order of their weight, and then selecting edges one by one if they do not form a cycle with the previously selected edges. The algorithm is applied on the given graph, sorting its edges by weight and sequentially selecting edges that do not form cycles. This results in a MST with total weight of 38.
This document discusses algorithms and their analysis. It defines an algorithm as a step-by-step procedure to solve a problem or calculate a quantity. Algorithm analysis involves evaluating memory usage and time complexity. Asymptotics, such as Big-O notation, are used to formalize the growth rates of algorithms. Common sorting algorithms like insertion sort and quicksort are analyzed using recurrence relations to determine their time complexities as O(n^2) and O(nlogn), respectively.
The document discusses the 0/1 Knapsack Problem, an algorithmic problem where given weights and values of items and a maximum weight capacity for a knapsack, the goal is to fill the knapsack with the most valuable items without exceeding the weight limit. The document covers the 0/1 Knapsack Problem itself and provides details on solving it using dynamic programming.
This document discusses the 0/1 knapsack problem and how it can be solved using backtracking. It begins with an introduction to backtracking and the difference between backtracking and branch and bound. It then discusses the knapsack problem, giving the definitions of the profit vector, weight vector, and knapsack capacity. It explains how the problem is to find the combination of items that achieves the maximum total value without exceeding the knapsack capacity. The document constructs state space trees to demonstrate solving the knapsack problem using backtracking and fixed tuples. It concludes with examples problems and references.
The document discusses the 0-1 knapsack problem and provides an example of solving it using dynamic programming. The 0-1 knapsack problem aims to maximize the total value of items selected from a list that have a total weight less than or equal to the knapsack's capacity, where each item must either be fully included or excluded. The document outlines a dynamic programming algorithm that builds a table to store the maximum value for each item subset at each possible weight, recursively considering whether or not to include each additional item.
The document describes the 0-1 knapsack problem and how to solve it using dynamic programming. The 0-1 knapsack problem involves packing items of different weights and values into a knapsack of maximum capacity to maximize the total value without exceeding the weight limit. A dynamic programming algorithm is presented that breaks the problem down into subproblems and uses optimal substructure and overlapping subproblems to arrive at the optimal solution in O(nW) time, improving on the brute force O(2^n) time. An example is shown step-by-step to illustrate the algorithm.
The document describes the 0-1 knapsack problem and provides an example of solving it using dynamic programming. The 0-1 knapsack problem involves packing items into a knapsack of maximum capacity W to maximize the total value of packed items, where each item has a weight and value. The document defines the problem as a recursive subproblem and provides pseudocode for a dynamic programming algorithm that runs in O(nW) time, improving on the brute force O(2^n) time. It then works through an example application of the algorithm to a sample problem with 4 items and knapsack capacity of 5.
This document discusses dynamic programming and its application to solve the knapsack problem.
It begins by defining dynamic programming as a technique for solving problems with overlapping subproblems where each subproblem is solved only once and the results are stored in a table.
It then defines the knapsack problem as selecting a subset of items with weights and values that fit in a knapsack of capacity W to maximize the total value.
The document shows how to solve the knapsack problem using dynamic programming by constructing a table where each entry table[i,j] represents the maximum value for items 1 to i with weight ≤ j. It provides an example problem and walks through filling the table and backtracking to find
The document discusses the 0-1 knapsack problem and presents a dynamic programming algorithm to solve it. The 0-1 knapsack problem aims to maximize the total value of items selected without exceeding the knapsack's weight capacity, where each item must either be fully included or excluded. The algorithm uses a table B to store the maximum value for each sub-problem of filling weight w with the first i items, calculating entries recursively to find the overall optimal value B[n,W]. An example demonstrates filling the table to solve an instance of the problem.
Longest common sub sequence & 0/1 KnapsackAsif Shahriar
This document describes an algorithm for finding the longest common subsequence between two strings. It initializes a 2D array c with zeros and iterates through the strings, incrementing values in c when characters match based on the longest matching subsequence from the previous strings. The final value in c provides the length of the longest common subsequence.
This paper analyze few algorithms of the 0/1 Knapsack Problem and fractional
knapsack problem. This problem is a combinatorial optimization problem in which one has
to maximize the benefit of objects without exceeding capacity. As it is an NP-complete
problem, an exact solution for a large input is not possible. Hence, paper presents a
comparative study of the Greedy and dynamic methods. It also gives complexity of each
algorithm with respect to time and space requirements. Our experimental results show that
the most promising approaches are dynamic programming.
Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).
Method 1: Recursion by Brute-Force algorithm OR Exhaustive Search.
Approach: A simple solution is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.
Optimal Sub-structure: To consider all subsets of items, there can be two cases for every item.
Case 1: The item is included in the optimal subset.
Case 2: The item is not included in the optimal set.
Therefore, the maximum value that can be obtained from ‘n’ items is the max of the following two values.
Maximum value obtained by n-1 items and W weight (excluding nth item).
Value of nth item plus maximum value obtained by n-1 items and W minus the weight of the nth item (including nth item).
If the weight of the ‘nth’ item is greater than ‘W’, then the nth item cannot be included and Case 1 is the only possibility.
This document discusses inner product spaces and properties of the inner product. It provides examples of determining the inner product of vectors and applying properties like commutativity, distributivity, and associativity. It also defines length in Rn and discusses the Euclidean plane E2, defining distance between points as the absolute value of their difference. Students will learn to determine if a function defines an inner product, find inner products of vectors, and solve for distances in E2.
0 1 knapsack using naive recursive approach and top-down dynamic programming ...Abhishek Singh
This slide contains 0-1 knapsack problem using naive recursive approach and using top-down dynamic programming approach. Here we have used memorization to optimize the recursive approach.
The document discusses greedy algorithms and how they work. It provides an example of using a greedy algorithm to solve the fractional knapsack problem in 3 steps: (1) sorting items by value to weight ratio, (2) initializing a selection array, (3) iteratively selecting highest ratio items that fit in the knapsack until full. While fast, greedy algorithms may not always find the optimal solution. The document also covers using Huffman coding to create efficient variable-length codes.
Dynamic programming is a technique for solving problems with overlapping subproblems by breaking them down into smaller subproblems and storing the results of already solved subproblems in a table to build up the solution. It was developed in the 1950s and involves setting up recurrences relating solutions to larger instances to smaller instances and solving the smaller instances once to extract the solution. Examples given include computing the nth Fibonacci number and solving the knapsack problem by filling a table and using solutions to smaller capacities and numbers of items.
Design and analysis of Algorithms - Lecture 15.pptQurbanAli72
The document describes the 0/1 knapsack problem, which involves selecting a subset of items to pack in a knapsack without exceeding the knapsack's weight limit, in order to maximize the total value of the items. It presents a dynamic programming algorithm that solves the problem in polynomial time by building up the optimal solution for subproblems. The algorithm fills a two-dimensional table where each entry represents the maximum value achievable for a given subset of items with a particular remaining weight capacity.
The document provides examples and worksheets for practicing integer operations using a counting sponge workspace. It introduces positive and negative integers using protons, electrons and atomic charge. Examples are given for adding, subtracting, multiplying and dividing integers by writing the problems as counters on a counting sponge and pairing opposite charges. Worksheets provide practice problems for students to model the integer operations using counters on a counting sponge.
This document discusses the 0/1 knapsack problem and provides an example of how it can be solved using dynamic programming. The 0/1 knapsack problem aims to maximize the total value of items placed in a knapsack without exceeding the knapsack's weight capacity. The example shows how to build a 2D table to systematically calculate the maximum value for each weight increment. Dynamic programming is used to break the problem down into overlapping subproblems to efficiently solve the problem in polynomial time.
Dynamic programming can be used to solve optimization problems involving overlapping subproblems, such as finding the most valuable subset of items that fit in a knapsack. The knapsack problem is solved by considering all possible subsets incrementally, storing the optimal values in a table. Warshall's and Floyd's algorithms also use dynamic programming to find the transitive closure and shortest paths in graphs by iteratively building up the solution from smaller subsets. Optimal binary search trees can also be constructed using dynamic programming by considering optimal substructures.
The document discusses inner product spaces. Some key points:
- An inner product is a function that associates a number (<u,v>) with each pair of vectors (u,v) in a vector space, satisfying certain properties like symmetry and homogeneity.
- An inner product space is a vector space with an additional inner product structure.
- Properties of inner products include positivity (<v,v>≥0), linearity, and defining the norm (||v||) of a vector.
- Examples show the weighted Euclidean inner product satisfies the inner product properties and define the unit sphere in an inner product space.
The document discusses different types of knapsack problems. It provides an example of a 0/1 knapsack problem where items must either be fully included or excluded from a knapsack with limited capacity. Brute force and greedy algorithms are presented as approaches to solve such problems. The document also briefly introduces fractional knapsack problems and provides pseudocode for a greedy algorithm solution.
In computer science, divide and conquer (D&C) is an algorithm design paradigm based on multi-branched recursion. A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.
In computer science, merge sort (also commonly spelled mergesort) is an O(n log n) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. A detailed description and analysis of bottom-up mergesort appeared in a report by Goldstine and Neumann as early as 1948.
C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
This reference will take you through simple and practical approach while learning C++ Programming language.
In graph theory, graph coloring is a special case of graph labeling; it is an assignment of labels traditionally called "colors" to elements of a graph subject to certain constraints. In its simplest form, it is a way of coloring the vertices of a graph such that no two adjacent vertices share the same color; this is called a vertex coloring. Similarly, an edge coloring assigns a color to each edge so that no two adjacent edges share the same color, and a face coloring of a planar graph assigns a color to each face or region so that no two faces that share a boundary have the same color.
Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it determines that c cannot possibly be completed to a valid solution.
Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it determines that c cannot possibly be completed to a valid solution.
Rules and steps for developing a software product (rsfsp) by vikas sharmaVikas Sharma
This document outlines 11 rules and 10 steps for developing a software product. The rules emphasize listening to customers, understanding their needs through questions rather than answers, treating them as friends, paying attention to unspoken needs, and only then discussing product offerings. The steps include understanding the customer's business, gathering requirements, documenting and reviewing them, estimating timelines, coding while avoiding scope changes, testing, releasing for customer testing, production release, and ongoing maintenance. The overall process focuses on understanding the customer first before discussing solutions.
Office automation system for scholl (oasfs) by vikas sharmaVikas Sharma
This document outlines a project report for an office automation system for a school. The system was developed using Visual Basic.NET and MS Access and allows users to manage employee records by adding, viewing, modifying, and generating reports. It includes modules for login, menu navigation, entering employee, transfer, and salary details, and viewing or printing reports. The system provides centralized data management and is easy to use but has limitations such as only allowing single users and high costs. Future enhancements could include multi-user access, administrator controls, and network integration.
Library and member management system (lamms) by vikas sharmaVikas Sharma
The project entitled LIBRARY AND MEMBER MANAGEMENT SYSTEM is a application software developed to maintain the records related to Book, Member of library like Stock Maintenance of Books, Book Search, Book Issue, Book Returns Member detail, Membership fee Collected, different Users as per status and all necessary requirements for the Library to manage day to day operations. This project was developed with Visual Basic .NET as Front End and M.S-Access as Back End.
This is fully click driven Windows based package. It consists of forms for adding, deleting, modifying, viewing the record, generation of report and creates the final report.
Optimization is an act, process, or methodology of making something (as a design, system, or decision) as fully perfect, functional, or effective as possible. Website optimization is a technique to designing and developing a website in such a way that it should be capable to satisfy some key points.
Optimizing website, you can attract relevant customers who are searching for your service using search engines like Google and Yahoo. In other words SEO means finding ways to increase your site's appearance in web visitors' search results.
Web Site Optimization include
1. Serving websites faster and uptime of the site
2. Search engine optimization (SEO)
3. Website design
4. Usability
Brand Guideline of Bashundhara A4 Paper - 2024khabri85
It outlines the basic identity elements such as symbol, logotype, colors, and typefaces. It provides examples of applying the identity to materials like letterhead, business cards, reports, folders, and websites.
The Science of Learning: implications for modern teachingDerek Wenmoth
Keynote presentation to the Educational Leaders hui Kōkiritia Marautanga held in Auckland on 26 June 2024. Provides a high level overview of the history and development of the science of learning, and implications for the design of learning in our modern schools and classrooms.
How to Download & Install Module From the Odoo App Store in Odoo 17Celine George
Custom modules offer the flexibility to extend Odoo's capabilities, address unique requirements, and optimize workflows to align seamlessly with your organization's processes. By leveraging custom modules, businesses can unlock greater efficiency, productivity, and innovation, empowering them to stay competitive in today's dynamic market landscape. In this tutorial, we'll guide you step by step on how to easily download and install modules from the Odoo App Store.
8+8+8 Rule Of Time Management For Better ProductivityRuchiRathor2
This is a great way to be more productive but a few things to
Keep in mind:
- The 8+8+8 rule offers a general guideline. You may need to adjust the schedule depending on your individual needs and commitments.
- Some days may require more work or less sleep, demanding flexibility in your approach.
- The key is to be mindful of your time allocation and strive for a healthy balance across the three categories.
2. 2
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
Item # Weight Value
1 1 8
2 3 6
3 5 5
Knapsack problem
3. 3
Knapsack problem
There are two versions of the problem:
1. “0-1 knapsack problem”
Items are indivisible; you either take an item or not. Some
special instances can be solved with dynamic programming
1. “Fractional knapsack problem”
Items are divisible: you can take any fraction of an item
4. 4
Given a knapsack with maximum capacity W, and
a set S consisting of n items
Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
Problem: How to pack the knapsack to achieve
maximum total value of packed items?
0-1 Knapsack problem
5. 5
Problem, in other words, is to find
∑∑ ∈∈
≤
Ti
i
Ti
i Wwb subject tomax
0-1 Knapsack problem
The problem is called a “0-1” problem,
because each item must be entirely
accepted or rejected.
6. 6
Let’s first solve this problem with a
straightforward algorithm
Since there are n items, there are 2n
possible
combinations of items.
We go through all combinations and find the one
with maximum value and with total weight less or
equal to W
Running time will be O(2n
)
0-1 Knapsack problem:
brute-force approach
7. 7
We can do better with an algorithm based on
dynamic programming
We need to carefully identify the subproblems
0-1 Knapsack problem:
dynamic programming approach
8. 8
Given a knapsack with maximum capacity W, and
a set S consisting of n items
Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
Problem: How to pack the knapsack to achieve
maximum total value of packed items?
Defining a Subproblem
9. 9
We can do better with an algorithm based on
dynamic programming
We need to carefully identify the subproblems
Let’s try this:
If items are labeled 1..n, then a subproblem
would be to find an optimal solution for
Sk = {items labeled 1, 2, .. k}
Defining a Subproblem
10. 10
If items are labeled 1..n, then a subproblem would be
to find an optimal solution for Sk = {items labeled
1, 2, .. k}
This is a reasonable subproblem definition.
The question is: can we describe the final solution
(Sn ) in terms of subproblems (Sk)?
Unfortunately, we can’t do that.
Defining a Subproblem
11. 11
Max weight: W = 20
For S4:
Total weight: 14
Maximum benefit: 20
w1 =2
b1 =3
w2 =4
b2 =5
w3 =5
b3 =8
w4 =3
b4 =4 wi bi
10
85
54
43
32
Weight Benefit
9
Item
#
4
3
2
1
5
S4
S5
w1 =2
b1 =3
w2 =4
b2 =5
w3 =5
b3 =8
w5 =9
b5 =10
For S5:
Total weight: 20
Maximum benefit: 26
Solution for S4 is
not part of the
solution for S !!!
?
Defining a Subproblem
12. 12
As we have seen, the solution for S4 is not part of the
solution for S5
So our definition of a subproblem is flawed and we
need another one!
Defining a Subproblem
13. 13
Given a knapsack with maximum capacity W, and
a set S consisting of n items
Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
Problem: How to pack the knapsack to achieve
maximum total value of packed items?
Defining a Subproblem
14. 14
Let’s add another parameter: w, which will represent
the maximum weight for each subset of items
The subproblem then will be to compute V[k,w], i.e.,
to find an optimal solution for Sk = {items labeled 1,
2, .. k} in a knapsack of size w
Defining a Subproblem
15. 15
The subproblem will then be to compute V[k,w], i.e.,
to find an optimal solution for Sk = {items labeled 1,
2, .. k} in a knapsack of size w
Assuming knowing V[i, j], where i=0,1, 2, … k-1,
j=0,1,2, …w, how to derive V[k,w]?
Recursive Formula for
subproblems
16. 16
It means, that the best subset of Sk that has total
weight w is:
1) the best subset of Sk-1 that has total weight ≤ w, or
2) the best subset of Sk-1 that has total weight ≤ w-wk plus the
item k
+−−−
>−
=
else}],1[],,1[max{
if],1[
],[
kk
k
bwwkVwkV
wwwkV
wkV
Recursive formula for subproblems:
Recursive Formula for
subproblems (continued)
17. 17
Recursive Formula
The best subset of Sk that has the total weight ≤ w,
either contains item k or not.
First case: wk>w. Item k can’t be part of the solution,
since if it was, the total weight would be > w, which
is unacceptable.
Second case: wk ≤ w. Then the item k can be in the
solution, and we choose the case with greater value.
+−−−
>−
=
else}],1[],,1[max{
if],1[
],[
kk
k
bwwkVwkV
wwwkV
wkV
18. 18
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0-1 Knapsack Algorithm
19. 19
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
< the rest of the code >
What is the running time of this
algorithm?
O(W)
O(W)
Repeat n times
O(n*W)
Remember that the brute-force algorithm
takes O(2n
)
Running time
20. 20
Let’s run our algorithm on the
following data:
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
Example
21. 21
for w = 0 to W
V[0,w] = 0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
Example (2)
22. 22
for i = 1 to n
V[i,0] = 0
0
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
Example (3)
23. 23
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
0
i=1
bi=3
wi=2
w=1
w-wi =-1
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
0
0
0
Example (4)
24. 24
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=2
w-wi =0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (5)
25. 25
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=3
w-wi =1
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3
Example (6)
26. 26
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=4
w-wi =2
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3 3
Example (7)
27. 27
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=5
w-wi =3
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3 3 3
Example (8)
28. 28
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=1
w-wi =-2
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (9)
29. 29
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=2
w-wi =-1
3 3 3 3
3
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0
Example (10)
30. 30
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=3
w-wi =0
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
43
Example (11)
31. 31
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=4
w-wi =1
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
43 4
Example (12)
32. 32
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=5
w-wi =2
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
73 4 4
Example (13)
33. 33
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=3
bi=5
wi=4
w= 1..3
3 3 3 3
0 3 4 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
7
3 40
Example (14)
34. 34
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=3
bi=5
wi=4
w= 4
w- wi=0
3 3 3 3
0 3 4 4 7
0 3 4 5
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (15)
35. 35
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=3
bi=5
wi=4
w= 5
w- wi=1
3 3 3 3
0 3 4 4 7
0 3 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
5 7
Example (16)
36. 36
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=4
bi=6
wi=5
w= 1..4
3 3 3 3
0 3 4 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
7
3 40
70 3 4 5
5
Example (17)
37. 37
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=4
bi=6
wi=5
w= 5
w- wi=0
3 3 3 3
0 3 4 4 7
0 3 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
5
7
7
0 3 4 5
Example (18)
39. 39
Comments
This algorithm only finds the max possible value
that can be carried in the knapsack
» i.e., the value in V[n,W]
To know the items that make this maximum value,
an addition to this algorithm is necessary
40. 40
All of the information we need is in the table.
V[n,W] is the maximal value of items that can be
placed in the Knapsack.
Let i=n and k=W
if V[i,k] ≠ V[i−1,k] then
mark the ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1 // Assume the ith
item is not in the knapsack
// Could it be in the optimally packed
knapsack?
How to find actual Knapsack
Items
41. 41
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i−1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
Finding the Items
42. 42
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i−1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
Finding the Items (2)
43. 43
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=3
k= 5
bi=5
wi=4
V[i,k] = 7
V[i−1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
Finding the Items (3)
44. 44
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
k= 5
bi=4
wi=3
V[i,k] = 7
V[i−1,k] =3
k − wi=2
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
7
Finding the Items (4)
45. 45
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=1
k= 2
bi=3
wi=2
V[i,k] = 3
V[i−1,k] =0
k − wi=0
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
3
Finding the Items (5)
46. 46
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the nth
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
i=0
k= 0
The optimal
knapsack
should contain
{1, 2}
Finding the Items (6)
47. 47
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the nth
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
The optimal
knapsack
should contain
{1, 2}
7
3
Finding the Items (7)
48. 48
Memorization (Memory Function Method)
Goal:
» Solve only subproblems that are necessary and solve it only once
Memorization is another way to deal with overlapping subproblems
in dynamic programming
With memorization, we implement the algorithm recursively:
» If we encounter a new subproblem, we compute and store the solution.
» If we encounter a subproblem we have seen, we look up the answer
Most useful when the algorithm is easiest to implement recursively
» Especially if we do not need solutions to all subproblems.
49. 49
for i = 1 to n
for w = 1 to W
V[i,w] = -1
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
MFKnapsack(i, w)
if V[i,w] < 0
if w < wi
value = MFKnapsack(i-1, w)
else
value = max(MFKnapsack(i-1, w),
bi + MFKnapsack(i-1, w-wi))
V[i,w] = value
return V[i,w]
0-1 Knapsack Memory Function Algorithm
50. 50
Dynamic programming is a useful technique of
solving certain kind of problems
When the solution can be recursively described in
terms of partial solutions, we can store these
partial solutions and re-use them as necessary
(memorization)
Running time of dynamic programming algorithm
vs. naïve algorithm:
» 0-1 Knapsack problem: O(W*n) vs. O(2n
)
Conclusion