Beruflich Dokumente
Kultur Dokumente
NAME: ____________________________SOLUTIONS____________________________________
RUID:___________________________________________________________________________
Electronic Devices are not permitted during the quiz. These include but are not restricted to calculators,
computers and cell phones.
No communication between students or anyone outside of class is permitted during the quiz.
Textbooks, notes and any other written materials are not permitted during the quiz.
1. In each blank, write the letter of the formula that most closely provides the number of iterations
performed by the corresponding loop. Ignore any requirement to use a ceiling or floor function.
Note that an option may be used zero or more times (i.e. once, more than once, not at all).
A) a
B) x0
C) (x0 a)/k
D) (a - x0)/k
E) x0 + a k
F) (x0 a)/k
G) x0 / a
H) a / x0
I) x0 ^ a
J) a ^ x0
K) logk(x0 a)
L) logk(a - x0)
M) logk(a)
N) logk(x0)
O) logk(x0 / a)
P) logk(a / x0)
Q) logx0(a)
R) loga(x0)
S) logk(logx0(a))
T) logk(loga(x0))
U) logx0(a)/lg(a)
V) loga(x0)/logk(x0)
W) logx0(a)/logk(x0)
X)
x
x0
while x > a
x
x - k
end
where:
x0 and a are integers
a < x0 and k > 1
first iteration: x0 - k
second iteration: (x0 k) k = x0 - 2k
third iteration: ((x0 k) k) k = x0 - 3k
i-th iteration: x0 i k
when does it stop?
_____P______ b)
x
x0
while x < a
x
kx
end
x0 i
i
i
i
k
k
k
k
i
a
a - x0
(a - x0)
x0 a
(x0 a) / k
where:
x0 and a are real numbers
0 < x0 < a and k > 1
first iteration: x0 k
second iteration: (x0 k) k = x0 k2
third iteration: ((x0 k) k) k = x0 k3
i-th iteration: x0 ki
when does it stop?
x0 ki a
ki a / x0
i logk(a / x0)
______O_____ c)
x
x0
while x > a
x
x / k
end
where:
x0 and a are real numbers
0 < a < x0 and k > 1
first iteration: x0 / k
second iteration: (x0 / k) / k = x0 / k2
third iteration: ((x0 / k) / k) / k = x0 / k3
i-th iteration: x0 / ki
when does it stop?
_____S______ d)
x
x0
while x < a
x
xk
end
x0 / ki a
x0 k-i a
k-i a / x0
-i logk(a / x0)
i -logk(a / x0)
i -(logk(a) - logk(x0))
i logk(x0) - logk(a)
i logk(x0 / a)
where:
x0 and a are real numbers
1 < a < x0 and k > 1
first iteration: x0 ^ k
second iteration: (x0 ^ k) ^ k = x0 ^ k2
third iteration: ((x0 ^ k) ^ k) ^ k = x0 ^ k3
i-th iteration: x0 ^ ki
when does it stop?
x0 ^ ki a
ki logx0(a)
i logk(logx0(a))
______T_____ e)
x
x0
while x > a
x
end
where:
x0 and a are real numbers
1 < a < x0 and k > 1
x0 ^ (1/ki)
1/ki
1/ki
ki
ki
i
a
logx0(a)
log(a)/log(x0)
log(x0)/log(a)
logx0(a)
logk(logx0(a))
2. Find the least integer n such that f(x) is O(xn) for each of these functions. If no such n is
possible, write Impossible.
_____3_______ a) f(x) = 2x3 + x2 log x
x2 log x x2 x = x3
(C = 3, k = 1)
(C = 5, k = 1)
f(x) = O(x)
____False____
C = 1, k = 1
____False____
C = 10, k = 1
____False____
C = 2, k = 6
____True_____
C = 100, k = 1
x
1
2
3
4
5
6
7
x + 10
11
12
13
14
15
16
17
b) f(x) = x2 + x,
C = 1
1
2
3
4
5
6
7
C = 2
2
4
6
8
10
12
14
C = 10
10
20
30
40
50
60
70
C = 100
100
200
300
400
500
600
700
f(x) = O(x2)
____False____
C = 2, k = 0
____True_____
C = 2, k = 1
____True_____
C = 2, k = 2
____False____
C = 0.5, k = 2
x2
0
0.25
1
2.25
4
6.25
x
0
0.5
1
1.5
2
2.5
c) f(x) = 2x + 2x,
x2 + x
0
0.75
2
3.75
6
8.75
C = 0.5
0
0.25
0.5
1.125
2
3.125
C = 2
0
0.5
2
4.5
8
12.5
f(x) = O(2x)
____False____
C = 2, k = 0
____False____
C = 2, k = 1
____True_____
C = 2, k = 2
____False____
C = 0.5, k = 2
2x
1
1.414
2
2.828
4
5.657
C = 0.5
0.5
0.707
1
1.414
4
2.828
x
0
0.5
1
1.5
2
2.5
2x
0
1
2
3
4
5
2x + 2x
1
2.414
4
5.828
8
10.657
C = 2
2
2.828
4
5.657
8
11.314
d) f(x) = lg(x) + 4,
____True_____
C = 3, k = 4
____False____
C = 4, k = 2
____True_____
C = 2, k = 16
____False____
C = 1, k = 256
x
1
2
4
8
16
32
lg(x)
0
1
2
3
4
5
e) f(x) = x! + 2x,
lg(x)+4
4
5
6
7
8
9
C = 1
0
1
2
3
4
5
f(x) = O(x!)
C = 2
0
2
4
6
8
10
C = 3
0
3
6
9
12
15
C = 4
0
4
8
12
16
20
____False____
C = 2, k = 2
____True_____
C = 2, k = 3
____False____
C = 1, k = 4
____True_____
C = 2, k = 4
x
0
1
2
3
4
5
f)
f(x) = O(lg(x))
x!
1
1
2
6
24
120
f(x) = 7 + 1/x,
2x
1
2
4
8
16
32
x! + 2x
2
3
6
14
40
152
C = 1
1
1
2
6
24
120
C = 2
2
2
4
12
48
240
f(x) = O(1)
____False____
C = 7, k = 0
____False____
C = 7, k = 1
____False____
C = 8, k = 0
____True_____
C = 8, k = 1
x
0.5
1
2
1/x
2
1
2
7 + 1/x
9
8
7.5
C = 7
7
7
7
C = 8
8
8
8
4. What is the effect in the time required to solve a problem when you increase the size of the input
from n to n+1, assuming that the number of milliseconds the algorithm uses to solve the
problem with input size n is each of the quantities expressed below? Let W1 denote the runtime
with input size n+1 and let W0 denote the runtime with input size n. In each blank, write the
corresponding letter from the table below. Note that an option may be used zero or more times
(i.e. once, more than once, not at all).
A) W1 - W0 = 0
B) W1 - W0 = 1
C) W1 - W0 = 2
D) W1 - W0 = n
E) W1 - W0 = 2n
F) W1 - W0 = 2n+1
G) W1 - W0 = n+1
H) W1 - W0 = lg(n)
I) W1 - W0 = lg(n+1)
J) W1 - W0 = lg(n+1) lg(n)
K) W1 / W0 = 2
L) W1 / W0 = 2n
M) W1 / W0 = n+1
N) W1 / W0 = 2n+1
O) W1 / W0 = 2n+2
P) W1 / W0 = lg(n)
Q) W1 / W0 = n2
R) W1 / W0 = n!
S) W1 / W0 = 2n
T) W1 / W0 = 2n+1
U) W1 / W0 = 2(2n+1)
____J______ a) lg n
lg(n+1) lg(n)
____B______ b) n
(n+1) n = 1
____C______ c) 2n
2(n+1) 2n = 2n+2 2n = 2
____F______ d) n2
(n+1)2 - n2 = (n2 + 2n + 1) - n2 = 2n + 1
____K______ e) 2n
2n+1 / 2n = 2(n+1)-n = 21 = 2
____U______ f) 2 ^ (n2)
2 ^ ((n+1)2) / 2 ^ (n2) = 2 ^ ((n+1)2 - n2) = 2 ^ (2n+1)
____M______ g) n!
(n+1)! / n! = n+1