0% found this document useful (0 votes)
66 views15 pages

Knap Sack Problem

This document discusses different approaches to solving the knapsack problem including brute force, dynamic programming, and greedy algorithms. It provides pseudocode for a recursive brute force solution, explains how to implement dynamic programming using a table to store solved subproblems and avoid recomputing them, and gives an example of a greedy algorithm that works for the fractional knapsack problem but not necessarily the 0/1 knapsack problem.

Uploaded by

Muhammad hanzla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views15 pages

Knap Sack Problem

This document discusses different approaches to solving the knapsack problem including brute force, dynamic programming, and greedy algorithms. It provides pseudocode for a recursive brute force solution, explains how to implement dynamic programming using a table to store solved subproblems and avoid recomputing them, and gives an example of a greedy algorithm that works for the fractional knapsack problem but not necessarily the 0/1 knapsack problem.

Uploaded by

Muhammad hanzla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Knap Sack Problem

Brute force + Dynamic Solution + Greedy


The Knapsack Problem
(0/1) Knap Sack Problem (Brute Force)
Simple recursive solution

int knapSack(int W, int[] weights, int[] val, int n) {


         if (n == 0 || W == 0)
            return 0;
 
        /*
            If weight of the nth item is more than Knapsack
            capacity W,then this item cannot be included
            in the optimal solution
        */
        if (weights[n] > W)
            return knapSack(W, weights, val, n - 1);
 
        /* Consider two cases, including item and excluding item.*/
        else return max((val[n]+ knapSack(W - weights[n], weights, val, n - 1)),(knapSack(W, weights, val, n - 1)));
    }
Dynamic Programming

 The basic idea of dynamic programming is to use a table to store the solutions
of solved sub problems. If you face a sub problem again, you just need to take
the solution in the table without having to solve it again. Therefore, the
algorithms designed by dynamic programming are very effective.
Recursive solution with Dynamic programming-
Top down approach
int knapSack(int W, int[] weights, int[] val, int n, int [][]V) {
         if (n == 0 || W == 0)
             result= 0;
  if(V[n,W] >=0)
return V[n,W]
         if (weights[n] > W)
             result= knapSack(W, weights, val, n – 1, V);
 
        /* Consider two cases, including item and excluding item.*/
         else
result=max((val[n]+ knapSack(W - weights[n], weights, val, n – 1, V)),(knapSack(W, weights, val, n – 1,
V)));

V[n, W]]=result
return result
    }
Example
0/1 Knapsack Dynamic Solution-Bottom
up
Example 2
Finding Actual Knapsack items
Knap Sack Problem (Greedy Solution)

Greedy solution does not always gives correct answer for 0/1 knapsack problem.
Greedy solution can be used for fractional knapsack problem. It always given correct answer
for fractional knapsack.
Knap Sack Fractional Problem (Greedy
Solution) Updated

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy