ch23problems

Embed Size (px)

Citation preview

  • 8/6/2019 ch23problems

    1/3

    CS330: PROBLEMS FOR MIDTERMAPRIL 22, 2011

    2.18 We look at a comparison-based algorithm for a search in a sorted array as a binary tree in which a

    path from the root to a leaf represents a run of the algorithm. At every node a comparison takes placeand, according to its result, a new comparison is performed. A leaf of the tree represents an output of thealgorithm: the index of the element x that we are searching or it says that the element x does not appearin the array. All indices 1, . . . , n must appear as leaves or the algorithm will fail when x is at one of themissing indices, implying that the tree must have at least n leaves, and its depth must be (log n), whichmeans that in the worst case it must perform at least (log n) comparisons.

    2.30 (a) In order to find for which , 2, . . . , 6 are distinct modulo 7, we start by checking these powersfor = 2, 3, . . . (Note that = 1 is trivial so we skip it)

    For = 2:

    = 2 2 (mod 7)2 = 22 = 4 4 (mod 7)3 = 23 = 8 1 (mod 7)4 = 24 = 16 2 (mod 7)

    Since 4 = 2 (mod 7), 2 does not satisfy our conditions.

    For = 3:

    = 3 3 (mod 7)2 = 32 = 9 2 (mod 7)3 = 33 = 27 6 (mod 7)4 = 34 = 81 4 (mod 7)5 = 35 = 243 5 (mod 7)6 = 36 = 729 1 (mod 7)

    It is easy to see that + 2 + 3 + 4 + 5 + 6 = 3 + 2 + 6 + 4 + 5 + 1 = 21 = 0 (mod 7)

    (b) The matrix M6() becomes

    M6(3) =

    1 1 1 1 1 11 3 2 6 4 51 2 4 1 2 41 6 1 6 1 61 4 2 1 4 21 5 4 6 2 3

    1

  • 8/6/2019 ch23problems

    2/3

    April 22, 2011

    and then

    FT

    011152

    = M6(3)

    011152

    =

    1 1 1 1 1 11 3 2 6 4 51 2 4 1 2 41 6 1 6 1 61 4 2 1 4 21 5 4 6 2 3

    011152

    =

    364233

    (c) The inverse ofM6() is

    M16

    (3) = 6

    1 1 1 1 1 11 5 4 6 2 31 4 2 1 4 21 6 1 6 1 61 2 4 1 2 41 3 2 6 4 5

    Note that the entries in M16

    (3) are obtained by taking the multiplicative inverse of the coresponding entryin M6(3) in modular arithmetic: e.g. 53 = 15 = 1 (mod 7), which means that 3 is at (2,6) and (6,2) positions

    in M16 (3) (5 is at these positions in M6(3)).

    6 in front of ( ) is1

    6(this is

    1

    nfrom our lectures)

    (d) 1 +x+x2 = (1, 1, 1, 0, 0, 0) and 1 + 2x+x3 = (1, 2, 0, 1, 0, 0) = (6, 2, 0, 1, 0, 0). Again all here is done(mod 7), so -1 becomes 6 since the cycles for (mod 7) are. . . , (7,6,5,4,3,2,1), (0, 1, 2, 3, 4, 5, 6), (7, 8, 9, 10, 11, 12, 13), . . .from where one can see that for example -1=6=13=-8.

    Now

    FT

    1

    11000

    = M6(3)

    1

    11000

    =

    1 1 1 1 1 1

    1 3 2 6 4 51 2 4 1 2 41 6 1 6 1 61 4 2 1 4 21 5 4 6 2 3

    1

    11000

    =

    3

    60103

    and

    FT

    62010

    0

    = M6(3)

    62010

    0

    =

    1 1 1 1 1 11 3 2 6 4 51 2 4 1 2 41 6 1 6 1 61 4 2 1 4 2

    1 5 4 6 2 3

    62010

    0

    =

    24431

    1

    Multiply the obtained FTs componentwise as follows:

    FT

    111000

    FT

    620100

    =

    360103

    244311

    =

    630303

    and take the FT inverse of the last vector:2

  • 8/6/2019 ch23problems

    3/3

    April 22, 2011

    M16

    (3)

    630303

    = 6

    1 1 1 1 1 11 5 4 6 2 31 4 2 1 4 21 6 1 6 1 61 2 4 1 2 41 3 2 6 4 5

    630303

    = 6

    166466

    =

    611311

    which implies that(1 + x + x2)(1 + 2x + x3) = 6 + x + x2 + 3x3 + x4 + x5 = 1 + x + x2 + 3x3 + x4 + x5

    (use 1 = 6 (mod 7))

    3.8. (a) Let S0 = 10, S1 = 7, S2 = 4 be the sizes of the corresponding containers, and let ai be the actualcontents of the ith container. Let G = (V,E) be a directed graph, in which the nodes are triples of numbers(a0, a1, a2). It must hold 0 ai Si fori = 0, 1, 2 and at any given node a0 + a1 + a2 = 11

    An edge between two nodes (a0, a1, a2) and (b0, b1, b2) exists if the following 2 conditions are satisfied:

    the two nodes differ in exactly two coordinates, and the third one is the same in both. if i, j are the coordinates they differ in, then either ai = 0 or aj = 0 or ai = Si or aj = Sj .

    The question is whether there exists a path between the nodes (0, 7, 4) and (#, 2,#) or (#,#, 2) where #stands for any allowed value of the corresponding coordinate.

    (b) Use DFS on the graph described in (a), starting from node (0, 7, 4) with an additional line of code thathalts and answers yes if one of the desired nodes is reached and no if the connected component of thestarting node is exhausted an no desired vertex is reached.

    (c) (0, 7, 4) (4, 7, 0) (10, 1, 0) (6, 1, 4) (6, 5, 0) (2, 5, 4) (2, 7, 2)

    3.24 First linearize the DAG, and note that since in a linearized DAG edges can go only in increasing di-rection we can label its nodes by their position in the linearized order. To check if there is a directed paththat touches every vertex exactly once it is enough to check if the DAG has an edge (i,i+1) for every pair ofconsecutive nodes i and i+1 (i and i+1 being their label (position) in the linearized order.).

    Linerization of DAGs takes linear time (see page 101 in DPV), and checking the edges for the pairs of con-secutive nodes also takes linear time, which implies that the total running time is also linear.

    3