Anda di halaman 1dari 5

CS 411 Analysis of Algorithms, Fall 2012

Midterm Exam Solutions


The Midterm Exam was given in lass on !ednesday, "to#er 1$, 2012%
A1% Time Complexity. &n eah 'art, indiate the (time) order of a fast algorithm
to solve the given 'ro#lem% Justify your answers. *o+ may ass+me that n is the
n+m#er of items in the given list, and that the #asi o'erations are all the
standard ,C- o'erators%
A1a% .iven a list of n+m#ers, ret+rn the s+m of the n+m#ers%
We must read all the input, so at least linear time. We can do this with a simple loop. Thus:
(n).
A1#% .iven a list of ('ossi#ly very large) n+m#ers, ret+rn a list of n+m#ers
ontaining every n+m#er that a''ears in the original list exatly one, with
d+'liates removed% For example, given /1, 2, 0, 1, 0, 0, 12, the algorithm
might return /0, 1, 2, 12.
We can do this by sorting (log-linear time) and then copying the list, while not copying any item
that is equal to the previous item (linear time). Thus: (n log n).
A2% Solving a Problem. &ndiate how yo+ wo+ld design a software 'a3age
(f+ntion, lass, et%) to solve the following 'ro#lem% *o+ may ass+me that yo+
have high45+ality im'lementations of all standard algorithms and data str+t+res
availa#le to yo+%
.iven a list of 100,000 n+m#ers, whih yo+ may ass+me to #e all distint,
'rint (in any order) the 1000 n+m#ers in the list with the lowest val+es%
Many answers are possible. Here are some reasonable ones:
a!e a minheap out o" the list, and then do heap-delete #$$$ times, printing the resulting #$$$
numbers.
%se a good selection algorithm (e.g., &ntroselect) to "ind the #$$$th smallest item, and then print
all items less than or equal to this value.
'ort the list (e.g., with &ntrosort or erge 'ort), and then print the "irst #$$$ numbers in the
resulting list.
Note that the first two options above are linear-time in the size of the whole list, while the last is log-
linear-time in the size of the whole list.
Exam 'age 2
A1% Efficiency. &n eah 'art, analy6e the time effiieny of the given f+ntion%
&ndiate what yo+ +se for ,n-, and what yo+r #asi o'eration is (these sho+ld #e
reasona#le hoies)% Ex'ress the effiieny +sing #ig4O, 7, or 8, whihever is
most a''ro'riate%
A1a% void foo(std::vector<int> & v)
{
assert(v.size() != 0);
for (std::size_t i = 0; i != v.size(); ++i)
{
for (std::size_t k = 0; k != i; ++k)
v[i] += v[k];

n is v.size().
(asic operation: v[i] += v[k].
Two nested ordinary loops, so (n
)
).
A1#% !! "#$ter %&st 'e a rando%(access iterator t)*e
te%*+ate <t)*ena%e "#$ter>
void 'ar("#$ter first, "#$ter +ast)
{
std::size_t size = +ast ( first;
if (size <= -)
ret&rn;
"#$ter %idd+e = first + size!.;
'ar(first, %idd+e);
'ar(%idd+e, +ast);
for ("#$ter it = first; it != +ast; ++it)
(/it) += size;

n is the number o" items in the given list.
(asic operation: (/it) += size.
*ecurrence "or number o" steps required: T(n) + )T(n,)) - n.
%se the aster Theorem. a + ), b + ), d + #.
'o a + b
d
. *esult: (n
d
log n) + (n log n).
Exam 'age 1
A4% Gaussian Elimination.
A4a% !hat is the time order of .a+ssian elimination9 :e s+re to s'eify
what yo+r ,n- means%
Two possible answers:
.or n equations in n un!nowns: (n
/
).
.or n being the number o" values in the input: (n
/,)
).
A4#% S+''ose we are doing .a+ssian elimination on a system whose first
two e5+ations #egin as follows;
)0
#
/0
)
x
/
...
10
#
)0
)
20
/
...
!e wo+ld not #e required to swa' the first two e5+ations% <owever, we
wo+ld 'ro#a#ly do the swa', anyway% !hy9 <ow does this hel' +s9
3aussian elimination is usually done with partial pivoting. This helps to avoid loss o"
signi"icance, which can lead to inaccurate results. When we do partial pivoting, we swap to get
the coe""icient with the greatest absolute value in the upper-le"t corner. 'ince 414 5 4)4, we would
swap the "irst two equations here.
A0% uic!select.
A0a% !hat 'ro#lem does the =+i3selet algorithm solve9 (>esri#e the
in'+t and o+t'+t? do not merely name the 'ro#lem%)
6uic!select solves the selection problem: 3iven a list o" orderable items and a number , return
the th smallest value. 7quivalently, return item in the list as it would be i" it were sorted.
A0#% =+i3selet has very 'oor worst4ase 'erformane% !hat an we do
a#o+t this9
6uic!select has linear-time average-case per"ormance, but quadratic-time worst-case
per"ormance. The standard solution is to !eep trac! o" the recursion depth, and switch to a
linear-time worst-case selection algorithm i" this depth e0ceeds some limit. (The usual
algorithm to switch to is (lum-.loyd-8ratt-*ivest-Tar9an selection, and the resulting algorithm
is !ntroselect.)
Exam 'age 4
:1% Solving "ecurrences. &n eah 'art, solve the given re+rrene exatly%
:1a%
x n=xn#2
for n). x#=:.
This is a "irst-order linear recurrence. 3eneral solution: x n=2 n .
8lugging in the initial condition x#=: , we get 2=: , and so =/.
'olution: x n=2 n/.
:1#%
x n=x n/ /)
for n/. x#=$. x )=$.
Note:

