0% found this document useful (0 votes)
27 views43 pages

Knapsack

This document discusses the knapsack problem in data structures and algorithms. It describes the 0/1 knapsack problem as determining which items to include in a collection such that the total weight is less than or equal to a given limit and the total value is maximized. It presents different types of knapsack problems and methods for solving the 0/1 knapsack problem, including recursion, dynamic programming, and representing it as a recursive tree. Dynamic programming is described as the most efficient method by avoiding redundant calculations through overlapping subproblems and optimal substructure. Applications mentioned include resource allocation and project planning.

Uploaded by

fahadshah1060
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)
27 views43 pages

Knapsack

This document discusses the knapsack problem in data structures and algorithms. It describes the 0/1 knapsack problem as determining which items to include in a collection such that the total weight is less than or equal to a given limit and the total value is maximized. It presents different types of knapsack problems and methods for solving the 0/1 knapsack problem, including recursion, dynamic programming, and representing it as a recursive tree. Dynamic programming is described as the most efficient method by avoiding redundant calculations through overlapping subproblems and optimal substructure. Applications mentioned include resource allocation and project planning.

Uploaded by

fahadshah1060
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/ 43

data structure &

Algorithm

ALGORITHM
KNAPSACK

2023
P R O B L E M S TAT E M E N T

Given a set of items, each with a


weight and a value, determine which
items to include in the collection so
that the total weight is less than or
equal to a given limit and the total
value is as large as possible
TYPES

0/1 KNAPSACK FRACTIONAL

BOUNDED UNBOUNDED
0/1 KNAPSACK
Recusrion
Invloves the Brute Force
Method for solving

Wa y s t o
Solve

Dynamic Programming
Most effiecnt method for solving this
problem
R E C U R S I V E E Q U AT I O N S

0/1 Knapsack (n , m)

if n==0 || m == 0 {return 0;}


n = number of items
if w[n] > m {return 0;} m= total bag capacity

0/1KS (n-1), ((m-w[n]) +P[n])


max
0/1KS (n-1), (m)
0 1 2
RECURSIVE TREE Weight = 1 1 1
01KS (3,3)
0 1 2
Profit = 2 3 1

01KS (2,2) 01KS (2,3)

01KS (1,1) 01KS (1,2) 01KS (1,2) 01KS (1,3)

01KS (0,0) 01KS (0,1) 01KS (0,1) 01KS (0,2) 01KS (0,1) 01KS (0,2) 01KS (0,2) 01KS (0,3)
CODE:
DYNAMIC PROGRAMMING:

• Overlapping subproblems

• Optimal substructure property

Reduce time complexity, by avoiding redundant calculations of


subproblems.
CODE:
Time Complexity

Recursion O(2^n)

Dynamic Programming O(n.W)

Fractional Knapsack O(n log n)

Multiple Knapsack Problem O(n.W.K)


Applications:

• Resource allocation
• Project Planning
• Determine optimal project set in funding
• Selecting optimal loadouts
Any Questions?

You might also like