HW3

5
dsa homework #3 due date : APRIL 28, 2015 Name : Jui-Hui Chung ID : B02202008 1 Asymptotic Complexity (1) For p(n) being a polynomial of degee d, there is p(n) p 0 (n)= cn d for c> sum of coefficient in p(n) ; thus log p(n) log p 0 (n)= cdn = O(n). The last equality is meant to be symbol . (2) We have df (n)e <f (n)+1 < 2f (n), and for the second comparison to hold true, we need 1 <f (n) which is given by the context. Thus df (n)e is O(f (n)). (3) For n →∞, f (n) is differnt from g(n) by a multiple of A. That is f (n)= Ag(n). We could write (A - 1)g(n) f (n) (A + 1)g(n) and conferm that f (n) = Θ(g(n)). (4) The solution to 40n 2 =2n 3 is n 0 = 20. (5) Other than a 0 , requiring a plus after, and a n , requiring a mutiplication before, each index requires a plus and a mutiplication. The number of operations is (n +1 - 2) · 2+2= 2n = O(n). (6) For f (n)= n 2 and g(n)= n 3 , lg f (n) = 2lg n = O(lg n)= O(lg g(n)) ; but f (n)= O(n 2 ) 6= O(n 3 )= O(g(n)) 2 Stack, Queue, Deque (1) Pop the elements from the stack S and enqueue them to queue Q. If the desired element is found, set a flag initially false to true. Dequeue the elements from queue Q and push them to the stack S in which the elements are now in reverse order. Repeat again to return the elements bact to stack S in their originally order. (2) Pop the elements from the stack T and push_back them to the deque D. Pop_back the elements from the deque D and push them to the top (the rear) of stack S. (3) DequeWithTwoStack 1 Stack F, B 2 push_back(e) 3 B[i] e 4 pop_back 5 t t-1 // t is the number of elements in stack B 6 push_front(e) 7 F[i] e 8 pop_front 1

description

HW3

