Branch and Bound
Branch and bound is an algorithm design paradigm for discrete and combinatorial optimization problems. It systematically enumerates candidates, eliminating suboptimal candidates through bounding functions.
Java example for 01 knapsack problem:


C++ example:


Python example:


Key ideas are systematically enumerating candidates and pruning inefficient branches. Allows finding optimal solutions to hard optimization problems.
Many problems involve minimizing or maximizing a target value: find the shortest path, get the maximum profit, etc. They’re called optimization problems. When the solution is a sequence of choices, we often use a strategy called branch and bound.
Its aim is to gain time by quickly detecting and discarding bad choices. To understand how bad choices are detected, we first need to learn the concepts of upper and lower bounds.
Richard Feynman Explanation
Let’s say we’re in a maze and we’re trying to find the quickest way out. Now, we don’t know the way, but we’re smart enough to know some tactics that might help. For instance, we know that going back to a place we’ve already been is going to be a waste of time.
Now, let’s imagine this maze is like a big, blooming tree. We’re sitting at the base of the tree, and the exit to the maze is somewhere out along one of the branches. We could explore every single branch, going up and down every twig, but that could take a really long time and we might end up going down a lot of deadends.
So we use a technique called “branch and bound”. Here’s how it might work. First, we make a guess about which branches are most likely to get us to the exit quickest  maybe they’re the ones pointing towards the sun, or towards the sound of a river. This guess is our “bound”  we’re going to ignore all other branches for now.
Then, we explore  we go “branching”. We follow these branches as far as we can go. If we reach a deadend, we backtrack and try the next branch. We’re optimistic but not stubborn; if a branch we thought was promising is taking too long, we’re not afraid to backtrack and try a different one.
As we keep exploring, we update our “bound”. Maybe we realize that branches pointing towards the sun are not as promising as we thought, but the ones towards the wind seem to be working. We shift our focus, and keep going.
That’s the basic idea behind the “branch and bound” method. In our search for the exit, we continuously make educated guesses (bounds) and explore our options (branching). It’s a clever balance of exploration and focus, helping us find the best solution without wasting time on unfruitful paths.
Robin Williams Explanation
Sure thing! Imagine you’re in the greatest game show of your life. You’re standing in a huge maze filled with prizes, and you’ve got a choice of hundreds of different paths. Each path could lead you to the grand prize, or it could be a dead end. Sounds pretty daunting, doesn’t it? Well, this is where branch and bound comes in.
Think of branch and bound as your overly enthusiastic, superspeedy game show host. He’s running around the maze, checking out every possible path for you. But he’s not just randomly sprinting around. Oh no, he’s got a strategy.
First, he’s ‘branching’  splitting the problem into smaller, more manageable chunks. Each path in the maze is a ‘branch’. But he’s not going to waste time exploring every single branch. Instead, he’s going to make some educated guesses about which branches are most likely to get you to that grand prize.
This is where the ‘bound’ part comes in. Your host is going to set some boundaries or limits. He’s going to rule out the paths that, based on his super host intuition, are unlikely to lead to the prize. Maybe they’re too long, or they go in the wrong direction. By bounding these out, he can focus on the more promising branches.
So, branch and bound is all about making a big, complex problem  like winning the grand prize in a giant maze  a little less overwhelming. It’s about breaking things down, making some smart guesses, and focusing your efforts where they’re most likely to pay off. And all the while, you’ve got this highenergy, fasttalking game show host leading the way. So come on down, let’s play Branch and Bound!
My Notes
Many problems involve minimizing or maximizing a target value: find the shortest path, get the maximum profit, etc. They’re called optimization problems. When the solution is a sequence of choices, we often use a strategy called branch and bound.
Its aim is to gain time by quickly detecting and discarding bad choices. To understand how bad choices are detected, we first need to learn the concepts of upper and lower bounds.
Upper and Lower Bounds
Bounds refer to the range of a value. An upper bound sets a limit on how high the value can be. A lower bound is the least one can hope for: it guarantees the value is equal to it or greater.
We can often easily get suboptimal solutions: a short path, but maybe not the shortest; a big profit, but maybe not the biggest. These solutions provide bounds to the optimal solution.
For instance, the shortest route between two places is never shorter than their straight linear distance. The linear distance is thus a lower bound of the shortest driving distance.
How Does it Work?
 Divide the problem into subproblems.
 Find upper and lower bounds of each new subproblem.
 Compare subproblem bounds of all branches.
 Return to step 1 with the most promising subproblem.
