DAA - Questions With Answer
DAA - Questions With Answer
DAA - Questions With Answer
Solution:
The Brute- Force approach of string matching algorithm is very simple and straightforward.
According to this approach, each character of pattern is compared with each corresponding
character of text.
int i,j,flag=1;
i=0;
j=0;
While(j<n)
{
if((t[j]==’r’)&&(p[i]==’a’))
{
i=i+1;
j=j+3;
}
else if((t[j]==’b’)&&(p[i]==’b’))
{
i=i+1;
j=j+4;
}
else
{flag=0;
j++;
}
}
return flag;
}
Step 1:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e D b l u e b l u e r e d
a b B a
P[]
Step 2:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e D b l u e b l u e r e d
A b b a
Step 3:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e D b l u e b l u e r e d
a b b a
Step 4:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e D b l u e b l u e r e d
return 1
a b b a
The simple logic to match pattern against text is that match first letter ‘a’ of string “asd” with
letter ‘a’ of pattern. The algorithm will be
int i,j,flag=1;
i=0;
j=0;
While(j<n)
{
if((t[j]==’x’)&&(p[i]==’a’))
{
i=i+1;
j=j+3;
}
else if((t[j]==’a’)&&(p[i]==’b’))
{
i=i+1;
j=j+3;
}
else
{flag=0;
j++;
}
}
return flag;
}
3. Show that the Hamiltonian path problem reduces to the Hamiltonian circuit problem and
vice versa.
Solution:
Hamiltonian path is a simple open path that contains each vertex in a graph
exactly once. And if the source vertex and the destination vertex of this
Hamiltonian path is the same then it is called Hamiltonian cycle.
The Hamiltonian path problem is the problem to determine whether a given graph
contains a Hamiltonian path.
To show that this problem is NP-Complete we first need to show that it actually
belongs to the class NP and then find a known NP-complete problem that can be
reduced to Hamiltonian path.
For a given graph G we can solve Hamiltonian path by deterministically choosing
edges from G that are to be included in the path. Then we traverse the path and
make sure that we visit each vertex exactly once. This obviously can be done in
polynomial time and hence the problem belongs to NP.
Now we have to find out an NP complete problem that can be reduce to
Hamiltonian path. One such closely related problem is the problem to determine
whether the graph contains Hamiltonian cycle. Hamiltonian cycle is NP complete
so we try to reduce this problem to Hamiltonian path.
For example: Consider a graph G, from which we can construct a graph G” such
that G contains a Hamiltonian cycle if and only if G” contains Hamiltonian path.
5. Find the closest asymptotic tight bound by solving the recurrence equation, T(n) =
8T(n/2)+n2 with (T(1)=1) using Recursion tree method. Assume that T(1) belongs to
Zi(1).
Solution
The recurrence relation can be written as 2kn2
T(n)=n2+2n2+22n2+23n2+24n2+…+2logn-1n2+8logn
=
∑log
𝑘=0
𝑛−1
2kn2 +8logn
log 𝑛−1
T(n)= n2 ∑𝑘=0 2k +(23)log n
n2- - - - - - - - - - - - - - - n2
(n/4)2 (n/4)2…… (n/4)2 (n/4)2 (n/4)2…… (n/4)2 (n/4)2 (n/4)2…… (n/4)2- -=(8/4)2 n2=22n2
. . .
. . .- - - - - =2logn-1 n2
. . .
T(1) T(1) T(1)….. ……..T(1) T(1) T(1)……… T(1) T(1) T(1)-------=8logn
6. If you have to solve the searching problem for a list of n number, how can you take
advantage of the fact that the list is known to be sorted? Give separate answers for,
a. Lists represented as arrays.
b. List represented as linked lists.
Compare the time complexities involved in the analysis of both the algorithms.
Solutions:
If the list is already sorted and if we have to solve searching problems using such
a list then binary search method can be applied. There are two ways to perform
this-
i) If Lists represented as arrays.
For example:
0 1 2 3 4 5
10 20 30 40 50 60
If key element i.e the element to be searched from the list is 50,then
according to binary search method we will perform following steps
Step 1: if A[mid]?=Key no
A[mid]<key
Search right sublist
0 1 2 3 4 5
10 20 30 40 50 60
Step 2:
.… 40 50 60
ii)List represented as linked lists.
For example –
10 20 30 40 50 60 NULL
If binary search approach is used for searching the key element then, we have to
obtain total number of elements in the list. For that purpose we have to traverse all the
n nodes. Then find middle element. Again we cannot access middle element directly,
we need to reverse the list from beginning to reach to the mid element and then the
comparison of mid element with key is made.
Step1: temp
10 20 30 40 50 60 NULL
Mid
If tempdata=?Key no.
Step 2: temp
Yes.
But performing binary search when list is represented as linked list – is really complex.
Analysis:
Sorted Array
If list is represented as assorted array then O(log n) comparison are required. The number of
travel steps required is 0 as we can directly access the mid element using array subscript. We
can say time complexity for traversal steps is O(log N).
In case of linked list O(log n) comparisons are required. The time complexity for traversal
steps is O(n), as we have to know the total number of nodes in a linked list for obtaining the
mid element and for that purpose it is necessary to traverse the entire list.
Solution :
And g(x)= x8
If x=1,then
8. Explain the convex hull problem and the solution involved behind it.
Ref page:2.3