Transcript of HW3

  • dsa homework #3due date : APRIL 28, 2015

    Name : Jui-Hui ChungID : B02202008

    1 Asymptotic Complexity

    (1) For p(n) being a polynomial of degee d, there is p(n) p(n) = cnd for c > sum ofcoefficient in p(n) ; thus log p(n) log p(n) = cdn = O(n). The last equality is meantto be symbol .

    (2) We have df(n)e < f(n) + 1 < 2f(n), and for the second comparison to hold true, weneed 1 < f(n) which is given by the context. Thus df(n)e is O(f(n)).

    (3) For n , f(n) is differnt from g(n) by a multiple of A. That is f(n) = Ag(n). Wecould write (A 1)g(n) f(n) (A+ 1)g(n) and conferm that f(n) = (g(n)).

    (4) The solution to 40n2 = 2n3 is n0 = 20.

    (5) Other than a0, requiring a plus after, and an, requiring a mutiplication before, each indexrequires a plus and a mutiplication. The number of operations is (n + 1 2) 2 + 2 =2n = O(n).

    (6) For f(n) = n2 and g(n) = n3, lg f(n) = 2 lg n = O(lg n) = O(lg g(n)) ; but f(n) =O(n2) 6= O(n3) = O(g(n))

    2 Stack, Queue, Deque

    (1) Pop the elements from the stack S and enqueue them to queue Q. If the desired elementis found, set a flag initially false to true. Dequeue the elements from queue Q and pushthem to the stack S in which the elements are now in reverse order. Repeat again toreturn the elements bact to stack S in their originally order.

    (2) Pop the elements from the stack T and push_back them to the deque D. Pop_backthe elements from the deque D and push them to the top (the rear) of stack S.

    (3) DequeWithTwoStack1 Stack F, B2 push_back(e)3 B[i] e4 pop_back5 t t-1 // t is the number of elements in stack B6 push_front(e)7 F[i] e8 pop_front

    1

  • 9 s s-1 // s is the number of elements in stack FEach of the four operations takes O(1) time. After N operations, the total running timeis O(N).

    (4) Pop the elements from the stack T and push them to the stack D. Pop the elementsfrom the stack D and push them to the top (the rear) of stack S.

    3 List, Iterator

    (1) We perfom the analysis using a design patter called amortization. Assuming I ambeing harsh, I charge each non-overflow push operation with three cyber-dollar. In themeantime I am being extravagant, paying each push operation with one cyber-dollar andgrowing the array from N to N + dN/4e with N cyber-dollar (apparently I pay morethan needed.) As a consequence, we can pay for the execution of n push operations using3n cyber-dollars, which is proportional to the total running-time O(n).

    (2) To shuffle an array a of n elements (indices 0..n 1)1 for i from n 1 downto 1 do2 j random integer such that 0 j i3 exchange a[j] and a[i]The Knuth shue is unbiased, so that every permutation is equally likely. The runningtime is O(n).

    4 Calculators

    (1) Sample Input

    (3 + 2)*(5&7)- 7*747/6+123*((!90)&&2)+!(!90)7+7

    Sample Output

    Indix Exp: ( 3 + 2 ) * ( 5 & 7 ) - 7 * 7# transform from indix to postfixpush additional ( to stackpush additional ) to infixpush ( to stackpush 3 to outputpush + to stackpush 2 to outputpush + to output and pop from stackpop ( from stackpush * to stackpush ( to stackpush 5 to outputpush & to stackpush 7 to output

    2

  • push & to output and pop from stackpop ( from stackpush * to output and pop from stackpush - to stackpush 7 to outputpush * to stackpush 7 to outputpush * to output and pop from stackpush - to output and pop from stackpop ( from stack# postfix expression transforming completePostfix Exp: 3 2 + 5 7 & * 7 7 * -RESULT: -24----------------------------------------------Indix Exp: 47 / 6 + 123 * ( ( ! 90 ) && 2 ) + ! ( ! 90 )# transform from indix to postfixpush additional ( to stackpush additional ) to infixpush 47 to outputpush / to stackpush 6 to outputpush / to output and pop from stackpush + to stackpush 123 to outputpush * to stackpush ( to stackpush ( to stackpush ! to stackpush 90 to outputpush ! to output and pop from stackpop ( from stackpush && to stackpush 2 to outputpush && to output and pop from stackpop ( from stackpush * to output and pop from stackpush + to output and pop from stackpush + to stackpush ! to stackpush ( to stackpush ! to stackpush 90 to outputpush ! to output and pop from stackpop ( from stackpush ! to output and pop from stackpush + to output and pop from stackpop ( from stack# postfix expression transforming complete

    3

  • Postfix Exp: 47 6 / 123 90 ! 2 && * + 90 ! ! +RESULT: 8----------------------------------------------Indix Exp: 7 + 7# transform from indix to postfixpush additional ( to stackpush additional ) to infixpush 7 to outputpush + to stackpush 7 to outputpush + to output and pop from stackpop ( from stack# postfix expression transforming completePostfix Exp: 7 7 +RESULT: 14----------------------------------------------

    (2) Sample Input

    -(fabs(sin(5.5*3.141592)+sqrt(+3.5)))pow( 5 , exp( log(2) ) )7 + 7

    Sample Output

    Indix Exp: @- ( fabs ( sin ( 5.5 * 3.141592 ) + sqrt ( @+ 3.5 ) ) )# transform from indix to postfixpush additional ( to stackpush additional ) to infixpush @- to stackpush ( to stackpush fabs to stackpush ( to stackpush sin to stackpush ( to stackpush 5.5 to outputpush * to stackpush 3.141592 to outputpush * to output and pop from stackpop ( from stackpush sin to output and pop from stackpush + to stackpush sqrt to stackpush ( to stackpush @+ to stackpush 3.5 to outputpush @+ to output and pop from stackpop ( from stackpush sqrt to output and pop from stackpush + to output and pop from stack

    4

  • pop ( from stackpush fabs to output and pop from stackpop ( from stackpush @- to output and pop from stackpop ( from stack# postfix expression transforming completePostfix Exp: 5.500000 3.141592 * sin 3.500000 + sqrt + fabs -RESULT: -0.870829----------------------------------------------Indix Exp: ( ( 5 ) $ ( exp ( log ( 2 ) ) ) )# transform from indix to postfixpush additional ( to stackpush additional ) to infixpush ( to stackpush ( to stackpush 5 to outputpop ( from stackpush $ to stackpush ( to stackpush exp to stackpush ( to stackpush log to stackpush ( to stackpush 2 to outputpop ( from stackpush log to output and pop from stackpop ( from stackpush exp to output and pop from stackpop ( from stackpush $ to output and pop from stackpop ( from stackpop ( from stack# postfix expression transforming completePostfix Exp: 5.000000 2.000000 log exp powRESULT: 25.000000----------------------------------------------Indix Exp: 7 + 7# transform from indix to postfixpush additional ( to stackpush additional ) to infixpush 7 to outputpush + to stackpush 7 to outputpush + to output and pop from stackpop ( from stack# postfix expression transforming completePostfix Exp: 7.000000 7.000000 +RESULT: 14.000000

    5