AD8351-Unit IV
AD8351-Unit IV
AD8351-Unit IV
n-Queens Problem
Problem Definition:
The problem is to place n queens on an n × n chessboard so that
no two queens attack each other by being in the same row or in the
same column or on the same diagonal.
For n = 1, the problem has a trivial solution. Q
For n = 2, it is easy to see that there is no solution to place 2
queens in 2 × 2 chessboard
Q
Since each of the four queens has to be placed in its own row, all
we need to do is to assign a column for each queen on the board
presented in the following Figure
Step 1: Start with the empty board
1 2 3 4
1 Q
2 Q
3
4
Step 6: The algorithm then backtracks all the way to queen 1 and
moves it to (1, 2).
1 2 3 4
1 Q
2
3
4
1 2 3 4
AD8351 Design & Analysis of Algorithm Unit-4
5 Q
6 Q
7 Q
8 Q
Figure: Solution 8 queens problem in 8 x 8 chessboard
There is a set Y = {m1,…,mn} of n men and a set X = {w1,…,wn} of n women. Each man
has a ranking list of the women, and each woman has a ranking list of the men (with no ties
in these lists).
given preferences.
Step 1:- Start with all the men and women being free
Step 2:- While there are free men, arbitrarily select one of them and do
the following:
o Proposal The selected free man m proposes to w, the next woman on
his preference list
o Response If w is free, she accepts the proposal to be matched with m.
If she is not free, she compares m with her current mate. If she prefers
m to him, she accepts m’sproposal, making her former mate free;
otherwise, she simply rejects m’s proposal,
leaving m free
AD8351 Design & Analysis of Algorithm Unit-4
Bipartite Graphs
Matching in a Graph
Algorithm
A matching M is maximum if and only if there exists no
augmenting path with respect to M.
•
Definition of a Flow
A flow is an assignment of real numbers xij to edges (i,j) of a
given network that satisfy the following:
flow-conservation requirements
The total amount of material entering an intermediate vertex
must be equal to the total amount of the material leaving the
vertex
capacity constraints
Finding a flow-augmenting path
iii) (2,5),(2,3),(4,3)
AD8351 Design & Analysis of Algorithm Unit-4
AD8351 Design & Analysis of Algorithm Unit-4
BRANCH-AND-BOUND
General Method
A branch and bound is a general algorithm for finding optimal
solutions of various optimization problems.
An optimization problem seeks to minimize or maximize an
objective function, subject to some constraints.
To deal with optimization problem we compute the best value
of best solution achieved so far.
A bound on the best solution that can be achieved by expanding
the node is computed and compared to best.
For a maximization problem the bound is an upper bound
The largest possible solution that can be achieved by
expanding the node is less or equal to the upper bound.
If upper bound > best so far, a better solution may be found
by expanding the node and the feasible node is promising .
For a minimization problem the bound is an lower bound
The smallest possible solution that can be achieved by
expanding the node is less or equal to the lower bound.
If lower bound < best so far, a better solution may be found
by expanding the node and the feasible node is promising .
A Feasible Solution is a point in the problem’s search space that
satisfies all the problem’s constraints.
Example:
a Hamiltonian circuit in the traveling salesman problem
a subset of items whose total weight does not exceed the
knapsack’s capacity
AD8351 Design & Analysis of Algorithm Unit-4
Then the first item gives the best payoff per weight unit and the
last one gives the worst payoff per weight unit, with ties resolved
arbitrarily:
v1/ w1 ≥ v2/ w2 ≥ . . . ≥ vn/ wn
Construction of the state space tree
Each node on the ith level of this tree, 0 ≤ i ≤ n, represents all
the subsets of n items that include a particular selection made
from the first i ordered items.
This particular selection is uniquely determined by the path from
the root to the node: a branch going to the left indicates the
inclusion of the next item, and a branch going to the right
indicates its exclusion.
Record the total weight w and the total value v of this selection in
the node, along with some upper bound ub on the value of any
subset that can be obtained by adding zero or more items to this
selection.
The upper bound ub is computed using the formula
ub = v + (W - w)(vi+1/w i+1)
where, v Total value of the items already selected
W-w Product of the remaining capacity of the knapsack
vi+1/w i+1 Best per unit payoff among remaining items, which
is:
Characteristics of knapsack while solved using Branch and Bound
Technique
The internal nodes of a state space tree do not define a point of
the problem’s search space, because some of the solution’s
components remain undefined.
AD8351 Design & Analysis of Algorithm Unit-4
Every node of the tree represents a subset of the items given and
uses this fact to update the information about the best subset seen
so far after generating each new node in the tree.
Example: Computing the value to weight ratios and sorting the items
in non increasing order of these efficiency ratios yields.
At the root of the state-space tree (see Figure 7.4), no items have
been selected as yet. Hence, both the total weight of the items
already selected w and their total value v are equal to $0. The
value of the upper bound computed by formula
ub = 0 + (10 - 0) (10) =100
Node 1, the left child of the root, represents the subsets that
include item 1. The total weight and value of the items already
included are 4 and $40, respectively; the value of the upper
bound is 40 + (10 - 4) * 6 = $76.
Node 2 represents the subsets that do not include item 1.
Accordingly, w = 0, v = $0, and ub =0+(10 - 0) * 6 = $60.
Since node 1 has a larger upper bound than the upper bound of
node 2, it is more promising for this maximization problem, and
we branch from node 1 first.
AD8351 Design & Analysis of Algorithm Unit-4
Selecting node 4 over node 2 for the next branching and get
nodes 5 and 6 by respectively including and excluding item 3.
Calculate the total weights and values as well as the upper
bounds for these nodes are computed in the same way as for the
preceding nodes.
Branching from node 5 yields node 7, which represents no
feasible solutions, and node 8, which represents just a single
subset {1, 3} of value $65.
The remaining live nodes 2 and 6 have smaller upper-bound
values than the value of the solution represented by node 8.
Hence, both can be terminated making the subset {1, 3} of node
8 the optimal solution to the problem.
Example 2
AD8351 Design & Analysis of Algorithm Unit-4
AD8351 Design & Analysis of Algorithm Unit-4
AD8351 Design & Analysis of Algorithm Unit-4
For example, for all the Hamiltonian circuits of the graph that
must include edge (a, d), the following lower bound by summing
up the lengths of the two shortest edges incident with each of the
vertices, with the required inclusion of edges (a, d) and (d, a):
┌[(1 + 5) + (3 + 6) + (1 + 2) + (3 + 5) + (2 + 3)]/2┐=16.
Now apply the branch-and-bound algorithm, with the bounding
function given by lb =┌ s/2┐ to find the shortest Hamiltonian
circuit for the given graph .
Example 2:
AD8351 Design & Analysis of Algorithm Unit-4
AD8351 Design & Analysis of Algorithm Unit-4