DAA Assignment1
DAA Assignment1
P.T.O.
right mid; if it is smaller than the left mid
id, perform a
recursive search in the leftmost partition; if itt is.
is greato
than the right mid, perform a recursive search in
the
rightmost partition; else pertorm a recursive search
in
the middle partition. Thus, the algorithm performs
comparisons in each iteration and recurses on one-third
of the array. Write the recurrence relation for the running
time of the algorithm and solve it.
(4)
(c) Consider an instance of the subset sum problem
where bound W =
6, Items with weights w,= 2, w, = 2,
W 2.
(4)
With the help of the above example argue that the
memorized recursive algorithm
solves lesser number of
subproblems than the corresponding iterative algorithm.
(d) Consider a linear chain of 3 nodes shown below :
O or
Argue that such a chain cannot form a red-black tree.
For each possible coloring of the three nodes, mention
the property that is violated.
(4)
2796 3
(e) A shopkeeper has W marbles and n empty bottles.
Let C C2 9 Crespectively denote the number of
marbles the botties can contain. The shopkeeper wants
to store the marbies in the bottles.
(i) Describe a greedy algorithm which minimizes the
number of bottles used.
(ii) How would you modify your algorithm if bottle i
also has an associated cost price p; and the
goal is to minimize the total cost of the bottles
used. (3+4)
(1) Suppose an input to the bucket sort algorithm is
not uniformly distributed. Then: (i) will the sort still
give output? (1i) what will be the impact of
correct
relaxing this condition on the running time? Justify.
(3)
(g) Discuss the run time complexity of the naive string
matching algorithm. (2)
(h) Compare the space requirements of adjacency list and
adjacency matrix representations of a graph having n
edges and n vertices. (3)
to find the minimum element
(1) Give an efficient algorithm
running time of the
inmax-heap. Give the
a
exact
algorithm.
(3)
P.T.O.
() Would you use BFS to find the shortest path .
algorithmn. (5)
(b) For each of the following operations does a Red Black
Tree work faster than a Binary Search Tree? Elaborate
your answer.
1) Search
a
(6)
111) Increase the priority of certain elemel
2796 5
Compute_opt0)
If j = 0 then
Return 0
Else
array A consists of
1's and 0's
(6) Suppose that an n x n
such that in any row of A all the 1's come before any
counting
O's in that row. Give an O(nlgn) algorithm for
(4)
the number of ' s in A.
5. (a) Give an example graph having four nodes that does not
have a topological ordering. (3)
(b) Suppose a large array is maintained with the following
policy: the list is initially sorted. When new elements are
added, they are inserted at the end of the array and
counted. Whenever the number of new elements reaches
10, the array is resorted and the counter is cleared.
What strategy would be good to use for the resorting of
the array? Why ? (4)
(c) We use Randomized-Select to select the minimum
element of the array A =
<3, 2, 9, 0, 7>. Describe a
sequence of partitions that would result in the worst
case performance of the algorithm. (3)
6. (a) Which red-black tree properties may be violated when a
node is deleted ?
(2)
(b) Will Dijkstra's algorithm still give shortest path between
two vertices if the edge
weights are allowed to be
negative. If yes,
justify your answer with an argument.
If no give an
example. (4)
(c) An instructor has
given a test to her class of n students
The maximum marks for the test is 100. The instructor
2796 7
t decided not to give fractional marks while grading the
test. The instructor wishes to sort the n
integer scores
in descending order. Design a linear time algorithm to
perform this task. Also list all assumptions (if any) that
you make to solve the problem. (4)
7. (a) Consider a k bit binary counter implemented using an
array A such that A[0] stores the lowest order bit and
A[k-1] stores the highest order bit. The only operation
that can be
performed on the counter is "increment'
Using the aggregate method of analysis, determine the
amortised cost per increment operation when a sequence
of n increments is performed. (3)
(b) A certain input to the merge sort algorithm is such that
the merging step always depicts the worst case behaviour.
Determine the running time of the merge sort algorithm
for this instance. 3)
(c) Consider the following algorithm that takes as input
an array of n integers and an integer T. It finds
whether there exist two elements in the array that
sum
sum up
up to T and returns 1 on success and 0 on
failure. (4)
2796 8
TargetSum (A, n, T)
Heapsort (A, 1, n)
for i = 1 to n
return 1
endif
endfor
return 0