Daa Endsem Paper Sol Unit IV
Daa Endsem Paper Sol Unit IV
Graph coloring is the problem of coloring each vertex in a graph such that no two
adjacent vertices have the same color.
If d is the degree of a given graph, then it can be colored with d+1 colors.
Chromatic number: minimum number of colors to color the graph such that no two
adjacent vertices have the same color. Chromatic number is decided by the graph type
i.e whether the graph is complete graph, wheel graph, star graph or cycle graph.
Some direct examples: Map coloring
The ‘m-colorability’ optimization problem asks for the smallest integer m for which
the graph G can be colored. This integer is referred to as the chromatic number of the
graph.
Explicit Constraint : Si ∈ {1..d} where d = chromatic no. of the graph
Implicit Constraint: Color all vertices of a given graph such that no two adjacent
vertices should be of same color.
As an example:
The vertices are enumerated in order x1 – x5 The min. no. of colors needed = 3
x1 x2
x5
x3 x4
2. What is backtracking? Discuss sum of subset problem with the help of an example.
(1,2,7) (0,2,7)
X2= X2= X2= X2=
1 0 1 0
(1,3,4) Backtrack Backtrack
Backtrack
X3=1
Subset
found(1,0,1)
X = < A, B, C, B, D, A, B >
Y = < B, D, C, A, B, A >
X = < A, B, C, B, D, A, B >
Y = < B, D, C, A, B, A >
Application : in DNA matching to find two similar strands. And find the longest strand
which appears in both previous strands in the same order.
A subsequence which is common to both and whose length is longest is called longest
common subsequence LCS.
ì 0 if i=0 or j=0
ï
c[i, j ] = í c[i - 1, j - 1] + 1 if i,j>0 and xi=y j
ïmax{c[i, j - 1] ¬, c[i - 1, j ] } if i,j>0 and x y
î i j
A divide and conquer approach would repeatedly solve the common subproblems. -
Top down approach.
Dynamic programming solves every subproblem just once and stores the answer in a
table.- Bottom up approach.
The first step in solving an optimization problem by dynamic or greedy programming
is to characterize the structure of an optimal solution.
We say that a problem exhibits optimal substructure if an optimal solution to the
problem contains within it optimal solutions to subproblems.
Investigating the optimal substructure of a problem by iterating on subproblem
instances is a good way to infer a suitable space of subproblems for dynamic
programming.
For example, the Shortest Path problem has following optimal substructure property:
If a node x lies in the shortest path from a source node u to destination node v then the
shortest path from u to v is combination of shortest path from u to x and shortest path
from x to v.
5. Solve the subset sum problem using Backtracking where n=4, m=18, w[4] =
{5,10,8,13}
(5,2,31) (0,2,31)
X2=1 X2=0 X2=1 X2=0
Subset found
=(1,0,0,1)
6. Give Floyd-Warshall Algorithm to find the shortest path for all pair of vertices in a
graph. Give the complexity of the algorithm. Explain with example. (2019-20)
Given:
o Directed graph G = (V, E)
o Weight function w : E → R
Compute:
o The shortest paths between all pairs of vertices in a graph
o Representation of the result: an n × n matrix of shortest-path distances δ(u, v)
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
3. for k ← 1 to n
4. for i ← 1 to n
5. for j ← 1 to n
6. dij(k) ← min (dij(k-1), dik(k-1) + dkj(k-1))
(n)
7. return D
Running time: (n3) : As there are three nested for loops each having [1..n] iterations
1 2 3 4
1 0 1 2 3 4
(0) 2 0 1 1 1 0
D =W=
3 1 0 1 2 2 0 1 1
4 1 0 D
(4) 3 1 2 0 1
4 3 1 2 0
(1) (0)
D =D
1 2 3 4 2
1
1 0
2 0 1 1
D
(2) 3 1 0 1
4 1 2 0 3 4
A feasible solution satisfies all the problem's constraints. Given n inputs and we
are required to form a subset such that it satisfies some given constraints then
such a subset is called feasible solution.
Dynamic programming is a technique where you store the result of previous calculation
to avoid calculating the same once again. DP is basically a memorization technique which
uses a table to store the results of sub-problem so that if same sub-problem is encountered
again in future, it could directly return the result instead of re-calculating it.
During recursion, there may exist a case where same sub-problems are solved multiple
times.
But Dynamic programming is a technique where you store the result of previous
calculation to avoid calculating the same once again. DP is basically a memorization
technique which uses a table to store the results of sub-problem so that if same sub-
During recursion, there may exist a case where same sub-problems are solved multiple
times.
S4 = {(0,0),(11,2),(21,11),(32,13),(33,15),(42,24),(44,17),(52,33),(54,26),(63,35)(65,28),
(75,39)}
For i = n down to 1
1 2 3 4 5 1 2 3 4 5
D 0 1 - ∞ 6 3 ∞ 1 - ∞ 6 3 ∞
2 3 - ∞ ∞ ∞ D1 2 3 - 9 6 ∞
3 ∞ ∞ - 2 ∞ 3 ∞ ∞ - 2 ∞
4 ∞ 1 1 - ∞ 4 ∞ 1 1 - ∞
5 ∞ 4 ∞ 2 - 5 ∞ 4 ∞ 2 -
1 2 3 4 5 1 2 3 4 5
D2 1 - ∞ 6 3 ∞ 1 - ∞ 6 3 ∞
2 3 - 9 6 ∞ D 3 2 3 - 9 6 ∞
3 ∞ ∞ - 2 ∞ 3 ∞ ∞ - 2 ∞
4 4 1 1 - ∞ 4 4 1 1 - ∞
5 7 4 13 2 - 5 7 4 13 2 -
1 2 3 4 5 1 2 3 4 5
1 - 4 4 3 ∞ 1 - 4 4 3 ∞
2 3 - 7 6 ∞ D5
2 3 - 7 6 ∞
D4 3 6 3 - 2 ∞ 3 6 3 - 2 ∞
4 4 1 1 - ∞ 4 4 1 1 - ∞
5 6 3 3 2 - 5 6 3 3 2 -
11. What is sum of subset problem? Draw a state space tree for sum of subset problem
using backtracking. N=6, m=30 and w[1..6] = {5,10,12,13,15,18}
12. What is travellingWhat is travelling salesman problem (TSP)? Find the solution of
following TSP using Branch & Bound method
1 2 3 4 Min. 1 2 3 4 Min.
1 0 1 15 6 1 = 1 ∞ 0 14 5 1
2 2 0 7 3 2 2 0 ∞ 5 1 2
3 9 6 0 12 6 3 3 0 ∞ 6 6
4 10 4 8 0 4 4 6 0 4 ∞ 4
Min. 0 0 4 1
1-2-3=24 1-2-4=21
1-2-4-3-1=21
1-2 : make row 1 and column 2 infinity and then select minimum row wise followed by
column wise and reduce the matrix.
1 2 3 4 1 2 3 4
1 ∞ ∞ ∞ ∞ 1 ∞ ∞ ∞ ∞
2 0 ∞ 1 0 0 2 0 ∞ 1 0 0
3 3 ∞ ∞ 5 3 3 0 ∞ ∞ 2 3
4 6 ∞ 0 ∞ 0 4 6 ∞ 0 ∞ 0
0 0 0 3
Reduction cost = 3
1-3 : make row 1 and column 3 infinity and then select minimum row wise followed by
column wise and reduce the matrix.
1-4 : make row 1 and column 3 infinity and then select minimum row wise followed by
column wise and reduce the matrix.
1 2 3 4 1 2 3 4
1 ∞ ∞ ∞ ∞ 1 ∞ ∞ ∞ ∞
2 0 ∞ 1 ∞ 2 0 ∞ 1 ∞ 0
3 3 0 ∞ ∞ 3 3 0 ∞ ∞ 0
4 6 0 0 ∞ 4 6 0 0 ∞ 0
0 0 0 0
Reduction cost = 0
1-2-3 1 2 3 4 1 2 3 4
1 ∞ ∞ ∞ ∞ 1 ∞ ∞ ∞ ∞
2 ∞ ∞ ∞ ∞ 0 2 ∞ ∞ ∞ ∞ 0
3 0 ∞ ∞ 2 2 3 0 ∞ ∞ 0 2
4 6 ∞ ∞ ∞ 0 4 6 ∞ ∞ ∞ 0
2 0 0 2
Reduction cost = 2
1-2-4
1 2 3 4
1 ∞ ∞ ∞ ∞
2 ∞ ∞ ∞ ∞ 0
3 0 ∞ ∞ ∞ 0
4 6 ∞ 0 ∞ 0
0 0 0 0
Reduction cost = 0
1-2-4-3-1
Prune (1-3) and (1-4) as both partial tours (28 and 22 resp) are greater than full tour (1-2-4-3-
1) cost 21.
13. Discuss N-Queen’s Problem. Solve 4 Queen’s problem using backtracking method. (2021-
22)
N-Queen’s Problem
The object is to place n queens on a n x n chess board in such as way as no queen can capture
another one in a single move
o Recall that a queen can move horizontal, vertical, or diagonally an infinite distance
This implies that no two queens can be on the
same row,
same column,
same diagonal
4-Queen’s Problem :
To place four queens on an 4x4 chessboard so that no two attack, that is, so that no two of
them are on the same row, column or diagonal.
Let the rows & columns are numbered 1 thru 4. Since each queen must be on different row, so
we can in general assume that we can place queen i on row i.
All solutions to 4-Queens problem can therefore be represented as 4-tuples (x1,x2,x3,x4),
where xi is the column in which the queen i is placed.
Explicit constraints are Si = {1,2,3,4} 1 ≤ i ≤ 4
Therefore, solution space consists of 44 4 tuples.
The implicit constraints are that no two xi ‘s can be the same. (i.e., all queens must be on
different columns and no two queens can be on the same diagonal.
NQueens(k,n)
for i = 1 to n
if Place(k,i)
x[k] = i
if (k = n)
write(x[1:n])
else
NQueens(k+1,n)
Place(k,i)
for j = 1 to k-1
2021-22
14. Explain Branch and Bound in brief.
An enhancement of backtracking
a. Similarity
i. A state space tree is used to solve a problem.
b. Difference
i. The branch-and-bound algorithm does not limit us to any particular way of
traversing the tree.
ii. Used only for optimization problems (since the backtracking algorithm
requires the using of DFS traversal and is used for decision problems / non-
optimization problems.
B&B technique is a systematic method for solving optimization problems.
The idea is to set up a bounding function, which is used to compute a bound (for the value
of the objective function) at a node on a state-space tree and determine if it is promising
c. Promising (if the bound is better than the value of the best solution so far): expand
beyond the node.
d. Nonpromising (if the bound is no better than the value of the best solution so far):
not expand beyond the node (pruning the state-space tree).
Backtracking is useful / effective for decision problems, but not designed for optimization
problems where we also have a cost fn f(n) that we wish to maximize or minimize.
15. What is travellingWhat is travelling salesman problem (TSP)? Find the solution of
following TSP using Branch & Bound method
1 2 3 4 5 1 2 3 4 5
1 - 20 30 10 11 10 1 - 10 20 0 1 10
2 15 - 16 4 2 2 Row wise 2 13 - 14 2 0 2
reduction
3 3 5 - 2 4 2 3 1 3 - 0 2 2
4 19 6 18 - 3 3 4 16 3 15 - 0 3
5 16 4 7 16 - 4 5 12 0 3 12 - 4
1 0 3 0 0 25
1 2 3 4 5
Column 1 - 10 17 0 1
wise
2 12 - 11 2 0
reduction Final Matrix with
3 0 3 - 0 2 Lower Bound = 25
4 15 3 12 - 0
5 11 0 0 12 -
1-2 : : make row 1 and column 2 infinity and then select minimum row wise followed by
column wise and reduce the matrix.
1-3 : : make row 1 and column 3 infinity and then select minimum row wise followed by
column wise and reduce the matrix.
1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
2 12 - ∞ 2 0 0
3 0 3 ∞ 0 2 0
4 15 3 ∞ - 0 0
5 11 0 ∞ 12 - 0
0 0 0 0 0
1-4 : : make row 1 and column 4 infinity and then select minimum row wise followed by
column wise and reduce the matrix.
1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
2 12 - 11 ∞ 0 0
3 0 3 - ∞ 2 0
4 15 3 12 ∞ 0 0
5 11 0 0 ∞ - 0
0 0 0 0 0
1-5: : make row 1 and column 5 infinity and then select minimum row wise followed by
column wise and reduce the matrix.
1-4-2 : : make row 1 and column 4 , row 4 column 2 infinity and then select minimum row
wise followed by column wise and reduce the matrix.
1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
2 12 ∞ 11 ∞ 0 0
3 0 ∞ - ∞ 2 0
4 ∞ ∞ ∞ ∞ ∞
5 11 ∞ 0 ∞ - 0
0 0 0 0
1-4-3 : : make row 1 and column 4 , row 4 column 3 infinity and then select minimum row
wise followed by column wise and reduce the matrix.
1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
2 12 - ∞ ∞ 0 0
3 0 3 ∞ ∞ 2 0
4 ∞ ∞ ∞ ∞ ∞
5 11 0 ∞ ∞ - 0
0 0 0 0
1-4-5 : : make row 1 and column 4 , row 4 column 5 infinity and then select minimum row
wise followed by column wise and reduce the matrix.
1-4-2-3 : : make row 1 and column 4 , row 4 column 2 followed by row 2 column 3 infinity
and then select minimum row wise followed by column wise and reduce the matrix.
1 2 3 4 5 1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞ ∞ ∞ ∞
2 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞
3 0 ∞ ∞ ∞ 2 0 3 0 ∞ ∞ ∞ 0 0
4 ∞ ∞ ∞ ∞ ∞ 4 ∞ ∞ ∞ ∞ ∞
5 11 ∞ ∞ ∞ - 11 5 0 ∞ ∞ ∞ - 11
0 2 13 0 2 13
1-4-2-5 : : make row 1 and column 4 , row 4 column 2 followed by row 2 column 5 infinity
and then select minimum row wise followed by column wise and reduce the matrix.
1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
2 ∞ ∞ ∞ ∞ ∞
3 0 ∞ - ∞ ∞ 0
4 ∞ ∞ ∞ ∞ ∞
5 11 ∞ 0 ∞ ∞ 0
0 0 0
1 2 3 4 5
1 ∞ ∞ ∞ ∞ ∞
2 ∞ ∞ ∞ ∞ ∞
3 0 ∞ - ∞ ∞ 0
4 ∞ ∞ ∞ ∞ ∞
5 ∞ ∞ ∞ ∞ ∞ 0
0 0
16. Explain the method of finding Hamiltonian cycles in a graph using backtracking
method with suitable example.
NextValue(k)