theorotical assignment 2.pdf

Embed Size (px)

Citation preview

  • 8/11/2019 theorotical assignment 2.pdf

    1/2

    Data Structure & Algorithms

    CS210A,ESO207A,ESO211Semester I, 2012-13, CSE, IIT Kanpur

    Theoretical Assignment II

    Deadline : 1st November

    Note: Your solution for each exercise must be formal and complete. The design of each algorithm ordata structure must be provided in full details. You must provide analysis of time and space complexityof each algorithm and data structure. You should give suitable and precise arguments to justify yourapproach and/or prove the correctness of the algorithm (Do not write stories).

    1 Splitting a red-black tree in O(logn) time (marks=30)

    In the class we discussed an algorithm for Special merge(T1, T2) on a red-black tree. Design an algorithmfor Split(T, x) which splits Tin to two red black trees T1 andT2 such that T1 < x < T2. Your algorithmmust be based on Special merge(T1, T2) such that it invokes Special merge as a black box during itsexecution. Your algorithm must run in O(log n) time only where n is the number of nodes in T. (Youwill lose at most 5 marks if your algorithm takesO(log2 n) time.)

    2 Low Point in DFS traversal (marks=35)

    Part 1 (the solution of this part is not to be submitted)We discussed the problem of finding articulation points in a graph. For this we introduced a terminol-ogy ofHigh ptas follows. For a vertexv V, High pt(v) is the smallestDFNnumber of any ancestor ofvto which there is a back edge fromsubtree(v). We initializedHigh pt[v] of any vertex as . However, wealso realized that we could initialize High pt[v] to be DFN[v]. With this initialization, convince yourselfthat the following becomes an alternative description of the definition ofHigh ptof each vertex.High pt[u]=min{DFN[v]|a path from u to v consisting of zero or more tree edges followed by at most one back edge}.

    Note that in this alternative description ofHigh pt, we are considering paths in the following directedgraph obtained through DFS: Each tree edge directed away from the root (start vertex), and each non-tree edge directed from descendant to ancestor.

    Part 2Let G= (V, E) be an undirected connected graph on n vertices and m edges given in the adjacency

    lists representation. A DFS traversal has been performed on the graph. Observe that there will be ndistinct DFS calls during this traversal. Furthermore, there will be distinct times at which each of theseDFS calls start and finish for any vertex. So each vertex v V can be assigned two unique numberss(v) and f(v) from the range [0, 2n 1]. The number s(v) is the time at which DFS call for v wasinvoked and the number f(v) is the time at which DFS call for v finishes. You are given two arraysS and F of size n storing s(v) and f(v) for each vertex v. For example, if r is the vertex from wherewe start the DFS traversal, then S[r] = 0 and F[r] = 2n1. Given the graph G, and arrays S andF, design anO(m+n) time algorithm that computes a function Low ptfor every vertex defined as follows.

    1

  • 8/11/2019 theorotical assignment 2.pdf

    2/2

    Low pt[u]=max{DFN[v]|a path from u to v consisting of one or more tree edges and at most one back edge}.

    Note that in the definition ofLow pt(u) given above, the back edge need not be the last edge on thepath from u to v. To get a better understanding of the definition of Low pt, see Figure 1 where thenumber on each vertex represents its DFN. You can observe that

    Low pt(a)=10,Low pt(b)=10,Low pt(c)=10.Low pt(d)=10,Low pt(e)=9,Low pt(g)=10.Low pt(k)=10,Low pt(h)=7,Low pt(i)=10.Low pt(j)=7.

    a

    b

    c

    d

    g

    hi

    j

    1

    3

    2

    6

    79

    4

    5 e k

    8

    10

    Figure 1: A DFS tree along with the DFN numbering; the back edges are shown dotted.

    3 A Gem from the world of Data structure (marks = 35)

    Solve Exactly oneof the following problems. You should choose that problem which you can solve fully

    on your own.

    1. You are given a sequencea0, . . . , an1ofn numbers. Design a suitable data structure which canperform each of the following operations in O(log n) time for any 0 i j < n.

    Report min(i, j): Report the smallest element from ai to aj .

    Increment(i,j ,x): Add x to each element from ai to aj .

    2. You are given a sequence b0, . . . , bn1 of n bits. Design a suitable data structure which canperform each of the following operations in O(log n) time for any 0 i j < n.

    Report bit(i): Report the value of bit bi.

    Flip bits(i, j): Flip all bits from bi to bj. Here flipping a bit means changing it from 0 to 1 or

    vice versa.

    2