8

Click here to load reader

cs3110 sp11 Prelim 2 solutions

  • Upload
    lee-gao

  • View
    426

  • Download
    0

Embed Size (px)

DESCRIPTION

Things to do with ocaml

Citation preview

Page 1: cs3110 sp11 Prelim 2 solutions

CS3110 Spring 2011

Prelim 2

April 19, 2011

Solutions

Page 2: cs3110 sp11 Prelim 2 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 asthey 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 = [([[]], ()); ([[]], ())]

Page 3: cs3110 sp11 Prelim 2 solutions

April 19, 2011 CS3110 Prelim 2 Page 2 of 7

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

let zardoz = (42, 43) in fst zardoz

(a) let zardoz = fun x -> 42 in

List.hd (List.map zardoz [zardoz; zardoz])

(b) let x = ref 6 in

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

!(!zardoz) x 7

(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) in

(zardoz := fun x -> if x = 43 then x - 1 else !zardoz (x - 2));

!zardoz 47

(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)) in

f zardoz 12

Page 4: cs3110 sp11 Prelim 2 solutions

April 19, 2011 CS3110 Prelim 2 Page 3 of 7

3. (15 points) Consider the following directed graph.

A

B C

D E F

G H

I

(a) Starting from A, give the sequence of nodes in order of first visit under depth-first search, assuming that the children of any node are visited in left-to-rightorder.A B D G I H E C F

(b) Do the same for breadth-first search.

A B C D E F G H I

(c) Draw the quotient graph obtained by collapsing the strongly connected com-ponents. Label each node of the quotient graph with the nodes of the stronglyconnected component that it represents.

AB CDF

E

GH I

Page 5: cs3110 sp11 Prelim 2 solutions

April 19, 2011 CS3110 Prelim 2 Page 4 of 7

4. (9 points) Recall that the union-find data structure uses two heuristics to improveperformance: in a union, always merge the smaller set into the larger, and in afind, 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 ofoperations:

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

Page 6: cs3110 sp11 Prelim 2 solutions

April 19, 2011 CS3110 Prelim 2 Page 5 of 7

5. (14 points) In the following concurrent sender/receiver program, the sender sendsdata to the receiver via a shared variable x, which is protected from concurrentaccess by a Mutex m.

let x = ref None

let m = Mutex.create()

let c = Condition.create()

0 let create_data() =

1 String.concat "" ((List.filter (fun s -> String.contains s ’z’)

["the"; "wi"; "zard"; "of"; "oz"]))

2 let sender() =

3 let data = create_data() in

4 Mutex.lock m;

5 x := Some data;

6 Condition.signal c;

7 Mutex.unlock m

8 let receiver() =

9 Mutex.lock m;

10 Condition.wait c m;

11 let data =

12 match !x with

13 | Some n -> n

14 | None -> failwith "should never happen" in

15 x := None;

16 Mutex.unlock m;

17 print_endline data

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 causethe program to deadlock. (Hint. Condition.signal c has no effect if thereare 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;

Page 7: cs3110 sp11 Prelim 2 solutions

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 smooth-ness 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. Ifso, say under which case (A), (B), or (C) it falls, and give the solution. (There isone 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)

Page 8: cs3110 sp11 Prelim 2 solutions

April 19, 2011 CS3110 Prelim 2 Page 7 of 7

7. (14 points) For the recurrence in the previous problem for which the MasterMethod did not apply:

(a) Draw the recursion tree of a hypothetical recursive program whose runningtime would be described by that recurrence called on an input of size n. Labelthe nodes of the tree with the running time of each recursive call, exclusive ofsubcalls. To the right of the tree, show on each level the sum of the runningtimes of all the calls occurring on that level. Indicate the height of the tree.

n log n

n2 log n

2

n4 log n

4

n8 log n

8

......

n8 log n

8

......

n4 log n

4

n8 log n

8

......

n8 log n

8

......

n2 log n

2

n4 log n

4

n8 log n

8

......

n8 log n

8

......

n4 log n

4

n8 log n

8

......

n8 log n

8

......

n log n

n(log n− 1)

n(log n− 2)

n(log n− 3)

heigh

t=

logn

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

O(n(log n)2)

End of Exam