CS3110 Spring 2011 Prelim 2 April 19, 2011

Solutions

April 19, 2011

CS3110 Prelim 2

Page 1 of 7

1. (12 points) Determine the type and value of each of the following expressions as they would be printed by the interpreter. Example:
let (s, t) = ([1; 2; 3], [4; 5; 6]) in [s] @ [t] - : int list list = [[1; 2; 3]; [4; 5; 6]]

(a) let x = ref 2 in
let y = 3 in let y = 4 and () = x := y in !x - : int = 3

(b) let x = 3 in
let f x = 2 * x in let y = 7 in let g y = x + y in g (f x) - : int = 9

(c) let x = ref [1; 2; 3] in
let y = ref [3; 2; 1] in let f x y z = z := List.iter2 (fun x y -> if x = y then () else !z) !y !x; !z in f y x (ref ()) - : unit = ()

(d) let x = ([[]], ()) in x :: [x]
- : (’a list list * unit) list = [([[]], ()); ([[]], ())]

April 19, 2011

CS3110 Prelim 2

Page 2 of 7

2. (20 points) Supply a value for zardoz that causes the entire expression to evaluate to 42. If impossible, say so and explain why. Example:
let zardoz = (42, 43) in fst zardoz

(a) let zardoz = fun x -> 42
List.hd (List.map zardoz [zardoz; zardoz])

in

(b) let x = ref 6 in
let zardoz = ref (ref (fun x y -> !x * y)) !(!zardoz) x 7 in

(c) let x = ref 2 in
let zardoz = ref (!x * 3) in let zed = ref (fun x -> !zardoz x) in zardoz := impossible, !zardoz cannot be both an int and a function ; !zed zardoz

(d) let zardoz = ref (fun x -> 3110)
(zardoz := fun x -> if x = 43 then x - 1 else !zardoz (x - 2)); !zardoz 47

in

(e) let f x y =
match x with (x, y, z) -> match z with (y, z) -> x + y in let zardoz = (40, 3110, (2, 3110)) f zardoz 12

in

April 19, 2011

CS3110 Prelim 2

Page 3 of 7

3. (15 points) Consider the following directed graph. A B D G I E H C F

(a) Starting from A, give the sequence of nodes in order of first visit under depthfirst search, assuming that the children of any node are visited in left-to-right order. ABDGI H ECF (b) Do the same for breadth-first search. ABCDEF GH I (c) Draw the quotient graph obtained by collapsing the strongly connected components. Label each node of the quotient graph with the nodes of the strongly connected component that it represents.

ABC DF

E

GH I

April 19, 2011

CS3110 Prelim 2

Page 4 of 7

4. (9 points) Recall that the union-find data structure uses two heuristics to improve performance: in a union, always merge the smaller set into the larger, and in a find, use path compression. Starting with the seven singletons A, B, C, D, E, F , G, draw the final state of the data structure after the following sequence of operations: union(A, B); union(B, C); union(D, E); union(E, F ); union(F , G); union(C, G); find(C).
Either D or E can be the root of the tree. Either A or B can be the root of the leftmost child. The order of children does not matter.

D A B C E F G

April 19, 2011

CS3110 Prelim 2

Page 5 of 7

5. (14 points) In the following concurrent sender/receiver program, the sender sends data to the receiver via a shared variable x, which is protected from concurrent access by a Mutex m.
let x = ref None let m = Mutex.create() let c = Condition.create()
0 1

let create_data() = String.concat "" ((List.filter (fun s -> String.contains s ’z’) ["the"; "wi"; "zard"; "of"; "oz"])) let sender() = let data = create_data() in Mutex.lock m; x := Some data; Condition.signal c; Mutex.unlock m let receiver() = Mutex.lock m; Condition.wait c m; let data = match !x with | Some n -> n | None -> failwith "should never happen" in x := None; Mutex.unlock m; print_endline data

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Suppose two threads are started up, one executing sender() and the other receiver(). (a) Give a sequence of instructions executed by the two threads that would cause the program to deadlock. (Hint. Condition.signal c has no effect if there are no threads waiting on c.) 2 3 0 1 4 5 6 7 8 9 10 (b) How would you modify the program to guarantee that it does not deadlock? Change line 10 to: if !x = None then Condition.wait c m; Also acceptable: while !x = None do Condition.wait c m done;

April 19, 2011

CS3110 Prelim 2

Page 6 of 7

6. (16 points) The Master Method says that for a recurrence T (n) = aT (n/b) + f (n), where a, b are constants, (A) if f (n) = O(n(logb a) − ε ) for some constant ε > 0, then T (n) = O(nlogb a ); (B) if f (n) = Θ(nlogb a ), then T (n) = Θ(nlogb a log n); (C) if f (n) = Ω(n(logb a) + ε ) for some constant ε > 0, and if f satisfies the smoothness condition af (n/b) ≤ cf (n) for some constant c < 1, then T (n) = Θ(f (n)). For each of the following recurrences, say whether the Master Method applies. If so, say under which case (A), (B), or (C) it falls, and give the solution. (There is one example of each, and one for which the Master Method does not apply.) (a) T (n) = 2T (n/2) + n log2 n M.M. does not apply (b) T (n) = 9T (n/3) + n log2 n (A) O(n2 ) √ (c) T (n) = 2T (n/4) + 1000 n √ (B) O( n log n) (d) T (n) = 4T (n/4.011) + n (C) O(n)

April 19, 2011

CS3110 Prelim 2

Page 7 of 7

7. (14 points) For the recurrence in the previous problem for which the Master Method did not apply: (a) Draw the recursion tree of a hypothetical recursive program whose running time would be described by that recurrence called on an input of size n. Label the nodes of the tree with the running time of each recursive call, exclusive of subcalls. To the right of the tree, show on each level the sum of the running times of all the calls occurring on that level. Indicate the height of the tree.

n log n
n 2 n 4 n 8

n log n
n 2

log

n 2 n 4

log

n 2 n 4

n(log n − 1) log
n 8 n 8 n 4

(b) Give an asymptotic solution to the recurrence. A big-O answer is sufficient. O(n(log n)2 )

height = log n

log
n 8 n 8

n 4

log
n 8 n 8

n 4

n 4 n 8 n 8

log
n 8 n 8

n 4

n(log n − 2)
n 8

log

log

n 8

n 8

log

log

log

log

n 8

n 8

log

log

n(log n − 3)

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

End of Exam

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.