COT5405  Exam 1
Page 1 of 6
Problem 1
Suppose you are choosing among the following three algorithms:
•
Algorithm A solves problems by dividing them into ﬁve subproblems of half the size, recur
sively solving each subproblem, and then combining the solutions in linear time.
•
Algorithm B solves problems of size n by recursively solving two subproblems of size
n

1
and then combining the solutions in constant time.
•
Algorithm C solves problems of size n by dividing them into nine subproblems of size
n/
3,
recursively solving each subproblem, and then combining the solutions in
O
(
n
2
) time.
What are the upper bounds of running time of each of these algorithms, and which would you
choose?
Solution:
C.
A
:
T
(
n
) = 5
T
(
n/
2) +
O
(
n
) =
O
(
n
log
2
5
)
B
:
T
(
n
) = 2
T
(
n

1) +
O
(1) =
O
(2
n
)
C
:
T
(
n
) = 9
T
(
n/
3) +
O
(
n
2
) =
O
(
n
2
log
n
)
Grading notes:
•
Each recurrence worth 7 pts.
•
Correct selection worth 4 pts.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Page 2 of 6
Problem 2
Suppose that you are given a sorted sequence of distinct integers
{
a
1
,a
2
,...,a
n
}
.
Give an
O
(log
n
i
such
that
a
i
=
i
. Note the beginning index of the sequence is 1. For example, for the sequence
{
3
,

2
,
3
,
8
,
20
}
, your algorithm should output that
a
3
= 3.
Solution:
Just use binary search. If the middle item satisﬁes
a
[
mid
] =
mid
, return; if the
middle item satisﬁes
a
[
mid
]
> mid
, we only need to search in the ﬁrst half of the sequence; if the
middle item satisﬁes
a
[
mid
]
< mid
, we only need to search in the second half of the sequence. The
correctness is guaranteed because the integers in the sequence are distinct (2pts) and sorted (3pts).
Repeat the search until an integer is found or the number of integers needed to search is equal to
1. There are at most log
n
steps in the search and at each step, we only need
O
(1) time to check
middle item. Thus the time complexity is
O
(log
n
).
procedure
int BinSearch
(A, low, high)
int
mid
= (
low
+
high
)
/
2;
if
(
A
[
mid
] ==
mid
)
then
return mid;
else if
(
A
[
mid
]
> mid
)
then
return
BinSearch
(
A,low,mid

1);
else
return
BinSearch
(
A,mid
+ 1
,high
);
end if
end procedure
procedure
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 UNGOR
 Big O notation, Analysis of algorithms, Computational complexity theory, Wikipedia, Divide and conquer algorithm

Click to edit the document details