Daa CT2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Question bank for CT-2

1. Write any 5 Characteristics of Dynamic Programming.


Ans:
Characteristics of Dynamic Programming:
a) If an optimal solution contains optimal sub solutions then a problem
exhibits optimal substructure.

b) When a recursive algorithm would visit the same subproblems


repeatedly, then a problem has overlapping subproblems.

c) If a problem has optimal substructure, then we can recursively define


an optimal solution.
d) If a problem has overlapping subproblems, then we can improve on a
recursive implementation by computing each subproblem only once.
e) If a problem doesn't have an optimal substructure, there is no basis for
defining a recursive algorithm to find the optimal solutions. If a
problem doesn't have overlapping subproblems, we don't have
anything to gain by using dynamic programming.

2. Explain graph coloring problem


Ans:
 Graph coloring: It is the procedure of assignment of colors to each
vertex of a graph G such that no adjacent vertices get same color. The
objective is to minimize the number of colors while coloring a graph.
The smallest number of colors required to color a graph G is called its
chromatic number of that graph. Graph coloring problem is a NP
Complete problem.
 Method to Color a Graph
The steps required to color a graph G with n number of vertices are as
follows −
Step 1 − Arrange the vertices of the graph in some order.
Step 2 − Choose the first vertex and color it with the first color.
Step 3 − Choose the next vertex and color it with the lowest numbered
color that has not been colored on any vertices adjacent to it. If all the
adjacent vertices are colored with this color, assign a new color to it.
Repeat this step until all the vertices are colored.
Example
In the above figure, at first vertex a is colored red. As the adjacent
vertices of vertex a are again adjacent, vertex b and vertex d are colored
with different color, green and blue respectively. Then vertex c is colored
as red as no adjacent vertex of c is colored red. Hence, we could color the
graph by 3 colors. Hence, the chromatic number of the graph is 3.

 Applications of Graph Coloring


Some applications of graph coloring include −
Register Allocation
Map Coloring
Bipartite Graph Checking
Mobile Radio Frequency Assignment
Making time table, etc.

3. Write down Component of Dynamic Programming.


Ans:
Components of Dynamic programming
i. Stages:The given problem can be divided into a number of
subproblems which are called stages. A stage is a small portion of
given problem.
ii. States:This indicates the subproblem for which the decision has to be
taken. The variables which are used for taking a decision at every
stage that is called as a state variable.
iii. Decision:At every stage, there can be multiple decisions out of which
one of the best decisions should be taken. The decision taken at each
stage should be optimal; this is called as a stage decision.
iv. Optimal policy:It is a rule which determines the decision at each and
every stage; a policy is called an optimal policy if it is globally
optimal. This is called as Bellman principle of optimality.
4. Compare Divide-and-Conquer and Dynamic Programming
Techniques.
Ans:

Divide and Conquer Method Dynamic Programming

1.It deals (involves) three steps at each 1.It involves the sequence of four steps:
level of recursion: o Characterize the structure of optimal
Divide the problem into a number of solutions.
subproblems. o Recursively defines the values of
Conquer the subproblems by solving optimal solutions.
them recursively.
o Compute the value of optimal
Combine the solution to the
solutions in a Bottom-up minimum.
subproblems into the solution for
o Construct an Optimal Solution from
original subproblems.
computed information.

2. It is Recursive. 2. It is non Recursive.


3. It does more work on subproblems 3. It solves subproblems only once and
and hence has more time consumption. then stores in the table.
4. It is a top-down approach. 4. It is a Bottom-up approach.
5. In this subproblems are independent 5. In this subproblems are interdependent.
of each other.
6. For example: Merge Sort & Binary 6. For example: Matrix Multiplication.
Search etc.

5. Consider the sum-of-subset problem, n = 6, Sum = 13, and w1 = 2,


w2 = 4, w3 = 7 and w4 = 8, w5=9 w6=5. Find a solution to the
problem using backtracking.
Ans:
NO COMMENTS PLEASE!!!!!
6. Explain N–Queens Problem ie 4 Queen & 8 Queen with example.
Ans:
N-Queens Problem

N - Queens problem is to place n - queens in such a manner on an n x n


chessboard that no queens attack each other by being in the same row,
column or diagonal.

It can be seen that for n =1, the problem has a trivial solution, and no
solution exists for n =2 and n =3.

4 queens problem

So first we will consider the 4 queens problem and then generate it to n -


queens problem.

Given a 4 x 4 chessboard and number the rows and column of the


chessboard 1 through 4.

Since, we have to place 4 queens such as q1 q2 q3 and q4 on the


chessboard, such that no two queens attack each other. In such a
conditional each queen must be placed on a different row, i.e., we put
queen "i" on row "i."

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 'q2' in (2, 4), the next best possible
solution. Then we obtain the position for placing 'q3' 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 'q1' and place it to
(1, 2) and then all other queens are placed safely by moving q2 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) i.e.

The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as


follows:
Fig shows the complete state space for 4 - queens problem. But we can
use backtracking method to generate the necessary node and stop if the
next node violates the rule, i.e., if two queens are attacking.

4 - Queens solution space with nodes numbered in DFS

It can be seen that all the solutions to the 4 queens problem can be
represented as 4 - tuples (x1, x2, x3, x4) where xi represents the column on
which queen "qi" is placed.

8 queens problem

One possible solution for 8 queens problem is shown in fig:

1. Thus, the solution for 8 -queen problem for (4, 6, 8, 2, 7, 1, 3, 5).


2. If two queens are placed at position (i, j) and (k, l).
3. Then they are on same diagonal only if (i - j) = k -
l or i + j = k + l.
4. The first equation implies that j - l = i - k.
5. The second equation implies that j - l = k - i.
6. Therefore, two queens lie on the duplicate diagonal if and only if |j-
l|=|i-k|

Place (k, i) returns a Boolean value that is true if the kth queen can be
placed in column i. It tests both whether i is distinct from all previous
costs x1, x2,....xk-1 and whether there is no other queen on the same
diagonal.

Using place, we give a precise solution to then n- queens problem.

Place (k, i)
{
For j ← 1 to k - 1
do if (x [j] = i)
or (Abs x [j]) - i) = (Abs (j - k))
then return false;
return true;
}

Place (k, i) return true if a queen can be placed in the kth row and ith
column otherwise return is false. x [] is a global array whose final k - 1
values have been set. Abs (r) returns the absolute value of r.

N - Queens (k, n)
{
For i ← 1 to n
do if Place (k, i) then
{
x [k] ← i;
if (k ==n) then
write (x [1....n));
else
N - Queens (k + 1, n);
}
}

You might also like