Вы находитесь на странице: 1из 37

. ..

. .
. .
. .
. .
, 2011















-
-


, 2011

(dynamic programming)


( )

,

( ):
(

F2

F1 = 1, F2 = 1, F3 = F1 + F2 , F4 = F2 + F3 , ...

, 2011

1940- . (Richard
Ernest Bellman)


, , IEEE (Institute of

Electrical and Electronics Engineers)

1953

, 2011

, 2011

: ,
,

: ,
,

, 2011

S ()
Wi = f i (S , xi ) / xi
i-
S = i (S , xi ) ,
xi

(. ):
S ,
,
.

Wi (S ) = max/ min{ f i (S , xi ) + Wi +1 (i (S , xi ))}


xi

xi

( )

, 2011

, 2011

: 2

: A, B, C, B, D, C, B, A
A, B, C, B D, C, B, A
, B

, 2011

O (2n ) ( n )

:
n1 , n2
f (n1 , n 2 )

0, n1 n2 = 0 ;

f (n1 , n2 ) = f (n1 1, n2 1) + 1, s[n1 ] = s[n2 ] ;


max ( f (n 1, n ), f (n , n 1)), s[n ] s[n ]
1
2
1
2
1
2

: O (n1 n2 )

, 2011

2
2

10

(longest
increasing subsequence problem):
.
: 5, 2, 8, 6, 3, 6, 9, 7
:

(i, j ) E xi < x j

, 2011

11

L( j ) , j-
: L( j ) = 1 + max{ L(1), L(2 ), ... , L( j 1) }
: for (j = 1; j <= n; j++)
L( j ) 1 + max{ L(i ) (i, j ) E

return max L( j )
1 j n

( ):

: O (n 2 )
.

, 2011

12

(edit distance problem):


, , ,
(- ).
:

, 2011

13

x[1..m] m, y[1..n] n,
E(m, n)

: E(i, j) x i
y j

:
E(i, j) = max{1+E(i-1, j), 1+E(i, j-1), |i-j|+E(i-1, j-1)}

E(i, j)

.

, 2011

14

:
for (i = 0; I <= m; i++) E(i, 0) = i;
for (j = 0; j <= n; j++) E(0, j) = j;
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
E(i, j) = min{E(i-1, j)+1, E(I, j-1)+1, |i-j|+E(i-1, j-1)}
return E(m, n)

O (m n )

, 2011

15

4
.
A, B, C , D 50 20, 20 1, 1 10, 10 100 .
.
, , ,
.
m n n p
O (m n p ) .

A ((B C) D)

120 000

(A (B C) D)

60 000

(A B) (C D)

7 000

, 2011

16

4
:

, 2011

17

Ai Ai +1 ... Aj 1 Aj

min{ C (i, k ) + C (k + 1, j ) + mi 1mk m j }, i < j;


C (i, j ) = i k < j
0, i = j

C (i, j ) ,

Ai Ai +1 ... Aj 1 Aj

mi 1mk m j
Ai Ai +1 ... Ak

Ak +1 Ak + 2 ... Aj

, 2011

18

4
:
for (i=1; i <= n; ++i) C(i, i)=0;
for (s=1; s <= n-1; ++s)
for (i=1; i <= n-s; ++i)
{
j=i+s;

C (i, j ) = min{ C (i, k ) + C (k + 1, j ) + mi 1mk m j }


i k < j

}
return C(1,n)

( )

: O n 3 ( n -)

, 2011

19

(traveling salesman problem)



(n 1) ! (n )

C (S , j )

, 1,

j
S {1, 2, ... , n}

, 2011

20

5
:

C ({1}, 1) = 0
for (s=2; s <= n; ++s)
for S {1, 2, ... , n} , S = s 1 S
{
C (S , 1) =
for j S , j 1
C (S , j ) = min {C (S \ { j}, i ) + d ij }
iS , i j
}
return min{ C ({1, ... , n}, j ) + d j1}

d ij (i, j )

: O n 2 2 n

, 2011

21


(independent set problem):

.

NP-,
, .

I (u )

: I (u ) = max 1 +

I (v ), I (v )

(u , v )E
d (u , v )= 2

, 2011

22


G = (V, E) s. s G.
.
.
1. G = (V, E)
w: E R+. p = (v1, v2, , vk) v1 vk 1 i j k,
pij = (vi, vi+1, , vj) vi vj.
. p s v. u v
. d(s, v) = d(s, u) + w(u, v).
d(u, v) u v
2. G = (V, E)
w: E R+. s V. (u, v ) E
:
d(s, v) d(s, u) + w(u, v)
.

, 2011

23

A R nm ( m , n ).

Aij i ,
j

:
for

V ,
E ,

do

for
to |V| 1 do
for
if d[v] > d[u] + w(u,v) then

w ,
,
d

return d

, 2011

24

( )

d A, O V

. ,
. P.
Aij s i, j ,
Pij i
(, ).
-:
for
for
for
for
{

to |V| 1 do
to |V| 1 do
if Avi > Au,i


:
while j > 0
{

+ w(u,v) then
}
return p

, 2011

25


-
:

: O (n 2 ) 1

, 2011

26


-

D[1..N,1..N], D[i, j]
i j.
,
[1 . .m] V Dm.
:

D m +1[i, j ] = min( D m [i, j ], D m [m + 1, j ])


. ., i j m+1,
2 : i (m+1) (m+1) j,
.
D0 .
.

, 2011

27


-
:
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (D[i][j] > D[i][k] + D[k][j])
D[i][j] = D[i][k] + D[k][j];
D
.
P,
D.

P 0 [i, j ] = i
P

( k +1)

P ( k ) [i, j ], D( k ) [i, j ] D( k ) [i, k ] + D( k ) [k , j ]


[i, j ] = ( k )
(k )
(k )
(k )
P [k , j ], D [i, j ] > D [i, k ] + D [k , j ]
.

, 2011

28


-
:

( )

O n 3

, 2011

29

V
.

. ,
.

. 0,
. ,
.
.

, 2011

30

. , .
, u,
. ,
u . ,
u, . u,
, ,
u , u .
,
. ,
u .

.
.

, 2011

31

O (n 2 ) 1 -
O ( E log V )
.

, 2011

32

3
C.
(adjacent matrix).

N seed,
CRS (Compressed Row Storage).

rand() stdlib.h.
30%
20%, N
2
(0.3 0.2 ) N .

.
N seed
.
N (
N) seed, ,
.

, 2011

33

-

-,
-
.

Intel Core 2 Duo E8500 3.17 GHz

2Gb

MS Windows XP SP3

MS VS 2008

, 2011

34

, 2011

35

1. ., ., ., . . .
, -, , 2005. 1292 .
2. . , 1. . .: ,
2006. 720 .
3. . , .
. .: ; -, 2003. 654 .
4. . C++. . .:
, 2002. 496 .
5. .., .. , 2.
.: , 2008. 288 .
6. . . .: - ,
1960.
7. ., . . .:
, 1965.
8. . . . .: , 1976.
9. . . .
. ., , 2010.
10. . . . c
SciLab. . : , 2011.
.

, 2011

36

???
.

, 2011

37