0% found this document useful (0 votes)
28 views7 pages

Exam 1

The document is an exam for a course on data structures and algorithms. It contains 9 questions testing knowledge of topics like pointers, first order logic, abstract data types, loop invariants, running time analysis, recursion, binary search trees, and rotations. The exam instructs students to keep answers short and clear without repeating the question, and to not write outside the specified areas. It provides 2 hours to complete the exam.

Uploaded by

Hiền Trần
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)
28 views7 pages

Exam 1

The document is an exam for a course on data structures and algorithms. It contains 9 questions testing knowledge of topics like pointers, first order logic, abstract data types, loop invariants, running time analysis, recursion, binary search trees, and rotations. The exam instructs students to keep answers short and clear without repeating the question, and to not write outside the specified areas. It provides 2 hours to complete the exam.

Uploaded by

Hiền Trần
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/ 7

NAME:

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.

3. ADT: (25 = 16 + 8 + 1 marks (1 extra for graph))


For each item in the left list, choose the most appropriate thing in two right lists, i.e. (a)..(v)
For each item in the top right list, choose the most appropriate thing in bottom right list, i.e. (i)..(v)
If there are two lines, it needs two answers.
Scenarios
Abstract Data Types
(a) Telling your boss that there is not
(a) Stack
likely a feasible algorithm for his scheduling
problem (b) Queue
(b) Waiting for the bus (c) Priority Queue
(c) Google finding shortest path from (d) Dictionary
home to school. (e) Graph
(d) Finding cycles in a graph (f) Rooted Tree
(e) Determining whether taking an (g) Ordered searchable list with insert
edge creates a cycle in the minimal spanning and delete
tree algorithm
(h) Union find data structure
(f) Which roads to snow plow
(g) Research your Ph.D. Data Structure Implementations
and Algorithms
(h) Getting commodities from facto-
ries to stores along roads (i) Linked list
(i) Representing the parsing a sen- (j) Array
tence/code in English/JAVA to see its struc- (k) Balanced binary search tree
ture
(l) Matrix
(j) Keeping track of unhandled nodes
(m) Pointers
in dykstra’s algorithm
(n) Heaps
(k) Associating social insurance num-
bers with employees. (o) Hash table
(l) Matching boys and girls to marry (p) Depth first search
(m) Choosing order in which to study (q) Breadth first search
for 2011 exam (r) Dykstra’s algorithm
(n) Finding the cheapest recipe for (s) Network flows
hot dogs that respect the constraints, eg.
(t) Linear programming
enough but not too much moisture.
(u) Minimal spanning tree
(o) Matching brackets ([{}]()).
(v) NP-completeness
(p) Layout of the city

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

ii. (8 marks) Maintaining LI

iii. (4 marks) Post Condition

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, ....

(a) The Kindergarten algorithm for multiplying a × b = a + a + a + . . .

(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.

(c) Adding an element to the top of a stack.

4
(d) Binary search

(e) Searching an unsorted list

(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.

(b) Compare a RIL to a tree.

6
(c) Give the running time needed. Why?

9. (4 × 5 marks) Binary Search Trees:

(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.

You might also like