Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

KNAPSACK PROBLEM

The Knapsack problem is an example of the combinational optimization


problem. This problem is also commonly known as the “Rucksack
Problem“. The name of the problem is defined from the maximization
problem as mentioned below:
 Given a bag with maximum weight capacity of W and a set of
items, each having a weight and a value associated with it.
Decide the number of each item to take in a collection such that
the total weight is less than the capacity and the total value is
maximized.
Applications of the Knapsack Problem:

The Knapsack problem has several real-life applications.


Some of them are mentioned here:
 One of the early applications of the Knapsack problem

was in construction and scoring of exams in which the


test takers have a choice as to which questions they
answer.
 The subset sum problem is solved using the concept of

the Knapsack problem.


 The multiple objective variations of the Knapsack

problem is frequently used for transportation logistics


optimization problems.
 The multiple knapsack problem is often used in many

loading and scheduling algorithms in Operational


Research.
BACKTRACKING
Backtracking is one of the techniques that can be used to solve the
problem. We can write the algorithm using this strategy. It uses the
Brute force search to solve the problem, and the brute force search
says that for the given problem, we try to make all the possible
solutions and pick out the best solution from all the desired
solutions. This rule is also followed in dynamic programming, but
dynamic programming is used for solving optimization problems. In
contrast, backtracking is not used in solving optimization problems.
Backtracking is used when we have multiple solutions, and we
require all those solutions.

Backtracking name itself suggests that we are going back and


coming forward; if it satisfies the condition, then return success,
else we go back again. It is used to solve a problem in which a
sequence of objects is chosen from a specified set so that the
sequence satisfies some criteria.
When to use a Backtracking algorithm?
When we have multiple choices, then we make the decisions from
the available choices. In the following cases, we need to use the
backtracking algorithm:

o A piece of sufficient information is not available to make the


best choice, so we use the backtracking strategy to try out all
the possible solutions.
o Each decision leads to a new set of choices. Then again, we
backtrack to make new decisions. In this case, we need to use
the backtracking strategy.
Basic Terminologies
 Candidate: A candidate is a potential choice or element that can be
added to the current solution.
 Solution: The solution is a valid and complete configuration that
satisfies all problem constraints.
 Partial Solution: A partial solution is an intermediate or incomplete
configuration being constructed during the backtracking process.
 Decision Space: The decision space is the set of all possible
candidates or choices at each decision point.
 Decision Point: A decision point is a specific step in the algorithm where
a candidate is chosen and added to the partial solution.
 Feasible Solution: A feasible solution is a partial or complete solution
that adheres to all constraints.
 Dead End: A dead end occurs when a partial solution cannot be
extended without violating constraints.
 Backtrack: Backtracking involves undoing previous decisions and
returning to a prior decision point.
 Search Space: The search space includes all possible combinations of
candidates and choices.
 Optimal Solution: In optimization problems, the optimal solution is the
best possible solution.

Types of Backtracking Problems

Problems associated with backtracking can be categorized into 3


categories:
 Decision Problems: Here, we search for a feasible solution.
 Optimization Problems: For this type, we search for the best solution.
 Enumeration Problems: We find set of all possible feasible solutions to
the problems of this type.
Applications of Backtracking

o N-queen problem
o Sum of subset problem
o Graph coloring
o Hamiliton cycle

The backtracking algorithm explores various paths to find


a sequence path that takes us to the solution. Along these
paths, it establishes some small checkpoints from where
the problem can backtrack if no feasible solution is found.
This process continues until the best solution is found.
In the above figure, green is the start point, blue is the
intermediate point, red are points with no feasible
solution, grey is the end solution.

When backtracking algorithm reaches the end of the


solution, it checks whether this path is a solution or not. If
it is the solution path, it returns that otherwise, it
backtracks to one step behind in order to find a solution.
The 4 Queens Problem consists in placing four queens on
a 4 x 4 chessboard so that no two queens attack each
other. That is, no two queens are allowed to be placed on
the same row, the same column or the same diagonal.

We are going to look for the solution for n=4 on a 4 x 4


