advertisement

**600.226 Data Structures Spring 2016 **

**Written Assignment 2 due Monday 2/29 **

**Hager & Selinski 32 points **

**NAME **

**JHED LOGIN**

INSTRUCTIONS: Write or type up your answers and submit a hard-copy in class on Monday by 4pm or a pdf on Blackboard by 11pm. Assume all logs are base 2. This is an individual assignment.

1) Rank these functions in growth rate order, starting with the slowest growth rate (ie, those resulting in a fast runtime) = 1, and ending with the fastest growth (worst time) = 8. [4 pts]

Function Rank

5

3

2

N

3

2) For each of these functions, first expand and simplify them where possible to combine constants, then determine the big Θ asymptotic complexity of each in terms of N. You must show all work for full credit. You do not have to identify little c and n values, but are welcome to do so to make things more clear. [2 pts each = 6 total] a) f(N) = 13 + log N + (18N + 4N 2 ) / (2N)

b) f(N) = 13 + (log 16) 3 + 3N 3 / 9N c) f(N) = 4 N log 16 + N * 2 log N

3) Suppose that T

1

(N) is O( f (N)) and T

2

(N) is Θ( g (N)). Determine whether each statement below is true or false, and prove why it is so using the formal definitions of these complexity types. [3 pts each = 6 total] a) T

1

(N) * T

2

(N) is Ω( f (N) * g (N) ) b) T

1

(N) + T

2

(N) is O( f (N) + g (N) )

4) Determine big-Theta bounds for the following code segments. If you can’t figure that out, give reasonable big-O and big-Omega complexities for partial credit. Obvious bounds such as constant time and exponential time will not earn credit.

// first segment =================================================== [3 pts] for (int i = N/2; i < N; i++) { for (int j = 1; j <= i; j *= 2) {

System.out.print("o");

}

}

// second segment ==================================================== [3 pts]

// assume keyboard is an instantiated Scanner for System.in

String s = keyboard.nextLine(); int N = s.length(); for (int i = 1; i <= 4N; i += 2) { if (i % N == 0) {

System.out.println(s.indexOf('a'));

}

}

5) Suppose you are given SortedSets with forward (only) Iterators from the Java API. How would you print the items in the un ion of the two sets in Θ(N) time where N is the size of the larger set, and with only constant extra space (in addition to the sets themselves)? Recall that the union of two sets is the collection of all items in the sets, but without any duplicates. The values in the printed union must also be displayed in order. Give a detailed description of and/or pseudocode for your algorithm, and also explain and justify why your solution satisfies the Θ(N) running time requirement. [10 pts]