The document describes the branch and bound algorithm for solving the 0/1 knapsack problem using breadth-first search and best-first search. It involves representing the problem as a state space tree where each node is a partial solution and branches correspond to including or excluding an item. It prioritizes exploring nodes with higher estimated profit (bound) to prune non-promising parts of the search space and find the optimal solution more efficiently.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
103 views21 pages
0-1knapsack-Branch and Bound
The document describes the branch and bound algorithm for solving the 0/1 knapsack problem using breadth-first search and best-first search. It involves representing the problem as a state space tree where each node is a partial solution and branches correspond to including or excluding an item. It prioritizes exploring nodes with higher estimated profit (bound) to prune non-promising parts of the search space and find the optimal solution more efficiently.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21
Branch and Bound
0/1 Knapsack Problem
Book:Foundations of Algorithms, by Richard Neapolitan, Kumarss Naimipour, Narosa Publication 0/1 Knapsack Problem (Using Breadth First Search) • Set of items with weight and profit • W is a capacity or size of the Knapsack • Knapsack to be filled by considering items one by one with the aim of maximum profit • Either particular item to be considered fully or reject fully in filling the Knapsack • Item will not be considered when the item considered for filling Knapsack gets overflow • Initially items are to be arranged in Descending order based on its unit profit. State Space Tree • Root node is Start node • Every node will have two branches • One branch for inclusion of item in Knapsack and other for rejection of item from Knapsack • Number of items represent Total Level in the Tree • Every level is for processing a new item • Each path from root to a leaf is a candidate solution • Best Solution: it is a value which represents maximum profit obtained during the processing of nodes from start to current node. It is called as Max Profit. • Max Profit is updated when inclusion of a new node gives maximum profit than the best solution obtained so far • Total Weight : sum of the weights of the added items in the knapsack • Nodes are visited Level by level, and in each level nodes are visited from left to right • Total Profit : Sum of the profits of the added item in the Knapsack • Bound : A value at a node represents sum of (profit obtained so far and profit of the unprocessed items to exactly fill the Knapsack) • Non-Promising (i) : when weight >= W, we use equal here because in optimization we should expand to the children for promising • Non-Promising (ii) : bound <= Best solution so far • We set bound to 0 when weight is not less than W • When bound is > Best solution so far(max Profit), we visit its children • Root node is a starting node • Root node is set with profit=0 and weight=0. Bound to be calculated • When a node is promising, all its children are visited Problem • N = 4 W = 16 • i pi wi pi/wi • 1 $40 2 $20 • 2 $30 5 $6 • 3 $50 10 $5 • 4 $10 5 $2 Pruned State Space Tree • void breadth_first_branch_and_bound(state space tree T, number& best) • { • queue_of_node Q; • node u,v; • initialize(Q); • v=root of T; • enqueue(Q,v); • best = value(v); • • while( !empty Q) • { • dequeue(Q,v); • for( each child u of v) • { • if (value(u) is better than best) • best = value(u); • if (bound(u) is better than best) • enqueue(Q,u); • } • } • void knapsack2(int n, const int p[], const int w[], int W, int& maxprofit) • { • queueof_node Q; • node u,v; • initialize(Q); • v.level =0; v.profit=0; v.weight=0; • maxprofit =0; • enqueue(Q,v); • while( !empty(Q)) • { • dequeue(Q,v); • u.level = v.level+1; • u.weight= v.weight+w[u.level]; • u.profit=v.profit+p[u.level]; • if (u.weight<=W && u.profit > maxprofit) • maxprofit = u.profit; • if bound(u) > maxprofit) • enqueue(Q,u); • u.weight = v.weight; • u.profit = v.profit; • if (bound(u) > maxprofit) • enqueue(Q,u); • } • } • float bound(node u) • { • index j,k; • int float weight; • float result; • if (u.weight >= W) • return 0; • else • { • result =u.profit; • j=u.level+1; • totweight = u.weight; • while( j<=n && totweight +w[j] <=W) • { • totweight = totweight + w[j]; • result = result + p[j]; • j++; • } • k=j; • if (k<=n) • { • result = result + (W-totweight)*p[k]/w[k]; • return result; • } • } 0/1 Knapsack Problem (Using Best First Search) • Breadth First Search is not advantage over backtracking. • It is improved by selecting best promising node for expansion • After visiting Root node, expand this node by visiting its children • After visiting and processing children nodes, select the best node for expansion. (Best node means a node with more bound value) • Proceed this until all promising nodes are expanded PROBLEM • N = 4 W = 16 • i pi wi pi/wi • 1 $40 2 $20 • 2 $30 5 $6 • 3 $50 10 $5 • 4 $10 5 $2 Pruned State Space Tree (Best First Tree Traversal) • void best_first_branch_and_bound ( state_space_tree T, number& best) • { • priority_queue_of_node PQ; • node u,v; • initialize(PQ); • v=root of T; • best = value(v); • insert(PQ,v); • while(!empty (PQ)) • { • remove(PQ, v); • if (bound(v) is better than best) • for ( each child u of v) • { • if (value(u) is better than best) • (best = value(u)); • if (bound(u) is better than best) • insert(PQ, u); • } • } • void knapsack3( int n, const int p[], int W, • const int w[], int& maxprofit) • { • priority queue of node PQ; • node u,v; • initialize (PQ); • v.level=0; v.profit=0; v.weight=0; • maxprofit =0; • v.bound = bound(v); • insert(PQ,v); • while (!empty(PQ)) • { • remove(PQ,v); • if (v.bound > maxprofit) • { • u.level = v.level +1; • u.weight = v.weight+w[u.level]; • u.profit = v.profit + p[u.level]; • if (u.weight<= W && u.profit > maxprofit) • maxprofit = u.profit; • u.bound = bound(u); • if (u.bound > maxprofit) • insert(PQ,u); • u.weight = v.weight; • u.profit = v.profit; • u.bound = bound(u); • if (u.bound > maxprofit) • insert(PQ,u); • } • } • } •