The backtracking strategy also led to the solution without exploring every possible solution candidate. In backtracking, we remove paths after having explored them as far as we can, and we stop when we’re OK with a solution. With branch and bound, we predict which paths are worst and we avoid wasting energy exploring them.
Implementation
Branch and bound method is used when we can evaluate cost of visiting each node by a utility functions. At each step, we choose the node with lowest cost to proceed further.
Branch and Bound algorithms are implemented using a priority queue. In branch and bound, we traverse the nodes in breadthfirst manner.
Backtracking vs Branch and Bound
Branchandbound design technique is similar to backtracking in the sense that it generates a search tree and looks for one or more solutions. However, while backtracking searches for a solution or a set of solutions that satisfy certain properties (including maximization or minimization), branchandbound algorithms are typically concerned with only maximization or minimization of a given function.
Moreover, in branchandbound algorithms, a bound is calculated at each node x on the possible value of any solution given by nodes that may later be generated in the subtree rooted at x. If the bound calculated is worse than a previous bound, the subtree rooted at x is blocked, i.e., none of its children are generated.
Searching a Tree
As a branchandbound algorithm searches a tree, it keeps track of the best solution it has found so far and the best possible solution that it can find from the current point in the tree.
If the best possible solution from that point cannot beat the current best solution, the algorithm abandons that node and considers other paths through the tree.
Branch and bound can be much faster than exhaustive search for finding an optimal solution, but it doesn’t change the algorithm’s Big O run time.
A* Algorithm
A* is sort of an elaboration on branchandbound. In branchandbound, at each iteration we expand the shortest path that we have found so far.
In A*, instead of just picking the path with the shortest length so far, we pick the path with the shortest estimated total length from start to goal, where the total length is estimated as length traversed so far plus a heuristic estimate of the remaining distance from the goal.
Branchandbound will always find an optimal solution that is shortest path. A* will always find an optimal solution if the heuristic is correct. Choosing a good heuristic is the most important part of A* algorithm.
Branch and Bound: A Strategy for Optimization Problems
In the world of computational algorithms, one common type of problem is the optimization problem. This involves finding the optimal, or “best”, solution to a problem within a given set of constraints. The goal could be to minimize something, such as finding the shortest path between two points, or maximize something, such as achieving the maximum profit in a business scenario.
For problems where the solution involves a sequence of decisions or choices, a strategy often employed is called “branch and bound”.
Understanding Upper and Lower Bounds
In the context of optimization problems, bounds refer to the limits within which a target value may fall. An upper bound sets the maximum limit for the value, while a lower bound sets the minimum. In other words, the upper bound is the worst case scenario while the lower bound is the best case scenario.
In many cases, it’s relatively straightforward to find a suboptimal solution, one that isn’t necessarily the best but is “good enough”. Such solutions can provide us with upper and lower bounds for the optimal solution.
For example, if we want to find the shortest driving distance between two places, we know that it can never be less than the straightline distance between them. Therefore, this straightline distance serves as a lower bound for our problem.
Working with Branch and Bound
The branch and bound strategy revolves around four steps:
Divide the problem into subproblems: This step involves breaking down the original problem into smaller, more manageable parts.
Find upper and lower bounds of each new subproblem: Here we try to determine the best and worst possible outcomes for each subproblem.
Compare subproblem bounds of all branches: We examine all the bounds to identify the most promising branches (i.e., those that seem most likely to lead us to the optimal solution).
Return to step 1 with the most promising subproblem: The process is repeated, continually narrowing down the scope of the problem and discarding branches that don’t appear promising.
The key advantage of branch and bound is that it allows us to discard certain branches early on, saving us from expending computational resources on paths that are unlikely to yield the optimal solution.
Implementing Branch and Bound
The branch and bound method is commonly used when each node can be evaluated for its cost or potential utility through a specific function. At each step, the node with the lowest cost is selected for further exploration.
To implement a branch and bound algorithm, a priority queue is typically used. The nodes are traversed in a breadthfirst manner, meaning that all the nodes at a particular depth in the tree are visited before moving on to the next depth.
Backtracking vs. Branch and Bound
While both branchandbound and backtracking techniques generate a search tree and look for solutions, the primary difference lies in their focus. Backtracking is used to find a solution or a set of solutions satisfying certain properties, while branchandbound is mainly concerned with finding the maximum or minimum of a given function.
In branchandbound algorithms, a bound is calculated at each node to estimate the value of potential solutions in the subtree rooted at that node. If this calculated bound doesn’t improve upon a previously established bound, the subtree is abandoned, saving resources.
Searching a Tree: The Efficiency of Branch and Bound
As a branchandbound algorithm searches through a tree, it keeps track of two key pieces of information: the best solution found so far and the best possible solution that can be found from the current node.
If the estimated best possible solution from the current node can’t beat the current best solution, the algorithm abandons that path in the tree and looks for other promising paths. This makes branch and bound faster than exhaustive search
Language Agnostic Coding Drills
The branch and bound algorithm is a search or decisionmaking method commonly used in computer science, especially in the field of combinatorial optimization. Here’s how we can break it down into smallest units of learning:
Understanding Variables: Variables are used to store data that may be used and changed in your code.
 Drill: Create simple variables and perform basic operations with them (like addition, multiplication, etc.).
