0% found this document useful (0 votes)
24 views29 pages

Unit - 4 DAA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views29 pages

Unit - 4 DAA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

21CB1401 Design & Analysis of Computer Algorithm UNIT-4

UNIT-4 ITERATIVE IMPROVEMENT AND BACKTRACKING

Iterative Improvement-The Maximum-Flow Problem-Stable


marriage Problem- Backtracking -N-Queens problem -
Hamiltonian Circuit Problem - Subset Sum Problem - Graph
Coloring Problem.

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.

Applications of Iterative Improvement


1. Simplex Method
2. Ford Flukersons algorithm for maximum flow
3. Matching a graph vertices
4. Stable Marriage Problem
Problem in Implementing this technique
1. Need an initial feasible solutionStart with trivial solution/
approximation solution / some effort to find
2. Not always clear that what changes improve feasible solution [That
improves object function]
3. Difficulty in finding local Vs global extreme (hill)
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
2. Maximum Flow Problem
Problem Definition:
Maximizing the flow of a material through a transportation
network like pipeline system, common system, and electrical
distribution system and so on.
Representation:
Problem is represented by a connected weighted digraph with n
vertices numbered from 1 to n and a set of edges E, with the following
properties:
1. Source : It contains exactly one vertex with no entering edges;
this vertex is called the source and assumed to be numbered 1.
2. Sink It contains exactly one vertex with no leaving edges; this
vertex is called the sink and assumed to be numbered n.
3. Capacity The weight u of each directed edge (i, j) is a positive
integer, called the edge capacity. (This number represents the upper
bound on the amount of the material that can be sent from i to j
through a link represented by this edge.)
A digraph satisfying these properties is called a flow network or
simply a network.
A small instance of a network is given in Figure 3.1

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.

This quantity, the total outflow from the source—or,


equivalently, the total inflow into the sink—is called the value of the
flow. We denote it by v. It is this quantity that we will want to
maximize over all possible flows in a network.

Feasible flow is an assignment of real numbers xij to edges (i, j) of a


given network that satisfy flow-conservation constraints and the
capacity constraints
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
The maximum-flow problem can be stated formally as the following
optimization problem:

Iterative Improvement Idea:


Step 1: Always start with the zero flow (set xij =0 for every edge (i, j)
in the network).
Step 2: Then, on each iteration try to find a path from source to sink
along which some additional flow can be sent. Such a path is called
flow augmenting.
Step 3: If a flow-augmenting path is
i. Found, then adjust the flow along the edges of this path to get a
flow of an increased value and try to find an augmenting path for the
new flow.
ii. If no flow-augmenting path can be found, then conclude that
the current flow is optimal.
This general template for solving the maximum-flow problem is
called the augmenting-path method, also known as the Ford-
Fulkerson method .
Let us consider the network in Figure 3.1 Start with the zero flow
shown in Figure 3.2.a. Among several possibilities, let us assume that
we identify the augmenting path 1→2→3→6 first. We can increase
the flow along this path by a maximum of 2 units, which is the
smallest unused capacity of its edges. The new flow is shown in
Figure 3.2.b.
Unfortunately, the flow shown in Figure 3.2.b is not optimal: its
value can still be increased along the path 1→4→3←2→5→6 by
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
increasing the flow by 1 on edges (1, 4), (4, 3), (2, 5), and (5, 6) and
decreasing it by 1 on edge (2, 3). The flow obtained as the result of
this augmentation is shown in Figure 3.2.c. It is indeed maximal.

Finding augmenting path for a flow x,


