This preview shows pages 1–3. Sign up to view the full content.
Mehran Sahami
Handout #20
CS103B
February 6, 2009
Practice Midterm Problem Solutions
(including some hints and shortcuts you might find useful for exams)
1.
Order
in which nodes are processed if we were to do a reverse preorder traversal on
the tree: a, c, f, j, i, b, e, h, d, g
Shortcut: this is just the “Columbus” method going around the tree starting at the
right side and going “clock

wise” around the tree.
2.
Give a tight bigOh bound in terms of n for the function
foo
:
To derive the answer we work from the outermost loop inwards.
The body of the outermost
for
loop that is indexed by
i
executes O(n) times since
it simply iterates from 1 up to n, inclusive.
The next
for
loop (working inwards) is indexed by
j
and executes O(
i
) times
since it simply iterates from 0 up to (
i
1), inclusive.
Thus, the number of times
that the body of the
for
loop indexed by
j
is executed is determined by the series:
1 + 2 + .
.. + n = n(n+1)/2 = O(n
2
).
The innermost
for
loop (indexed by
k
, which is in the body of the loop indexed
by
j
), by itself would be executed O(n) times (regardless of the value of
i
or
j
)
since it simply iterates from 0 up to n1, inclusive.
Now we combine the number of times that the innermost
for
loop is executed
with the fact that it is in the body of the
for
loop indexed by j.
The instructions in
the body of the innermost
for
loop (indexed by
k
) are the most executed.
By
multiplying the number of times the body of the
for
loop indexed by
j
is executed
(O(n
2
)) with the number of times that the body of the
for
loop indexed by
k
is
executed (O(n)), we obtain: O(n
2
) * O(n) = O(n
3
)
So the overall bigOh running time of
foo
is O(n
3
).
Hint: note that sometimes it’s easier to start by analyzing the part of the function that
looks familiar to something we’ve seen before (like the outer two
for
loops that
we’ve seen in many examples) and then using that to help derive the final result.
3.
Find a tight bigOh bound for the following recurrence relation using repeated
substitution.
T(0) = 1
T(n) = T(n1) + n
2
Using iterative substitution, we get:
T(n) = T(n2) + (n1)
2
+ n
2
T(n) = T(n3) + (n2)
2
+ (n1)
2
+ n
2
T(n) = T(n4) + (n3)
2
+ (n2)
2
+ (n1)
2
+ n
2
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document –
2
–
Generalizing in terms of i gives us:
T(n) = T(ni) +
1
0
2
)
(
i
j
j
n
Setting i = n, yields:
T(n) = T(0) +
1
0
2
)
(
n
j
j
n
= 1 +
1
0
2
)
(
n
j
j
n
The simplest way to solve this problem at this point, is simply to bound the
summation (as tightly as we can without using
j
) as follows:
1
0
2
)
(
n
j
j
n
<=
1
0
2
)
(
n
j
n
=
n
3
This yields: T(n) <= 1 + n
3
= O(n
3
)
The derivation above would be sufficient for full credit, and is what we would
probably expect to get as a fully correct answer.
However, just for completeness, we
actually expand the sum further to show a few more tricks you might find useful.
First, we expand the sum:
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 09/26/2009 for the course CS 103B taught by Professor Sahami,m during the Winter '08 term at Stanford.
 Winter '08
 SAHAMI,M

Click to edit the document details