Exam 1
Exam 1
STUDENT NUMBER:
IT3010E Fall 2022 – Exam 1
Instructor: Jeff Edmonds
Time 2 hrs.
Keep your answers short and clear.
Do not repeat the question.
DON’T WRITE OUTSIDE SPECIFIED AREAS.
No question needs more than 4 or 5 sentences.
1. Pointers: Consider the tree implementation discussed in class in which nodes of the tree are hidden
within the tree object and there are parent pointers. Include size. The variable tree references this
tree object.
1 2
\ /
2 1
(a) (8 marks) Draw a picture of what the data structure when it contains the 1 \2 tree on the left as
it looks like in actual memory. Give the names of variables, data fields, and values. Show where
pointers/references are pointing both with arrows AND by giving actual values to the variables
and showing in the drawing what these values represent.
(b) (8 marks) Give code to rotate the root node left, i.e. from the 1 \2 on the left to the 1 /2 on the
right.
Hint: Use variables temp1 and temp2.
1
2. First Order Logic (5 marks): A computational problem is a function P from inputs I to re-
quired output P (I). An algorithm is a function A from inputs I to actual output A(I).
[A(I) = P (I) and T ime(A, I) ≤ T (|I|)] states that algorithm A solves problem P on instance I in
the required time. State in first order logic (i.e. forall ∀ and exists ∃) the statement “Problem P is
computable in time T (n)”. In what order are A and I provided and by whom? What do they know
when they give it? Relate this to the concept of a worst case.
2
4. Simple alg gives sum - Loop Invariants:
Hint: Give minimal thought to the value of
algorithm Sum(a, b) r other than the following two facts.
hpre−condi: b is a positive integer. a can be - We know that r ≥ 12 y because r is only
any real. halved when it is bigger than y.
- We know that r ≥ 1 because if r ever
hpost−condi: a + b is returned
reaches 1, then y will decrease to zero.
begin
x=a
y=b
r = 2⌊log2 y⌋
(the biggest power of 2 not bigger than y.)
loop
hloop−invarianti : x + y = a + b
exit when y = 0
if( y ≥ r )
hcase 1i
x=x+r
y =y−r
else
hcase 2i
r = r/2
end if
end loop
return(x)
(a) Use loop invariants to prove that if the loop exits then the code returns the sum a + b.
i. (4 marks) Establishing LI
3
(b) (4 × 4 marks) Running Time:
i. How many times does the second case get executed as a function of the input values a and b?
ii. How many times does the first case get executed as a function of the input values a and b?
iii. Give the time complexity (running time) as a function of the size to the input.
iv. Change the line of code r = 2⌊log2 y⌋ to r = 1 and delete the line r = r/2. Argue what the
bigOh of the number of iterations now is as a function of the size of the input.
5. (7×4 marks) Running Time : What is the time complexity of the following and why? Is this considered
feasible, poly-time, linear, logarithmic, constant time, ....
(b) The high school algorithm for multiplying, i.e. multiply each of the n digits of a with each of the
n digits of b.
4
(d) Binary search
(f) Your boss gives you a big and/or/not boolean circuit with n input variables and g gates. He also
gives you a particular input to the circuit. What is the running time to determine if it satisfies
the circuit? Why?
(g) Same circuit, but no input. Give the running time for you to find a satisfying instance, i.e. an
input for which the output is true. Why?
P 2 h 5.5
i
n −i
6. (6 marks) Compute Θ i=1 2 + 5 · logi100 (i) .
Why?
If possible classifying the function into Θ(1), 2Θ(n) , logΘ(1) (n), or nΘ(1) .
Why?
What is this class called?
5
7. (5 marks) Consider the code
algorithm OddRecursion(N )
hpre−condi: N is an integer.
hpost−condi: Q(N ) “Hi”s are printed
for some odd function Q
begin
if( N ≤ 1 )
Print(“Hi”)
else
loop i = 1 . . . N 2
Print(“Hi”)
end loop
loop i = 1 . . . 8
OddRecursion( N2 )
end loop
end if
end algorithm
Give and solve the recurrence relation for the number of “Hi”s Q(N ). Show your work.
8. (10+4+4 marks) Recursion: Define a Recursive Integer List (RIL) to be a list of a finite number of
objects, where each object is either an integer or is an RIL. For example, L = [3, 2, [4, 3], [[3, 9], [], 8], 7]
is an RIL.
(a) (10+5+5 marks) Give the pseudo code for a recursive algorithm RILSum that given a RIL L,
returns the sum of all the integers appearing in it. For example, RILSum(L) = 3 + 2 + 4 + 3 +
3 + 9 + 8 + 7.
6
(c) Give the running time needed. Why?
(a) Write the keys 1..9 into the figure so that it is a Binary Search Trees. (They were not inserted in
this order.)
BST with values 1−9
Delete value at root
(b) Delete the node that happens to be at the root. Either make this change to the current figure or
redraw the resulting tree.
(c) Is the original tree an AVL (balanced) tree? Is the new tree? Why or why not?
(d) Lets work with the original tree in case you formed the new tree incorrectly. Rotate node root.right
to the right. Hint: Jeff likes to think about rotating the edge from root.right.lef t to root.right.
Redraw the tree.