In the paths from source to sink , consider any two consecutive
vertices i, j are either
i. connected by a directed edge from i to j with some positive
unused capacity rij= uij- xij (so that increased the flow through that
edge by up to r units),
ii. connected by a directed edge from j to i with some positive
flow x (so that decrease the flow through that edge by up to xji units).
Type of Edges
1.Forward edges because their tail is listed before their head in the
vertex list 1 →...i →j...→n defining the path.
2.Backward edges because their tail is listed after their head in the
path list 1 → ...i ←j...→n.
To illustrate, for the path 1→4→3←2→5→6 of the last example,
(1, 4), (4, 3), (2, 5), and (5, 6) are the forward edges, and (3, 2) is the
backward edge.
For a given flow-augmenting path, let r be the minimum of all
the unused capacities rij of its forward edges and all the flows x of its
backward edges.
Step 1: Then increase the current flow by r on each forward edge and
decrease it by this amount on each backward edge, we will obtain a
feasible flow
21CB1401 Design & Analysis of Computer Algorithm UNIT-4

Figure 3.2: Illustration of the augmenting-path method. Flow-


augmenting paths are shown in bold. The flow amounts and edge capacities
are indicated by the numbers before and after the slash, respectively.

There are four possible combinations of forward and backward edges


incident to vertex i:
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
After doing step-1 that satisfy the capacity constraints well, Finally,
adding r to the flow on the first edge of the augmenting path will
increase the value of the flow by r units.

Figure 3.3: Efficiency degradation of the augmenting-path method.

There are several ways to generate flow-augmenting paths efficiently


and avoid the degradation in performance illustrated by the previous
example.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
Shortest-Augmenting-Path or First- Labeled - First scanned
Algorithm
If the breadth-first search is used to generate augmenting paths
with the least number of edges .
 Labeling refers to marking a new (unlabeled) vertex with two
labels. The first label indicates the amount of additional flow that
can be brought from the source to the vertex being labeled.
 The second label is the name of the vertex from which the vertex
being labeled was reached. It is also convenient to add the + or -
sign to the second label to indicate whether the vertex was
reached via a forward or backward edge, respectively.
 The source can be always labeled with , -.
For the other vertices, the labels are computed as follows:
1.If unlabeled vertex j is connected to the front vertex i of the
traversal queue by a directed edge from i to j with positive unused
capacity r , then vertex j is labeled with lj,i+ , where lj = min{ li , rij}
2.If unlabeled vertex j is connected to the front vertex i of the
traversal queue by a directed edge from j to i with positive flow x ij ,
then vertex j is labeled with lj,i- , where lj = min{ li , xij}
 If this labeling-enhanced traversal ends up labeling the sink, the
current flow can be augmented by the amount indicated by the
sink’s first label.
 The augmentation is performed along the augmenting path traced
by following the vertex second labels from sink to source: the
current flow quantities are increased on the forward edges and
decreased on the backward edges of this path.
 If, on the other hand, the sink remains unlabeled after the
traversal queue becomes empty, the algorithm returns the current
flow as maximum and stops.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4

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

An application of this algorithm to the network in Figure 9.1 is


illustrated in Figure 9.3

FIGURE 3.4: Illustration of the shortest-augmenting-path algorithm. The


diagrams on the left show the current flow before the next iteration begins; the
diagrams on the right show the results of the vertex labeling on that iteration,
the augmenting path found (in bold), and the flow before its augmentation.
Vertices deleted from the queue are indicated by the ↑ symbol.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4

A cut induced by partitioning vertices of a network into some subset


