0% found this document useful (0 votes)
168 views20 pages

Daa r22 Unit-2 QB Answers Key

Uploaded by

anilsevakula123
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)
168 views20 pages

Daa r22 Unit-2 QB Answers Key

Uploaded by

anilsevakula123
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/ 20

GURU NANAK INSTITUTE OF TECHNOLOGY

(A UGC Autonomous Institution – Affiliated to JNTUH)


Ibrahimpatnam, Ranga Reddy (District) - 501 506.

DESIGN AND ANALYSIS OF ALGORITHMS (22PC0CS08)


UNIT-II
Short Answer Questions
1 Define disjoint set and give example.
2 What are the operations can be performed on disjoint set?
3 How to represent the disjoint sets?
4 Write simple union and find algorithms
5 Define weighting rule.
6 Define E-node and Live node.
7 Differentiate the Dead node and Live node
8 What is a chromatic number?
9 List the applications of disjoint set
10 What is the need of collapsing find?

Answers
1. Define disjoint set and give example.
Ans: Disjoint Set: If Si and Sj, i ≠ j, are two sets, then there is no element that is in both Si and Sj.
Example: Consider two disjoint sets S1={1, 2, 4, 6} and S2={7, 8}.
The possible tree representations for S1, S2 and S1US2 are:

S1 S2 S1 U S2
_______________________________________________________________________

Figure: Possible Tree Representations of the disjoint sets S1, S2, and S1 U S2

2. What are the operations can be performed on disjoint set?


Ans: Disjoint Set Operations:
 There are two operations that can be performed on Disjoint Sets.
They are:
i) Disjoint set Union and
ii) Disjoint set Find
3. How to represent the disjoint sets?
Ans: Disjoint Set: If Si and Sj, i ≠ j are two sets, then there is no element that is in both Si and Sj.
 Disjoint sets can be represented in following ways:
1. Tree Representation
2. Data Representation
3. Array Representation

4. Write simple union and find algorithms.


Ans:

Algorithm:
5. Define weighting rule.
Ans: Weighting Rule:
 If the number of nodes in tree with root “i” is less than the number of nodes in tree
root “j”, then make j the parent of i, otherwise make i the parent of j.

6. Define E-node and Live node.


Ans: E-node: The live node whose children are currently being generated is called the E-node (node
being expanded).
Live node: Live node is a node that has been generated but whose children have not yet been generated.

7. Differentiate the Dead node and Live node.


Ans: Dead node: It is generated node that is either not to be expanded further or one for which all of
its children have been generated.
Live node: Live node is a node that has been generated but whose children have not yet been generated.

8. What is a chromatic number?


Ans: Chromatic Number:
 The minimum number of colors required to color a graph is called chromatic number and the
