0% found this document useful (0 votes)
9 views6 pages

Midterm With Sol

Uploaded by

tako shonia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Midterm With Sol

Uploaded by

tako shonia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Midterm Exam

Algorithms & Data Structures


Winter Semester 2024

A
1. Using just the definition of big-O, prove that (2.01)n ∈
/ O(2n ). (6 p)
n n
Solution: By definition of big-O, (2.01) ∈/ O(2 ) means that for any c, and any n0 , there exists
n > n0 such that
(2.01)n > c2n .
This means
n
2.01

>c
2
or
2.01
 
n logc > 1.
2
Thus if take
 
 1 
n > max 
2.01
 , n0
 log 
c 2

we get that n > n0 and (2.01)n > c2n .

2. Give an example of an input (a[1..10], x) for the Binary Search algorithm, such that x ∈ a[1..10]
and the algorithm makes the most amount of comparisons possible. Describe every steps that
algorithm takes. (5 p)
Solution: Take for example
a[1..10] = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
and x = 10. Steps of the algorithm will be
• compare x = 10 with a[⌊(0 + 11)/2⌋] = 5;
• compare x = 10 with a[⌊(5 + 11)/2⌋] = 8;
• compare x = 10 with a[⌊(8 + 11)/2⌋] = 9;
• compare x = 10 with a[⌊(9 + 11)/2⌋] = 10 and print.

3.
(a) Say Quicksort algorithm is given an input (7, 2, 4). Describe the corresponding probability
space of all possible paths algorithm can take. Do not forget to indicate probabilities for
each path. (6 p)
(b) Draw the binary tree (with nodes as comparisons and leafs as permutations) that represents
algorithm Quicksort on a three element input (a1 , a2 , a3 ) if we know that the algorithm first
picked the element a2 . (6 p)
Solution: part (a) is QS3 ; it should have five elements (paths) with four of them with probabilities
1/6 and one 1/3. Part (b) is given in figure 1
Midterm Exam Algorithms and Data Structures

Figure 1. Quicksort binary comparison tree if we pick a2 first.

4. Say in the order statistics algorithm Select, instead of groups of 5, we split the array in groups
of 9. Do the analysis to find a lower bound on the number of elements in the array that are smaller
than the median of medians. (6 p)
Solution: There are
$ %
⌊n/9⌋ − 1
2

groups to the left of median of medians. In each group, there are 5 elements guaranteed to be less
than median of medians, plus 4 more elements from the group of the median of medians itself. So,
for the lower bound we get
$ %
⌊n/9⌋ − 1
· 5 + 4.
2

Than we just simplify this (there are many ways to do this, but most of the points come from
writing this down)
$ %
⌊n/9⌋ − 1
· 5 + 4 ≥ ··· .
2

5. Write down the pseudocode for Heapsort. Take an input (2, 5, 3, 1, 4) and describe (by drawing
consecutive trees) steps Heapsort takes to sort this input (also show steps of build-heap calls).(6 p)
Solution: pseudocode is given on slides I2EA9 page 18. For input something like Figure 2
2
Midterm Exam Algorithms and Data Structures

Figure 2

6. Describe how to implement a queue in an array of size N . (5 p)


Solution: Slide I2EA10 page 7 in students words.

3
Midterm Exam Algorithms and Data Structures

B
1. Say an algorithm works in time


1, for n = 1,
t(n) =
2t(⌊n/2⌋) + n, for all n > 1.

Use induction to show that this algorithm works in time O(n log n). (6 p)
Hint: For the base case of the induction you should use n = 2 and n = 3 (why?).
Solution: Master theorem does not apply. Solution with induction is given in Figure 3

Figure 3

2. Use Mergesort algorithm to sort an input

A = (67, 11, 3, 7, 4, 23, 20, 15).

Describe (with words or a diagram) every step the algorithm takes and indicate the number of
comparisons for each step. (6 p)
Solution: Solution is given in Figure 4
4
Midterm Exam Algorithms and Data Structures

Figure 4. Green numbers indicate number of comparisons to get to that step.

3.
(a) Say Quicksort algorithm is given an input (4, 9, 6). Describe the corresponding probability
space of all possible paths algorithm can take. Do not forget to indicate probabilities for
each path. (6 p)
(b) Draw the binary tree (with nodes as comparisons and leafs as permutations) that represents
algorithm Quicksort on a three element input (a1 , a2 , a3 ) if we know that the algorithm first
picked the element a3 . (6 p)
Solution: Similar to the clone exercise in the first version.

4. Let C = {0, 1} be a probability space of the coin flip experiment and D = {1, 2, 3, 4, 5, 6}
the roll of a die. Let X be a random variable on C × C × D given by X(a, b, c) = 4a − 2b + 2c.
Calculate the expectation of X. Explain and show your work. (5 p)
Solution: Simply

1 1 7
E(X) = 4E(c) − 2E(c) + 2E(d) = 4 · − 2 · + 2 · = 8.
2 2 2

5. Define what is a 2-3-tree. Apply deleteson(v, u) to the 2-3-tree given in Figure 5. Sketch every
intermediary tree you get while running the algorithm. (6 p)
5
Midterm Exam Algorithms and Data Structures

Figure 5. Apply deleteson(v, u).

Solution: Something like in Figure 6

Figure 6

6. Describe how to implement a stack in an array of size N . (5 p)


Solution: Slide I2EA10 page 5 in students words.

You might also like