0% found this document useful (0 votes)
43 views6 pages

Experiment No.5: Branch and Bound Algorithm

The document describes an experiment to implement the 0/1 knapsack problem using the branch and bound algorithm. It provides the problem statement, explains the branch and bound algorithm approach, gives pseudocode for the branch and bound knapsack algorithm, and provides analysis of the time and space complexity. It also includes C code implementation of the branch and bound knapsack algorithm.

Uploaded by

Amisha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

Experiment No.5: Branch and Bound Algorithm

The document describes an experiment to implement the 0/1 knapsack problem using the branch and bound algorithm. It provides the problem statement, explains the branch and bound algorithm approach, gives pseudocode for the branch and bound knapsack algorithm, and provides analysis of the time and space complexity. It also includes C code implementation of the branch and bound knapsack algorithm.

Uploaded by

Amisha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.

5: Branch and Bound Algorithm


Date:

Aim: To implement the following Algorithms using the Branch and Bound technique:
a) 0/1 Knapsack using Branch and Bound

Theory:

 The Branch and Bound algorithm is used to solve optimization problems, particularly
combinatorial optimization problems.
 It involves dividing the problem into smaller subproblems and constructing a search
tree.
 The algorithm starts with an initial solution and initializes upper and lower bounds.
 The search process begins by exploring the search tree using a depth-first search
(DFS) or breadth-first search (BFS) strategy.
 There are primarily three strategies for branch and bound : FIFO ( First In First Out),
LIFO ( Last In First Out), and LC (Least Cost).
 At each node of the search tree, the algorithm evaluates the bounds to determine if
further exploration is warranted.
 If the lower bound of a node is higher than the current best solution, the subtree
rooted at that node is pruned.
 If the upper bound of a node is lower than the current best solution, the algorithm
backtracks to the parent node.
 At each node, a decision is made to include or exclude a particular element or
constraint from the solution.
 The current solution and the bounds are updated accordingly.
 The algorithm continues exploring the search tree until all nodes have been visited or
until the best solution is found.
 The final solution obtained is the optimal solution to the optimization problem.

116 | P a g e
211105032
a)0/1 Knapsack
Date:

Problem Statement: Solve the 0/1 knapsack problem for the following knapsack instance
M = 12, N = 4, P(1 .. 4) = {30,28,20,24}, W(1 .. 4) = {5,7,4,2}

Algorithm:

Algorithm BNBKnap(int k, int cp, int cw) if (c <= upper)


