Assignment 1
COSC 3P03, Algorithms
Winter, 2011
Due: Feb. 4, Friday, noon.
1. (10) For any integer
n
≥
0, design an algorithm that computes the total number of 1’s in
n
’s
binary representation.
For example, let
A
be your algorithm (function), then
A
(10) should
return 2, while
A
(7) should return 3. Note that your algorithm should have a running time
of
O
(log
n
). Justify your answer. Note that the input to your algorithm is a decimal integer
n
.
You need to do the following: (1) give a description of your algorithm; and (2) give its
analysis.
One such recursive function/algorithm is as follows:
A(n)
if n < 2
return n
else
return ((n%2) + A(n/2))
This algorithm is similar to the binary search and has a running time of
O
(log
n
). Specifically,
let
t
(
n
) be the running time for the input
n
, then
t
(
n
) =
t
(
n/
2) +
c
which we know has a
solution of
O
(log
n
).
An observation is that the length of the binary representation of
n
is
ceilingleft
log
n
ceilingright
bits, so the time to add up all the binary bits would also be
O
(log
n
), although you
need to convert
n
to its binary equivalent first which takes
O
(log
n
) time as well.
2. (5) What is the largest
k
such that if you can multiply 3
×
3 matrices using
k
multiplications,
then you can multiply
n
×
n
matrices in time
o
(
n
log7
)? What would the running time of this
algorithm be? You can assume that
n
is a power of 3.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '11
 Qiu
 ........., Big O notation

Click to edit the document details