0% found this document useful (0 votes)
26 views

DAA - Internal 2 QP (With Key)

Daa

Uploaded by

R GAYATHRI
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)
26 views

DAA - Internal 2 QP (With Key)

Daa

Uploaded by

R GAYATHRI
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/ 22

Reg.

No:

AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING


INTERNAL ASSESSMENT II, 2023-2024
Subject Code : AD3351
Subject Title : DESIGN AND ANALYSIS OF ALGORITHM
Year /Sem: II/III
(Regulations R- 2021)

Answer ALL Questions Date: 14.11.2024

Implement and analyze the problems using dynamic programming and greedy algorithmic
CO3
techniques.
CO4 Solve the problems using iterative improvement techniques for optimization.
Compute the limitations of algorithmic power and solve the problems using backtracking
CO5
and branch and bound techniques.
BL – Bloom’s Taxonomy Levels.
(L1-Remembering, L2-Understanding, L3- Applying, L4-Analyzing, L5- Evaluating, L6-Creating)
PART – A (10  2=20 Marks)
1. What do you mean by dynamic programming? L1 CO3
2. Define Bipartite graph? L1 CO3
3. Define Augmenting path? L1 CO4
4. What is blocking pad? L1 CO4
5. Define backtracking and branch & bound technique? L1 CO5
PART – B [2 X 13 = 26 Marks]
a) Construct optimal binary seacrh tree with suitable example? 13 L2 CO3
6.
OR
b) Discuss in detail stable marriage problem? CO3
13 L2
L3
a) Explain travelling salesman using branch and bound technique? 13 CO4

7. OR
L2
b) Explain in detail about job assignment problem? 13 CO5

PART – C [1 X 14 = 14 Marks]
a) ) Give any 5 undecidable problem and explain the famous halting problem? 14 L2 CO4
8.
OR
CO5
b) Explain P, NP, NP complete and NP hard? 14 L1

Prepared by Subj. Co-Ordinator Faculty Chairman HOD/AIDS Principal


KEY
1)DYNAMIC PROGRAMMING:
Dynamic programming is a technique for solving problems by
breaking them down into smaller subproblems and storing the
solutions to those subproblems.
Dynamic programming breaks down problems into smaller
subproblems, solves them, and then uses those solutions to solve
larger subproblems. This technique is often used in decision-making
processes that involve sequential processes, such as manufacturing
management, stock management, and investment strategy

2) Bipartite graph: a graph whose vertices can be partitioned into


two disjoint sets V and U, not necessarily of the same size, so that
every edge connects a vertex in V to a vertex in U. A graph is bipartite
if and only if it does not have a cycle of an odd length.

3) An augmenting path for a matching M is a path from a free vertex


in V to a free vertex in U whose edges alternate between edges not
in M and edges in M∙ The length of an augmenting path is always
odd
∙ Adding to M the odd numbered path edges and deleting from it the
even numbered path edges increases the matching size by 1
(augmentation)
∙ One-edge path between two free vertices is special case of
augmenting path.

4) A pair (m, w) is said to be a blocking pair for matching M if man m


and woman w are not matched in M but prefer each other to their
mates in M.
5)

Backtracking Technique Branch and Bound technique


Backtracking constructs its Branch-and-bound is an algorithm
state- space tree in the depth- design technique that enhances
first search fashion in the the idea of generating a state-
majority of its applications. space tree with the idea of
estimating
the best value obtainable from a

If the sequence of choices If the estimate is not superior


represented by to the best solution seen up to
a current node of the state-space that point in the processing,
tree can be developed further the node
violating the problem's constraints, without is eliminated from
it is done by considering the first further consideration.
remaining legitimate option for the
next component. Otherwise, the
method backtracks by undoing the
last component of the partially
built

PART-B
6A) OPTIMAL BINARY SEARCH TREE Problem:
Given n keys a1 < …< an and probabilities p1, …, pn searching for them, find a
BST with a minimum average number of comparisons in successful search.
Since total number of BSTs with n nodes is given by C(2n,n)/(n+1), which
grows exponentially, brute force is hopeless.
Let C[i,j] be minimum average number of comparisons made in T[i,j], optimal
BST for keys ai < …< aj , where 1 ≤ i ≤ j ≤ n. Consider optimal BST among all
BSTs with some ak (i ≤ k ≤ j ) as their root; T[i,j] is the best among them.
The recurrence for C[i,j]:
C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n
C[i,i] = pi for 1 ≤ i ≤ j ≤ n
C[i, i- 1] = 0 for 1≤ i ≤ n+1 which can be interpreted as the number of
comparisons in the empty tree.
01jn
1 0 p 1 goal

