DAA - Questions With Answer

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

DAA

1. Discuss the approximation algorithm for NP-hard problems.

Solution: i) Approximation Algorithm for the Travelling salesman problem

ii) Approximation Algorithm for Knapsack Problem

Refer Page NO: 7-85

2. Solve the following using Brute-Force algorithm.


a. Find whether the given string follows the specified pattern and return 0 or 1
accordingly. Examples.
i. Pattern : “abba”, input: “redblueredblue” should return 1
ii. Pattern : “aaaa”, input: “asdasdasdasd” should return 1.
iii. Pattern: “aabb”, Input : “xyzabcxzyabc” should return 0.

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.

i) Consider pattern: abba text : redblueredblue


In given text/pattern pair
1) If we map ‘r’ of string “red” with ‘a’ of pattern and
2) If we map ‘b’ of string “blue” with ‘b’ of pattern then the algorithm will return 1.
Such an algorithm is as given below

int BruteForceAlgo(char t[14], char p[4],int n)


{// array t[] contains “redblueredblue”
// array p[] contains “abba”
// n represent length of text t[]

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

ii) Consider Pattern : “aaaa”, input: “asdasdasdasd” should return 1.

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 BruteForceAlgo(char t[14], char p[4],int n)


{
// array t[] contains “asdasdasdasd”
// array p[] contains “aaaa”
// n represent length of text t[]
int i,j,flag=1;
i=0;
j=0;
While(j<n)
{
if((t[j]==’a’)&&(p[i]==’a’))
{
i=i+1;
j=j+3;
}
else
{flag=0;
j++;
}
}
return flag;
}
iii) Similarly for Pattern: “aabb”, Input : “xyzabcxzyabc” should return 0.
We will map “x” of “xyz” string with ‘a’ of “abc” string with ‘b’. The algorithm will be-

int BruteForceAlgo(char t[20], char p[10],int n)


{// array t[] contains “xyzabcxzyabc”
// array p[] contains “aabb”
// n represent length of text t[]

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.

 For instance in graph G the Hamiltonian cycle is A-B-D-E-C-F-A and G”


contains the Hamiltonian Path V-A-B-D-E-C-F-A-V..
 Conversely if G” Contains Hamiltonian path then we can convert it to the
Hamiltonian cycle present in G by removing the end points V and V’ and
mapping A’ to A.
 Thus we can show that G contains Hamiltonian cycle if and only if G” contains
Hamiltonian path which concludes the proof that Hamiltonian path is NP
complete.
4. Write the insertion sort algorithm and estimate its running time.
Algorithm Insert_sort(A[0…n-1]
/* Function to sort an array using insertion sort*/
//Input: An array of n elements
for i 1 to n-1 do
{
temp A[i]
j i-1
while (j >= 0 && A[j] > temp) do
A[j+1] = A[j]
j j-1;
}
A[j+1] key;
}

Analysis: Best case: O(n)

AVERAGE Case: O(n2)

Worst case : O(n2)

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/2)2 + (n/2)2 … + (n/2)2 ---------


=8n2/4=2n2

(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

For above equation


∑log
𝑘=0
𝑛−1
2𝑘 is a geometric sum.Hence
∑log
𝑘=0
𝑛−1
2𝑘 = Θ(2logn-1)= Θ(n)
(23) log n = (2logn)3= n3
T(n) = n2 Θ(n)+n3
= Θ(n3)
Tme complexity = Θ(n3)

We solve recurrence relation using a formula denoted by Master Method


T(n)=aT(n/b) +F(n) where n≥d and d is some constant.
Then the Master theorem can be stated for efficiency analysis as-

If F(n) is Θ(nd) where d≥0 in the recurrence relation then,

1. T(n) =Θ(nd) if a,bd


2. T(n)= Θ(nd log n) if a=b
3. T(n)= Θ(n logba) if a>bd

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

Left sublist Mid Right sublist

Step 2:
.… 40 50 60
ii)List represented as linked lists.

For example –

10 20 30 40 50 60 NULL

Assuming again key =50

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 tempdata=?Key no.

Tempdata < key

Traverse right sublist

Step 2: temp

If tempdata =?key 40 50 60 NULL

Yes.

Thus element is found mid

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).

Sorted singly Linked List:

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.

7. Derive a loose bound on the following equation.


F(x) = 35x8 - 22x7 + 14x5 – 2x4 – 4x2 + x – 15.

Solution :

Let f(n) and g(n) are two negative functions

Let c be some constant.

The equation f(n)≤c*g(n)

Then f(n)€O(g(n)) with tight bound

But if f(n) < c*g(n)

Then f(n) € O(g(n)) with loose bound

Consider the function

f(x)= 35x8 - 22x7 + 14x5 – 2x4 – 4x2 + x – 15.

And g(x)= x8

If x=1,then

f(x)= 35(1)8 – 22(1)7 + 14(1)5 – 2(1)4 – 4(1)2 +(1) – 15


f(x)= 7
g(x)= x8 =(1)8

if we assume c=35 then

we will always get f(x),g(x) for x≥1

8. Explain the convex hull problem and the solution involved behind it.
Ref page:2.3

You might also like