ΠΛΗ30 ΜΑΘΗΜΑ 2.2...

Click here to load reader

  • date post

    13-Apr-2017
  • Category

    Education

  • view

    2.127
  • download

    0

Embed Size (px)

Transcript of ΠΛΗ30 ΜΑΘΗΜΑ 2.2...

  • 30

    2:

    2.2:

    ! !

    " #$

    !""

    # $%&%

    ' ()*+

    ,-

    ,-

    ./(

    . % "

    ! & " '(:

    %(%')

    (-)

    %(%') *

    '&" !&')(! +, !

    '&" !&')(! +, !

    + .. +( Fibonacci

    %(%')

    + .. +!), %++%+!! %,

    + .. '$'! -! ." /%+(

    B. ',('&- &')(! +(,

    2 ' " !&+$! ' ' '&- % -&

    % &'(, , '- ')+(' !'" ' +(:

    '&" (' *!(+'' ( 2.1)

    ( 2.2)

    !'" , 0%+! , +, ( 2.3)

    /%& )')' '&- !'" +(, % '( '

    $+.

  • B. ',(1. !

    !

    1 -&' - %2+ % -&' '3" ) ':

    ) , *-+ ! , %- 4: ( +$!' %2+

    '( %+(!' 2-+ ! +$! !' % %%2+" ,

    !", ' )".

    /%%2+ ,: %+" , %%2+ , % /%%2+ ,: %+" , %%2+ , %

    %-%' +$!' '( ()+)" %+, , % -'

    %2+" )

    %+% ' %- 2+" : 1 +$' %++- 5- ()

    %%2+" ' % -+'! &' &

    % ! '( +$!!

    ( +$' % '+$ ' + ' %2+"

    6'' % ' %+(7' + '+$ ' %2+" !!

    %'$ ')'!' +$!' 3%"!' '!

    B. ',(1. !

    "

    !'!' - + $ !$ ' '3"

    2" :

    1. '5' - ) + % +$' %2+

    2. (' )" !&-! % %+(7' 2-+ ! +$! ('%(+! %

    %, % ,)%, % ,)

    3. %! 4' !&$ ' !"' !'" +(

    )$ % !$.

    4. ' 2! )" !&-!, !'7' ))!( '%(+! %

    %2+" !' + '+$ ' ('%(+! % , % %,)

    5. (' '%+% + % ' '%(+!" %2+"

    6. /%+(7' %+%+ '%+% $ +(

    *. ',(1. !1. +( Fibonacci

    #

    *: (' - 5! n. %+! '( n-

    Fibonacci.

    %4 15 ( Fibonacci:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

    . % +( (7' -!, )" !&-!:

    1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

    >+

    ===

    2,

    21,1

    21 nff

    nnf

    nn

    n

    *. ',(1. !1. +( Fibonacci (1. $%,! )$ +()

    $

    8 ) + % +%'( %%, ))!( '(

    '3":

    procedure FibRec(n)

    if n=1 or n=2 then

    return 1

    else

    O + -&' %+%+ % %'5' %

    )" !&-!:

    a=FibRec(n-1)

    b=FibRec(n-2)

    c=a+b

    return c

    end if

    end procedure

    (1), 1 2( )

    ( 1) ( 2) (1), 2

    n nT n

    T n T n n

    = ==

    + + >

  • *. ',(1. !1. +( Fibonacci (1. $%,! )$ +()

    %

    )" !&-! T(n)=T(n-1)+T(n-2)+1 %'( %!'! '( -!,

    5 ,

    !"

    !&$' '( ) '( 1) '( 2) 1 '( 1) '( 1) 1T n T n T n T n T n= + + + +

    1)1(2)( += nAnA'%4 +$' )"

    1)1(2)( += nAnA

    )2(12

    12)1(2

    122...2)0(2

    ...

    122...2)(2

    ...

    122)3(2

    12)2(2

    1)1(2)(

    11

    21

    21

    23

    2

    nn

    n

    nn

    kk

    A

    knA

    nA

    nA

    nAnA

    =

    +=

    =+++++=

    ==

    =+++++=

    ==

    =+++=

    =++=

    =+=

    +

    )2()( nnT =

    *. ',(1. !1. +( Fibonacci (1. $%,! )$ +()

    &

    )" !&-! T(n)=T(n-1)+T(n-2)+1 %'( %!'! '( -!,

    5 ,

    #" !"

    !&$' '( ) '( 1) '( 2) 1 '( 2) '( 2) 1T n T n T n T n T n= + + + +

    1)2(2)( += nKnK'%4 +$' )"

    1)2(2)( += nKnK

    )2(12

    12)1(2

    122...2)0(2

    ...

    122...2)2(2

    ...

    122)6(2

    12)4(2

    1)2(2)(

    2/11)2/(

    21)2/(2/

    21

    23

    2

    nn

    n

    nn

    kk

    K

    knK

    nK

    nK

    nKnK

    =

    +=

    =+++++=

    ==

    =+++++=

    ==

    =+++=

    =++=

    =+=

    +

    )2()( 2/nnT =

    *. ',(1. !1. +( Fibonacci (1. $%,! )$ +()

    &&

    %+%+ +( '(

    ( %+$ +!!

    , % ' -+'! )" ))!(. .&. n=5:

    )2()( 2/nnT = )2()( nOnT =

    )5(fib

    )4(fib

    )3(fib )2(fib

    )2(fib )1(fib )1(fib )0(fib

    )1(fib )0(fib

    )3(fib

    )2(fib )1(fib

    )1(fib )0(fib

    *. ',(1. !1. +( Fibonacci (3."' ..)

    &

    %+'$' !&$ !"' !'" ' +

    $ !$:

    / / .. +( Fibonacci:

    ) , *-+ ! , %- 4: %+(!'

    +$!, %+(7' +$! ! )$ -!, %$' %%2+"-

    . .

    /%%2+ ,: %+" , %%2+ ,

    %-%' +$!' '( (!'- %-%' +$!' n

    %%2+" )

    %+% ' %- 2+" : 7! '

    +$!' %++- 5- () %%2+" , +, % ' -+'!

    )" ))!(.

  • *. ',(1. !1. +( Fibonacci (4. )!( %(+!)

    &

    ) % ! % '(':

    +$!' %2+ % , % %,, )+)"

    +$!' ))& %%2+" :

    Fib(1)

    Fib(2) Fib(2)

    Fib(n)1 2 3 4 5 6 n

    1 1 fib(n)

    *. ',(1. !1. +( Fibonacci (5.%+% + 6.+%+ )

    &

    8 + )$ % !$ % +%'(

    %%, ))!( '( '3":

    procedure FibSeq(n)

    A[1]=1

    A[2]=1

    for i=3 to n

    %+%+ +( '( (n).

    A[i]=A[i-1]+A[i-2]

    end for

    return A[n]

    end procedure

    *. ',(1. !2. +!), ++%+!! ,

    &!

    *: () %(' 1,2,,n % %( Ai '(

    )! ! di-1 x di. ' % !' (' %++%+!!

    - 1 x 2 x x n

    $%:

    A1 x A2 x A3 % 1: 5x8, A2: 8x4, A3: 4x2

    A1 x A2 x A3 x A4 % 1: 10x6, A2: 6x3, A3: 3x8, A4: 8x2

    +:

    5" +: .' %3' % ! ' % )'3:

    4 A1 x A2 = C1 -%' C1 x A3 ... %3' % ( '(:

    d0d1d2 + d0d2d3 + + d0dn-1dn + $ !$: %-+'3' !' '

    %( ( %++%+!!( , %,. % &'

    ! " 2'+ (,! ! %+%+ .

    *. ',(1. !2. +!), ++%+!! ,

    &"

    )$' ( -&' 3( - + !$ !' ,%++%+!!4. )':

    A1 x A2 x A3 % 1: 5x8, A2: 8x4, A3: 4x2 %3' %$ (:

    (A1 x A2) x A3 %+! A1 x A2: 5x8x4=160 %3' %$% ' %+! A1 x A2: 5x8x4=160 %3' %$% '

    %( 5x4, -! , C

    %+! C x A3: 5x4x2=40 %3' %$% ' %(-' 5 x 2

    $+ 200 %3'

    A1 x (A2 x A3) %+! A2 x A3: 8x4x2=64 %3' %$% '

    %( 8x2, -! , C

    %+! 1 x C: 5x8x2=80 %3' %$% ' %(-' 5 x 2

    $+ 144 %3'

    0 ' ++ !' -, + 4' %++- %3'!

  • *. ',(1. !2. +!), ++%+!! ,

    %'!! ' %(' %& %'!! ' % '

    %++%+!!, '+$ ' 5'+ ! %3':

    A1 x A2 x A3 x A4 %'( %'' %'( , '3":

    ((A1 A2) A3) A4)

    ((A1A2)(A3A4)) ((A1A2)(A3A4))

    (A1(A2A3))A4 A1((A2A3)A4)

    A1(A2(A3A4))

    , '%+-3' +$ ' %'' %(! &,( &'! '(

    '+' "!' ' ( 3'&,! ;

    *. ',(1. !2. +!), ++%+!! , (1. %(+! ' )")

    &$

    '%(+! ' )" %2+" !'5 ! ' , '3":

    ',$' -&' ))!( (i,j) % -&' ") ! '( )+'$'

    % %(' , ! +( AiAj %'

    2-+ ! %'' %(!.

    8! , 4 ' ! %: A1A2An-1An. '%+-3' +$ '

    % ':1 2 n-1 n

    % ':

    A1(A2An)

    (A1A2)(A3An)

    (A1An-2)(An-1An)

    (A1An-1)An %+! 2-+ ! %'' %(! ' %%2+

    (' ' +"! ))!( T(i,j)

    " " -&' -&' - %( (0 %3')

    *. ',(1. !2. +!), ++%+!! , (2. )" &-!)

    &%

    + % +%'( %%, ))!( ' )" '(

    +:

    procedure MatMult(Ai,Ai+1,,Aj)

    if i=j then

    return 0

    else

    ' 2! %%, 2-+ ! %+" , %++%+!!4

    +( %, Ai,,Aj )()' % )" !&-!:

    for k=i to j-1

    ck=MatMult(Ai,,Ak)+MatMult(Ak+1,,Aj)+di-1dkdjend for

    end if

    return ck k=i,,j-1

    end procedure

    { }

  • *. ',(1. !2. +!), ++%+!! , (4.' %(+! /%%2/ ,)

    &

    %5!(!' ' % !' %-%' ' %3'% $' % % %%2+" '3 '%(+! '%%2+" .

    .&. ',"!' %++%+!7' 5 %(', ' %3' %-% ( ' '3" !':

    & !( ' '3" !':

    -%' %4 %+(!' ' 1 %(, ' 2 %,, ' 3 %, ...

    '+( (i,j) %'$' 2-+ ! %+" %3', % % $ ! %+/! +( %, Ai,Ai+1,Aj

    & !

    &

    !

    *. ',(1. !2. +!), ++%+!! , (4.' %(+! /%%2/ ,)

    )' -+'! %++%+!! %, 1234,

    1: 5x3, A2: 3x4, A3: 4x8, A4:8x2, A5: 2x3

    &

    !

    ' '

    '

    '' ($()"

    '

    ''( ($)%"

    ''' *''+' )%",($()%", $)& '*'' +)" ,( ()" , )$$

    $$

    & '&

    '&'!(( )"

    '&''*'&'+')",!( ($)'&*''+)%",!(($)&"

    &"

    '&''' '&*''' +$$,!(()$$,)&&$*'&'+*'' +"," ,!( ()&" *'&''+' &",!($()%"

    &&$

    *. ',(1. !2. +!), ++%+!! , (5. %+% +)

    + % +%'( %%, ))!( !' %3', '(

    +:

    procedure DP_MatMult(A1,A2,,An)

    for i=1 to n

    m[i,i]=0

    end for

    for p=2 to nfor p=2 to n

    for i=2 to n-p+1

    j=i+p-1

    m[i,j]=+

    for k=1 to j-1

    q=M[i,k]+M[k+1,j]+d[i-1]*d[k]*d[j]

    if (q>M[i,j]) then M[i,j]=q

    end for

    end for

    end for

    return M[1,n]

    end procedure

    *. ',(1. !2. +!), ++%+!! , (6. +! +%+ )

    +'( +! %+%+ :

    %4 for -&' %+%+ (n)

    )$ '%' for +%$ ))!( %+!$ , '+4

    )4 %, '()' ! ))!( '%(+!. '+ '(

    n2/2, %3' 4 , for, ' '+'! $ (n2) 5-.

    %3' % ( !' ' '%+: '( (n)

    '%4 '+" %+%+ '( O(n3)

  • *. ',(1. !2. +!), ++%+!! , (%'! $!)

    !

    + % -&' !'!':

    /%+(7' 2-+ ! +$!, %'$ " 2-+ !

    %'' %"! ' +( %, % ! &'( !' -

    !- '+( %(.

    '%! -5' 2-+ ! +$!, $ ' 5' %, (' 2-+ !

    %'' %"!

    + %%$' '+5 4) , 4! ' '

    %'! 2-+ ! +$! ' '+$.

    *. ',(1. !2. +!), ++%+!! , (%'! $!)

    "

    %%$' + , '3":

    procedure DP_MatMult(A1,A2,,An)

    for i=1 to n

    m[i,i]=0

    end for

    for p=2 to n

    for i=2 to n-p+1

    H ' +" % %! -' !2+(7' !'( )&,!$ %++%+!!$ %, AiAi+1Aj (' ! !'( s[i,j]=k, )+)" 2-+ ! %'' %(! '( (AiAi+1..k)(Ak+1Aj)

    for i=2 to n-p+1

    j=i+p-1

    m[i,j]=+

    for k=1 to j-1

    q=M[i,k]+M[k+1,j]+d[i-1]*d[k]*d[j]

    if (q>M[i,j]) then M[i,j]=q , s[i,j]=k

    end for

    end for

    end for

    return M[1,n]

    e