0% found this document useful (0 votes)
38 views

Module 5 AOA

Uploaded by

Niramay K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Module 5 AOA

Uploaded by

Niramay K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Branch and Bound

Technique

Ms. Mrunali Desai


Branch and Bound

• Branch and bound is an algorithm design paradigm for discrete and


combinatoric optimisation problems, as well as mathematical optimisation.

• A branch-and-bound algorithm consists of a systematic enumeration of


candidate solutions. That is, the set of candidate solutions is thought of as
forming a rooted tree with the full set at the root.

• The algorithm explores branches of this tree, which represent the subsets
of the solution set. Before enumerating the candidate solutions of a
branch, the branch is checked against upper and lower estimated bounds
on the optimal solution and is discarded if it cannot produce a better
solution than the best one found so far by the algorithm.
Travelling Salesman Problem
using
Branch and Bound
https://www.youtube.com/watch?v=1FEP_sNb62k&t=672s
Traveling Salesman Problem using Branch And Bound
• Given a set of cities and distance between every pair of cities, the
problem is to find the shortest possible tour that visits every city
exactly once and returns to the starting point.
15 PUZZLE PROBLEM
USING BRANCH AND BOUND

BY:- Ms. Mrunali Desai


Branch and Bound

• The search for an answer node can often be speeded by using an


“intelligent” ranking function, also called an approximate cost
function to avoid searching in sub-trees that do not contain an
answer node. It is similar to backtracking technique but uses
BFS-like search.

• There are basically three types of nodes involved in Branch and


Bound
• 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 to be expanded or
explored any further. All children of a dead node have already
been expanded.
Cost function

• Each node X in the search tree is associated with a cost. The cost
function is useful for determining the next E-node. The next E-node is
the one with least cost. The cost function is defined as,
• C(X) = g(X) + h(X) where
• g(X) = cost of reaching the current node from the root
• h(X) = cost of reaching an answer node from X
Ideal Cost function for 15-puzzle Algorithm :
• We assume that moving one tile in any direction will have 1 unit cost.
Keeping that in mind, we define cost function for 15- puzzle algorithm
as below :
• c(x) = f(x) + h(x) where
• f(x) is the length of the path from root to x (the number of moves
so far)
• h(x) is the number of non-blank tiles not in their goal position (the
number of mis- - placed tiles). There are at least h(x) moves to
1 2 3 4

5 6 8
9 10 7 11
13 14 15 12

Initial arrangement

1 2 3 4

5 6 7 8
9 10 11 12
13 14 15

Goal arrangement
1 2 3 4
5 6 8
9 10 7 11
13 14 15 12
left
up
right down

C=1+4 C=1+4 C=1+2 C=1+4


1 2 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 3 8 5 6 8 5 6 7 8 5 6 8
9 10 7 11 9 10 7 11 9 10 11 9 10 7 11
13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12
1 2 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 3 8 5 6 8 5 6 7 8 5 6 8
9 10 7 11 9 10 7 11 9 10 11 9 10 7 11
13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12

right down left


C=2+1 C=2+3 C=2+3

1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8
9 10 11 9 10 15 11 9 10 11
13 14 12 13 14 15 12
13 14 15 12

up down
C=3+2 C=3+0
1 2 3 4 1 2 3 4
5 6 7 5 6 7 8
9 10 11 8 9 10 11 12

13 14 15 12 13 14 15

Goal node
Pseudocode
Procedure15-PUZZLE
// live_node_set: set to hold the live nodes at any time
// lowcost: variable to hold the cost of the best cost at any given node

Begin
lowcost = ∞;
While live_node_set ≠∞ do
choose a branching node, k, such that k ∈ live_node_set; // k is a E-node
live_node_set= live_node_set - {k};
Generate the children of node k and the corresponding lower bounds;
Sk={(i, zi): i is child of k and zi its lower bound}
For each element (i, zi) in Sk do
if zi > U //more than lower bound??
Kill child i; // i is a child node
else
if child i is a solution
U = zi; current best = child i;
else
add child i to live_node_set;
endif;
endif;
endfor;
Endwhile;
End
Backtracking
Ms. Mrunali Desai
Backtracking Algorithms

• Backtracking is an algorithmic-technique for solving problems


recursively by trying to build a solution incrementally, one piece at a
time, removing those solutions that fail to satisfy the constraints of
the problem at any point of time.
Backtracking Example
For example, consider the SudoKo solving
Problem, we try filling digits one by one.

Whenever we find that current digit cannot lead


to a solution, we remove it (backtrack) and try
next digit.

This is better than naive approach (generating all


possible combinations of digits and then trying
every combination one by one) as it drops a set
of permutations whenever it backtracks.
N Queen Problem

https://www.codesdope.com/course/algorithms-backtracking/
N Queen Problem
• The N Queen is the problem of placing N chess
queens on an N×N chessboard so that no two
queens attack each other. For example,
following is a solution for 4 Queen problem.

• The expected output is a binary matrix which


has 1s for the blocks where queens are placed.
For example, following is the output matrix for
above 4 queen solution.
Procedure
• For avoiding queen attack, don’t place queen in
• Same row
• Same column
• Same diagonal

• Example: 4 Queens (q1, q2, q3, q4) and chessboard 4*4.


• Place queen qi into ith row. Now we need to decide correct column.
Procedure in detail
• Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put
queen q2 so that both these queens do not attack each other. We find that if we
place q2 in column 1 and 2, then the dead end is encountered. Thus the first
acceptable position for q2 in column 3, i.e. (2, 3) but then no position is left for
placing queen 'q3' safely. So we backtrack one step and place the queen 'q 2' in (2,
4), the next best possible solution. Then we obtain the position for placing 'q 3'
which is (3, 2). But later this position also leads to a dead end, and no place is
found where 'q4' can be placed safely. Then we have to backtrack till 'q 1' and
place it to (1, 2) and then all other queens are placed safely by moving q 2 to (2,
4), q3 to (3, 1) and q4 to (4, 3).
• That is, we get the solution (2, 4, 1, 3). This is one possible solution for the
4-queens problem.
• For another possible solution, the whole method is repeated for all partial
solutions. The other solutions for 4 - queens problems is (3, 1, 4, 2).
Solution for 4 Queen problem

The other solutions for 4 - queens The other solutions for 4 - queens
problems is (3, 1, 4, 2). problems is (2, 4, 1, 3).
Algorithm
Sum of Subsets
Subset sum
• Subset sum problem is to find subset of elements that are selected
from a given set whose sum adds up to a given number K.

• We are considering the set contains non-negative values.

• It is assumed that the input set is unique (no duplicates are


presented)
• Example: Given a set S = (3, 4, 5, 6) and X =9. Obtain the subset sum
using Backtracking approach.
Algorithm
• Input − The given set and subset, size of set and subset, a total of the subset,
number of elements in the subset and the given sum.
• Output − All possible subsets whose sum is the same as the given sum.

Time Complexity:
= O(2^n)
Example

Output:
Graph coloring
M-coloring Problem

• Given an undirected graph and a number m, determine if the graph


can be colored with at most m colors such that no two adjacent
vertices of the graph are colored with the same color. If the solution
exists, then display which color is assigned on which vertex.

• Starting from vertex 0, we will try to assign colors one by one to


different nodes. But before assigning, we have to check whether the
color is safe or not. A color is not safe whether adjacent vertices are
containing the same color.
Examples
Algorithm

Time Complexity: O(m^V).


There is a total O(m^V) combination of
colors. So the time complexity is O(m^V).

You might also like