{ {
c = Bound(k, cp, cw); y[k] = 1;
u = UBound(k, cp, cw); if (k < n - 1)
if (u < upper && cw <= m) {
upper = u; //Update global upper bound if (cw <= m)
// Left child

117 | P a g e
211105032
BNBKnap(k + 1, cp + K[k].p, cw + {
K[k].w); //Recursively go to left child BNBKnap(k + 1, cp, cw);
} }
if (k == n - 1) if (k == n - 1)
{ {
if (cw + K[k].w <= m) //Calculate new bounds for right child
{ c = Bound(k, cp - K[k].p, cw - K[k].w);
fp = cp + K[k].p; u = UBound(k, cp - K[k].p, cw - K[k].w);
fw = cw + K[k].w; if (cw <= m && c <= upper)
Print final profit; {
for (j = 0; j < k; j++) fp = cp;
x[j] = y[j]; fw = cw;
x[k] = 1; Print final profit;
Print updated array; for (j = 0; j < k; j++)
} x[j] = y[j];
} x[k] = 0;
} Print updated array;
// Right child }
if (c <= upper) }
{ }
y[k] = 0; }
if (k < n - 1)

Time complexity:
O(2n + n2)

Space complexity:
O(n)

Code:
#include <stdio.h> int b = cp, c = cw;
#include <math.h> for (int i = k; i < n; i++)
#define MAX 100 {
struct knap c += K[i].w;
{ if (c < m)
int p; b += K[i].p;
int w; else
float rat; {
}; return (-1) * (b + (int)ceil((1 - (c - m) /
int m, n, x[MAX], y[MAX], z[MAX], fp, fw, (float)K[i].w) * K[i].p));
upper = __INT32_MAX__; }
int Bound(int k, int cp, int cw, struct knap K[]) }
{ return -b;

118 | P a g e
211105032
} for (int j = 0; j < k; j++)
int UBound(int k, int cp, int cw, struct knap x[j] = y[j];
K[]) x[k] = 1;
{ printf("Array updated.\n");
int b = cp, c = cw; for (int i = 0; i < n; i++)
for (int i = k; i < n; i++) printf("%d ", x[i]);
{ printf("\n");
if (c + K[i].w <= m) }
{ }
c += K[i].w; }
b += K[i].p; // Right child
} if (c <= upper)
} {
return -b; y[k] = 0;
} if (k < n - 1)
void BNBKnap(int k, int cp, int cw, struct knap {
K[]) printf("Right Child of K:%d P:%d W:
{ %d.\n", k + 1, cp, cw);
int c = Bound(k, cp, cw, K); BNBKnap(k + 1, cp, cw, K);
int u = UBound(k, cp, cw, K); }
printf("%d %d \n", c, u); if (k == n - 1)
if (u < upper && cw <= m) {
upper = u; printf("Right Child of K:%d P:%d W:
// Left child %d\n", k + 1, cp, cw);
if (c <= upper) int c = Bound(k, cp - K[k].p, cw - K[k].w,
{ K);
y[k] = 1; int u = UBound(k, cp - K[k].p, cw -
if (k < n - 1) K[k].w, K);
{ printf("%d %d \n", c, u);
printf("Left Child of K:%d CP:%d CW: if (cw <= m && c <= upper)
%d\n", k + 1, cp, cw); {
if (cw <= m) fp = cp;
BNBKnap(k + 1, cp + K[k].p, cw + fw = cw;
K[k].w, K); printf("New cost is %d.\n", fp);
} for (int j = 0; j < k; j++)
if (k == n - 1) x[j] = y[j];
{ x[k] = 0;
printf("Left Child of K:%d P:%d W:%d\ printf("Array updated.\n");
n", k + 1, cp, cw); for (int i = 0; i < n; i++)
printf("%d %d \n", c, u); printf("%d ", x[i]);
if (cw + K[k].w <= m) printf("\n");
{ }
fp = cp + K[k].p; }
fw = cw + K[k].w; }
printf("New cost is %d.\n", fp); }

119 | P a g e
211105032
void sort(struct knap K[]) {
{ K[i].p = p_values[i];
struct knap key; K[i].w = w_values[i];
for (int i = 1; i < n; i++) K[i].rat = (float)K[i].p / K[i].w;
{ }
key = K[i]; sort(K);
int j = i - 1; fp = -1;
while (j >= 0 && K[j].rat < key.rat) BNBKnap(0, 0, 0, K);
{ printf("The final array is:\n");
K[j + 1] = K[j]; for (int i = 0; i < n; i++)
j = j - 1; {
} for (int j = 0; j < n; j++)
K[j + 1] = key; {
} if (p_values[i] == K[j].p && w_values[i]
} == K[j].w)
int main() {
{ if (x[j])
printf("Enter number of knapsack values z[i] = 1;
and size of knapsack.\n"); else
scanf("%d%d", &n, &m); z[i] = 0;
struct knap K[n + 1]; }
int p_values[MAX], w_values[MAX]; }
printf("Enter profits of knapsack.\n"); }
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
scanf("%d", &p_values[i]); printf("%d ", z[i]);
printf("Enter weights of knapsack.\n"); printf("\nThe max profit is %d.\n", fp);
for (int i = 0; i < n; i++) return 0;
scanf("%d", &w_values[i]); }
for (int i = 0; i < n; i++)

120 | P a g e
211105032
Output:

Conclusion:
Optimization problems were studied and implemented using the Branch and Bound
Programming Algorithm. 0/1 Knapsack Problem was implemented using the Branch and
Bound Approach.

121 | P a g e
211105032

You might also like