algo pyq
algo pyq
except that
7. (a) A d-ary heap is like a binary heap, children.
two
nodes may have d children instead of
Consider a 3-ary heap represented using an array,
three children of a
how would the indices of the
(3)
node be computed?
all the gold dust from
(b) A thief wants to steal
kg of it. The thief has n
a store having W
capacities. Give an
sacks having different sacks
thief to fill his
efficient algorithm for the
that the number of sacks used is
with dust so
(3)
minimized.
function:
time of the following
(c) Discuss the running
func(A)
for i = 0to A.length -2
A.length 1
for j = itl to
if (A[[-1] > Aj]) (4)
exchange (A[j-1] and Aj] )
(1400)
This question paper contains 8+2 printed pagcs];
Rol|No.}
3 s S.No, ofQuestion Paper 6512
p Unique Paper Code%323414011 HC
PTO.
( 2 ) 6512
(3 ) 6512
sacks usedis minimized. Consider an algorithm A with run time O(n). What is
Interval number. Start tím (s) Finish time (f) Weight (v) (e) Let G = (V,E) be an-undifccted pathgraph with n
nodes. We call :a graph a 'path if its nÍdes, can be
PT.O.
6512
6512
(4 )
(). Would you use DPS.to find, the, shoetes-path
Pick a node v, of maximm1 weight
di_tance: between two nodes in 'a graph ? Justify your
Add v, lo S
answer:
Delete v; and its neighbors fromG
(9) Consider an adjacency list representation of a directed
Endwhile anhwhereia an array of the outgoing edges (and not
graph
Return S the incoming edges) for each vertex is: maintained.
Give an example to show that the above algorithm does Give an algorithm to compute the indegree. for each
vertex and discuss the timecomplexíty of the.
not `always tind an' optimal 'solution.
complevibai: algorithm.
(ü) Discuss the run time of the narve string
exists at least
()Insertion
can be used to determine if there
the iy Preorder traversal. 5
one s-t path in G. What is the running time of
algorithm ?
3 (a Consider.the following. álgorithm. that takesias input an
oRarray,:of n: integersand anineger S. It finds: whether
element
Give an eficient algorithm to.find the maximåm
of the there exi_t, two elements in the, array tlhat: suin up to.
in a min-heup. Giye the exact running, time
S) and returFs l on: suçcess and0 on failure.
al:orithm.
PT.O.
6512
(. ) 6512
4.
FindPair (Arr, n, S) (a) Consider,astackS that,supports the following
operations
Quicksort (AT, 1, n) Push(S,x)pushelement x onto stack S
for, i = | to n
Pop(S): pop the top element from stack S and. returnit
tlag = BinarySearch(Art, i+l;.n, |S-ArT[ill
/f ilag) Multipon(S K) cemové. top, k;eleménts: of stack' S
return 1
Using the aggregateamethod of;analysis, determine the
endif
amortised cost per operatjon when: ia sequence! of n,
endFor
operations is performed onan empty: stack.
return 0
(6) Foreach of the following sorting .algoithms gve: an
FindPair uses the,following algorithms
input of sizefive
Quicksort (Ariy, First, Last): forwhichitshowsworstcase
behaviour
Binary Search (Array, First, last, element)
Analyze the 'worst case running time -of EindPair )Mergesort
(b) Çan a graph G in which edge weights CDQuick sort
arennot
necessarily distinct. have more. than one mihimum For thevariant of nterval scheduling problem that
spanning trees-(MST). 1fyes, give an èxample; if no, minjmizeslateness, give an instancer with two different
justify.
optimal solutions, neither of which,has any
inver_ions.
() Is Merge sort () in place (ii)
stable:? Explain. Or idletime
PTO,
6512. (9 ) 6512
( 8 )
npdes that has two we replace each edge cost c by its square, c,", thereby
(a) Give an example graph having five
show -the creating a new-instanceof.the problem with the same
different topological orderlngs. Also,
topological orderings. graph:but different costs.Is -it necessary for. Pto still
and place
amongst the elements yet to be procëssed based sorting algorithm-(where no- comparison of keys
-
running
appropriaiely. However, they have different is -performed for sorting the list) despite,. the fact that
complexity of both
times. Give the asymptotic time insertion sort, which is used-to sort individual buckets,
theitrunning times
and comment on what makes is comparison -based ?
different. (c). What kind. of inputs(will lead to (i) best ça[e (i)
worst
successive insertions. for
Create a Red-black tree with case performance for insertion'sort algorithm ? Give the
numbers: 10;8,14,12,13.2
the following sequence of rünning time for both,thecases. 4
of the Shortest
6. Suppose we are given an instançe 7. (a) ,Show that at most 3*floor(n/2) comparisons are
sufficient
graph, G. We assume
S-t path problem on a. directed to:find-both the minimum and maximum in a given array
and distinct. Let.Pbe
all the edge costs are positive .of, size-n.
instance. Now suppose
a minimum çost $-t path for this
P.TO.
(10 ). 6512
Compute_opt(i,w)
If i = 0 or w =0 then
Retutn- 0.
Else
lf _w<w, then:.
Compute op(i-lw)
take exponential!
) Explain why does thisalgorithm
theWorstcase.
time to rün in
the above
(i) What changes should be made to
algorithm to make it run in polynomial time.
above
i)Consider an optimal solution to the
contain
problem. Is it poSsible for this solution to
subproblem
a- sub-optimal solution to, a.
2+2+3
Explain.
10 1,400
6512
[This qucstion papcr contains 8printed pages.]
Semester
Duration: 3. Hours Maximsm Marks : 75
P.T.O.
2796 2/796 3
right mid; if it is smaller than the left mid, perform (c) A'shopkeeper has W marbles and n empty bottles.
recursive scarch in the lcftmost partition; if it is grcater Let c, Cgi... c, respectively denote the number of
than thc right mid, perform a recur_ive search in the marbles the bottles can contain. The shopkeeper wants
rightmost partition; clse porform n recursive search in to store the marbles in the bottles.
the middle partition. Thus, thc algdrithm performs 4
comparisons in cach iteration and reourses on one-third (i) Describe a greedy algorithm which minimizes the
number of bottles used.
of the array. \Write thc rccurrcnce rolation for thc running
time of the algorithm and isolve it. .!(4): (ü) How would you modify your algorithm if bottle i
also has an associated cost' price p, and the
(c) Consider an instance of the subsct sum problem gÍalis.tÍ minimize. the total cost of the bottles
where bound W = 6, Items with weights w, =2, w, = 2, used.
1i (4) (3+4)
W 2.
or
3."(h) Compare the space requirements of adjacency list and
adjacency matrix representations of a graph having m
edges and n vertices. (3)
(i) Give an efficient algorithm to find the minimåm element
Arguc that such a chain cannot form a red-black trec.
in a max-heap. Give the exact running time of the
For each possible coloring of the three nodes, mention
(4) algorithm. (3)
thc property that is violatcd.
P.T.0.
2796 5
2796 4
() Would you use BS to fnd the shortest path betwe (b) Suppose agraph G has two edge-disjoint spanning trees
(two spanning trees that have no edges in common).
two nodes in a weighted graph with arbitrary cdge
(3) Argue that in this graph G, every pair of nodes forms
weights ? Justify vour answer. (4)
part of a cyclc.
(b) For each of the following operations does a Red Black Compute_optG)
Trec work faster than a Binary Scarch Trce ? Elaborate
If j = 0 then
your answver
Return 0
(i) Search
Else
(ii) Postorder traversal
(5)
Retun max(v, + Compute opt(p), Compute_ opt(j-1))
different ways () Explain why does this algorithm take exponential
3 (a) A prioTity qucuc is implemented in two
max heap and an array sorted in decreasing
time to run in the worst case.
USing
order of key values (higher key value indicates higher (ü) What changes should be made to the above
following
priornty). Compare the time complexity of the
algorithm to make it run in polynomial time.
operations when performed on the two different
(6)
implementations of the priority qucuc.
(b) Suppose that an n x n array A consists of 1?s and 0°s
i) Finding the maximum element such that in any row of A all the 1's come before any
0's in that row. Give an O(nlgn) algorithm for counting
(ü) Deleting the maximum element the number of 1's in A. (4)
(6)
(iii) Increase the priority ofacertain element
P.T:0.
2796
2796 7
5.
(a) Give an cxample graph having four nodes that doos
have a dccidcd not to give fractional marks while grading the
topological ordering. (3) test. The instructor wishcs to sort thc n intcgcr scorcs
(b) Suppose a large array is in descending order. Design a linear time algorithm to
maintaincd with the
policy: the list is initially sorted. When new following perform this task. Also list all assumptions (if any) that
added, thcy are inserted at the end of elements are you, make to solvc the problcm. (4)
the array and
counted. Whenever the number of new
10, the array is resorted and the elements reaches
counter is 7. (a) Considet a k bit binary counter implemented using an
What strategy would be good tÍ use cleared. array Ansuch that AfO] stores the lowest order bit and
for the resorting of
the array? Why ? A[k-1] stores the highest order bit. The only operation
(4)
that can be performed on the counter is increment'.
(c) We use
Randomized-Select to select the minimum Using the aggregate method of analysis, determinc the
element of the array A =<3, 2,9, 0, 7>. amortised costper increment operation when a sequence
Describe a
sequence of partitions that would result in the worst of ninchements isis performed:' (3)
case performnanc- of the algorithm.
(3)
(b) A certain input to the merge sort algorithm is such that
6. (a) Which red-black tree properties may be violated when a the merging step always depicts the worst case behaviour.
node is deleted ? Determine the running time of the merge sort algorithm
(2)
for this instance. (3)
(b) Will
E Dijkstra's
.U msStill givc shÍrtest path between
two vertices if the edge weights are allowed to be (c).Consider the following algorithm that takes as input
an array of n integers and an integer T. It finds
negat+ve. If yes, justify yosr answer with an argument.
If no give an example. whether there exist two elements in the array that
(4)
Sum up to T and returns 1 on success and On
(c) An instructor has given a test to her class of n students. failure. (4)
",.
P.T.O.
2796
Targetsum (A, n, T)
Heapsort (A, I, n)
for i 1to n
(1200)