is the floor of k: the greatest integer less than or equal to k.
S+#stit+ting, we see that
x#=$ , x/=), x1=2 , x ):=;.
The
sol+tion sho+ld #e something li3e
x n=)log
/
n.
S+#stit+ting some more;
x#=$ , x )=$ , x/=), x 2=). x<=) ,
x;=). x:=), x==) , x1=2. & will g+ess that x n=) log
/
n.
This is easily he3ed for n=#, ). For n/ , we need to show that the
g+ess ma3es the e5+ation
x n=x n/ /)
tr+e%
x n=) log
/
n=) log
/
n/ /)=x n/ /),
and so the g+ess is orret%
Sol+tion;
x n=) log
/
n.
:1%
x n2 x n#2 xn)=$
for n). x$=/. x#=/.
This is a second-order linear recurrence. The characteristic polynomial is x
)
2 x2 , which
has 9ust one root: x=). %sing the appropriate case, we obtain the general solution:
x n=a
#
)
n
a
)
n)
n
.
8lugging in the "irst initial condition x$=/, we get
a
#
=/.
Then plugging in the second initial condition x#=/ , we get
a
)
=
/
)
.
'olution:
x n=/)
n

/
)
n )
n
.
Exam 'age 0
:2% #eap Sort. <ea' Sort re5+ires 8(n log n) time and +ses only onstant
additional s'ae% @o other sorting algorithm we have overed has these
'ro'erties (and neither, & #elieve, do any of the algorithms overed in CS 111)%
!hy, then, is <ea' Sort not the 'rimary algorithm +sed for most sorting9 Give
two different reasons.
The two primary reasons are that >eap 'ort is slower than other algorithms, and it is not stable.
?nother reason is that >eap 'ort requires random-access data, and so is not a good choice "or sorting
(say) a @in!ed @ist.
:1% "ed$%lac! Trees.
:1a% !hat is the order of the CAB> o'erations for a Aed4:la3 Tree9
?ll "our o" the A*%B operations are logarithmic-time "or a *ed-(lac! Tree.
:1#% >es'ite their s+'erior 'erformane (see 'art a), Aed4:la3 Trees are
+sed less and less for in4memory assoiative data% !hy is this9
>ash Tables are the usual choice "or in-memory associative data, since they o""er much better
average-case per"ormance than balanced search trees.
:1% A #alaned searh tree of some sort is generally onsidered to #e a
good im'lementation for an external assoiative data (e%g%, a large 3ey4
val+e dataset 3e't on external storage)% <owever, Aed4:la3 Trees are
rarely +sed for this, either% !hy not9
70ternal data is o"ten bloc-access: reading a single byte gets you the rest o" the bloc! "or no
additional cost. (-Trees, and the various variants o" these, are balanced search trees whose
per"ormance can be optimiCed "or bloc!-access data. This is done by setting the siCe o" a node
to the siCe o" a bloc! (or two bloc!s, etc.). *ed-(lac! Trees, with 9ust one item in each node,
cannot be optimiCed in this way.
Exam 'age C

Anda mungkin juga menyukai