Unit - 4 DAA
Unit - 4 DAA
1. Iterative Improvement
Iterative Improvement method is a computational technique .
It consists of following steps:
1.Start with some feasible solution(i.e., satisfy problem’s constraints)
2.Repat following steps until no improvement is found
i. Change current feasible solution to a feasible solution with a
better value of objective function.
3.Return the last feasible solutions an optimal solution.
Figure 3.1 Example of a network graph. The vertex numbers are vertex
“names”;the edge numbers are edge capacities.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
It is assumed that the source and the sink are the only source and
destination of the material, respectively; all the other vertices can
serve only as points where a flow can be redirected without
consuming or adding any amount of the material.
In other words, the total amount of the material entering an
intermediate vertex must be equal to the total amount of the material
leaving the vertex. This condition is called the flow-conservation
requirement.
If we denote the amount sent through edge (i, j) by x ij , then for
any intermediate vertex i, the flow-conservation requirement is
expressed as:
where the sums in the left- and right-hand sides express the total
inflow and outflow entering and leaving vertex i, respectively.
ALGORITHM ShortestAugmentingPath(G)
//Implements the shortest-augmenting-path algorithm
//Input: A network with single source 1, single sink n, and positive
integer capacities uij on its edges (i, j)
//Output: A maximum flow x
assign xij = 0 to every edge (i, j) in the network
label the source with , - and add the source to the empty queue Q
while not Empty(Q) do
i ←Front(Q); Dequeue(Q)
for every edge from i to j do //forward edges
if j is unlabeled
rij ←uij - xij
if rij > 0
lj ← min{ li , rij};label j with lj, i+
Enqueue(Q, j )
for every edge from j to i do //backward edges
if j is unlabeled
if xij > 0
lj ← min{ li , xij};label j with lj, i-
Enqueue(Q, j )
if the sink has been labelled
//augment along the augmenting path found
j ←n //start at the sink and move backwards using second labels
while j ≠ 1 //the source hasn’t been reached
if the second label of vertex j is i+
xij ← xij + ln
else //the second label of vertex j is i
xij ← xij - ln
j ←i; i ←the vertex indicated by i’s second label
erase all vertex labels except the ones of the source
reinitialize Q with the source
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
return x //the current flow is maximum
v≤c
Thus, the value of any feasible flow in a network cannot exceed
the capacity of any cut in that network.
Let v* be the value of a final flow x* obtained by the
augmenting-path method.
If a cut is found whose capacity is equal to v*, then from v ≤ c ,
it can be concluded that
i. The value v* of the final flow is maximal among all feasible flows,
ii. the cut’s capacity is minimal among all cuts in the network, and
iii. the maximum-flow value is equal to the minimum-cut capacity.
To find such a cut,
Consider the set of vertices X* that can be reached from the
source by following an undirected path composed of forward
edges with positive unused capacities and backward edges with
positive flows on them.
The set X* contains the source but does not contain the sink: if it
contains, then there exists an augmenting path for the flow x* ,
which contradicts the assumption that the flow x* is final.
Consider the cut C(X*,X*) . By the definition of set X* , each
edge (i, j) from X* to X* has zero unused capacity, i.e., x*ij = uij ,
and each edge (j,i) from X* to X* has zero flow on it
From the equality defined above, the final flow x* and the set X* is
Analysis
The number of augmenting paths needed by the shortest-
augmenting-path algorithm never exceeds nm/2, where n and m
are the number of vertices and edges, respectively.
Since the time required to find a shortest augmenting path by
breadth-first search is in O(n + m) = O(m) for networks
represented by their adjacency lists, the time efficiency of the
shortest-augmenting-path algorithm is in O(nm2).
A preflow is a flow that satisfies the capacity constraints but not
the flow-conservation requirement. Any vertex is allowed to have
more flow entering the vertex than leaving it.
A preflow push algorithm moves the excess flow toward the
sink until the flow-conservation requirement is reestablished for
all intermediate vertices of the network. Faster algorithms of this
kind have worst-case efficiency close to O(nm).
Figure 5.1: Data for an instance of the stable marriage problem. (a)
Men’s preference lists; (b) women’s preference lists. (c) Ranking matrix (with
the boxed cells composing an unstable matching).
A marriage matching M is a set of n (m, w) pairs whose members
are selected from disjoint n-element sets Y and X in a one-one
fashion, i.e., each man m from Y is paired with exactly one woman w
from X and vice versa.
A pair (m, w), where m ∈ Y, w ∈ X, is said to be a blocking pair
for a marriage matching M if man m and woman w are not matched in
M but they prefer each other to their mates in M.
For example, (Bob, Lea) is a blocking pair for the marriage matching
M = {(Bob, Ann), (Jim, Lea), (Tom, Sue)} (Figure 5.1c) because they
are not matched in M while Bob prefers Lea to Ann and Lea prefers
Bob to Jim.
A marriage matching M is called stable if there is no blocking pair for
it; otherwise, M is called unstable.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
Stable marriage problem is to find a stable marriage matching for
men’s and women’s given preferences. Surprisingly, this problem
always has a solution
4. Backtracking
Backtracking is a more intelligent variation of this approach.
The principal idea is to construct solutions one component at a
time and evaluate such partially constructed candidates as
follows.
If a partially constructed solution can be developed further
without violating the problem’s constraints, it is done by
taking the first remaining legitimate option for the next
component.
If there is no legitimate option for the next component, no
alternatives for any remaining component need to be
considered. In this case, the algorithm backtracks to replace
the last component of the partially constructed solution with its
next option.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
To implement this kind of processing by constructing a tree of
choices being made, called the state-space tree.
State-Space Tree.
Its root represents an initial state before the search for a solution
begins.
The nodes of the first level in the tree represent the choices made
for the first component of a solution.
The nodes of the second level represent the choices for the
second component, and so on.
A node in a state-space tree is said to be promising if it
corresponds to a partially constructed solution that may still lead
to a complete solution; otherwise, it is called nonpromising.
Leaves represent either nonpromising dead ends or complete
solutions found by the algorithm.
In the majority of cases, a statespace tree for a backtracking
algorithm is constructed in the manner of depthfirst search.
If the current node is promising, its child is generated by adding
the first remaining legitimate option for the next component of a
solution, and the processing moves to this child.
If the current node turns out to be non-promising, the algorithm
backtracks to the node’s parent to consider the next possible
option for its last component; if there is no such option, it
backtracks one more level up the tree, and so on.
Finally, if the algorithm reaches a complete solution to the
problem, it either stops (if just one solution is required) or
continues searching for other possible solutions.
General Remarks
From a more general perspective, most backtracking algorithms
fit the following description.
An output of a backtracking algorithm can be thought of as an n-
tuple (x1,x2,...,xn) where each coordinate xi is an element of some
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
finite lin early ordered set Si. For example, for the n- queens
problem, each Si is the set of integers(column numbers) 1
through n.
A backtracking algorithms generates , explicitly or implicitly , a
state space tree: its nodes represent partially constructed tuples
with the first i coordinates defined by the earlier actions of the
algorithm. If such a tuple(x1,x2,...,xn) is not a solution, the
algorithm finds the next element in Si+1 that is consistent with the
values of (x1,x2,...,xi) and the problem’s constraints, and adds it to
the tuple as its (i+1)st coordinate.
If such an element does not exist, the algorithm backtracks to
consider the next value of xi, and so on.
Algorithm Backtrack(X[1..i] )
//Gives a template of a generic backtracking algorithm
//Input: X[1..i] specifies first i promising components of a solution
//Output: All the tuples representing the problem’s solutions
if X[1..i] is a solution write X[1..i]
else
for each element x Si+1 consistent with X[1..i] and the
constraints do
X[i + 1] ←x
Backtrack(X[1..i + 1])
4.1. 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
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
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
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
Step 7: Queen 2 then goes to (2, 4). 1 Q
2 Q
3
4