chessboard in this article.
4 Queens Problem using Backtracking Algorithm:
Place each queen one by one in different rows, starting from the
topmost row. While placing a queen in a row, check for clashes
with already placed queens. For any column, if there is no clash
then mark this row and column as part of the solution by placing
the queen. In case, if no safe cell found due to clashes, then
backtrack (i.e, undo the placement of recent queen) and return
false.
Illustration of 4 Queens Solution:
Step 0: Initialize a 4×4 board.
Step 1:
 Put our first Queen (Q1) in the (0,0) cell .
 ‘x‘ represents the cells which is not safe i.e. they are under
attack by the Queen (Q1).
 After this move to the next row [ 0 -> 1 ].

Step 2:
 Put our next Queen (Q2) in the (1,2) cell .
 After this move to the next row [ 1 -> 2 ].
Step 3:
 At row 2 there is no cell which are safe to place Queen (Q3) .
 So, backtrack and remove queen Q2 queen from cell ( 1, 2 ) .
Step 4:
 There is still a safe cell in the row 1 i.e. cell ( 1, 3 ).
 Put Queen ( Q2 ) at cell ( 1, 3).
Step 5:
 Put queen ( Q3 ) at cell ( 2, 1 ).
Step 6:
 There is no any cell to place Queen ( Q4 ) at row 3.
 Backtrack and remove Queen ( Q3 ) from row 2.
 Again there is no other safe cell in row 2, So backtrack again
and remove queen ( Q2 ) from row 1.
 Queen ( Q1 ) will be remove from cell (0,0) and move to next
safe cell i.e. (0 , 1).
Step 7:
 Place Queen Q1 at cell (0 , 1), and move to next row.

Step 8:
 Place Queen Q2 at cell (1 , 3), and move to next row.
Step 9:
 Place Queen Q3 at cell (2 , 0), and move to next row.

Step 10:
 Place Queen Q4 at cell (3 , 2), and move to next row.
 This is one possible configuration of solution
Follow the steps below to implement the idea:
 Make a recursive function that takes the state of the

board and the current row number as its parameter.


 Start in the topmost row.

 If all queens are placed, return true

 For every row.

o Do the following for each column in current row.

o If the queen can be placed safely in this

column
o Then mark this [row, column] as

part of the solution and recursively


check if placing queen here leads to
a solution.
o If placing the queen in [row,
column] leads to a solution, return true.
o If placing queen doesn’t lead to a solution

then unmark this [row, column] and track


back and try other columns.
 If all columns have been tried and nothing worked,

return false to trigger backtracking.


 Sum of Subsets:
 Given positive numbers wi, 1 ≤ i ≤ n, and m, this problem
requires finding all subsets of wi whose sums are ‘m’.
 All solutions are k-tuples, 1 ≤ k ≤ n. Explicit constraints:
 xi Є {j | j is an integer and 1 ≤ j ≤ n}.

 Implicit constraints:
 No two xi can be the same.

 The sum of the corresponding wi’s be m.

 xi < xi+1 , 1 ≤ i < k (total order in indices) to avoid generating
multiple instances of the same subset (for example, (1, 2, 4)
and (1, 4, 2) represent the same subset).

 A better formulation of the problem is where the solution
subset is represented by an n-tuple (x1, . . . . . , xn) such that
xi Є {0, 1}.
 The above solutions are then represented by (1, 1, 0, 1) and
(0, 0, 1, 1). For both the above formulations, the solution
space is 2n distinct tuples.
 For example, n = 4, w = (11, 13, 24, 7) and m = 31, the desired
subsets are (11,
 13, 7) and (24, 7).
The tree corresponds to the variable tuple size formulation. The
edges are labeled such that an edge from a level i node to a level
i+1 node represents a value for xi. At each node, the solution space
is partitioned into sub - solution spaces. All paths from the root node
to any node in the tree define the solution space, since any such
path corresponds to a subset satisfying the explicit constraints.
The possible paths are (1), (1, 2), (1, 2, 3), (1, 2, 3, 4), (1, 2, 4), (1,
3, 4), (2), (2,
3) , and so on. Thus, the left mot sub-tree defines all subsets
containing w1, the next sub-tree defines all subsets containing w2
but not w1, and so on.

You might also like