chromatic number is represented by (G).
9. List the applications of disjoint set.
Ans: Applications of Disjoint Set:
1) It is used to keep track of connected components in an undirected graph.
2) It is used to detect cycles in the `.
3) It is used to calculate the minimum spanning tree in Kruskal's algorithm.
4) It is used in Maze generation problems.
5) Calculating mutual friends.

10. What is the need of collapsing find?


Ans: Collapsing Rule for Find:
 If ‘j’ is a node on the path from ‘i’ to its root and p[i] ≠ root[i], then set p[j] to root[i].
 Collapsing find algorithm is used to perform find operation on the tree created by
WeightedUnion.
UNIT-II LONG ANSWERS
a) Describe disjoint sets with its various representations.
1
b) Write Simple union and find algorithms.
a) Explain weighting rule for union with example.
2 b) Write a short note on: i) Explicit constraints and
ii) Implicit constraints

3 Elaborate the need of collapsing find with an algorithm.

a) Discuss the backtracking technique.


4
b) Explain the constraints that are used to solve any problem using backtracking.

a) Give the strategy to represent the solution vector for 4-Queens problem with
neat sketch.
b) In a class, there will be only one “three-seater” bench is available. 2 boys and
5 1 girl must sit in that bench. Explain the following using backtracking with
state space tree
1) What are the possibilities of seating arrangements?
2) If “girl must not sit in between 2 boys” means, what are the possibilities?
a) Explain n-queens problem with algorithm.
6
b) Draw the complete/full state-space tree for the 4-queens problem.
a) Solve the sum of subset problem:
b) Given weights w[1:6] = {5, 10, 12, 13, 15, 18}. Sum of weights for subset (m)
7
should be 30. Draw the portion of state-state space tree that is generated using
SumOfSubsets algorithm.
a) Explain recursive backtracking algorithm.
8 b) Write a short note on:
i) Live node ii) E-node iii) Dead node
a) Discuss Graph-coloring problem.
b) Draw a state-space tree for the below 4-node graph for m-
colorability problem.
9
c) Two sets S1 and S2 are given as S1={1, 2, 4, 6} and S2 = {7, 8}.
i) Draw disjoint sets for S1 and S2 using trees.
ii) Draw disjoint set S3 using trees such that S3=S1 U S2.
a) Explain sum of subsets problem with algorithm.
10 b) Draw three graphs, among which, two graphs must have Hamiltonian cycle
and one cannot have it.
UNIT-II LONG ANSWERS KEY
1. a) Describe disjoint sets with its various representations.
b) Write Simple union and find algorithms.
Ans:
1. a) Disjoint Set:
 If Si and Sj, i ≠ j are two sets, then there is no element that is in both Si and Sj.
 Various Representations of Disjoint sets are:
1. Tree Representation
2. Data Representation
3. Array Representation
 Example: Let n = 10 elements can be partitioned into three disjoint sets as follows:
S1 = { 1, 7, 8, 9 }, S2 = { 2, 5, 10 } and S3 = { 3, 4, 6 }
1. Tree Representation:
The Tree Representations for these disjoints are:

--------------------------------------------------------------------------------------------------
Figure: Possible Tree representations for the disjoint sets S1, S2, S3
2. Data Representation:
 To perform Union or Find operations efficiently on sets, it is necessary to represent the set
elements in a proper manner.
 The data representation for S1, S2, S3 take the following form:

Figure: Data Representation of Disjoint sets


 If we wish to unite Si and Sj then we need to unite the trees with roots FindPointer(S i) and

FindPointer(Sj).
 Here “FindPointer” is a function that takes a set name and determines the root of the tree that
represents it.
 This is done by an examination of the “[setname, pointer]” table.
 In many applications, the “setname” is just the element at the root.
 The Find(i) operation now becomes:
˗ Determine the root of the tree containing element “i”.
3. Array Representation:
 Since the set elements are numbered 1 through n, we represent the tree nodes using an array
p[1:n], where n is the maximum no. of elements.
 The ith element of this array represents the tree node that contains element i.
 This array element gives the parent pointer of the corresponding tree node.
 The below figure shows the array representation of the sets S1, S2 and S3 of above tree
representation.
 The root nodes have a parent of -1.

Figure: Array Representation of sets


1. b) Write Simple union and find algorithms.
Ans: Algorithm for Simple Union operation:
 To perform union, the SimpleUnion(i,j) function takes the inputs as the set roots i and j . And make the
parent of i as j i.e, make the second root as the parent of first root.

Algorithm :
----------------------------------------------------------------------------------------------------------------------------- -----
2. a) Explain weighting rule for union with example.
b) Write a short note on: i) Explicit constraints and ii) Implicit constraints.
Ans:
2. a) Weighted Union:
 To improve the performance of union and Find algorithms and avoiding the creation of degenerate
trees, the “Weighting rule for Union (i, j)” is used.
 Definition: If the number of nodes in tree with root “i” is less than the number of nodes in tree root
“j”, then make j the parent of i, otherwise make i the parent of j.
 When we use the weighting rule to perform the sequence of set unions given before, we obtain the
trees of below figure.

Figure: Trees obtained using the weighting rule

 The trees are obtained in above figure using the Weighting rule to perform the sequence of set
unions given before.
 In this figure, the unions have been modified so that the input parameter values correspond to the
roots of the trees to be combined.

Algorithm: Union algorithm with Weighting rule.


2. b) Write a short note on: i) Explicit constraints and ii) Implicit constraints.
Ans: i) Explicit constraints:
 Explicit constraints are the rules that restrict each xi to take on values only from a given set.
Examples:
i) xi ≥ 0 or si={all non-negative real numbers}
ii) Xi=0 or 1 or Si={0, 1}
iii) li ≤ xi ≤ ui or si={a: li ≤ a ≤ ui }
ii) Implicit constraints:
 Implicit constraints are the rules that determine which of the tuples in the solution space of
Instance characteristics I satisfy the criterion function.
 Thus implicit constraints describe the way in which the xi must relate to each other.
Example:
The implicit constraint for n-queens problem is that no two queens can be on the same row, same
column or same diagonal.
----------------------------------------------------------------------------------------------------------------------------- -----
3. Elaborate the need of collapsing find with an algorithm.
Ans: Collapsing Find:
 If ‘j’ is a node on the path from ‘i’ to its root and p[i] ≠ root[i], then set p[j] to root[i].
 Collapsing find algorithm is used to perform find operation on the trees created by WeightedUnion.

Algorithm: Find algorithm with Collapsing Rule


Collapsing Find Example (Can also write from DAA Notes)

----------------------------------------------------------------------------------------------------------------------------- -----
4. a) Discuss the backtracking technique.
b) Explain the constraints that are used to solve any problem using backtracking.
Ans:
4 a) Backtracking:
 In solving some problems, a situation may arise where there are different ways leading from a
given position but unfortunately, none of them known to lead to a solution.
 After trying one path successfully, we return to the previous position and try to find a solution
using another path.
 However, we must ensure that, we must ensure that such a return is possible and all paths can be
tried. This technique is called “Backtracking”.

Algorithm: Recursive Backtracking algorithm

Explanation:
 The solution vector (xi, ..., xn) is treated as a global array x[l:n].
 All of the possible elements for the kth position of the tuple which satisfy B k are generated, one by
one, and adjoined to the current vector (x1, ..., xk-1).
 Each time xk is attached a check is made to determine if a solution has been found. Then the
algorithm is recursively invoked.
 When the for loop is exited, no more values for xk exist and the current copy of Backtrack ends.
 The last unresolved call now resumes, namely the one which continues to examine the remaining
elements assuming only k – 2 values have been set.
 Note that this algorithm causes all solutions to be printed and assumes that tuples of various sizes may
make up a solution. If only a single solution is desired, then a flag can be added as a parameter to
indicate the first occurrence of success.
4. b) Explain the constraints that are used to solve any problem using backtracking.
Ans: Backtracking is a Depth-First Search (DFS) with a bounding function known to be criterion function.
 Criterion Function: It is a function P(x1, x2, … , xn) which needs to be maximized or minimized for a
given problem.
 Many problems while solving by backtracking are required to satisfy a complexity set of constraints.
 For any problem, these constraints are categorized into two types: Explicit and Implicit constraints.
Explicit constraints:
 Explicit constraints are the rules that restrict each xi to take on values only from a given set.
Common Examples:
iv) xi ≥ 0 or si={all non-negative real numbers}
v) Xi=0 or 1 or Si={0, 1}
vi) li ≤ xi ≤ ui or si={a: li ≤ a ≤ ui }
 Explicit constraints depend on a particular instance I of the problem being solved.
 All tuples that satisfy the explicit constraints define a possible “solution space” for I.
ii) Implicit constraints:
 Implicit constraints are the rules that determine which of the tuples in the solution space of
Instance characteristics I satisfy the criterion function.
 Thus implicit constraints describe the way in which the xi must relate to each other.
Example:
The implicit constraint for n-queens problem is that no two queens can be on the same row, same
column or same diagonal.
----------------------------------------------------------------------------------------------------------------------------- -----
5. a) Give the strategy to represent the solution vector for 4-Queens problem with neat sketch.
b) In a class, there will be only one “three-seater” bench is available. 2 boys and 1 girl must sit in
that bench. Explain the following using backtracking with state space tree
1. What are the possibilities of seating arrangements?
2. If “girl must not sit in between 2 boys” means, what are the possibilities?
Ans:
5. a) 4-Queens Problem:
 Consider a 4x4 chessboard. Let there be 4-queens.
 The objective is to place these 4-queens on a 4x4 chessboard in such a way that no two queens should be
placed in the same row, same column or same diagonal positions.
 The explicit constraints are 4-queens have to be placed on 4x4 chessboard.
 The implicit constraint is that – no two queens are to be placed in the same row, same column or diagonal.
Solving 4-queens problem:
 Consider 4-queens and a 4x4 chessboard.
 Let (x1, x2, x3, x4) be the solution vector, where xi – column on which the queen-I must be placed.
 Start with an empty chessboard and place first queen in first row and first column.

 The second queen Q2 should not be placed in 1 st row and 2nd column. But it can be placed in 2 nd
row and, in third or fourth columns.
If we place in 2nd column, Q1 and Q2 will be in same diagonal, so let us place in 3 rd column.

 We cannot place the third queen Q3 in 3rd row, so we should go back to second queen and place it in 4th
column.
 The third queen now, can be placed in second column.

 Now, the fourth queen Q4 cannot be placed in fourth row. So, trace back and try to change the position of
Q3.

 So, go back to second row and try to change the position of Q2. But Q2 is already in last column, and
cannot move further. So, backtrack to Q1 and change the position of first queen Q1 to second column.

 In above figure, we placed the remaining queens-Q2, Q3 & Q4 in their appropriate positions.
 Hence the solution vector for the 4-Queen’s problem is (x1, x2, x3, x4) = (2, 4, 1, 3).
[which means that Q1 is placed in 2nd column, Q2 in 4th column, Q3 in 1st column and Q4 is placed in
3rd column].
5. b) In a class, there will be only one “three-seater” bench is available. 2 boys and 1 girl must sit in
that bench. Explain the following using backtracking with state space tree
1. What are the possibilities of seating arrangements?
2. If “girl must not sit in between 2 boys” means, what are the possibilities?
Answer:
 Given that there is a three-seater bench, with two boys and a girl must sit on it.
 There are total of 3! = 6 possibilities. We will try all the possibilities and get the possible solutions.
 Let B1, B2 and G be two boys and a girl respectively.

Figure: All the possible seating arrangements


i) The State-space tree for all the possibilities of seating arrangements are:

 The above state-space tree represents all the possible seating arrangements of the two boys and a girl
on a three-seater bench.
ii) The possible State-space tree for “girl must not sit in between 2 boys”:
 Here the given constraint is that “The girl must not sit in between two boys”.

 In the above state-space tree, whenever G comes in between B1 & B2, there the further nodes get
killed, as it is a bounding function.
6. a) Explain n-queens problem with algorithm.
b) Draw the complete/full state-space tree for the 4-queens problem.

6. a) Answer:
n-queens Problem:
 Given n-queens and a nxn chessboard.
 The objective is to place these n-queens on a nxn chessboard in such a way that no two queens should be
placed in the same row, same column or same diagonal positions.
 The explicit constraints are n-queens have to be placed on nxn chessboard.
 The implicit constraint is that – no two queens are to be placed in the same row, same column or diagonal.

Algorithm: All solutions to the n-queens problem

Algorithm: To check whether a new queen be placed.


6. b) Draw the complete/full state-space tree for the 4-queens problem.
Ans: State-Space Tree for 4-Queens problem:
 The following figure shows the complete state-space tree for the 4-queens problem.

Figure: Tree organization of the 4-queens solution space.


Nodes are numbered as in Depth-first search manner.
----------------------------------------------------------------------------------------------------------------------------- ------
7. Solve the sum of subset problem: Given weights w[1:6] = {5, 10, 12, 13, 15, 18}. Sum of weights for subset
(m) should be 30. Draw the portion of state-state space tree that is generated using SumOfSubsets
algorithm.
Solution: Given that, no. of objects, n=6
The given weights are w1=5, w2=10, w3=12, w4=13, w5=15, and w6=18 and
the total weight, m=30.
Let us initialize, Sum (S) = 0,
Index (k) = 1 and
the remaining sum (r) = 73 [sum of given weights:5+10+12+13+15+18]
The state-space tree for the given sum of subsets problem is given as follows:
Figure: Portion of State-space tree that is generated using SumOfSubsets algorithm

 In above figure, the rectangular nodes list the values of S, k, and r.


 Circular nodes represent a point at which subsets with sum ‘m’ are printed out.
 At nodes A, B, and C, the output is respectively given as
(1, 1, 0, 0, 1), (1,0, 1, 1) and (0, 0, 1, 0, 0, 1)
----------------------------------------------------------------------------------------------------------------------------- -----
8. a) Explain recursive backtracking algorithm.
b) Write a short note on:
i) Live node ii) E-node iii) Dead node
8. a) Recursive Backtracking algorithm:
Ans: Backtracking:
 In solving some problems, a situation may arise where there are different ways leading from a
given position but unfortunately, none of them known to lead to a solution.
 After trying one path successfully, we return to the previous position and try to find a solution
using another path.
 However, we must ensure that, we must ensure that such a return is possible and all paths can be
tried. This technique is called “Backtracking”.
Algorithm: Recursive Backtracking algorithm

Explanation:
 The solution vector (xi, ..., xn) is treated as a global array x[l:n].
 All of the possible elements for the kth position of the tuple which satisfy B k are generated, one by one,
and adjoined to the current vector (x1, ..., xk-1).
 Each time xk is attached a check is made to determine if a solution has been found. Then the algorithm is
recursively invoked.
 When the for loop is exited, no more values for xk exist and the current copy of Backtrack ends.
 The last unresolved call now resumes, namely the one which continues to examine the remaining elements
assuming only k – 2 values have been set.
8. b) Write a short note on:
i) Live node ii) E-node iii) Dead node
Ans: i) Live node: Live node is a node that has been generated but whose children have not yet been
generated.
ii) E-node: The live node whose children are currently being generated is called the E-node (node being
expanded).
iii) Dead node: It is generated node that is either not to be expanded further or one for which all of its
children have been generated.
9. a) Discuss Graph-coloring problem.
b) Draw a state-space tree for the below 4-node graph for m-colorability problem.
c) Two sets S1 and S2 are given as S1={1, 2, 4, 6} and S2 = {7, 8}.
i) Draw disjoint sets for S1 and S2 using trees.
ii) Draw disjoint set S3 using trees such that S3=S1 U S2.

9. a) Graph-coloring problem:
Ans:
 Let G be an undirected graph and ‘m’ be a given positive integer.
 The graph coloring problem is assigning colors to the vertices of an undirected graph with the restriction
that no two adjacent vertices are assigned the same color yet only ‘m’ colors are used.
 The optimization version calls for coloring a graph using the minimum number of colorings.
 The decision version, known as m-coloring asks whether a graph is colourable using at most m-colors.
 Note that, if ‘d’ is the degree of the given graph then it can be colored with ‘d+1’ colors.
 The m-colorability optimization problem asks for the smallest integer ‘m’ for which the graph G can be
colored. This integer is referred as “Chromatic number” of the graph.
9. b) State-space tree for the given 4-node graph is:
Ans: Given graph:

The state-space tree for the above graph is:

Figure:
9. c) Two sets S1 and S2 are given as S1={1, 2, 4, 6} and S2 = {7, 8}.
i) Draw disjoint sets for S1 and S2 using trees.
ii) Draw disjoint set S3 using trees such that S3=S1 U S2.
Solution: Given two disjoint sets S1={1, 2, 4, 6} and S2 = {7, 8}.
i) The possible tree representation for the disjoint sets S1 and S2 is:

S1 S2

ii) The possible tree representation for S3 = S1 U S2 is given as:

S3 = S1 U S2

----------------------------------------------------------------------------------------------------------------------------
10. a) Explain sum of subsets problem with algorithm.
b) Draw three graphs, among which, two graphs must have Hamiltonian cycle and one cannot have it.

10. a) Ans:
Sum of Subsets Problem:
 Given n-distinct positive numbers, usually called weights, wi, 1 ≤ i ≤ n, and m, this problem requires for
finding all subsets of wi whose sums are ‘m’.

 The Explicit constraints are xi { j | j is an integer and 1 ≤ j ≤ n }.

 The implicit constraints require that no two xi be the same and that the sum of the corresponding wi ‘s
be “m”.
 To avoid generating multiple instances of the same subset (for instance, (1, 2, 4) and (1, 4, 2) represent
the same subset). So another implicit constraint that is imposed is that xi < xi+1, 1 ≤ i ≤ n.
Algorithm: Recursive backtracking algorithm for Sum of Subsets problem.
----------------------------------------------------------------------------------------------------------------------------------------------

10. b) Draw three graphs, among which, two graphs must have Hamiltonian cycle and one cannot have
it.

Ans:

The Hamiltonian Cycles are:


1 – 3 – 4 – 5 – 6 – 7 – 8 – 2 – 1 and
1–2–8–7–6–5–4–3–1

The Hamiltonian Cycles are:


1 – 3 – 5 – 2 – 4 - 1 and
1–4–2–5–3-1

No Hamiltonian Cycle

End of Unit-II; By Mr. M. Ajaykumar

You might also like