Branch and Bound Algorithm

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only




Problem Description

Here's a  program that demonstrates "Exhaustive" and "Branch and Bound" search methods for finding the
best solution for "Knapsack" problems.  It was adapted with author's kind permission from the book "Ready-to-Run Delphi 3.0 Algorithms", Rod Stephens, Wiley Computer Publishing.

Background & Techniques

The Knapsack Problem requires that we figuratively fill a knapsack by selecting from a set of items whose
weight (or other "cost" measurement) and value (or "profit") are known. The objective is to maximize the value (profit) of the items in the sack without exceeding a predefined weight (cost) limit.

The "Exhaustive Search" method tries every combination of including or excluding items to find the valid set with the largest value .  For N items, there are 2N  combinations because for each item there are 2 choices; include the item or exclude this item.  Actually, we'll call it  2N - 1 because the empty solution is not really a solution.  The possible combinations can be represented as a "binary tree" with the each path from the starting "root" to the terminal "leaf" representing a unique set of items. These paths or combinations can also be represented by the binary notation of numbers from 0 to 2^N-1 . So, for example with 3 items, 8 combinations, the path including items 1 and 2, but excluding item 3 could be represented by the number 6 (binary 110).  Exhaustive search times for current PCs soon become unreasonable when we have more than  30 items to choose from.  The 30 item  decision tree has about  a billion possible paths.   My Dell Studio 17 Laptop checks about 50 million paths per second so takes 20 seconds to solve the 30 item case.  Each additional item will double the number of paths to check and  double the time to solve by exhaustive search.

"Branch and bound" is an algorithm that significantly reduces the number of combinations to be checked. It "prunes" the search tree" by aborting search paths when they could not not possibly improve the best solution found to date.  The program  allows viewing the process step by step here is a simple example:

Assume we have three items  with [Cost, Profit] values of [30,8], [25,4]. [40,3] and maximum cost of 50.  Well  start the process down the right side of the tree by including item #1.  The 4 paths including item #1 are {#1}, {#1,#2}, {#1,#3} and {#1,#2,#3}.  Adding  either item #2 or #3 would exceed our cost limit so out first "best" solution is just item #1 with profit of 8.    Now we have 3 paths left to check, {#2}, {#2,#3} and  {#3}.   But before doing that we'll do the Branch and Bound trick of checking the maximum profit if item #1 is excluded.  That number is the maximum profit if all items could be included (15) minus the profit represented by the solution so far (8)  So the maximum profit from any path on the left side the tree is 7  (15 - 8 = 7). That number is less than our best profit so far (8), so no need to check any of those paths and we are done!  A example with 4 items might  be more convincing, but hard to explain.  Run Branch and Bound on a small case with the "Visualize steps" box checked for further study.

The Demo page is self explanatory. You can enter you own data or generate random cases and solve them using either algorithm. The "Start" button will turn into a "Stop" button during solving in case a problem is running longer than you care to wait. For instructional purposes, you can see click the checkbox at the top right of the form to follow the steps taken by Branch and Bound algorithm as it finds the best solution. there are "Save and "Load" buttons in case you want to preserve a case for future reference or for reporting a bug to me,

I want to thank author Rod Stephens for permission to use his program as the basis for this one and for the book that helped me understand how it works.   The book is   "Ready-to-Run Delphi 3.0 Algorithms", Rod Stephens, Wiley Computer Publishing.  It was an experiment by Rod into Delphi as a second (or third or fourth) language and he has since concentrated on Visual Basic and C#.  That's OK, I have investigated VB,  (the best "other" language for beginners IMO), but reverted to Delphi.  Experienced programmers typically have a "native" language as their language of choice.  Rod runs websites  VB Helper ( and C# Helper ( where we might find items of interest to Delphians.   

Running/Exploring the Program 

bullet Download source
bulletDownload  executable

Suggestions for Further Explorations

Showing an animated tree structure would be helpful for better understanding the search algorithms. .


Original:  November 9, 2011

Modified:  May 15, 2018

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.