EXAMPLE

Let us illustrate the algorithm by applying it to the four key set we used at the
beginning of this section:
KEY A B C D

PROBABILITY 0.1 0.2 0.4 0.3

Initial Tables:
Main Table Root Table

01234 01234
1 0 0.1 11
2 0 0.2 22
3 0 0.4 33
4 0 0.3 5 0 445

C(1,2): [i=1, j=2]


Possible key values k=1 and k=2.
K=1: c(1,2) = c(1,0) + c(2,2) + 0.1 + 0.2 = 0 + 0.2 + 0.1 + 0.2 = 0.5

K=2: c(1,2) = c(1,1) + c(3,2) + 0.1 + 0.2 = 0.1 + 0 + 0.1 + 0.2 = 0.4

Main Table Root Table

0 1 2 3 4 0 1 2 3 4

1 0 0.1 0.4 1 1 2
2 0 0.2 2 2

3 0 0.4 3 3

4 0 0.3 4 4

5 0 5

C(2,3): [i=2, j=3]


Possible key values k=2 and k=3.
K=2: c(2,3) = c(2,1) + c(3,3) + 0.2 + 0.4 = 0 + 0.4 + 0.2 + 0.4 = 1.0

K=3: c(2,3) = c(2,2) + c(4,3) + 0.2 + 0.4 = 0.2 + 0 + 0.2 + 0.4 = 0.8

Main Table Root Table

01234 01234
1 0 0.1 0.4 112
2 0 0.2 0.8 223
3 0 0.4 33
4 0 0.3 5 0 445

C(3,4): [i=3, j=4]


Possible key values k=3 and k=4.
K=3: c(3,4) = c(3,2) + c(4,4) + 0.4 + 0.3 = 0 + 0.3 + 0.4 + 0.3 = 1.0

K=4: c(3,4) = c(3,3) + c(5,4) + 0.4 + 0.3 = 0.4 + 0 + 0.4 + 0.3 = 1.1

Main Table Root Table

01234
1 0 0.1 0.4
2 0 0.2 0.8
3 0 0.4 1.0 4 0 0.3 5 0

C(1,3): [i=1, j=3]


Possible key values k=1, k=2 and k=3.
K=1: c(1,3) = c(1,0) + c(2,3) + 0.1 + 0.2 + 0.4 = 0 + 0.8 + 0.1 + 0.2 + 0.4 = 1.5

K=2: c(1,3) = c(1,1) + c(3,3) + 0.1 + 0.2 + 0.4 = 0.1 + 0.4 + 0.1 + 0.2 + 0.4 = 1.2

K=3: c(1,3) = c(1,2) + c(4,3) + 0.1 + 0.2 + 0.4 = 0.4 + 0 + 0.1 + 0.2 + 0.4 = 1.1

Main Table Root Table

0 1 2 3 4 0 1 2 3 4

1 0 0.1 0.4 1.1 1 1 2 3

2 0 0.2 0.8 2 2 3
3 0 0.4 1.0 3 3 3

4 0 0.3 4 4

5 0 5

C(2,4): [i=2, j=4]


Possible key values k=2, k=3 and k=4.
K=2: c(2,4) = c(2,1) + c(3,4) + 0.2 + 0.4 + 0.3 = 0 + 1.0 + 0.2 + 0.4 + 0.3 = 1.9

K=3: c(2,4) = c(2,2) + c(4,4) + 0.2 + 0.4 + 0.3 = 0.2 + 0.3 + 0.2 + 0.4 + 0.3 = 1.4

K=4: c(2,4) = c(2,3) + c(5,4) + 0.2 + 0.4 + 0.3 = 0.8 + 0 + 0.2 + 0.4 + 0.3 = 1.7

Main Table Root Table

01234 01234
1 0 0.1 0.4 1.1 1123
2 0 0.2 0.8 1.4 3 0 0.4 1.0 4 0 0.3 5 0 2 2 3 3 3 3 3 4 4 5

C(1,4): [i=1, j=4]


Possible key values k=1, k=2, k=3 and k=4.
K=1: c(1,4)=c(1,0)+c(2,4)+0.1+0.2+0.4+0.3= 0 + 1.4 + 0.1+ 0.2 + 0.4 + 0.3 =
2.4
K=2: c(1,4)=c(1,1)+c(3,4)+0.1+0.2+0.4+0.3= 0.1 + 1.0 + 0.1+ 0.2 + 0.4 + 0.3 =
2.1
K=3: c(1,4)=c(1,2)+c(4,4)+0.1+0.2+0.4+0.3= 0.4 + 0.3 + 0.1+ 0.2 + 0.4 + 0.3 =
1.7

