Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

by HITESH SAHANI

(rollno- k18ms 11816118)


1. SUBSET SUM PROBLEM (USING BACKTRACKING)

This is a simple algorithm, but it demonstrates that sometimes you need to return
to a previous state and re-evaluate a previous decision in order to solve a
problem.
BACKTRACKING is a general algorithmic technique that considers searching every
possible combination in order to solve an optimization problem. Backtracking
uses depth-first-search approach. By inserting more knowledge of the problem,
the search tree can be pruned to avoid considering cases that don’t look
promising. While backtracking is useful for hard problems to which we do not
know more efficient solutions, it is a poor solution for the everyday problems that
other techniques are much better at solving.

complexity
- worst case time complexity: Ɵ(2^n)
- space complexity: Ɵ(1)

example….
Write an algorithm of sum of subsets. Solve following problem and draw portion of state space tree M =
35, W = (5, 7, 10, 12, 15, 18, 20)

Problem statement:

Let, S=S1….S−n

be a set of n positive integers, then we have to find a subset whose sum is equal to given positive
integer d.It is always convenient to sort the set’s elements in ascending order. That is,
S1≤S2≤….≤Sn

Algorithm:

Let, S is a set of elements and m is the expected sum of subsets. Then:


1. Start with an empty set.
2. Add to the subset, the next element from the list.
3. If the subset is having sum m then stop with that subset as solution.
4. If the subset is not feasible or if we have reached the end of the set then backtrack
through the subset until we find the most suitable value.
5. If the subset is feasible then repeat step 2.
6. If we have visited all the elements without finding a suitable subset and if no
backtracking is possible then stop without solution.

Example:

Solve following problem and draw portion of state space tree M = 35, W = (5, 7, 10, 12, 15, 18,
20)
(roll no- k18ms 11816118)
2. .ASSIGNMENT PROBLEM (using BRANCH AND BOUND
ALGORITHM)
● Assignment problem refers to special class of linear programming
problems that involves determining the most efficient assignment of
people to projects,salespeople to territories, contracts to bidders and so
on.
● It is often used to minimize total cost or time of performing task.
● One important characteristic of assignment problems is that only one job
is assigned to one machine(or project)
● Each Assignment problem has a Matrix associated with it.
● The number in the table indicated COST associated with the assignment
● The most efficient linear programming algorithm to find optimum
solution to an assignment problem is Branch and Bound method.

Given n jobs (j1,j2…..jn) and n persons(p1…pn) it is required to assign all n jobs


to all n persons with the contraints that one job has to be assigned to one
person and the cost involved in completing all jobs should be minimum.
JOBS

9 2 1 8
6 4 3 7
5 8 1 8
7 6 9 4
a
b
c
d
ALGORITHM
/* find min cost uses least() and Add() to maintain the list of live nodes */
/*least() finds a live node with least cost, deletes it from the list and return it */
/*Add(x) calculates the cost of x and adds it to the list of live nodes */
/*Implements list of live nodes as a min heap*/

// Search Space Tree Node


node
{
int job_number;
int worker_number;
node present;
int cost;
}
//Input: cost matrix of job Assignment Problem
//Output:optimal cost and Assigment of jobs
Find mincost(cost Matrix mat [][])
//Intitalize list of live nodes(min heap)
//with root of search tree ie Dummy node
while(true)
//Find live node with least estimated cost
E🡨least();
//The found node is delted from the list of live nodes
if(E is leafnode)
printsolution()
return
for each child X of E
Add(x): //Add X to list of live nodes
(X🡪parent)🡨E//pointer for path to root

roll no- k18ms 11816118 (hitesh sahani)

3. KNAPSACK PROBLEM
(using DYNAMIC PROGRAMMING )

The Knapsack problem and a dynamic programming solution


The knapsack problem is a problem in combinatorial optimization:
given a set of items, each with a weight and a value, determine the
number of each item to include in a collection so that the total weight
is less than or equal to a given limit and the total value is as large as
possible. It derives its name from the problem faced by someone who
is constrained by a fixed-size knapsack and must fill it with the most
valuable items [17]. The most famous knapsack problem is the binary
(0–1) knapsack problem, where the decision maker is allowed to pick
(1) or not to pick (0) the item, in other words, the items are not
dividable.
The problem can be formulated as follows:
Let there be n items, x1 to xn where xi has the vi value and weight wi.
The maximum weight the knapsack can carry is W. It is common to
assume that all values and weights are non-negative. To simplify the
representation, it is also assumed that the items are listed in
increasing order of weight.
COMPELXITY
The dynamic programming algorithm for the knapsack problem has a time complexity
of O(nW)O(nW) where nn is the number of items and WW is the capacity of the knapsack.

EXAMPLE
KNAPSACK ALGORITHM
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi<= w // item i can be part of the solution
if bi+ B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi+ B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi> w

You might also like