ECS122A Midterm Answer Key
ECS122A Midterm Answer Key
INSTRUCTIONS
You will use your y variable which is calculated by taking the last digit of your student
id mod 4 then add 4 to that result. Everything in the test is self-explanatory if you
are confused state your assumption and answer the question. We willgrade with your
assumption in mind. We cannot answer any questions related to your interpre
tation.
1
(b) 20points What is the asymptotic run time of fo0? Fill out below to help answer the
question. Solve for the closed form via the recurrence tree method.
n
4
2uoyk
l6
dsk done
ANALYSIS- PROBLEM 2: (30 POINTS)
Problem Description: Suppose you are choosing between the following 3algorithms
Algorithm A Runs in T(n)T(n-y)+1 time. Find the asymptotic run-time of
algorithm A either by the use of the recurrence tree method or the master's theorem.
Algorithm BRuns in T(n)=vT(n/y)+6n time. Find the asymptotic run-tine of
algorithm B by any method.
Algorithm C Runs in T(n)=(y+1)T(n/y) +6n time. Find the asymptotic run
time of algorithm Cby any method.
Finally, order them from most asymptotically lowest to highest, and explain why.
ATN=T(h-)+1
4
Vs.
A-4 | n-4=
Care 2
Alyl ol:)
Masteu's eoenn (Mayta's teeorn
Kecurence tree
mcthod
Lautt lemma
Compant' Aaud B
(B>A)
n
Conpare Baud c
o-84
0-16 n
m -o(c7s)
3
A<B<
ANALYSIS-PROBLEM 3: (2 POINTS)
What is your $y$ ?.
a.) Given you have two sorted lists A.B such that |A| =
and size of B| =
is theworst-case number of comparisons preformed by our merge function n!. What
Sort) required to merge the two sorted lists? (from merge
n \4
c.) Given
43
A=[-2y,2y,y, -3y ,y,7y]
4
For your y what integers does A contain?
44 Use your A array to answer the question below
(-8,3,6,-i2,,28)
[-8,8 ,4.
-2,4,29 4
A 2 4 -12 -8 20
DESIGN-PROBLEM 4: (30 POINTS)
a.) Create a divide-and-conguer algorit hm that finds the product of an array of integers
with yrecursive calls. So givenA 4,3,5| the algorithm would return 60.
b.) Analyze the run-time of youñ algorithm using recurrence trees assuming the base
case is input of y or less.
c.) Confirm your result via substitution.
oln)
Indutte Hypstiests:
a=) yetun Alo)*A; To cn-d n<K
i 3 ) etum A*A(O* A(2); Induttve step: (n=k)
d-fdkodut(alo 4)); --:) T()-4T)+ éck-d
/ - )+as dd
Bhra a b*c%d; ’ o )
dza3
)T)= 47A)+ o)
Ja cotat d
’ 4
So ue Cau Confirm kat
tie nente fie
..
fet
wk ) 4
5
4 4
3
DESIGN-PROBLEM 5: (30 PoINTS)
ou have an array of both negative and positive integers and your goal is to ind a
Subarray (continuous set of cells) such that the product of the subarray is max. Please
provide a brute-force algorithm and analyze it in terms of n- the input size.
trt n=
masloddSbaay
A-stze )
(Alo
(n==)
ehrn A(o);
Magfodut =Alo) ’ o )
fr (t-oto ) o)
to )
o (n)
fon(k=t to)í ’ o)
us =ay t ALk,
malradt = Max axfrdet,a);o)
3
ehoun masodut; ’ o()
velall tMe
Coylety =ol)* ol) xola)
(o:)
6
1 ANALYSIS-PROBLEM 6: (15 POINTS)
a.) In class we clained that the base of a log of a function does not affect asymptotie
upper bounds of functions. Prove this claim via the definition of big-0.
0(logn). Prove
b.} In class we claimed that there exists no x where X>0 such that n
this claim.
c.) Would the above n*-0(logn) be true if xwas allowed to be negative?
a) fve
we mut show no 2
’ t c wheee
Jc.st.
lo
-.
lon? =