K=4: c(1,4)=c(1,3)+c(5,4)+0.1+0.2+0.4+0.3= 1.1 + 0 + 0.1+ 0.2 + 0.4 + 0.3 =


2.1

Main Table Root Table

0 1 2 3 4 0 1 2 3 4

1 0 0.1 0.4 1.1 1.7 1 1 2 3 3

2 0 0.2 0.8 1.4 2 2 3 3

3 0 0.4 1.0 3 3 3

4 0 0.3 4 4

5 0 5

Thus, the average number of key comparisons in the optimal tree is equal to

1.7.
key D.
Since R(1, 4) = 3, the root of the optimal tree contains the third key, i.e., C. Its
left subtree is made up of keys A and B, and its right subtree contains just
Since R(1, 2) = 2, the root of the optimal tree containing A and B is B, with A
being its left child (and the root of the one node tree: R(1, 1) = 1).
(OR)

6B) STABLE MARRIAGE PROBLEM


Consider set Y = {m1,…,mn} of n men and a set X = {w1,…,wn} of n women.
Each man has a ranking list of the women, and each woman has a ranking list
of the men (with no ties in these lists) as given below. The same information
can also be presented by an n-by-n ranking matrix 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 m's preference list; the second is the position (ranking)
of m in the w's preference list.

Marriage 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.
Blocking Pair:
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)} because they are
not matched in M while Bob prefers Lea to Ann and Lea prefers Bob to Jim.
Stable and unstable marriage
matching:
A marriage matching M is called stable if there is no blocking pair for it;
otherwise, M is called unstable.
The stable marriage problem is to find a stable marriage matching for men's
and women's given preferences.
Stable Marriage Algorithm:
Step 0 Start with all the men and women being free
Step 1 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 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 mens proposal, making her former
mate free; otherwise, she simply rejects mens proposal, leaving m free Step 2
Return the set of n matched pairs
7a) TRAVELING SALESMAN PROBLEM
Given n cities with known distances between each pair, find the shortest tour that passes
through all the cities exactly once before returning to the starting city
The branch-and-bound technique can be applied to instances of the traveling salesman
problem if the result comes up with a reasonable lower bound on tour lengths.
A lower bound on the length l of any tour can be computed as follows.
Calculation of lower bound:
For each city i, 1 ≤ i ≤ n,
∙ find the sum si; of the distances from city i to the two nearest cities;
∙ compute the sum s of these n numbers;
∙ divide the result by 2; and,
∙ if all the distances are integers, round up the result to the nearest
integer:
lb= s/2
Moreover, for any subset of tours that must include particular edges of a given graph,
the lower bound can be modified accordingly.
For example, for the instance above the lower bound is:
lb = [(1+ 3) + (3 + 6) + (1 + 2) + (3 + 4) + (2 + 3)]/2 = 14.
The bounding function can be used, to find the shortest Hamiltonian circuit for the
given
Construction of state space tree:
Root:
First, without loss of generality, consider only tours that start at a.
First level
Second because the graph is undirected, tours can be generated in which b is visited
before c. In addition, after visiting n - 1 = 4 cities, a tour has no choice but to visit the
remaining unvisited city and return to the starting one.
Lower bound if edge (a,b) is chosen: lb = ceil([(3+1)+(3+6)+(1+2)+(4+3)+(2+3)]/2)=14
Edge (a,c) is not include since b is visited before c. Lower bound if edge (a,d) is chosen: lb
= ceil([5+1)+(3+6)+(1+2)+(5+3)+(2+3)]/2)=16. Lower bound if edge (a,e) is chosen: lb =
ceil([(8+1)+(3+6)+(1+2)+(4+3)+(2+8)]/2)=19 Since the lower bound of edge (a,b) is
smaller among all the edges, it is included in the solution. The state space tree is
expanded from this node.
Second level
The choice of edges should be made between three vertices: c, d and e.
Lower bound if edge (b,c) is chosen. The path taken will be (a ->b->c):
lb = ceil([(3+1)+(3+6)+(1+6)+(4+3)+(2+3)]/2)=16. Lower bound if edge (b,d) is chosen.
The path taken will be (a ->b->d):
lb = ceil([(3+1)+(3+7)+(7+3)+(1+2)+(2+3)]/2)=16

Lower bound if edge (b,e) is chosen. The path taken will be (a ->b->e):
lb = ceil([(3+1)+(3+9)+(2+9)+(1+2)+(4+3)]/2)=19. (Since this lb is larger than other values,
so further expansion is stopped)
The path a->b->c and a->b->d are more promising. Hence the state space tree is
expanded from those nodes.