X containing the source and Ẍ, the complement of X, containing the
sink is the set of all the edges with a tail in X and a head in X. We
denote a cut C(X, X ) or simply C.
For example,
if X ={1} and hence X ={2, 3, 4, 5, 6}, C(X,X)={(1,2),(1,4)}
if X ={1,2,3,4,5} and hence X ={6}, C(X,X)={(3.6),(5,6)}
if X ={1,2,4} and hence X ={3, 5, 6}, C(X,X)={(2,3),(2,5,(4,3)}
Property of cut
If all the edges of a cut were deleted from the network, there would be
no directed path from source to sink.
1.capacity of a cut C(X,X), denoted c(X,X), is defined as the sum of
capacities of the edges that compose the cut.
For the three examples of cuts given above, the capacities are equal to
5, 6, and 9, respectively.
2. A minimum cut, i.e., a cut with the smallest capacity.
Theorem (Max-Flow Min-Cut Theorem) The value of a maximum
flow in a network is equal to the capacity of its minimum cut.
Proof
 Let x be a feasible flow of value v and let C(X,X) be a cut of
capacity c in the same network.
 Let the flow across this cut defined as the difference between the
sum of the flows on the edges from X to X of the flows on the
edges from X to X
From the equations expressing the flow-conservation requirement and
the definition of the flow value , that the flow across the cut C(X,X)
is equal to v, the value of the flow is:
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
Since the second sum is nonnegative and the flow xij on any dege(i,j)
cannot exceed the edge capacity uij, implies that

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

which proves the theorem (OR)


21CB1401 Design & Analysis of Computer Algorithm UNIT-4
Definition of a Cut
Let X be a set of vertices in a network that includes its source but
does not include its sink, and let X, the complement of X, be the rest
of the vertices including the sink. The cut induced by this partition of
the vertices is the set of all the edges with a tail in X and a head in X.
Capacity of a cut is defined as the sum of capacities of the edges that
compose the cut.
 we’ll denote a cut and its capacity by C(X,X) and c(X,X)
 Note that if all the edges of a cut were deleted from the network,
there would be no directed path from source to sink
 Minimum cut is a cut of the smallest capacity in a given network

If X = {1} and X = {2,3,4,5,6}, C(X,X) = {(1,2), (1,4)}, c = 5


If X ={1,2,3,4,5} and X = {6}, C(X,X) = {(3,6), (5,6)}, c = 6
If X = {1,2,4} and X ={3,5,6}, C(X,X) = {(2,3), (2,5), (4,3)}, c = 9
Max-Flow Min-Cut Theorem
1. The value of maximum flow in a network is equal to the capacity of
its minimum cut
2. The shortest augmenting path algorithm yields both a maximum
flow and a minimum cut:
• Maximum flow is the final flow produced by the algorithm
• Minimum cut is formed by all the edges from the labeled vertices to
unlabeled vertices on the last iteration of the algorithm.
• All the edges from the labeled to unlabeled vertices are full, i.e.,
their flow amounts are equal to the edge capacities, while all the edges
from the unlabeled to labeled vertices, if any, have zero flow amounts
on them.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4

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).

3. The Stable Marriage Problem


It is a version of bipartite matching called the stable marriage
problem.
Problem Definition:
Consider a set Y = {m1, m2, . . . , mn } of n men and a
set X = {w1, w2, . . . , wn } of n women. Each man has a preference list
ordering the women as potential marriage partners with no ties
allowed. Similarly, each woman has a preference list of the men, also
with no ties.
Problem Representation
1. Matrix form n × n (see Figure 5.1a and b)
2. Ranking matrix (see Figure 5.1c).
The rows and columns of the matrix represent the men and
women of the two sets, respectively. A cell in row m and column w
contains two rankings: the first is the position (ranking) of w in the
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
m’s preference list; the second is the position (ranking) of m in the w’s
preference list.
For example, the pair 3, 1 in Jim’s row and Ann’s column in the
matrix in Figure 5.1c indicates that Ann is Jim’s third choice while
Jim is Ann’s first.

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

Stable marriage algorithm


Input: A set of n men and a set of n women along with rankings of
the women by each man and rankings of the men by each woman with
no ties allowed in the rankings
Output: A stable marriage matching
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:
Proposal The selected free man m proposes to w, the next
woman on his preference list (who is the highest-ranked woman
who has not rejected him before).
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’s proposal, making her
former mate free; otherwise, she simply rejects m’s proposal,
leaving m free.
Step 3: Return the set of n matched pairs.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4

