Sei sulla pagina 1di 3

CS330: P ROBLEMS FOR M IDTERM A PRIL 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 place and, according to its result, a new comparison is performed. A leaf of the tree represents an output of the algorithm: the index of the element x that we are searching or it says that the element x does not appear in the array. All indices 1, . . . , n must appear as leaves or the algorithm will fail when x is at one of the missing indices, implying that the tree must have at least n leaves, and its depth must be (log n), which means that in the worst case it must perform at least (log n) comparisons.

2.30 (a) In order to nd for which , 2 , . . . , 6 are distinct modulo 7, we start by checking these powers for = 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

1 1 1 M6 (3) = 1 1 1

1 3 2 6 4 5

1 2 4 1 2 4

1 6 1 6 1 6

1 4 2 1 4 2

1 5 4 6 2 3

April 22, 2011 and then 0 0 1 1 1 1 1 1 1 F T = M6 (3) = 1 1 1 5 5 1 2 2 1 (c) The inverse of M6 () is 1 3 2 6 4 5 1 2 4 1 2 4 1 6 1 6 1 6 1 4 2 1 4 2 1 0 3 1 6 5 4 1 4 = 6 1 2 2 5 3 3 2 3

1 1 1 1 M6 (3) = 6 1 1 1

1 5 4 6 2 3

1 4 2 1 4 2

1 6 1 6 1 6

1 2 4 1 2 4

1 3 2 6 4 5

1 Note that the entries in M6 (3) are obtained by taking the multiplicative inverse of the coresponding entry in M6 (3) in modular arithmetic: e.g. 53 = 15 = 1 (mod 7), which means that 3 is at (2,6) and (6,2) positions 1 in M6 (3) (5 is at these positions in M6 (3)). 1 1 6 in front of ( ) is (this is from our lectures) 6 n

(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 1 1 1 1 1 1 1 = M6 (3) 1 = 1 FT 0 1 0 0 0 1 0 0 1 and 1 6 6 2 1 2 0 = M6 (3) 0 = 1 FT 1 1 1 0 1 0 0 1 0 1 3 2 6 4 5 1 2 4 1 2 4 1 6 1 6 1 6 1 4 2 1 4 2 2 6 1 5 2 4 4 0 4 = 6 1 3 2 0 1 0 1 3 1 3 2 6 4 5 1 2 4 1 2 4 1 6 1 6 1 6 1 4 2 1 4 2 1 1 3 5 1 6 4 1 0 = 6 0 1 2 0 0 3 0 3

Multiply the obtained FTs componentwise as follows: 1 6 3 2 6 1 2 6 4 3 1 0 0 4 0 FT FT = = 0 1 1 3 3 0 0 0 1 0 0 0 3 1 3 and take the FT inverse of the last vector: 2

April 22, 2011 6 1 3 1 0 1 = 6 1 M6 (3) 1 3 0 1 3 1 1 6 1 6 3 6 1 3 2 0 = 6 6 = 1 4 3 6 3 6 1 4 0 5 3 6 1

1 5 4 6 2 3

1 4 2 1 4 2

1 6 1 6 1 6

1 2 4 1 2 4

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 actual contents 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 satised: 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 that halts and answers yes if one of the desired nodes is reached and no if the connected component of the starting 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 direction we can label its nodes by their position in the linearized order. To check if there is a directed path that touches every vertex exactly once it is enough to check if the DAG has an edge (i,i+1) for every pair of consecutive 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 consecutive nodes also takes linear time, which implies that the total running time is also linear.

Potrebbero piacerti anche