Next level
There are four possible routes:
a ->b->c->d->e->a a
->b->c->e->d->a a-
>b->d->c->e->a a-
>b->d->e->c->a
Lower bound for the route a ->b->c->d->e->a: (a,b,c,d)(e,a) lb =
ceil([(3+8)+(3+6)+(6+4)+(4+3)+(3+8)])
Lower bound for the route a ->b->c->e->d->a: (a,b,c,e)(d,a) lb =
ceil([(3+5)+(3+6)+(6+2)+(2+3)+(3+5)]/2)=19
Lower bound for the route a->b->d->c->e->a: (a,b,d,c)(e,a)
lb =
ceil([(3+8)+(3+7)+(7+4)+(4+2)+(2+8)]/2)
=24
Lower bound for the route a->b->d->e->c->a: (a,b,d,e)(c,a)
lb =
ceil([(3+1)+(3+7)+(7+3)+(3+2)+(2+1)]/2)
=16
Therefore from the above lower bound the solution is The
optimal tour is a ->b->c->e->d->a

The better tour is a ->b->c->e->d->a


The inferior tour is a->b->d->c->e->a
The first tour is a ->b->c->d->e->a

(OR)

7b) JOB ASSIGNMENT PROBLEM:


The job assignment problem is the problem of assigning n people to n jobs so that the
total cost of the assignment is as small as possible. An instance of the assignment
problem is specified by an n-by-n cost matrix C so that state the problem can be stated as
follows:
Select one element in each row of the matrix so that no two selected elements are in the
same column and their sum is the smallest possible. This is done by considering the same
small instance of the problem:

To find a lower bound on the cost of an optimal selection without actually solving the
problem, several methods can be used. For example, it is clear that the cost of any
solution, including an optimal one, cannot be smaller than the sum of the smallest
elements in each of the matrix‗s rows. For the instance given, the lower bound is lb= 2
+3 + 1 + 4 = 10.
It is important to stress that this is not the cost of any legitimate selection (3 and 1
came from the same column of the matrix); it is just a lower bound on the cost of any
legitimate selection.
Apply the same thinking to partially constructed solutions. For example, for any
legitimate selection that selects 9 from the first row, the lower bound will be lb = 9 + 3 +
1 + 4 = 17.

This problem deals with the order in which the tree‗s nodes will he generated. Rather
than generating a single child of the last promising node as in backtracking, all the
children of the most promising node among non- terminated leaves in the current tree
are generated.
To find which of the nodes is most promising, compare the lower bounds of the live
node. It is sensible to consider a node with the best bound as most promising, although
this does not, of course, preclude the possibility that an optimal solution will ultimately
belong to a different branch of the state-space tree.
This variation of the strategy is called the best-first branch-and-bound.
Returning to the instance of the assignment problem given earlier, start with the root
that corresponds to no elements selected from the cost matrix. As the lower bound
value for the root, denoted lb is 10.
The nodes on the first level of the free correspond to four elements (jobs) in the first
row of the matrix since they are each a potential selection for the first component of the
solution. So there are four live leaves (nodes 1 through 4) that may contain an optimal
solution. The most promising of them is node 2 because it has the smallest lower bound
value.
By following the best-first search strategy, branch out from that node first by
considering the three different ways of selecting an element from the second row and
not in the second column—the three different jobs that can be assigned to person b.
Of the six live leaves (nodes 1, 3, 4, 5, 6, and 7) that may contain an optimal solution, we
again choose the one with the smallest lower bound, node 5.
First, consider selecting the third column’s element from c‗s row (i.e., assigning person c
to job 3); this leaves with no choice but to select the element from the fourth column of
d‗s row (assigning person d to job 4). This yield leafs that corresponds to the feasible
solution (a →2, b→1, c→3, d →4) with (The total cost of 13. Its sibling, node 9,
corresponds to the feasible
Solution:
solution {a → 2, b →1, c → 4, d → 3) with the total cost of 25, Since its cost is larger than
the cost of the solution represented by leafs, node 9 is simply terminated. o Note that if
its cost were smaller than 13 then it would have to be replaced with the information
about the best solution seen so far with the data provided by this node. o Now, as
inspect each of the live leaves of the last state-space tree (nodes 1, 3, 4, 6, and 7 in the
following figure), it is discovered that their lower bound values are not smaller than 13
the value of the best selection seen so far (leaf 8).
o Hence all of them are terminated and the solution represented by leaf 8 is recognized
as the optimal solution to the problem.
PART-C
8a)

(OR)
8b)

You might also like