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.
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