Ada 2016
Ada 2016
May 2016
Paper Code: CSE-306-F
Average Case
=
O(n*log(n)
Best Case = O(n*log(n))
code is optimized
Quicksort Space Complexity: The space complexity O(n) ifthe
is but
be reduced to O{log(n))
ifcode uses iterative bottom up space complexity can
approach)
Q.1.d) What is the difference between greedy and dynamic approach ?
Ans. Differences are:
Greedy approaches Dynamic approaches
[Link] efficient 1. Less efticient
[Link] solution cannot be guaranteed [Link] solution can be guaranteed.
by a greedy algorithm.
3. No efficient solution. 3. DP provides efticient solutions for
some problems for which brute force
a
recurrence.
I. The first idea of B&B is to develop "a predictor" ofthe likelihood (in a loose sense)of
a node in the solution tree that it will lead to an optimal solution. This predictor is quantitative.
2. Which node to expand next: B&B chooses the live node with the best predictor value
3.B&B simply expands that node (i.e., generate all its children)
The node is computed, just expanded node is
4. predictor value of cach newly generated
now designated as a dead node, and the newly generated nodes are designated as live nodes
[Link] criterion : When the best node chosen for expansiom turn out to be a final
leaf(i.e, at level n), that when the algorithm terminates, and that node corresponds to the optimal
solution.
49
2016
BTech, 6 Semester Solved papers, Ma
with examnples.
Hamiltonian cycles or Hamilton
Q.1.(h) Discuss
Hamiltonian circuit, Hamilton cycle,
Hamiltonian cycle, also
called a
that visits each node exactly
once.
Ans. A
closed loop) through graph
a
circuit, is graph cycle (i.e.,
a
matrix multiplication
with example.
Q.2.(a) Explain strassen's Volker Strassen, is an algorithm
used for matrix
named after
Ans. The Strassen algorithm,
than the standard matrix multiplication
algorithm and is useful in practice
multiplication. It is faster for extremely lar
but would be slower than the fastest known algorithm
for large matrices,
matrices.
matrices A and B.
Given: Two N by N
Problem: Compute C =A x B
Brute Force
for i:-I to N do
forj:=l to N do
C[ij):=0;
fork=1 to N do
Cfij-C[ij] +A[ik]* B[kj]
ON) multiplications
Divide and Conquer
B
C2 B B2
PP. = (4,,
= t
A,,)XB,t B,)
(A., + A,,)XB,,)
P, = (4,,)B, - B,)
P, = (4,)XB,- B,)
P. = (4,, t AMB,.)
C(P, P,)
C(P,P )
C (P+P- P. + P)
50 1nalysis und
Design of Algoriu
From
7T(n/2)+ Cn ifn>1|
T(n) =
C ifn=1)
Tn)- Oury') = O(n").
Q.2.(b) Write algorithms for Union& Find operations for disjoint sets.
Ans. A union-find algorithm is an algorithm that performs two useful operations on suet
a data structure:
Find: Determine which subset aparticular element is in. This can be used for detemining
if two elements are in the same subset.
Union: Join two subsets into a single subset.
procedure Make-Set (x)
[Link][x] 1
2. parent[x X
end.
procedure UNION(a, b) {with weightbalancing
a and b are roots of two distinct trees in the forest.}
{Makes the root of the smaller tree a child of the root of the larger tree.}
1. if sizefa] < size[b] then a b
2. parent[b] a
3. sizefa sizela] +size[b]
end.
function FIND(x) {with path compression}
{Returns the root ofthe tree that contains node x.}
1. if parent[x] =* x then
2. parent[x] FIND(Parent[ )
3. return parent|x
end.
Q.3.(a) What is Merge Sort ? Write a recursive algorithm for same and show
that its running time is Ofn log n).
Ans. Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves.
calts itself for the two halves and then merges the two sorted halves. The merg() function is used
for merging two halves. The merge(arr, 1, m, r) is key process that assumes that arr[l.m] and
sorted and merges the two sorted sub-arrays into one. See following C
arr(m+1..r] are
implementation for details.
Algorithm: Merge sort (4, P, q, r)
()n4-ptl
(2) n r - 4
51
May 2016
[Link],
Semester Sohed pupers,
Array L{1,
2 .
1 . . .
n1 +
Create
(3) R{1,2.. 2 +1
(4) f o r i | to n.+
i-)
(5) do L[i]-A[P
t0 n2
(6) for+
(7) do
R[ A (q+)
(8) L[n+1]
(9) R[n2+1]o
(10) i
(1) - 1
(12) k - p to r
(13) do
ifLI> R[l
(14) then A[k] - L[J
(15) i - i + 1
(16) else
A [K]- R[Ll
(17)j-j+1 each divide step
that 'n' at a power of 2 i.e.
Analyzing Merge
sort : We assume
e(1) ifn=1
Toa0 ifn>l
We can write as
C ifn=1
+Cn ifn>l
Analysts and Design of Alg
ntm
52
Recursion Tree
Cn
sort algorithm
Q.3.(b) What is Divide and Conquer strategy? Explain Merge
with example. Also give its recurrence relation.
Ans. Divide and Conquer strategy : Divide and conquer is a technique for designing
of smaller
algorithms that consists of decomposing the instance to be solved into number
a
subinstance of the same problem, solving successively and independently each of these
subinstances, and then combining the subsolutions thus obtained to obtain the solution of the
original instance.
Most ofthe algorithms are recursive in natare. To solve a given problem they call them
selves recursively one or more times. All these algorithms follows "Divide and conquer" approach
The Divide and conquer paradigm involves three steps at each level ofrecursion
(1) Divide: In this step whole problem is divided into number of several subproblems.
(2) Conquer: The subproblems by solving them recursively. Ifthe subproblem size are.
small enough, however, just solv the subproblem in a straight forward manner.
(3) Combine: Finally, the solutions obtained by the subproblems are combined to create
solution to the original problem.
Algorithm:
Input: An array a and two indices p and q between which we want to do the sorting
Output: The array a will be sorted between p and q as a side effect
ifpq then
int m -pq/2
mergeSort(a.p.m)
mergeSort(a,m* 1.9)
merge(a.p.m.q)
The pseudo code of the merging routine is as follows:
Algorithm merge(a,p,m,q)
Input: An array a, in which we assume that the halves from p:::m and m-[Link]q are eacn
Sorted
2016
0* Semester Solvedpaers. Mar
B Tech. and q
sorted between p
The array should be result
Output: will hold the temporary
pt| // this array
Array tmp ofsize q
inti-p
int-j mt
int k -
do
while (i<= m orj<=q)
<=a.i) then
ifg q+l or alil
tmp[k]-a[i
ii+l
else if(i =
m+1 or afi]> a[j}) then
tmp[kl-a[)
J
k-k+1
fork=pto q do
a[k]-tmp[k]
the data is partitioned into two, one list of 6.
call of the function,
Example: For the first two smaller
7 and 2. The list (6 58 1) is then partitioned into
5,8 and 1, and a second list of 4, 3,
we can sort ( 6 5) into
and ( 8 1). The base case has now been reached, and
lists (6 )5
We compare
8 We can now merge (S 6 ) and (I 8).
(56) and we can sort ( 8 1) into (1 ). second
is 1. We next compare 5 and 8, so the
and 5, so the first element ofthe merged sequence
element will be 6, leaving 8 as the last element.
element is 5. Next we compare 6 and 8 the third
to the second halfofthe original data
The merged result is(1568). We now turn our attention
7 these we get ( 3 4) and
[Link] we partition ( 4 3 72 ).into ( 4 3) and ( 2).Sorting
two halves of the data sorted as
(2 7) Merging these we get ( 2347). We now have
(1568) and (2 3 4 7).All that remains to be done is to mergethe two halves together.
(12345 678)
Recurrence relation: T(n)=2T(n/2) + C,
Section B
Q.4.(a) Use Dijkstra algorithm to find single source shortest path for following
graph taking vertex *A' as the source.
10 B
D E
Analys1s and
Design of Al
Ans.
2
4
D
3
E
C D E
A B
0
2
Node A is selected
AB C D E
10 7
Now, select next node with min. value. i.e. next selected node is D
2
10
10
3
Relaxing edges from node D.
10
10
8Tevk, Semester. Sohed papers, May 2016 55
A B C D
10 12 10
10
2
10 12
O
10
A 10
C
12
D E
2
10
10 12
/3
(7 3
A B C D E
D 12
56 Analysis und Destgiu of Algurithms
2
12
10
(10
(o
7
MST-KRUSKAL(G w)
1. A?
2. for each vertex v V[G]
3.
3. do MAKE-SET(V)
4. sort the edges of E into nondecreasing order by weight w
5. for each edge (u, v) E, taken in nondecreasing order by weight
6. do if FIND-SET(u) ? FIND-SET(v)
7. then A?A {(u, v)}
8. UNION (u, v)
9. return A
every step requireda msec we would need about 770 centuries of [Link],the
exhausting examination of all possible solutions is out ofthe [Link] we are not aware o
any other quick algorithm that finds a best solution we will use a heuristic algorithm. According
BTech, Semester Solvedpapers, lay 2016 57
to this algorithm whenever the salesman is in town he chooses as his next city, the city j for
which the c(i.j) cost,is the minimum among all c(i.k) costs, where k are the
salesman has not visited [Link] is also a simple rule
pointers ofthe city the
in
just case more than one cities give the
minimum cost,for example in such a case the city with the smaller k will be
[Link] is a
greedy algorithm which selects in every step the cheapest visit and does not care whether this
will lead to a wrong result or not.
Input Number of cities n and array of costs c(ij) ij=1,..n (We begin from
number 1) city
Output:Vector of cities and total cost.
minimum=c(ej)Fmin{c(e,k)};visits(k)=0
cost cost+minimum
andk=I,..n}
ej
Cr)=j
end r-loopp
C(n)=1
cost=cost+c(e,1)
Q.5.(6) Set n =
7; (pl, p2 ..p7) (3,
5, 20, 18, 1, 6, 30) and
=
What is the
solution generated by job
Ans. n7 sequencing algorithm for given problem?
3
5
d
20
d-
18
da
d
6
P 30 d-1
Sort the task in
descending order of protit.
4 6
P 30 20
d 18 6 3
20 4 3 3 2
and
58 Analysis esign of lgorithms
Size of array is 7
initialized array with 0
3 4 56 7
PlaceQueen(oldboard,oldplace+1)
End
puzzle.
Initial and goal states.
-
Search the state space for the goal state and use the path from initial state to goal state
as the answer.
Number of possible arrangements for tiles: 16! H" 20.9x 1012.
-
Let x=1 ifin the initial state, the empty spot is in one of fhe following positions: 2, 4. 5.
7, 10, 12, 13, 15; otherwise x = 0.
do
break
i f (j = n+ 1)
return;
while (true)
Q.7.(b) What O/1 knapsack problem ? Slove this problem using Branch & Bound
method taking suitable example.
Ans. O-1 Knapsack problem: If a thief carries all or none of a good in the store
which cannot be parted or broken then it is called O-1 Knapsack problem.
Branch & Bound is very powerful tool to solve optimal problem when you haveto
maximise something when given some constraints.
Branch & Bound Problems solve 0/1 knapsack problem Statement:
Given 'n' items withbenefits b.b..b& weights ". W ", & max. capacity of
knapsack M. Find the items that should be chosen that maximize benefit.
0 not choose the items
choose theitems.
Process: Let us have X1, X2 . .
x, e {0, 1}
We need to maximize
Constraint 2 xism
i=l i=
(Maximize)
as we have to make "n' choices
2" possibilities are there
[Link]., 6h Semester: Solved papers. May 2016 61
ub v+ (w w) (V; +
1" + 1
= -
Arrange the table according to its descending values of per weight Ratio.
Now start with root node, th upper bound for the root can be computed as
ub = 0 + ( 1 2 - 0 ) * 10= 120
V = 0, 1r = 0, v,h = 10
w 0,v =0|
ub 12
Include I, Exclude I,
w = 5 , v = 50|
w=0,v=0
ub 92 ub 72
Include l. Exclude X
ww 13 w=5. v= 50|
2th 85
Not feasible l
Include 1 Exclude
X
W=11, v= 80 wW=5.v =50|
ub 84 ub= 78 X
Include I Exclude
w 15 w=11, v = 80|
Not feasible 1b-80
Optimal solution
X
Section D
Q.8.(a) Giving suitable example prove that traveling Salesperson Problem is
NP-complete.
Ans. TSP is NP-complete: Instance: Aúnite set of cities tc,: c.: c . ) , a positive
integer distance di. j) between each pair (e, c), and an integer B.
Aulysas Und 1estgn of Algorithme
62
ot h e CIles Is a certilicate th
Cng salesman is NP complete since a permutation a reduction
that traveling salesman is NP-complete
we descriC from
e [Link] prove
hamiltonian cycle.
OVen an instance of hamiltonian cycle g (with n vertices), create an instance of traveling
salesman.
-
For each vertex v, create a city cv:
-It there is an edge (7,v), then let the distance from cu to cv be l
-
(x, =
0, x =0, x, 0) (r, 0, x,
= =
0, x, = 1 ,x , = 0) (r, = 0, *, =
=
0) (r, =
0, x,
) ( r , = 0, x, = 1)
=
0)...
(a, = 0 , , = 1, x ) , 1 , x , = ) « , = 1 ,x , = 0)
( 1, , 0, x, 0)
= =
(r, 1, x, 0, ,= I)
=
(r, 1, 1,x, 0)
r 1, x, 1, x, 1)
= = =
We then put an edge between two nodes ifthe partial assignments are consistent. Notice
is between any two nodes
because there edges
that the maximum possible clique size m are no
NP
Np-Har