COSC 336 Data Structures and Algorithm Analysis, Fall 2014 Assignment 1 Release date: September 10, 2014 Due date: September 17, 2014, 12:00pm EST
Question 1 Define and explain mathematically the relevance of the three asymptotic notations for Best- Case, Expected (Average) case, and Worst-case running time.
Question 2 Give the worst case running time of the following code? a) Input: A, n curMax = A[0][0] for i=0 i<n i++ for j=0 j<n j++ if curMax < A[i][j] curMax = A[i][j] return curMax
Best Case ( or Omega) i s a r unni ng t i me f or whi ch t he set of al l pos- si bl e r unni ng t i mes ( whi ch we can cal l c) i s bound f r omt he bot t omby t he f unct i on of ( g( n) ) . No f unct i ons of r un- t i me can exi st l ower t han , def i ni ng i t as t he absol ut e f ast est r un- t i me. Worst Case ( or O) i s a r unni ng t i me f or whi ch t he set of al l possi bl e r unni ng t i mes ( whi ch we can cal l c) i s bound f r omt he t op by t he f unct i on of O( g( n) ) . No f unct i on of r un- t i me can exi st hi gher t han O, def i ni ng i t as t he absol ut e sl owest r un- t i me. Average case ( or Thet a) i s a f unct i on t hat i s def i ned as t he case when t he wor se case r unni ng t i me and t he best case r unni ng t i me ar e equi val ent . Ther e ar e 2 nest ed f or l oops: O(n^2) James Ruth 9/17/2014 Const ant Const ant N ( f or ) N ( f or ) Const ant Const ant Const ant Dr. Subrata Acharya Towson University 2
b) procedure Sort( A : list of sortable items ) defined as: do swapped := false for each i in 0 to length(A) - 2 inclusive do: if A[i] > A[i+1] then swap( A[i], A[i+1] ) swapped := true end if end for while swapped end procedure
Question 3 - Determine the Big-Oh for the following:
1. 41n 4 + 7n
2. 4nlogn + 12nlogn - 100
3. n e + n 5 logn + c n
One f or l oop, and one do- whi l e l oop: O(N^2) n^4 is a higher polynomial Assuming multi-core: O(n^4) nlogn is the highest function present Assuming multi-core: O(nlogn) Exponential > Polynomial > Log Assuming multi-cre: O(c^n) James Ruth 9/17/2014 N ( do- whi l e) Const ant N ( f or ) Const ant Const ant Const ant Const ant Const ant Dr. Subrata Acharya Towson University 3
Question 4 Write down the pseudo-code for Fibonacci sequence and give the Big-Oh bound for your algorithm.
Question 5 What can you say about the input size and the algorithm type for the best-case (Omega) and worst-case (Big-Oh) bounds?
Question 6 Give an example and explain where you can have different worst case bounds for the same problem. Explain.
Maxi mumcont i guous subsequence sums can have di f f er ent bounds because t her e ar e mul t i pl e sol ut i ons f or t he same pr obl em, wi t h some sol ut i ons havi ng an O( n^3) and some havi ng an O( n) . Thi s i s because of t he di f f er ent sol ut i ons i nvol vi ng di f f er i ng amount s of nest ed f or l oops. Each compl et es t he t ask, but wi t h t he mor e l oops i nvol ved, t he mor e cycl es needed t o get t hr ough each t ask. Input: n = sequence l i mi t Variables: num1 = 0 num2 = 1 temp = 0 r ead n i f n = 1 pr i nt 0 i f n = 2 pr i nt 1 el se pr i nt num1 pr i nt num2 f or ( i = 3, i <=n, i ++) t emp = num1+num2 pr i nt num3 num1 = num2 num2 = num3 end f or Onl y 1 f or - l oop: O(n) Bi g- Oh bounds al ways i nt er pr et t he i nput si ze as t he absol ut e maxi mum. For i nst ance, i f a f unct i on exi t s af t er f i ndi ng a number , Bi g- Oh assumes t he number i s i n t he ver y l ast pl ace. Most al gor i t hmt ypes ar e j udged on t hei r Bi g- Oh val ue, and not t hei r Omega. Occasi onal l y, an Omega bound i s eval uat ed, i n whi ch case t he i nput si ze woul d be t heor et i cal l y t he absol ut e mi ni mumneeded f or t he al gor i t hmt o wor k. Thi s i s done t o check i f t he Omega i s i n f act equal t o t he Bi g- Oh. James Ruth 9/17/2014 Dr. Subrata Acharya Towson University 4
Question 7 Arrange the following in decreasing order of running time. a. N b. log N c. 10 d. log c e. N 2
f. c N
g. N N
h. N! i. e 5
Question 8 Write a pseudo code for an O (n 5 log 2 n) algorithm.
log c, 10, e^5, log N, N, N^2, C^N, N!, N^N for(i=1,i<=n, 2i) for(j=1, j<n, j++) for(k=1, k<n, k++) for(l=1, l<n, l++) for(m=1, m<n, m++) for(o=1, o<n, o++) //code goes here end for end for end for end for end for end for James Ruth 9/17/2014 Dr. Subrata Acharya Towson University 5
Question 9
Let T 1 (N) = N 5 , T 2 (N) = e, T 3 (N) = N 7/2 ,T 4 (N) = N c logN, T 5 (N) = N
a. What would be Big-Oh of T1+T2+T3+T4+T5 and explain why?
b. What would be Big-Oh of T1*T2*T3*T4*T5 and explain why? Assumi ng mul t i - cor e: O(N^c*logN) Assumi ng si ngl e- cor e: O(T1+T3+T4+T5) Thi s i s because i n a mul t i - cor e pr ocessor , t asks can be spl i t up among t he cor es, and so i t wi l l onl y t ake as l ong as i t t akes a cor e t o f i ni sh t he hi ghest degr ee of O. Compar ed t o si ngl e, wher e i t must sequence each f or mul a i n a sor t of queue. N*N^5*N^( 7/ 2) *( N^c*l ogN) = N^( 2c+( 19/ 2) ) Bi g- Oh di sr egar ds const ant s l i ke 2c or e. Thi s l eaves O(N^(19/2)*logN). James Ruth 9/17/2014