0% found this document useful (0 votes)
16 views7 pages

ECS122A Midterm Answer Key

The document contains instructions for a midterm exam for a computer science course, including analysis problems and design problems related to algorithm runtime and complexity. Students are required to calculate asymptotic runtimes, analyze algorithms using recurrence relations, and provide brute-force solutions for specific problems. The document emphasizes the importance of assumptions in answering questions and provides a structured format for responses.

Uploaded by

hamza.ahmed.us
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

ECS122A Midterm Answer Key

The document contains instructions for a midterm exam for a computer science course, including analysis problems and design problems related to algorithm runtime and complexity. Students are required to calculate asymptotic runtimes, analyze algorithms using recurrence relations, and provide brute-force solutions for specific problems. The document emphasizes the importance of assumptions in answering questions and provides a structured format for responses.

Uploaded by

hamza.ahmed.us
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Midtem- Ansuet

Name: fCs 122A student ID:

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.

What is your $y$ ?_. 4


ANALYSIS-PROBLEM 1: (40 POINTS)
(a) 20points Find the asymptotic run-time of the code below. Show the run-time line
by line like we did in class and fill out the below questions to help answer.

1.void foo (int A[]){


2. let n=A.size();’o()
2a. i-1; 90()
3. while(i<=n) {
4. for (j=1 to n){
5.
6. }//endfor
print "hello" ;
o6)
7. i-i*y;
8. }//endwhile
10.}//end foo()

lines 3-8: why? Because tie wkile leopp'=*un4;aud


final answer: n ttme due to
ovelall
Slun-time

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.

1.void foo (int A[]){


2.
let n=A.size() ; -’ o)
3. for( i = 1 to n) {
4. for (j=1 to sqrt (n) ){ -
5.
6.
7.
8.
}//endfor
i++;
print "hello"; olr)
}//endfor
9.
10. for (i=1 to
11. foo (A [1. .A.size() /y])
12. }llendf or
13.}//end foo ()
lines 3-8:
why? trcsemeded too; so ute fhy op
lines 10-12 4T2) why? times the vcttn
Recurrence equation for th work: T()+ o)
depth of recurrence tree: show work
work at each level:
show work
Asymptotic run-time: show work

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

b.) Given T(n) = 5T(n/y) + n, How much


single function call at the 4th level of work is being done by the algorithm for a
i-D recursion (i.e. where depth i=4)?
4
Work dne bhy a

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

Show the work done by the algorihtm at top-level


on array A. (In other words, show the input to the recursion maxSubarray algorithm
of
is returned, and any other work done at the top recursive calls at the top level, what
level including the final return answer.)

(-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? =

Thastoe' e Cau Conclude that no x>o st.


ri-oly)
) Yu,t wald ee tue
Assune =-|
1
m
n

You might also like