Click here to load reader

طراحي الگوريتم ها

  • View
    89

  • Download
    0

Embed Size (px)

DESCRIPTION

طراحي الگوريتم ها. Computer algorithms. برنامه ریزی پویا. فصل ششم روش برنامه ریزی پویا در طراحی الگوریتم. Computer algorithms. مساله ضریب چند جمله ای مساله فلوید مساله فروشنده دورگرد مساله درختهای جستجوی دودویی بهینه مساله کوله پشتی صفر و یک. Week 9. برنامه نویسی پویا ، - PowerPoint PPT Presentation

Text of طراحي الگوريتم ها

  • Computer algorithms

  • Computer algorithms

  • : .

  • (n!) n2 n3

  • DP ( ) ( )

  • C(5,2)

  • B+

  • tn : n

  • M

  • M[i][j]

    :

  • M

  • :

  • : 64

  • : 132

  • N: d:

    P

    int minimult ( int n, const int d [], index P [][] ) { index i , j , k , diagonal; int M [1..n] [1..n]; for ( i = 1 ; i n ; i ++) M [i] [i] = 0; for (diagonal = 1; diagonal n -1 ; diagonal ++) for ( i = 1 ; i n diagonal ; i ++) { j = i + diagonal ; M[i][j] = minimum (M[i][k] + M[k +1 ][j] + d [ i 1] * d [k] * d [j]); P [i][j] = a value of k that gave the minimum; } return M[1][n]; }

    Min for diagonal

  • P 6

    void order ( index i, index j) { if ( i == j) cout

  • :

  • :

  • W D

  • 514239153432231

  • 514239153432231 2 1

    1234510115290323044203530

  • 514239153432231 2 ,3 4 1D0[2,3]= 3D0[2,1]+D0 [1,3]=9+=

    D0[2,4]= 2D0[2,1]+D0[1,4]=9+1

    1234510115290323044203530

  • 514239153432231 2 5 1D0[2,5]= D0[2,1]+D0[1,5]=9+5=14D1

    123451011529032143044203530

  • 514239153432231 ( ) 1D11444

    12345101152903214304420353440

  • 514239153432231 ( ) 2D2144

    12345101152903214304420353440

  • 514239153432231D2144 3 2D1[3,4]= 4 D1[3,2]+D1[2,4]=3+4=7D1[3,5]= D1[3,2]+D1[2,5]= +14=

    12345101152903214304420353440

  • 514239153432231D2144 3 2D1[3,4]= 4 D1[3,2]+D1[2,4]=3+4=7D1[3,5]= 4 D1[3,2]+D1[2,5]= +14=

    12345101152903214304420353440

  • 514239153432231D2144 4 2D1[4,1]= D1[4,2]+D1[2,1]= +9= D1[4,3]= 2 D1[4,2]+D1[2,3]= 2 +5= 7D1[4,5]= 3 D1[4,2]+D1[2,5]= +14=

    12345101152903214304420353440

  • 514239153432231D2144 5 2D1[5,1]= 3 D1[5,2]+D1[2,1]=4 +9= 13 D1[5,3]= D1[5,2]+D1[2,3]= 4 +3= 7D1[4,5]= 3 D1[4,2]+D1[2,5]= +14=

    123451011529032143044203534740

  • D

  • vi vj vk

  • n pi pi

  • : 5 ( Brute Force)

  • Brute Force

  • : A[i][j] Ki Kj A[i][i]= pi

  • kA[1][k-1]+p1+p2+p3+. . . +pk-1+pk+A[k+1][n]+pk+1+pk+2+. . . pn=A[1][k-1]+A[k+1][n]+pi

  • A k i j

  • A[1][2] = min{k=1: a[1][0]+a[2][2] } +(p1+p2) Min{3/8}+3/8+3/8 = 9/8

  • void optsearchtree ( int n , const float p[]; float & minavg, index R[][]) { index i , j , k , diagonal ; float A [1..n + 1] [0..n]; for ( i = 1 ; i n ; i ++) { A [i] [i -1] = 0 A [i] [i] = p [i]; R [i] [i] = i ; R [i] [ i 1 ] = 0;}A[ n + 1 ] [n] = 0 ;R[ n + 1 ] [n] = 0 ;for (diagonal = 1;diagonal n 1; diagonal++) for ( i = 1; i n diagonal ; i ++) { j = i + diagonal ; A[i][j] = minimum(A[i][k-1]+A[k+1][j])+pm R[i][j] = a value of k that gave the minimum; }minavg = A [1][n];}P: Minavg : R A

  • R