FIGURE 10.12 Application of the stable marriage algorithm. An accepted


proposal is indicated by a boxed cell; a rejected proposal is shown by an
underlined cell.
One can prove that the algorithm always yields a stable matching that
is man-optimal: it assigns to each man the highest-ranked woman
possible under any stable marriage. Of course, this gender bias can be
reversed, but not eliminated, by reversing the roles played by men and
women in the algorithm, i.e., by making women propose and men
accept or reject their proposals.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
Analysis:

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

 For n = 3, it is easy to see that there is no solution to place 3


queens in 3 × 3 chessboard

 For n = 4 , there is solution to place 4 queens in 4 × 4 chessboard.


The four- queens problem solved by the backtracking technique.
 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

Figure 6.1: Board for the four-queens problem.


Step 2: Place queen 1 in the first possible position of its row, which is
in column 1 of row 1.
1 2 3 4
1 Q
2
3
4
Step 3: Place queen 2, after trying unsuccessfully columns 1 and 2, in
the first acceptable position for it, which is square (2, 3), the square in
row 2 and column 3.
1 2 3 4
1 Q
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
2 Q
3
4

Step 4: This proves to be a dead end because there is no acceptable


position for queen 3. So, the algorithm backtracks and puts queen 2 in
the next possible position at (2, 4).

1 2 3 4
1 Q
2 Q
3
4

Step 5: Then queen 3 is placed at (3, 2), which proves to be another


dead end.
1 2 3 4
1 Q
2 Q
3 Q
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

Step 8: Queen 3 to (3, 1).


1 2 3 4
1 Q
2 Q
3 Q
4

Step 9: Queen 4 to (4, 3). This is a solution to the problem.


1 2 3 4
1 Q
2 Q
3 Q
4 Q
The state-space tree of this search is shown in Figure 6.2
21CB1401 Design & Analysis of Computer Algorithm UNIT-4

Figure 6.2: State-space tree of solving the four-queens problem by


backtracking.× denotes an unsuccessful attempt to place a queen in the
indicated column. The numbers above the nodes indicate the order in which
the nodes are generated.
For n =8. There is solution to place in 8 x 8 chessboard.
1 2 3 4 5 6 7 8
1 Q
2 Q
3 Q
4 Q
5 Q
6 Q
7 Q
8 Q
Figure 6.3: Solution 8 queens problem in 8 x 8 chessboard
21CB1401 Design & Analysis of Computer Algorithm UNIT-4

4.2.Hamiltonian Circuit Problem


A Hamiltonian circuit (also called a Hamiltonian cycle, Hamilton
cycle, or Hamilton circuit) is a graph cycle( i.e., closed loop) through
a graph that visits each node exactly once. A graph possessing a
Hamiltonian cycle is said to be a Hamiltonian graph.
Example - 1: Find Hamiltonian circuit starts at vertex a.

Solution: Assume that if a Hamiltonian circuit exists, it starts at


vertex a. accordingly, we make vertex a the root of the state-space tree
as in Figure 6.4
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
Figure 6.4: State-space tree for finding a Hamiltonian circuit. The numbers
above the nodes of the tree indicate the order in which the nodes are
generated.

 In a Graph G, Hamiltonian cycle begins at some vertex V 1 G,


and the vertices are visited only once in the order V 1, V2, . . . ,
Vn.(Vi are distinct except for V1 and Vn+1 which are equal)
 The first component
of our future solution, if it exists, is a first intermediate vertex of
a Hamiltonian circuit to be constructed. Using the alphabet order
to break the three-way tie among the vertices adjacent to a.
 Then select vertex b. From b, the algorithm proceeds to c, then to
d, then to e, and finally to f, which proves to be a dead end.
 So the algorithm backtracks from f to e, then to d, and then to c,
which provides the first alternative for the algorithm to pursue.
 Going from c to e eventually proves useless, and the algorithm