Understanding Data Structures: Branch and Bound utilizes various data structures such as trees and queues.
 Drill: Learn how to define, add elements to, and retrieve elements from these structures.
Understanding Conditional Statements (ifelse): The algorithm uses conditional statements to determine how to navigate the tree structure.
 Drill: Write a program that uses conditional statements to execute different sections of code based on certain conditions.
Understanding Loop Constructs: You need to understand how to use loops to traverse over data structures.
 Drill: Create a loop that traverses over a data structure (like an array or list), and performs operations on each element.
Understanding Function Definitions and Calls: You need to understand how to define a function and call it.
 Drill: Create a simple function and call it within your code.
Understanding Recursion: Branch and Bound uses recursive calls to explore different branches of the search tree.
 Drill: Write a simple recursive function, such as calculating the factorial of a number.
Principle of Branch and Bound: Understand how the Branch and Bound algorithm works conceptually. It involves exploring a treelike graph structure, branching at each level, and using bounds to prune branches that cannot possibly lead to a better solution than the current best.
 Drill: Try to describe the process of the algorithm in your own words or write pseudocode for it.
Implementing Branch and Bound: The final step is to use all of the concepts above to implement the Branch and Bound algorithm.
 Drill: Implement a basic version of the Branch and Bound algorithm to solve a problem, such as the Traveling Salesman Problem or a simple game.
Remember that these drills start from very basic programming concepts and gradually build up to complex algorithm implementation. Each concept is important and builds on the previous one. Practicing each concept individually will make the final implementation of the algorithm easier.
Solution for Coding Drills in Python
Here’s how you would code the drills in Python:
 Understanding Variables:
 Creating variables and performing basic operations:


 Understanding Data Structures:
 Using lists (arrays) and queues:


 Understanding Conditional Statements (ifelse):
 Using conditional statements:


 Understanding Loop Constructs:
 Using loops:


 Understanding Function Definitions and Calls:
 Defining and calling functions:


 Understanding Recursion:
 Using recursion:


Principle of Branch and Bound:
 This drill is theoretical and doesn’t involve code. It’s about understanding the principle of the Branch and Bound algorithm.
Implementing Branch and Bound:
 Implementation of the Branch and Bound algorithm can vary greatly depending on the problem at hand. Here’s a simplified example of how it can be implemented for the Traveling Salesman Problem:


In this example, we are calculating the shortest possible route that a traveling salesman can take to visit every city exactly once and return to his city. This is a simplified version and realworld problems may require additional considerations and improvements.