Algorithms Math Intro

Embed Size (px)

Citation preview

  • 8/18/2019 Algorithms Math Intro

    1/9

    Assignment 1

    1. Suppose that an algorithm A runs in time fA(n) = 2n2 + 7n and that an algorithm B runs intime fB(n) = 45n + 4, for prolems si!es of n. "or #hat $alues of n is algorithm A faster than

    B. %hat is, for #hat n is fA(n) & fB(n)'

    Sol

    Algorithm runs in time gi$en e*pression f A(n)=2n2+7n

    Algorithm runs in time gi$en e*pression f B(n)=45n+4

    "or #hat $alues of n f A(n) & f B(n)

    2n2+7n & 45n+4

    2n2+7n 45n+4 &

    2n2  -n / 4 &

     n2  10n / 2 & (2 taen ommon)

    a*2 + * + &

    3t loos lie a uadrati ineuation, and to find the for #hat $alues of n its true #e ha$e to findthe roots of ineuation.

    a = 1, =10, =2

    − ±√ 2

    =5−4

    −(−19)±√361+8

    −19±19.20

    −19−19.20 = 10.1, −19+19.20 = .12 2 2 2 4

    So, (n10.1) 6 (n+.1) &

     n lies in the range of n & 10.1, n .18

    i.e. for all n = .1,.2,.-9999.10.18 algorithm A is faster than algorithm B

  • 8/18/2019 Algorithms Math Intro

    2/9

  • 8/18/2019 Algorithms Math Intro

    3/9

    -. >o# man different arrangements for a 52 ard de are there'

    >o# does this ompare to the 11

     seonds that ha$e passed sine the Big Bang'

    Sol ifferent arrangements for a 52 ard de is 52 = 52651659961 = .5617

    Age of uni$erse sine ig ang is 1-.2 6 10 ears

    1 ear = -15- seonds

     age of uni$erse in seonds = 4-527526 10

    == 3n times of 11

     

     .4-5 6 11

    Comparing different arrangements for a 52 ard de #ith the 11

     seonds that ha$e passed sine the Big Bang

    8.0658∗1067

     1.5 6 1049 

     1.5 6 1050

    18

    0.4358 ∗ 10

    %hus the arrangement of 52 is 1.5 6 1050 times the 1018 times seonds sine ig ang.

    4. Sho# from definitions that :(S) = : (S D %) :(%) + : (S D not %) : (not %).

    Sol Ei$en t#o e$ents S and % #hih are stohastiall independent or simpl independent

    %hen onditional proailit in entire population ased on $alue %. :(S) = : (S D %)

    :(S) = ∑ : (S= )

    %his denotes the onditional proailit on entire population

    S

     T

    Not T

    %his denotes the onditional proailit of stohasti $ariale ased on $ariale % and the population is di$ided ased on %and not %

  • 8/18/2019 Algorithms Math Intro

    4/9

    %hus :(S) = :(: (S D %)) = ∑  ( = Ti) ∗ P (STi)

    %hus

    %hus :(S) = :(: (S D %)) = ∑  ( = Ti) ∗ P (STi) = : (%i = %) 6 : (SF%) + : ( %i = not%) 6 :(S F not %)

    5. :ro$e euation 1. of our te*too (page 21).

    Sol ∑2 = 2

    3+3 2+

    6 =1

    %aing sum of ues for instane∑ =1 

    3

    Griting in the form of sum of integers from to (i+1)- and sutrating the last term

    == ∑ =1 3 = ∑(( + 1)3 − ( + 1)3) 

    ∑ =1 3 =∑(( 3 + 3 2 +

    3 + 1) − ( + 1)3∑ 3 = (∑ 3 + 3∑ 2 + 3∑ + ∑1) − ( + 1)3== ( + 1)3= 3∑ 2 + 3∑ + ∑1

    ==3 + 3

    2 + 3 + 1 = 3∑

    2 + 3∑ + ∑1 Ge no# ∑ =  ( +1)

    , ∑ 1 = + 1 82

    %here fore 3 + 3 2 + 3 + 1 

      ( +1)

     / ( + 1) = 3∑ 2

    2

      2 3+3 2+ = 3∑ 2 2

     T! ∑ 2 = 2 3+3 2+ 6

  • 8/18/2019 Algorithms Math Intro

    5/9

  • 8/18/2019 Algorithms Math Intro

    6/9

    Biggest2 = ;istIi+1J@

    8

    8:rint Biggest 1, Biggest 2@

    %he numer of omparisons this algorithm does in #orst ase is H1 omparisons for findinglargest integer, H omparisons for eliminating iggest1 from list and H11 for finding seondlargest numer 

    So in #orst ase m algorithm does H1+H+H2 = - H - omparisons.

    7. :ro$e ;emma 1.1 of our te*too (page 51).

    Sol ;emma 1.11. f " L(g) if and onl if g " M(f).

    Suppose f " L(g) %here are t#o $ariales 1 and n1 suh that 1 and n1 suh that for n 1&= n,

    f (n) & = 6g(n). %hen for n 1&= n,g(n) & = (1F)6 f (n).%he other diretion is pro$ed similarl.2. if f " N(g) then g " N(f).

    f " N(g) means f " L(g) # M (g) B 1, g " L(f) # M (f) %hus g " N(f).

    -. N defines an eui$alene relation on the funtions. (Setion 1.-.1 e*plains #hat is

    neessar to demonstrate.) Kah set N (") 3s an eui$alene lass, #e all itomple* lass.

    3f f " L(E) and g" L(>) then " " L(>)@ ie, L is transitive. So the are M, N, o and #. "or all f, f " N(g) then g " N(f) gi$es symmetry."or an f, f " N(f) and for an g " N(g) gi$es refle*i$it.

    4. L(f + g) = L(mO*(f, g)).Similar euations hold for M and N. (%he are usefulto anal!e

    omple* algorithms #here f and g ould desrie the #or done different parts of the algorithm.)

  • 8/18/2019 Algorithms Math Intro

    7/9

    "or L (f+g) = L(ma*(f,g)), %here are 1 and n1 suh that n n1 , h(n) &= 6( f +g)(n)for and n n1

    %hen h(n) & = 2ma*( f ,g)(n).

    . :ro$e or dispro$e ∑ =1 2

     " $(2)

    Sol "rom 5 ∑2 =

    23+3

    2+

    =

    3

    +

    2

    +6 3 2 6 =1

    means running time of the algorithm as n (input si!e) gets larger is proportional to 2 ut the sum of suares is ha$ing a 3 term #hih is not proportional to 2 so the sum of suares o$er an inter$al 1 to n does not elong to$( 2)

    >ene dispro$ed ∑ =1 2 " $( 2).

    0. ra# a deision tree for the inar searh algorithm (Algorithm 1.4 on pages555) for n = 17.

    Algorithm

    int inarSearh(intIJ K, int first, int last, int P)

    1. if (last & first)

    2. inde* = 1@

    3. else if (last == first)

    4. if (P == KIfirstJ)

    5. inde* = first@

    6. else

    7. inde* = 1@

    8. else

    9. int mid = (first + last) F 2@10. if (P & = KImidJ)11. inde* = inarSearh(K, first, mid, P)@

    12. else13. inde* = inarSearh(K, mid+1, last, P)@

    14. return inde*@

    $( 2)

  • 8/18/2019 Algorithms Math Intro

    8/9

    eision %ree for n = 17

  • 8/18/2019 Algorithms Math Intro

    9/9

    1. Add a ro# to %ale 1.1 (page 40) sho#ing the ma*imum input si!e that an e sol$ed in onehour.

    Sol

    ma*imum input si!e that an e sol$ed in one hour 

    1 hour 1 415-4 27272 1- -2