has to backtrack from e to c and then to b. From there, it goes to
the vertices f , e, c, and d, from which it can legitimately return to
a, yielding the Hamiltonian circuit a, b, f , e, c, d, a.
 If we wanted to find another Hamiltonian circuit, we could
continue this process by backtracking from the leaf of the
solution found.
4.3 Subset-Sum Problem
The subset-sum problem finds a subset of a given set A ={a1, . . . ,an}
of n positive integers whose sum is equal to a given positive integer d.
 For example, for A ={1, 2, 5, 6, 8} and d = 9, there are two
solutions:{1, 2, 6} and {1, 8}.
 Of course, some instances of this problem may have no solutions.
 It is convenient to sort the set’s elements in increasing order. So,
we will assume that a1 < a2 < . . . < an.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
 A ={3, 5, 6, 7} and d = 15 of the subset-sum problem. The
number inside a node is the sum of the elements already included
in the subsets represented by the node.
 The inequality below a leaf indicates the reason for its
termination.
Example:
The state space tree can be constructed as a binary tree like in
Figure 6.5 for the instance A ={3, 5, 6, 7} and d = 15

Figure 6.5: Complete state-space tree of the backtracking algorithm applied


to the instance A ={3, 5, 6, 7} and d = 15 of the subset-sum problem.
The number inside a node is the sum of the elements already included in the
subsets represented by the node.
The inequality below a leaf indicates the reason for its termination.

 The root of the tree represents the starting point, with no


decisions about the given elements made as yet.
 Its left and right children represent, respectively, inclusion and
exclusion of a1 in a set being sought.
 Similarly, going to the left from a node of the first level
corresponds to inclusion of a2 while going to the right
corresponds to its exclusion, and so on.
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
 Thus, a path from the root to a node on the i level of the tree
th

indicates which of the first i numbers have been included in the


subsets represented by that node.
 Record the value of s, the sum of these numbers, in the node. If s
is equal to d, we have a solution to the problem.
 Report this result either stop or, if all the solutions need to be
found, continue by backtracking to the node’s parent.
 If s is not equal to d, then terminate the node as non-promising if
either of the following two inequalities holds:
s’ + si+1 > d (the sum s’ is too large)
s’ + j < d (the sum s’ is too small)
5. GRAPH COLORING PROBLEM
The process of assigning colors to the vertices such that no two
adjacent vertexes have the same color is caller Graph Colouring. This
is also known as vertex coloring.
Using Backtracking Algorithm

The backtracking algorithm makes the process efficient by avoiding


many bad decisions made in naïve approaches.
In this approach, we color a single vertex and then move to its
adjacent (connected) vertex to color it with different color.
After coloring, we again move to another adjacent vertex that is
uncolored and repeat the process until all vertices of the given graph
are colored.
In case, we find a vertex that has all adjacent vertices colored and no
color is left to make it color different, we backtrack and change the
color of the last colored vertices and again proceed further.
If by backtracking, we come back to the same vertex from where we
started and all colors were tried on it, then it means the given number
21CB1401 Design & Analysis of Computer Algorithm UNIT-4
of colors (i.e. ‘m’) is insufficient to color the given graph and we
require more colors (i.e. a bigger chromatic number).

Steps To color graph using the Backtracking Algorithm:


1. Different colors:
 Confirm whether it is valid to color the current vertex with
the current color (by checking whether any of its adjacent
vertices are colored with the same color).
 If yes then color it and otherwise try a different color.
 Check if all vertices are colored or not.
 If not then move to the next adjacent uncolored vertex.
2. If no other color is available then backtrack (i.e. un-color last
colored vertex).
Here backtracking means to stop further recursive calls on adjacent
vertices by returning false. In this algorithm Step-1.2 (Continue) and
Step-2 (backtracking) is causing the program to try different color
option
Continue – try a different color for current vertex.
Backtrack – try a different color for last colored vertex.

You might also like