Backtracking and Branch and Bound

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Backtracking & Branch and Bound

Algorithms

Presented by:

Name: Roll No:


Monodip Biswas 10000220062

Retam Paul 10000220055

Anwesha Das 10000220056

Rahul Chatterjee 10000220059

Sagar Kumar 10000220061

Tapan Kumar 10000220057
Backtracking


Backtracking is an algorithmic-technique for solving recursive problems by
trying to build every possible solution incrementally and removing those
solutions that fail to satisfy the constraints of the problem at any point of
time.


Can be applied only for problems which admit the concept of a “partial
candidate solution” and a relatively quick test for completeness to a valid
solution


Based on Depth First Recursive Search.
Pictorial Represntation
Algorithm

boolean pathFound (Position p)


if (p is finish) return true;


for each option O from p

{


Boolean isThereAPath= pathFound(0);


if(isThereAPath)


return true;


}


return false;
WHY BACKTRACKING


Whenever applicable, Backtracking is often much faster than brute
force enumeration of all complete candidates, since it eliminates a
large no of candidates with a single test.


Simple and easy to code.


Different states are stored into stack so that data can be useful
anytime.
Applications of Backtracking


To solve the N Queen problem.


Maze solving problem.


To find all Hamiltonian Paths present in a graph.
Example: Rat in a Maze


Problem: Given a maze(2D matrix) with obstacles, starting from (0,0) you
have toreach (n-1, n-1). If you are currently on (x,y), you can move to
(x+1,y) or (x,y+1). You can not move to the walls.
Solution: Rat in a Maze
Step 1: Step 2:

Step 3: Step 4:
Solution: Rat in a Maze
Source Code
Source Code
Example: N Queen Puzzle


PROBLEM DESCRIPTION: In a N X N square Board , place N queens
so that no two queens threaten each other(Non Attacking Position).

Let N=4

So, on 4X4 board we have to place 4 queens
N Queen Puzzle
Step 1 for Q1: Step 2 for Q2:

No Place for
Q3 hence
backtrack to
other
positon of
Q2.

Step 3 for next position of Q2: Step 4 for Q3:

No Place for
Q4 hence
backtrack to
other positon
of
Q3 ->Q2->Q1
N Queen Puzzle
Step 5 for another position of Q1: Step 6 for Q2:

Step 7 for Q3: Step 8 for Q4:

Reached
Solution...
N Queen Puzzle Algorithm
Nqueen(k,n)
{
Step1: fori<-to n do
{
step1.1: if Place(k,i)then
{
Step1.1.1: x[k]<-i
}
Step1.2: if k<-nthen
Step1.2.1: write(x[1:n]);
Step1.3: else
Step 1.3.1: Nqueen(k+1,n)
}
}
//The algorithm Place() is used to place a queen in k-th row and i-th coloumn//
Place(k,i)
{
step1: for j<- to k-1 do
{
Step1.1: if((x[j]==i) or (|(x[j]-i|==|j-k|))then
{
Step1.1.1: return false;
}
}
step2: return true;
}
Branch and Bound Algorithm


Algorithm consist of a systematic enumeration of set of possible solutions by
means of state space search (in which successive states are considered with
the intention of finding goal state with desired property).


It is a solution approach that can be applied to a number of different types of
problems.


Before enumerating the set of possible solutions of a branch, the branch is
checked against upper(lower) bounds on the optimal solution, and discard if it
cannot produce a better solution than the best one found so far.


Based on Breadth First Search.
Types of Branch and Bound

There are 3 types of search stragies:


A BFS(Breadth First Search) like state space search is called as
FIFO (First in First Out) search as the list of live nodes in a first
in first out.


A D-search like state space search is called as LIFO (Last in
First Out) search as the list of live nodes in a last in first out list.


LC (Least Cost) Search where we will use ranking function or cost
function. We generate the children of E-node, among these live
nodes; we select a node which has minimum cost.
Types of Nodes


Live node is a node that has been generated but whose
children have not yet been generated.


E-node is a live node whose children are currently being
explored. In other words, an E-node is a node currently
being expanded.


Dead node is a generated node that is not be expanded
or explored any further. All children of a dead node have
already been expanded.
FIFO Branch and Bound Search:

For this we will use a data structure called Queue. Initially Queue is
empty.

Example: Assume the node 12 is an answer node (solution).
FIFO Branch and Bound Search:

In FIFO search, first we will take E-node as a node 1.

Next we generate the children of node 1. We will place all these live nodes in a queue.


Now delete an element from queue, i.e. node 2, next generate children of node 2 and place
in this queue.


Next, delete an element from queue and take it as E-node, generate the children of node 3,
7, 8 are children of 3 and these live nodes are killed by bounding functions. So we will
not include in the queue.


Again delete an element an from queue. Take it as E-node, generate the children of 4.
Node 9 is generated and killed by boundary function. Next delete an element an from
queue.Take it as E-node, generate the children of 4. Node 9 is generated and killed by
boundary function.
LIFO Branch and Bound Search:

For this we will use a data structure called Stack. Initially Stack is
empty.

Example: Assume the node 12 is an answer node (solution).
LIFO Branch and Bound Search:


Generate children of node 1 and place these live nodes into stack.


Remove element from stack and generate the children of it, place those
nodes into stack. 2 is removed from stack. The children of 2 are 5, 6.
The content of stack is:


Again remove an element from stack, i.,e node 5 is removed and
nodes generated by 5 are 10, 11 which are killed by bounded function,
so we will not place 10, 11 into stack.


Delete an element from stack, i.,e node 6. Generate child of node 6, i.,e
12, which is the answer node, so search process terminates.
LC Branch and Bound Search:

In this we will use ranking function or cost function. We generate the children of E-node,
among these live nodes; we select a node which has minimum cost.

Example: Assume the node 12 is an answer node (solution).
LC Branch and Bound Search:


Initially take node 1 as E-node. Generate children of node 1, the children are 2,
3, 4. By using ranking function we will calculate the cost of 2, 3, 4 nodes is
ĉ =2, ĉ =3, ĉ =4 respectively.


Now we will select a node which has minimum cost i.,e node 2. For
node 2, the children are 5, 6. Now we will select a node which has
minimum cost i.e. node 2. For node 2, the children are 5, 6.


Between 5 and 6 we will select the node 6 since its cost minimum.
Generate children of node 6 i.e. 12 and 13.


We will select node 12 since its cost (ĉ =1) is minimum. Moreover 12 is the
answer node. So, we terminate search process.
Algorithm

//maximizing an arbitrary objective function f

//requires bounding function g

1. Let B be the best possible value achieved. Initialize B <- 0;


2. Initialize a priority queue to hold a partial solution with none of variables of the
problem assigned 3. Loop until the queue is empty:


1. take a node N (with highest bound value) from the queue.


2. if N represent a single candidate solution x and f(x)>B


then x is the best solution. Set B-f(x).

3. else branch on N to produce new nodes N. For each of these:


if g(N) < B. Discard it.

2 else store N, on the queue.
Applications of Branch and Bound


To solve N Puzzle Problems.


To solve Traveling Salesman Problem.
Example: 8 Puzzle Problem

In 8 puzzle problem ,there are 8 tiles which are numbered from 1
to 8 placed on a 9-tile capacity square frame.

The Objective of 8 puzzle problem is to transform the arrangement of
tiles from initial arrangement to goal arrangement.

The Initial and Goal arrangement is shown in the figure below:

1 2 3

4 6

a) Initial arrangement

