LOGO
11
0/1 Knapsack Problem0/1 Knapsack Problem
(fixed tuple) using(fixed tuple) using
BACKTRACKINGBACKTRACKING
Submitted to: Submitted by:Submitted to: Submitted by:
Mrs. Deepti Shrimali Krati KatyalMrs. Deepti Shrimali Krati Katyal
MCA 3rd SemMCA 3rd Sem
Topics Covered
‱ Introduction
‱ Difference between Backtracking and Branch
& Bound
‱ Advantages & Disadvantages
‱ Application
‱ Knapsack Problem
‱ Application
‱ Advantages & Disadvantages
‱ Problem
‱ Reference
IntroductionIntroduction
In this seminar we will learn about an optimization
technique which is known as backtracking and
solve the 0/1 knapsack problem using fixed tuple.
Backtracking is a recursive method for building up
feasible solutions one at a time.
It provides a simple recursive method of generating
all possible n-tuples. After each n-tuple is
generated, it is checked for feasibility. If feasible,
the solution incurred is compared to the current
optimal solution
Difference Between BacktrackingDifference Between Backtracking
& Branch and Bound& Branch and BoundBACKTRACKING BRANCH & BOUND
It is used to find all
possible solutions
available to the
problem.
It is used to solve
optimization problem.
It traverse tree by DFS
(Depth First Search).
It may traverse the tree
in any manner, DFS or
BFS.
It realizes that it has
made a bad choice &
undoes the last choice
by backing up.
It realizes that it already
has a better optimal
solution that the pre-
solution leads to so it
abandons that pre-
solution.
It search the state space
tree until it found a
solution.
It completely searches
the state space tree to
get optimal solution.
It involves feasibility
function
It involves bounding
function
Advantages:
1) Simple to implement.
2) State changes are stored in stack, meaning we
do not need to concern ourselves about them.
3) Intuitive approach of trial and error.
4) Code size is usually small.
Advantages & Disadvantages of Backtracking
Disadvantages:
1) Multiple function calls are expensive.
2) Inefficient when there is lots of
branching from one state.
3) Requires large amount of space as the
each function state needs to be stored on
system stack.
 
 
ApplicationApplication
This approach is used for a number of problems
like:
‱N Queen Problem
‱Sorting
‱Sum Of Subset Problem
‱0/1 Knapsack Problem
‱Solving a Maze
‱Coloring a Map
‱Solving a Puzzel
Knapsack ProblemKnapsack Problem
A knapsack problem consist of profit vector P = (p1,p2,p3,

.,pn), a weight vector W = (w1,w2,w3,
.wn) a capacity
C of the Knapsack.
Problem: How to pack the knapsack to achieve maximum
total value of packed items?
Problem, in other words, is to find
∑∑ ∈∈
≀
Ti
i
Ti
i Wwb subject tomax
‱ This problem is called a “0-1” problem, because
each item must be entirely accepted or rejected.
‱ 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
).
‱ This problem is sove by state space diagram.
State space tree:
Left child is always xi= 1 in our formulation
Right child is always xi= 0.
‱ Cryptography
‱ Applied Mathematics
‱ Complexity Theory
‱ Computer science
Application of Knapsack ProblemApplication of Knapsack Problem
Advantage:
‱ Will find an optimal solution if an optimal
solution exists.
Disadvantage:
‱ Computationally very demanding: O ( n · 2 n ).
‱ Very memory intensive. Number of nodes and
links both grow with 2 n, node label grows with
n.
Q1:Q1: In this problem n=4, profit P = (2,1,4,3) , weight of
knapsack W = (5,3,7,6) and capacity of knapsack is
C=11.
SOL: Search tree of fixed length tuple approach is:
ProblemsProblems
The solution state are 17 and 19 representing the solution
(1,0,0,1) and (0,1,1,0) respectively with the same profit 5
Search tree of variable length tuple approach is:
The solution state are 8 and 9 representing the solution
(1,4) and (2,3) respectively with the same profit 5.
Q2:Q2: In this problem n=4, profit P = (3,5,6,10) ,
weight of knapsack W = (2,3,4,5) and capacity
of knapsack is C=8.
Practice QuestionPractice Question
ReferenceReference
‱ www.google.comwww.google.com
‱ www.encyclopedia.comwww.encyclopedia.com
‱ www.wikipedia.comwww.wikipedia.com
‱ Design Method and Analysis of Algorithm By S.K
Basu.
Thank You
MLSU
UNIVERSITY

Knapsack problem using fixed tuple

  • 1.
    LOGO 11 0/1 Knapsack Problem0/1Knapsack Problem (fixed tuple) using(fixed tuple) using BACKTRACKINGBACKTRACKING Submitted to: Submitted by:Submitted to: Submitted by: Mrs. Deepti Shrimali Krati KatyalMrs. Deepti Shrimali Krati Katyal MCA 3rd SemMCA 3rd Sem
  • 2.
    Topics Covered ‱ Introduction ‱Difference between Backtracking and Branch & Bound ‱ Advantages & Disadvantages ‱ Application ‱ Knapsack Problem ‱ Application ‱ Advantages & Disadvantages ‱ Problem ‱ Reference
  • 3.
    IntroductionIntroduction In this seminarwe will learn about an optimization technique which is known as backtracking and solve the 0/1 knapsack problem using fixed tuple. Backtracking is a recursive method for building up feasible solutions one at a time. It provides a simple recursive method of generating all possible n-tuples. After each n-tuple is generated, it is checked for feasibility. If feasible, the solution incurred is compared to the current optimal solution
  • 4.
    Difference Between BacktrackingDifferenceBetween Backtracking & Branch and Bound& Branch and BoundBACKTRACKING BRANCH & BOUND It is used to find all possible solutions available to the problem. It is used to solve optimization problem. It traverse tree by DFS (Depth First Search). It may traverse the tree in any manner, DFS or BFS. It realizes that it has made a bad choice & undoes the last choice by backing up. It realizes that it already has a better optimal solution that the pre- solution leads to so it abandons that pre- solution. It search the state space tree until it found a solution. It completely searches the state space tree to get optimal solution. It involves feasibility function It involves bounding function
  • 5.
    Advantages: 1) Simple toimplement. 2) State changes are stored in stack, meaning we do not need to concern ourselves about them. 3) Intuitive approach of trial and error. 4) Code size is usually small. Advantages & Disadvantages of Backtracking
  • 6.
    Disadvantages: 1) Multiple functioncalls are expensive. 2) Inefficient when there is lots of branching from one state. 3) Requires large amount of space as the each function state needs to be stored on system stack.    
  • 7.
    ApplicationApplication This approach isused for a number of problems like: ‱N Queen Problem ‱Sorting ‱Sum Of Subset Problem ‱0/1 Knapsack Problem ‱Solving a Maze ‱Coloring a Map ‱Solving a Puzzel
  • 8.
    Knapsack ProblemKnapsack Problem Aknapsack problem consist of profit vector P = (p1,p2,p3, 
.,pn), a weight vector W = (w1,w2,w3,
.wn) a capacity C of the Knapsack. Problem: How to pack the knapsack to achieve maximum total value of packed items? Problem, in other words, is to find ∑∑ ∈∈ ≀ Ti i Ti i Wwb subject tomax
  • 9.
    ‱ This problemis called a “0-1” problem, because each item must be entirely accepted or rejected. ‱ 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 ). ‱ This problem is sove by state space diagram.
  • 10.
    State space tree: Leftchild is always xi= 1 in our formulation Right child is always xi= 0.
  • 11.
    ‱ Cryptography ‱ AppliedMathematics ‱ Complexity Theory ‱ Computer science Application of Knapsack ProblemApplication of Knapsack Problem
  • 12.
    Advantage: ‱ Will findan optimal solution if an optimal solution exists. Disadvantage: ‱ Computationally very demanding: O ( n · 2 n ). ‱ Very memory intensive. Number of nodes and links both grow with 2 n, node label grows with n.
  • 13.
    Q1:Q1: In thisproblem n=4, profit P = (2,1,4,3) , weight of knapsack W = (5,3,7,6) and capacity of knapsack is C=11. SOL: Search tree of fixed length tuple approach is: ProblemsProblems The solution state are 17 and 19 representing the solution (1,0,0,1) and (0,1,1,0) respectively with the same profit 5
  • 14.
    Search tree ofvariable length tuple approach is: The solution state are 8 and 9 representing the solution (1,4) and (2,3) respectively with the same profit 5.
  • 15.
    Q2:Q2: In thisproblem n=4, profit P = (3,5,6,10) , weight of knapsack W = (2,3,4,5) and capacity of knapsack is C=8. Practice QuestionPractice Question
  • 16.
    ReferenceReference ‱ www.google.comwww.google.com ‱ www.encyclopedia.comwww.encyclopedia.com ‱www.wikipedia.comwww.wikipedia.com ‱ Design Method and Analysis of Algorithm By S.K Basu.
  • 17.