7 5 8

1 2 3

b) Goal arrangement
4 5 6
7 8
8 Puzzle Problem

There is an always an empty slot in initial arrangement.

Legal Moves are the Moves in which the tiles adjacent to empty slot are moved in either
LEFT,RIGHT,UP,DOWN.

The initial Arrangement is called as Initial state and the goal Arrangement is called as
Goal State0

The state Space Tree of 8 puzzle is large because there can be 9! Different
arrangements.

In the state space Tree ,the nodes are numbered as per levels.

Each next move Generated based on empty slot positions.
8 Puzzle Problem

Edges are labeled according to the direction in which Empty space
moved.

The Root node becomes E-Node.

We can decide which node becomes E-NODE based on Estimation
Formula.

FORMULA: C(x)=F(x)+G(x)

Where,

C(x)=Lower Bound Cost of NODE ‘x’.

F(x)=length of the path from root node to node ‘x’.

G(x) =number of non-blank tiles which are not in Goal position.
8 Puzzle Problem

C(13)=3+4=7
C(14)=3+2=5
C(15)=3+0=3
8 Puzzle Problem Algorithm
E = t; // E-node
/* Algorithm LCSearch uses c(x) to find an answer node
* LCSearch uses Least() and Add() to maintain the list Initialize the list of live nodes to be empty;
of live nodes while (true)
* Least() finds a live node with least c(x), deletes {
it from the list and returns it for each child x of E
* Add(x) adds x to the list of live nodes {
* Implement list of live nodes as a min-heap */ if x is an answer node
{
struct list_node print the path from x to t;
{ return;
}
list_node *next;
Add (x); // Add x to list of live nodes;
x->parent = E; // Pointer for path to root
// Helps in tracing path when answer is found }
list_node *parent;
float cost; if there are no more live nodes
} {
print ("No answer node");
algorithm LCSearch(list_node *t) return;
{ }
// Search t for an answer node
// Input: Root node of tree t // Find a live node with least estimated cost
// Output: Path from answer node to root E = Least();
if (*t is an answer node)
{ // The found node is deleted from the list of
// live nodes
print(*t);
}
return;
}
}
WHY Branch and Bound


An enhancement of backtracking.


Determine the bound as tightly as possible.


Doesn’t try all possible combinations and hence reach the solution fast.


Solve some problems which are not solvable by Dynamic Programming.
References


https://www.geeksforgeeks.org/backtracking-algorithms/


https://www.youtube.com/watch?v=3RBNPc0_Q6g


Thomas H. Cormen


https://www.javatpoint.com/
 Thank You

You might also like