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

"" -

"Books.Ru - "
. (ISBN 5-8459-0987-2)
- "Books.Ru - ".
- ,
. ,
"" .

Introduction to
The Design &
Analysis of
Algorithms
Anany Levitin
Villanova University

ADDISONWESLEY
London
Mexico City

Boston
San Francisco
New York
Toronto
Sydney
Tokyo
Singapore
Madrid
Munich
Paris
Cape Town
Hong Kong
Montreal

-
2006

32.973.26018.2.75
36
681.3.07

. ..
. .-. .. ,
.. ..
:
info@williamspublishing.com, http://www.williamspublishing.com
115419, , / 783; 03150, , / 152

, .
36
: . : . . . :
, 2006. 576 . : . . . .
ISBN 5845909872 (.)
, , , .
,
,
,
, .
, .

. , ,
, .

32.973.26018.2.75
.

, , , Addison-Wesley
Publishing Company, Inc.
c 2003.
Authorized translation from the English language edition published by Addison-Wesley, Copyright 
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, recording or by any information storage retrieval system, without permission
from the publisher.
Russian language edition is published by Williams Publishing House according to the Agreement with R&I
c 2006.
Enterprises International, Copyright 

ISBN 5845909872 (.)


ISBN 0201743957 (.)

c , 2006

c Addison-Wesley, 2003


1.
2.
3.
4.
5.
6.
7. -
8.
9.
10.
11.

. ,
.


14
23
73
141
167
203
247
305
339
369
401
441
487
491
495
509
517
562

14

1.
1.1
1.1
1.2








1.2
1.3






1.3
1.4

23
25
31
33
34
34
35
36
36
37
38
39
40
43
45
45
47
48
48
50
50
51
51
54
55
58
63
67


1.4

2.
2.1





2.1
2.2


O-
-
-


2.2
2.3
2.3
2.4
2.4
2.5 :
n-


2.5
2.6
2.6

3.
3.1


3.1

7
70
71
73
75
75
76
78
80
84
85
87
88
88
89
90
91
92
94
96
98
105
107
116
119
120
122
125
127
133
135
139
141
142
143
144
146

3.2

3.3

3.4



3.2




3.3




3.4

147
147
148
150
152
152
154
157
159
159
160
163
164
166

4.
4.1
4.1
4.2
4.2
4.3
4.3
4.4
4.4
4.5



4.5
4.6



4.6

167
169
172
174
179
180
183
184
188

5.
5.1
5.1
5.2

203
206
209
211

189
189
192
194
195
196
198
200
201

5.3
5.4

5.5

5.6



5.2

5.3



5.4


-

5.5




5.6

6.
6.1
6.1
6.2
LU-


6.2
6.3
AVL-
2-3-
6.3
6.4


6.4
6.5

9
212
215
218
220
224
226
227
229
231
232
233
234
235
237
238
238
240
242
243
244
247
248
252
254
259
261
262
264
265
267
271
274
275
276
281
282
284
284
286

10

6.6

6.5






6.6

289
291
292
293
293
295
297
299
301

7. -
7.1
7.1
7.2


7.2
7.3
( )
( )
7.3
7.4 B-
7.4

305
307
310
312
313
317
322
323
325
326
329
331
335
336

8.
8.1
8.1
8.2



8.2
8.3
8.3
8.4

8.4

339
341
343
345
345

9.
9.1

369
371

349
353
354
360
361
364
366
368

9.2

9.3
9.4

9.1

9.2

9.3

9.4

11
376
378
381
385
386
390
392
397
398

10.
10.1

-


10.1
10.2

10.2
10.3 P , N P N P -
P N P -
N P -
10.3
10.4
10.4

401
402
403
404
405
406
408
409
411

11.
11.1
n



11.1
11.2


441
442
443
444
445
447
449
451
452
455
458

412
415
417
418
423
426
428
437
438

12

11.2
11.3 N P -


11.3
11.4



11.4

460
461
463
468
473
475
476
480
481
484
485

487

. ,

491
491
491
492
492
493
493
493

495
495
497
501

509

517

562



,
.
(George Forsythe),

(What to do till the computer scientist comes) (1968)

, .
, . , .
.
, , ,
.. .
,
. ,
,
.
, . , , ,
.
[11] , ,
. , . -,
. -,
.
,

15

. ,

. -, , ,
.
, ( , [22, 54, 82]). , :
.
,
, . ,
. ,
, , . ,
.



1 [74], .
.
,
. ,
, ; , .
, . : , , ,
, , . ,
1

,
( , , , , ). , 3- ., .: . ., 2000.
. .

16


.
.

(. ).



, , .
. , , .
(
2001 Computing Curricula 2001 [29]
.)
; .
. -, , , .
-,
.
, , ,
,
,
. ,
, . ,
. ,
, .
, , , ,
.

17


, ,
. ,
.
(. ), , . ,
,
, . ,
.
2, ,
, .
,
.
.
, ,
. .
.
,
.
, , .
600 . .
,
, ,
. Internet.
, .
, , ,
, .
. http://www.aw.com/cssupport.

18


, .
. ,
1.4, , . (2.2, 10.4 11.4), .
,
, .


, ,
. . , 3 11
,
. .
, 2.6 2.7, , , ,
.
. 80- (40-) .
80-

1, 2
3, 4
5

1.11.3
2.1, 2.2
2.3

6, 7

; O,

2.4, 2.5
3.1, 3.2 3.3
3.4

19

1012
13
1416

17
18
1921

22
23
24
2527

2830
3133
34
35
36
37
38
39
40

: ,

4.14.3

:
, ,


, , ,
:
:

:
, , B-

: , , ,



R, NP NP-



NP-

4.4 4.5
4.6
5.15.3

5.5
5.6
6.16.3

6.4
6.5
6.6
7.27.4

8.18.4
9.19.4
10.1
10.2
10.3
10.4 11.4
11.1
11.2
11.3

20

, ,
. , , . (Simon Berkovich) (George Washington
University), (Richard Borie) (University of Alabama), . (Douglas M. Campbell)
(Brigham Young University), (Bin Cong)
(California State University, Fullerton), (Steve Homer) (Boston University), (Roland Hubscher) (Auburn University),
(Sukhamay Kundu) (Louisiana State University), (SheauDong Lang)
(University of Central Florida), . (John C. Lusth) (University of Arkansas), . (John F. Meyer)
(University of Michigan), . (Steven
R. Seidel) ,
(Ali Shokoufandeh) (Drexel University)
. (George H. Williams) .
(Mary
Angela Papalaskari) ,
, , . (Villanova University) .

. , (John Matulis),
.
(Andiswa Heinegg).
.

, ,
, .
.
, , ,
:
, .
AddisonWesley
, .
(Maite SuarezRivas) -

21

(Lisa Hogue) ,
.

. , ,
, , .
, . : 250 !
.
, .
!

anany.levitin@villanova.edu
2002 .


, , .
, ,
.
, .
. Web-
. , ,
, ,
.
, , .
.
:
E-mail:
info@williamspublishing.com
WWW:
http://www.williamspublishing.com
:
:
115419, , / 783
:
03150, , / 152

: . , ,
. , .
(David Berlinski),
(The Advent of the Algorithm) (2000)

? : .
, . ,
. ,
(algorithmics),
(computer science). (David Harel) , Algorithmics: the Spirit of Computing1 ,
:

, .
, ,
, ,
[[48], . 6].

, , ,
. ,
. , ( !),
.
1

: . . .

24

1.

, . , ,
, . , , ,
, . , ,
- , . , ,
. , . ,
, :
,
: , , . ,
,
, , , ..
: , , - .
: ,
, .. - . . .
,
[[64], . 9].

, 1.1.
(). .
-,
. -, : ,
.
, ,
. -, , , (
), . , ,
, ,
.
1.2 . , -

1.1

25

.
, .
. , . 1.2
.
1.3 , , .
, .
, :
.
, .

.
1.4 .
, .
, . , , ,
.

1.1

? , , :
,
. ,
,
.


(. 1.1).
, , ( ), .
. 1.1 . ,
, . ,
, , .. ,

26

1.

. 1.1.

. , ,
,
.
, ,

.
.
.

.

, .

.

.

, .
m n
( m n ) gcd (m, n).2
, m, n.
(III ..), , ,
. ,
2

greatest common divisor. . .

1.1

27

:
gcd (m, n) = gcd (n, m mod n) .
(m mod n) m n.
, (m mod n) .
gcd (m, 0) = m (, ?), m
m n.
, (60, 24) :
gcd (60, 24) = gcd (24, 12) = gcd (12, 0) = 12.
( ,
, , 4
1.1.)

.
m n
1 n = 0, m ; 2.
2 m n r.
3 n m, r n. 1.
.
Euclid (m, n)
// gcd(m, n)
// : m n,
//

// : m n
while n = 0 do
r m mod n
mn
nr
return m
, ? :
(n) , ,
, . ,

28

1.

n,
(m mod n), , n. , 0,
.
, .
. ,
m n . , ,
t = min {m, n}.
, , m n, t . , t
; , t . ( ,
?). , (60, 24), 24, 23 .. ,
t 12,
.
m n
1 min {m, n} t.
2 m t. , 3;
4.
3 n t. , t
; 4.
4 1 t. 2.
, ,
, . ,
,
.
.
m n
1 m.
2 n.
3 m n, 1 2,
. ( p m n
, ,
pm pn , min {pm , pn } .)

1.1

29

4
.
, (60, 24), :
60 = 2 2 3 5
24 = 2 2 2 3
gcd (60, 24) = 2 2 3 = 12.
,
, . ( .)
, , , ( ). , ? ,
.
, - ,
, . , , ,
. ,
, , , -
, . (,
3 .
, . ,
?)
, ,
n.
(. 200 ..). ,
2 n,
. ,
, 2, .. 4, 6 ..
( 3) ,
. (
, , 6, .) 4
, 4 2,
. (
, .) ,
, 5. ,

30

1.

, . ,
, .

, n = 25:
2
2
2
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
3
5
7
9
11
13
15
17
19
21
23
25
3
5
7
11
13
17
19
23
25
3
5
7
11
13
17
19
23

,
.
, ,
25.
: p
? , , ,
p ,
, p p,
2p, . . . , (p 1) p
.
. , p p

, n, p n,
.

 n, .. ( , oor).
,

 n.
p p  n.
Sieve (n) ( )
//
// : n  2
// : L ,
//
n
for p 2 to n do
A[p] p

// .
for p 2 to  n do
if A[p] = 0
// , p
j pp
//
while j  n
A[j] 0
//
j j+p
// , p,

1.1

31

// A
// L
i0
for p 2 to n do
if A[p] = 0
L[i] A[p]
ii+1
return L
, , ,
. , ,
1. 1 , , ,
.
, . , ,
, , , .
, , . , ,
,
.

1.1
1. , . , ,
.
2. ,
. ,
?
3. ) (
) .
) ( ).
4. ) gcd (31415, 14142) .
) ,
gcd (31415, 14142)

32

1.
min {m, n} gcd (m, n).
5. , m n gcd (m, n) = gcd (n, m mod n).
6. ,
?
?
7. ) m n ,
, 1  m, n  10?
) m n ,
, 1  m, n  10?
8. ) ,

. .
) (. [20]). . .
,
, .
, .. . , .
, , :
?

9.  n.
10.
m n ( d),
x y ( ), ,
mx + ny = d.
) (, . [[65], c. 28])
.
) , ax + by = c
a, b c. (, .)

1.2

1.2

33

,
:
.

, .
, , () .
(. 1.2).

:
,

, ,

. 1.2.

34

1.


, , .
, - . ,
.
, . .
, . , ,
, ,
.
.
, .
, ,
(problems instance), .
, . (,
, .) ,
,
. ,
, (
!) .
, , , .

, , .

, , 3 .
(random-access machine, RAM). ,
3

,
(John von Neumann) (19031957)
. (A. Burks) . (H. Goldstine) 1946 .

1.2

35

, , .
, ,
(sequential algorithms).
. , (.. ) . ,
, (parallel algorithms).

.
- ,
? ,
. 2.1, ,
. - ,
. . ,
. ,
,
.
, ,
.




. (exact
algorithm), (approximation algorithm).
? -, , .
,
. -, , .
(traveling salesman problem),

36

1.

n . 3, 10 11 . -, ,
, .


, - . , ,

. , ,
6 7, , .
. : Algorithms + Data Structures = Programs
[123]. , - ,
.
1.4.


, , ,
. .
. ?
(algorithm design technique) (
, ) , ,
.

, ,
. ,
. .
-, , , ..
, .
, , , , . ,

1.2

37

,
.
, .
-, . ,
.
.
, .


, -
. 1.1 (.. ),
.
.
, . .
, .
(pseudocode)
4 , . . ,
. ,
- .
.
, , , ,
.
.
, ,
for, if while, . , , ,
, ,
, //.
4

, .
. .

38

1.

- (owchart), .. , , (
. 1.2). ,
,
.
, (- )
.

. ,
, ,
, .


- ,
(correctness). , ,
. , , m n,
gcd (m, n) = gcd (n, m mod n), , ,
(. 1.1.5), , , ,
.
,
. . ,

, . , ,
,
. ,
. , ,
, .., ,
(. . 1.2).

1.2

39

,
. , . 11.


, . .
: . (time efciency)
. (space efciency),
, . 2.
(simplicity). , , -
, ,
. , , ,
, , , .

. , . , ? ,
. , . ,
. ,
, . ,
. .
,
(generality). , : , ,
. ,
,
. : , . ,
, ,
1. -

40

1.

. , ,
gcd ,
1. ,
, . ,
, n , ,
m- ,
m = n/2. : ,

.
,
. ,

. , , 1. ,
,
, .
, ,
.
, . , . ,
, . , ,
. ,
, , 1.1. ( ?)
(Antoine de SaintExupery), , : ,
, ,
.5


,
.
5

(Jon Bentley) [15]. Programming Pearls [15].
. !

1.2

41

. ,
, , . , ,
, . (. [47]).

.
.
, , , .
,
:
.
, ,
, .
.
, , ,
. ,
.
, (.. , ) ,
, .. (
, , [15]
[61].) ,
,
. ,
1050%
.
.
, .
2.6.

, . 1.2.

42

1.
, ,
, .

,
, ,
- . , ,
.
(, ! The Joy of Algorithms.6 )
, . ,

, , .
: , ,
, .
,
,
.
, , , (optimality) . , ,
. ,
, ? . ,
n log2 n , n
(. 10.2). , , .
, , , ? , , .
, , .
.
, ,
.
10.3. ,
.

, .
6

. . .

1.2

43

. 1.2,
. ! ( ?)
, , .

1.2
1. . ,
, . .
(.. , ,
). , . ,
. (.
, ,
, , . , ,
, .)
2. . , .
17 .
, . (.. , ),
.
, .
: 1 , 2 ,
5 10 .
, . , ,
10 .
,
20 , . (. Internet ,
, ,
.)

44

1.
3. ,
a, b c?

) S = p(p a)(p b)(p c), p = (a + b + c)/2;
) S = 12 bc sin A, A b c;
) S = 12 aha , ha , a.
4. ax2 + bx + c = 0, a, b c
. (, sqrt (x).)
5. :
) ;
) .
6. (, , ). ( ,
.)
7. ) ?
) ?
) World Wide Web.
8. , , .
? ?
9.
.
M inDistance (A [0..n 1])
// : A[0..n 1]
// :
//
A
dmin
for i 0 to n 1 do
for j 0 to n 1 do
if i = j and |A[i] A[j]| < dmin
dmin |A[i] A[j]|
return dmin
, , ? (

1.3

45

.
, ?)
10. , , How to Solve It, (George Polya)
(18871985) [89]. , . Web (,
, )
,
1.2. ? ?

1.3
, , ,
. ,
- , . , ,
.
:
;
;
;
;
;
;
.
.

(sorting problem)
- 7 . ,
. ( ,
7

, . . .

46

1.

, .) ,
, , , ,
, ,
, .
, ,
. ,
,
. (key). ,
, , , .
? , ,
, .
. ,
, ..
. 6.1 ,
.
,
, .
. ,
.
,
(.. ) .
, . , ,
n n log2 n . , ,
( , , ) , .
, . ,
,
. , , ,
. , , .
, (.. ),

1.3

47

,
( , ..).
. (stable), ,
. , , i j, i < j, ,
, , i j  , i < j  . , , ,

( ).
,
. , ,
, , .
,
.
, .
(in place), ,
.
, , .

, (search key), ( 8 ).
, .
, ,
, ,
, . ,
, .
,
.
,
8
,
.

48

1.

. ,
, .. ,
. , , ,
,
. ,
, . ,
( )
, .


,
, . (string)
, . , , , , ; ,
; , , {A, C, G, T}.
,
,
.
,
. .
(string matching). . 3
, 7 ,
. (R. Boyer) . (J. Moore).


, , . ,
, ,
. ( .)

1.3

49

, .
, , , ..
Web, , Web-
9 .
:
(
, );
( :
);

( ,
).
,
. .
. ,
, .
. ,
n ,
. (graph-coloring problem)
, , .
, ,
, .
, ,
.
9

(University of Notre Dame),


19 [6].

50

1.



.
, ,
, (,
.)
,
, . . -,
. -,
.
, , . ,
. .
10.3.

, , .


,
, , .
(, ) ,
(, ..)
. 2000 ,
.
,
. , .
,
.

:
.
: n , .
,

1.3

51

.
, (, [90]) ,
(, [102]).


. , .
: , ..
.
,
, . ,
,
.

,
. 25
. ,
.
, .
, ,
.
6.2, 10.4 11.4.

1.3
1. . , , , .

52

1.
ComparisonCountingSort (A [0..n 1])
//
// : A[0..n 1],
//

// : S[0..n 1],
//
A,
//

for i 0 to n 1 do
Count[i] 0
for i 0 to n 2 do
for j i + 1 to n 1 do
if A[i] < A[j]
Count[j] Count[j] + 1
else
Count[i] Count[i] + 1
for i 0 to n 1 do
S[Count[i]] A[i]
return S
) : 60,
35, 81, 98,14, 47.
) ?
) ?
2. . . (
,
.)
3. .
4. . ,
. (Leonard Euler) (17071783). ,
? . 1.3
, ,
.
) .
) ? , , . , , , -

1.3

53

. 1.3.

, ,
.
5. . (. 1.3.4),
(Sir William Hamilton) (18051865)
Icosian Game. , . 1.4.

. 1.4.

(Hamiltonian circuit) , ..
.
6.
, , .
) ,
- , . , ,
? ?
)
?

54

1.
7. ) .
) .
8. , . 1.5.
b
a
d
c
e
f

. 1.5.

) , ,
.
) a

9. . ,
n , (x, y). , ?
10. , (x, y) P1 Q1 P2 Q2
, (.. , ).

1.4

,
.
(data structure) . ,
. (,
), (,
, , ,
). -

55

1.4

.
( ),
.


. (array) n ,
,
(. . 1.6).
[0] [1]

...

[n 1]

. 1.6. n

, n ,
0 n 1 ( . 1.6)
1 n. , ( ).
, ,
.
. . , 12 ,
, .

.
, . ,
.
.
(string), .. , .
. , ,
() (binary strings) (bit strings).
,
, , ,
.
. , , ,
. :

56

1.
;
, , , .. ;

() ,
, .
( )
, (nodes). :
, (pointers).
,
.. NULL; ,
. (singly
linked list) , , ,
(. 1.7).
[0]

[1]

...

[n 1]

null

. 1.7. , n


,
. , , ,
. . . ,
. ,

.
. , ,
, (header). ,
.
,
(doubly linked list).

57

1.4

, (
)
(. 1.8).
null

[0]

[1]

...

[n 1]

null

. 1.8. , n


,
. (list) , .. ,
. ,
, .
: .
(stack) , , .
(top) ,
( ,
). : , (last-in-rst-out, LIFO),
, .
,
, . , ,
.
(queue) , ( (front)),
( (rear)). (dequeue), (enqueue).
, :
, (rst-in-rst-out, FIFO). , ,
.
, ,
.
. , , (priority queue). -

58

1.

, 10 ( ).
: , .
, ,
( ).
,

. , (heap)11 .
6.4.

,
, , , , , . G =
V, E
: V ,
, E, , .
, .. (u, v) ,
(v, u), . , , (u, v) u v,
G (directed).
(digraph).
, , (. 1.9).
, . 1.9a, 6 :
V = {a, b, c, d, e, f } ,

E = {(a, c) , (a, d) , (b, c) , (b, f ) , (c, e) , (d, e) , (e, f )} .

, . 1.9, ():
V = {a, b, c, d, e, f } ,
10

E = {(a, c) , (b, c) , (b, f ) , (c, e) , (d, a) , (d, e) , (e, c) , (e, f )} .

, ,
,
. . , . 5, .: . ., 1985. . .
11
heap (heapsort),
,
( , Lisp Java) .
heap ( )
. . .

59

1.4
a

. 1.9. ) ; )

(loops),
, .
, , .
, , , |V |
,
|E| :
0  |E|  |V | (|V | 1)/2.
|V | |V | 1
, .
, |V | (|V | 1) 2, (u, v) 2 : u v v u.
, , (complete). , |V | , K|V | . ,
, (dense), , , ,
12 , (sparse).
, ( ),
, ,
.


, ,
:
. (adjacency matrix)
, n ,
n n,
. , i- j-
12

,
. . .

60

1.

, 1 , i- j- ,
0 13 . , . 1.10
, . 1.9. , (, ?),
.. A [i, j] = A [j, i] 0  i, j  n 1.
a
a 0
b 0
c 1

b c d e f
0 1 1 0 0

0 1 0 0 1
1 0 0 1 0

b
c

c
a

f
b

d 1 0 0 0 1 0

e 0 0 1 1 0 1

0 1 0 0 1 0

e
f

. 1.10. ) ) ,
. 1.9

(adjacency linked lists) ( ),


(.. ,
). ,
, , , . , . 1.10 , . 1.9.
,
, .
,
(, ,
). , ,
, . . ,
, ,
, , ( ,
).
13

0 1 (..
). , i- j-
, , i- j- . . .

61

1.4


(weighted graph) , , . 1.11.
(weights) , (costs).
,

. , ,
.

. A, A [i, j] ,
i- j- . ,
A [i, j] - , . (weight
matrix) (cost matrix). . 1.11. ( ,
, .)
, ,
, ,
. 1.11.
a
1

5
7

b
4

a
a
b 5
c

b
5

c
1

d
a

b, 5

a, 5

c, 7

d, 4

a, 1

b, 7

d, 2

b, 4

c, 2

7
4

2
)

c, 1

. 1.11. ) )
)



: (connectivity) (acyclicity). .
, , (path) u v
(.. ) , u v. ,
(simple). (length) -

62

1.

, ,
. , ,
. , . 1.9 a f 3: a, c, b, f .
( ) 5: a, c, e, c, b, f .
. (directed path)
,
(u, v) , u
v. , . 1.9
a f : a, c, e, f .
(connected), u v
u v. , , : ,
( ) (
), .
, ,
. (connected component) 14
,
, . , ,
. 1.9 1.11, , , . 1.12,
, , , a
f . , . 1.12,
{a, b, c, d, e} {f, g, h, i} .
a

. 1.12.


. (subgraph) G = V, E G = V  , E  , , V  V
E E.
14

63

1.4

(highway system), ( , ?).


,
. (cycle) , . ,
, . 1.12, : f , h, i, g, f . ,
, (acyclic).
.

(tree) (, (free tree))


(. 1.13). , , , (forest).
(. 1.13).
a

j
)

. 1.13. ) )

, .
, ,
:
|E| = |V | 1.
, . 1.12, ,
, , .
,
, .


:
.

(root) (rooted

64

1.

tree). , , .. .
. . ,
, (.. ) , .. . 1.14 ,
.
i

a
d

b
c

e
f

c
h

f
)

i
)

. 1.14. ) )

, . .
, .
, , ( ),
(. 7.4),
(. 9.4). 2,
. ,
, (state-space trees).
: (backtracking) (branch-and-bound).
11.1 11.2.
v T : , , (ancestors) v. v . , ,
(proper ancestors), .
(u, v) , , v ( u = v), , u
(parent) v, v , (child) u. , , ,

1.4

65

(sibling). , , (leaf). ,
, (parental).
, v ,
(descendants) v. v (subtree) T c () v.
, , . 1.14, a;
d, g, f , h i ; a, b, e . b a; b
g; b d e;
b : {b, c, g, h, i}.
(depth) v
. (height) . , ,
. 1.14, c 2, 3. ,
, (
), , ,
. ( , .
, , .)


(ordered) , . ,
, . () (binary tree) ,
, (left child) (right child)
. , () , () .
. 1.15.
. 1.15 , . 1.15, . , ,
, , , , , , .
(binary search trees).
. .
, -

66

1.
9
12

5
7

10

1
4
)

. 1.15. ) )

(multiway search trees).


.
,
, ,
. h, n ,
:
log2 n  h  n 1.

, .
( , ..),
. . 1.16
, . 1.15.

. , . ,
,
( ).
, .
(rst child next sibling). ,
.
. . 1.17 ,
. 1.14. , .

67

1.4

null

12

null

null

null

null

10

null

null

null

. 1.16. , . 1.15

, .
,
45 , 1.17.
a

null

null

null

null

null

null

null

null

null

null

. 1.17. ) , . 1.14
; )


. (set)
(

68

1.

) , .
(, S =
= {2, 3, 5, 7}), ,
(, S = {n, n ,
10}). :
(.. ;
, ), (.. , , ,
), (..
,
).

. , U ,
, (universal set).
U n , S
n , (bit vector), i- 1 , i-
U S. , ,
U = {1, 2, 3, 4, 5, 6, 7, 8, 9}, S = {2, 3, 5, 7}
011010100.

. , 15 .
.
. ,
. , ,
.

. -,
, .
(multiset), (bag), .. ,
. -, ,
. , 15
. 7.

1.4

69

, .
,
, , . ,
, , ,
.

: ,
. , , (dictionary). ,

1.3. , .

.
. ,
( ) , .
, .

, n , . n

.
(set union). 9.2 .
, , , , .

.
(abstract data
type, ADT), .. ,
, , .
, ,
. , Pascal ( . [5]), - , C++ Java,
(classes).

70

1.

1.4
1. , ,
n.
) i- 1  i  n).
) i- (, ).
2. , , n . , ?
,
) ;
) .
3. ) , ,
:
push (a), push (b), pop, push (c), push (d), pop.
) , ,
:
enqueue (a), enqueue (b), dequeue, enqueue (c), enqueue (d),
dequeue.
4. ) , A . ,
, :
1) ;
2) , .. - ;
3) , ..
, .
)
.
5.
, .

71

6.
h, n :
log2 n  h  n 1.
7. ,
:
) () ;
) ;
) .
8. , n, ,
(, 50 )?
, .
9. .
)
.
) ,
.
)
.
10. ,
, .. ,
. (,
.)

,
. , ,
, .
. .
,
:

72

1.

;
.
: , , ,
, ,
.
, , .
, , ,
, , . 1.2.
, ,
, .
. ,
: , ,
. :
.
.
. .
, , , ,
(
), .
,
, , (ADT). , ,
, .
.

, ,
- .
, , : , ,
,
.
(Lord Kelvin) (18241907)
, , , , , .
(Albert Einstein) (18241907)

.
American Heritage Dictionary (analysis)
. , , 1.2,
.
, :
.
. -, ,
, .
-, , .
, .
.

74

2.

2.1
. , . , ,
.
2.2 : O ( ), ( ) ( ).
, .
2.3 , 2.1
.
,
.
2.4 , 2.1
.
, , . ,
, .

, 2.5 , .
800 , - , .

, , 2.4. ,

.
, 2.3. 2.4, , , .
( ), , 2.3 2.4. , ,
.

2.1

2.1

75

,
. : . (time
efciency) .
(space efciency) , . () :
. ,
. , , , .
, , ,
,
-.
, , , ,
. ,
, , ,
( ). ,
.

.


,
(..
, ). ,
,
..
n, 1 . . ,
, , , , ,
1


(, ,
).

76

2.

. n
p (x) = an xn + + a0
, .
,
.
, , , , . n n.
.
n. ,
, N .
,
. ,
, ,
,
(. 2 2.1).
. , , ?
,
, .
, .
, ,
(, ,
n ). b
n:
(2.1)
b = log2 n + 1.
.


,
. , , .. , -

2.1

77

.
, :
;
;
, ;
.
,
,
, .
, .
, , .
, , , (basic operation),
,
, , .
, . , . ,
() , .
. .
: . , 2 .

.
, , ,
, n. ,
, 2.3 2.4, .
. , cop
, C (n)
, .
2

RISC-.
, [[61], c. 185186].

78

2.

T (n) :
T (n) cop C (n) .
, .
C (n) ,
. , . ,
cop
. ,
n. :
, 10 ? , 10 .
, , C(n) = n (n 1)/2. , ? :
. , n
:
C(n) =

1
1
1
1
n (n 1) = n2 n n2 .
2
2
2
2

1
(2n)2
T (2n) cop C(2n)

21 2 =4
T (n)
cop C(n)
2n

,
cop , .
, 1/2 C (n) .
, (order of growth)
.


? ,

. , ,
, 1.1. ,

2.1

79

, .
, , ,
, . n .
. 2.1 ,
.
2.1.
n
10
102
103
104
105
106

log2 n
3.3
6.6
10
13
17
20

n
10
102
103
104
105
106

n log2 n
3.3 101
6.6 102
1.0 104
1.3 105
1.7 106
2.0 107

n2
102
104
106
108
1010
1012

n3
103
106
109
1012
1015
1018

2n
103
1.3 1030

n!
3.6 106
9.3 10157

, . 2.1,
. ,
. , ,
,

. , ,
, , ,
, (
):
loga n = loga b logb n.
,
,
: log n.
: 2n n!. ,
n. ( . 2.1
n > 102 .) , 2100
, (1012 )
, 4 1010 !
, 100! .

80

2.

, , , 4.5 (4.5 109 ) . 2n


n!, ,
. , ,
. , ,
.
,
,
.


, . 2.1.
, , n. log2 n
1, log2 2n = log2 2 + log2 n =
= 1 + log2 n. .
n log2 n , . n2 n3
, , (2n)2 =
= 4n2 (2n)3 = 8n. 2n ,
22n = (2n )2 . n!, ,
.


, n,
. , ,
. . , ( K) ,
n , K . ,
, .
. ,
. ( , , A [i] = K , , ,
.)

2.1

81

SequentialSearch (A [0..n 1] , K)
// : A[0..n 1] K
// :
//
A, K, 1,
//

i0
while i < n and A[i] = K do
i i+1
if i < n
return i
else
return 1
,
n.
, ..
, n : Cworst (n) = n.
(worst-case efciency)
n, .. n
, .
: , n
C (n) , Cworst (n)
. ( ,
.
.) ,
, . , ,
n
Cworst (n),
.
(best-case efciency)

n, .. n
, . , . ,

82

2.

n C (n) . ( ,
, .
n, .).
Cbest (n) . , n,
K. Cbest (n) = 1.
,
, . ,
. ,
. ,
, .
. , . ,
, . ,
,
.
, ,
, , ,
.
,
(average-case efciency). n.
. :
1. p, 0  p  1;
2. i- i.
( , ),
Cavg (n), . , , i- ,
p/n i. , , i.

2.1

83

, n,
(1 p).
 p
p
p
p
+ n (1 p) =
Cavg (n) = 1 + 2 + + i + + n
n
n
n
n
p
= [1 + 2 + + i + n] + n (1 p) =
n
p (n + 1)
p n (n + 1)
+ n (1 p) =
+ n (1 p) .
=
n
2
2

. , p = 1 (.. , , ), (n + 1)/2.
, , . p = 0 (.. ),
n,
n .
, , . ,

n , ,
, . ( ?)


. , , ,
. , ,
. ,
, [8, 65, 66, 67] [105].
- ? ! ,
, , . ,
. ,
,
, .

84

2.

,
.
,
(amortized efciency). , , .
, ,
n , , ,

, n. ( )
,
.
(Robert Tarjan).
. (
, , [117], [116] .)

9.2, .


,
.
,
n,
.
, . , .
. , .
( -

2.1

85

) ,
.
. 2.3 2.4 . ,
. .

2.1
1. : 1) ; 2)
; 3) , , .
) n .
) n!.
) n .
) .
) .
) n- .
2. ) n n, .
.
n. .
)
n n, .
3. , , .
?
4. ) 22 : 5 , 4 2 . ,

86

2.
, , ,
. ,
,
?
. ( [80], 18.)
) , 5 , ,
. , ,
, .
4 , 3. ,
10 .
,
, . (
[80], 48.)
5. ) (2.1), .
) .
) , , , ( )
n .
6. , , ,
n 1, n, , . ,
?
7. n , n
,
, n3 /3 , .
) ,
1000
500 .
) , , 1000 , . ,

.

2.2

87

8. ,

.

) n
) n
) n2
) log2 n
) n3
) 2n
9. , (
), .
) 100n2 0.01n3
) n (n + 1) 2000n2
) log2 n ln n
) log22 n log2 n2
) 2n1 2n
) (n 1)! n!
10. ,
, - . . ,
, .
,
.
, , ,
, 32 , 64
.
?

2.2

,
,
. ,
: O ( ), ( )
( ). , , , .
t (n) g (n) , . t (n)
.
, C (n). g (n)

88

2.

,
.


, O (g (n)) , n (.. ) , g (n).
:
 
 
 
n O n2 , 100n + 5 O n2 , n (n 1)/2 O n2 .
, , , g (n) = n2 . ,
, n2 . ,
 
 
 
/ O n2 , 0.00001n3
/ O n2 , n4 + n + 1
/ O n2 .
n3
, n3 0.00001n3 , , n2 .
n4 + n + 1.
, (g (n)), ,
n (.. )
, g (n). :
 
 
 
/ n2 .
n3 n2 , n (n 1)/2 n2 , 100n + 5
, (g (n)) ,
n ,
2
g (n). ,
 2  an + bn +
+ c a > 0  n . ,
n2 n2 +
+ sin n n2 + log n ( , ?).
,
, .
.

O-
1. , t (n) O (g (n)),
t (n) O (g (n)), n
t (n) ,
g (n), .. c
n0 , t (n)  cg (n) n  n0 .


2.2

89

. 2.1. , n.
cg(n)
t(n)

n0

. 2.1. O-:
t (n) O (g (n))


  , : 100n + 5 O n2 . ,
n  5 :
100n + 5  100n + n = 101n  101n2 .
, c n0 ,
, , , 101
5.
, ,
c n0 .
: n  1
100n + 5  100n + 5n = 105n.
, c = 105, n0 = 1.

-
2. , t (n) (g (n)),
t (n) (g (n)), n
t (n) ,
g (n), .. c
n0 , , t (n)  cg (n) n  n0 .


90

2.
t(n)
cg(n)

n0

. 2.2. -:
t (n) (g (n))

. 2.2.
 
, n3 n2 .
, n  0 n3  n2 . ,
c = 1 n0 = 0.

-
3. , t (n) (g (n)),
t (n) (g (n)), n t (n) ,
g (n), .. c1 c2 ,
n0 , c2 g (n)  t (n)  c1 g (n)
n  n0 .

. 2.3.

, n (n 1)/2

n2 . , (..
). n  0 :
1
1
1
1
n (n 1) = n2 n  n2 .
2
2
2
2
(.. ).
n  2 :
1
1
1
1
1 1
1
n (n 1) = n2 n  n2 n n = n2 .
2
2
2
2
2 2
4
, c2 = 1/4, c1 = 1/2,
n0 = 2.

2.2

91

c1g(n)
t(n)
c2g(n)

n0

. 2.3. -:
t (n) (g (n))

. (
1 2.1). ,
,
.
1. t1 (n) O (g1 (n)) t2 (n) O (g2 (n)), t1 (n) + t2 (n) O
(max{g1 (n), g2 (n)}). ( ).
. ( ,
a1 , b1 , a2 b2 : a1  b1 a2  b2 ,
a1 + a2  2 max {b1 , b2 }.) t1 (n) O (g1 (n)),
c1 n1 , n  n1
t1 (n)  c1 g1 (n).
, t2 (n) O (g2 (n)), c2 n2 , n  n2 t2 (n) 
 c2 g2 (n).
c3 = max {c1 , c2 } n  max {n1 , n2 }, .. . ,

92

2.

:
t1 (n) + t2 (n)  c1 g1 (n) + c2 g2 (n) 
 c3 g1 (n) + c3 g2 (n) = c3 [g1 (n) + g2 (n)] 
 c3 2 max {g1 (n) , g2 (n)} .
, t1 (n) + t2 (n) O (max {g1 (n) , g2 (n)}). O, c n0 :
c = 2c3 = 2 max {c1 , c2 } n0 = max {n1 , n2 }.

,
, ? ,
, , .. :

t1 (n) O (g1 (n))
t1 (n) + t2 (n) O (max {g1 (n) , g2 (n)}) .
t2 (n) O (g2 (n))
, , ,
. , . ,
. , , , ,
 n (n 1)/2
 2
( O n ), , , n 1
(.. O (n)), 



 2  2
O max n, n
=O n .



O, , ,
. , ,
.

2.2

3 :

0
t (n)
= c
lim
n g (n)

93

t (n) , g (n)
t (n) , g (n)
t (n) , g (n).

, t (n) O (g (n)),
t (n) (g (n)), t (n) (g (n)).
, ,
, ,
. , ,
, (LHopital)
t (n)
t (n)
= lim 
n g (n)
n g (n)
lim

(Stirling):
n n

n.
n! 2n
e

.
1. n (n 1)/2 n2 . (
, .)


1
1
1
n (n 1)/2
n2 n
1
= .
= lim
= lim 1
lim
2
2
n
n
2 n n
2 n
n
2
,
  , n (n 1)/2 n2 .


2. log2 n n. ( 1 .)

(log2 e) n1
log2 n
(log2 n)
n
= lim
lim
= 0.
= 2 log2 e lim
 = lim
1
n
n
n
n

n
n
( n)
2 n
3

, , . ,
, , , ,
O, , .

94

2.

, log2 n ,

log n
n. ( lim 2n = 0,
n

: log2 n o ( n). O o
.)

3. n! 2n . (
.) ,
:

 n
n n

2n ne
nn
n!
= lim
= lim 2n n n = lim 2n
= .
lim
n 2n
n
n
n
2n
2 e
2e
, 2n , n!
, : n! (2n ). , , ,
, n! ,
2n , , , .



,
, . ,
an a.
, . , ,
, . 2.2
.
2.2.

,
.

2.2

95

. 2.2

log n


(. 5.5). , (
).

, , n ,

n log n

n-log-n

(. 4),

n2

, ,
(. ).
, n n

n3

, ,
(. ).

2n

, , n . , ..

n!

, , n

, ,
. , , -

96

2.

, , ,
, , . ,
n3 , 106 n2 ,
, n 106 . . , , , ,
(. 4.5) .
, .
, . , , , ,
, , ,
. , ,
.

2.2
1. O, ,


(. 2.1):
) ;
) ;
) .
2. O, , ,
.
 
 
n (n + 1)
n (n + 1)
O n3
O n2
)
)
2
2
 3
n (n + 1)
n (n + 1)
n
(n)
)
)
2
2
3. (g (n)),
. (
g (n)). .

10

) 10n2 + 7n + 3
) n2 + 1
) 2n lg (n + 2)2 + (n + 2)2 lg (n/2)
) 2n+1 + 3n1

) log2 n

2.2

97

4. ) . 2.1 ,
. ,
log n, n, n log n, n2 , n3 , 2n , n! .
?
) ,
.
5. ( ).

(n 2)!, 5 lg (n + 100)10 , 22n , 0.001n4 + 3n3 + 1, ln2 n, 3 n, 3n


k
k1 +
6. ) , p (n) = ak n
 k+
 ak1 n
+ + a0 , ak > 0, n .
) , an
a.

7. ,
, , :
) t (n) O (g (n)), g (n) (t (n))
) (g (n)) = (g (n)), > 0
) (g (n)) = O (g (n)) (g (n))
) t (n) g (n), ,
t (n) O (g (n)), t (n) (g (n)),
.
8. :
) -;
) -.
9. , , , ,
.
) ,
, (n log n),
?
)
n, ?

98

2.
10. , , . , ,
, .
, .
,
O (n) , n .
( [88], 652.)

2.3

2.1
.
,
, .
1. n .
, .
.
M axElement (A [0..n 1])
// : A[0..n 1]
// :
//
A
maxval A[0]
for i 1 to n 1 do
if A[i] > maxval
maxval A[i]
return maxval
,
, .. n. , , for . :
(A [i] > maxval) (maxval A [i]). ? ,
, , . ( , n
.
, .)

2.3

99

C (n) ,
n. ,
. i, 1 n 1 . C (n)
:
C (n) =

n1


1.

i=1

, n 1 .
C (n) =

n1

i=1

1 = n 1 (n) .


.

1. ( ),
.
2. . ( , .)
3. ,
. ,
, , .
4. ,
4 .
5. ,
.
, .
, , ,
4

, , .
(. . 2.4).

100

2.

.
:
u


cai = c

i=l
u


(ai bi ) =

i=l

u


ai

(R1)

i=l
u


u


i=l

i=l

ai

bi

(R2)

:
u


1 = u l + 1, l  u ,
i=l

n
n


 
n (n + 1) 1 2
n n2
i=
i = 1 + 2 + + n =
2
2
i=0

(S1)

(S2)

i=1


, n1
i=1 1 = n1,
1, (S1) l = 1 u = n 1.
2. . , , .
.
U niqueElements (A [0..n 1])
// : A[0..n 1]
// : true,
//
A , false
//

for i 0 to n 2 do
for j i + 1 to n 1 do
if A[i] = A[j]
return false
return true
, , , .. n.

, . ,
n , ,
, ,
. .

2.3

101


,
Cworst (n) n. ,
(..
, )
: ) ; )
.

. j i + 1 n 1.
.
i 0 n 2. , :
Cworst (n) =

n2
 n1


1=

n2


i=0 j=i+1

i=0

n2


n2


i=0

(n 1)

i=0

[(n 1) (i + 1) + 1] =

n2


(n 1 i) =

i=0

i = (n 1)

n2

i=0

(n 2) (n 1)
=
2

 
(n 1) n 1 2
(n 2) (n 1)
=
n n2 .
= (n 1)2
2
2
2
n2
i=0 (n 1 i)
, (S2):
n2

i=0

(S2)

(n 1 i) = (n 1) + (n 2) + + 1 =

(n 1) n
.
2

, : , n , (n 1) n/2
.

3. A B n n C = AB, . C n n,
A B, .

102

2.
A

i$

j$

[i, j]

0  i, j  n 1
C [i, j] = A [i, 0] B [0, j] + + A [i, k] B [k, j] + + A [i, n 1] B [n 1, j] .
M atrixM ultiplication (A [0..n 1, 0..n 1] , B [0..n 1, 0..n 1])
//
// n n. ,
//
// : n n A B
// : C = AB
for i 0 to n 1 do
for j 0 to n 1 do
C[i, j] 0.0
for k 0 to n 1 do
C[i, j] C[i, j] + A[i, k] B[k, j]
return C

n. :
. ,
, . ,
(. 2.1). ,

, . , ,
, .
M (n), . , ,
.
, . k
0 n 1. ,


n1
i j, k=0 1. ,

2.3

103

M (n) :
M (n) =

n1
 n1
 n1


1.

i=0 j=0 k=0

(S1)
 (R1). , n1
k=0 1 = n
(?), :
M (n) =

n1
 n1
 n1

i=0 j=0 k=0

1=

n1
 n1

i=0 j=0

n=

n1


n2 = n3 .

i=0

.
. ,
?
n2 .
n- n-
. n . ,

n n2 = n3 . 2.1.2 (
, ).
:
T (n) cm M (n) = cm n3 ,
cm . ,
:
T (n) cm M (n) + ca A (n) = cm n3 + ca n3 = (cm + ca ) n3 ,
ca . ,
,
.

,
. ,
,
, ,

104

2.

.

. , .
, , .
4. ,
.
Binary (n)
// : n
// :
//
n
count 1
while n > 1 do
count count + 1
n n/2
return count
, n > 1,
, while.
, . ,
, .
,
. ,
, . n ,
log2 n. n > 1
log2 n + 1, (2.1)
n. ,
, .
, .


2.3

105

2.3
1. .
) 1 + 3 + 5 + 7 + + 999
) 2 + 4 + 8 + 16 + + 1024
n+1
n+1
n1



1
)
i
)
i (i + 1)
)
)

i=3
n


3j+1

j=1

i=3
n 
n


i=0

ij

i=1 j=1

2. .
n1
n1
2
2

)
lg i2
i +1
)
)

i=0
n


(i + 1) 2i1

i=1

i=2
n1
 i1


(i + j)

i=0 j=0

(g (n)) g (n).
3. , n x1 , . . . , xn

n
n
)2
xi
2
i=1 (xi x
, x
= i=1 ,
s =
n1
n

n


( ni=1 xi )2 /n
.
s =
n1
, , (
), .
2

2
i=1 xi

4. .
M ystery (n)
// : n
S0
for i 1 to n do
S S+ii
return S
) ?
) .
) ?

106

2.
) ?
)
. , , .
5. .
Secret (A [0..n 1])
// : n A[0..n 1]
minval A[0]
maxval A[0]
for i 1 to n 1 do
if A[i] < minval
minval A[i]
if A[i] > maxval
maxval A[i]
return maxval minval
)) 4.
6. .
Enigma (A [0..n 1, 0..n 1])
// :
//
A[0..n 1, 0..n 1]
for i 0 to n 2 do
for j i + 1 to n 1 do
if A[i, j] = A[j, i]
return false
return true
)) 4.
7. (. 3)
.
?
8. , 4,
n?
9.
n

i=1

i = 1 + 2 + + n =

n (n + 1)
.
2

2.4

107

10-
(Karl Friedrich Gauss) (17771855),
.
10. ,
.
GE (A [0..n 1, 0..n])
// :
//
A[0..n 1, 0..n] n n + 1
for i 0 to n 2 do
for j i + 1 to n 1 do
for k i to n do
A[j, k] A[j, k] A[i, k] A[j, i]/A[i, i]
) .
) - ? ,
?

2.4

, 2.1
.
,
.
1. F (n) = n! n. n! = 1 . . . (n 1) n =
= (n 1)! n n  1 0! = 1,
F (n) = F (n 1) n .
F (n)
//
// : n
// : n!
if n = 0
return 1
else
return F (n 1) n

108

2.

,
n, .
, M (n).5 n > 0
F (n) :
F (n) = F (n 1) n,
M (n), ,
n > 0:
M (n) = M (n 1) +

F (n1)


F (n1) n

, n > 0, F (n 1)
M (n 1) . F (n), .. F (n 1) n.
M (n),
. , M (n) ,
.. n. ,
,
.. n 1. ,
(recurrence relations), , , (recurrences). ,
. .
. , M (n) = M (n 1) + 1, ..
M (n), n.
, , . ( ?)
, (initial condition),
.. , . , ,
:
5

n = 0 , , F (n) ,
(. 2 2.4).

2.4

109

if n = 0 return 1
. -, n = 0,
n, , ,
M (n), 0. -,
, , , n = 0
. ,
, :
M (0) = 0.


n=0

n=0

, M (n) :
M (n) = M (n 1) + 1 n > 0,

(2.2)

M (0) = 0.
,
.
. , ,
F (n), :
F (n) = F (n 1) n

n > 0,

F (0) = 1.
M (n), ,
F (n), .
, M (n) (2.2).
.
(,
0 n = 0 1
?), . . ,
(method of backward substitutions). , ,
,

110

2.

:
M (n) = M (n 1) + 1 =

M (n 1) = M (n 2) + 1

= [M (n 2) + 1] + 1 =
= M (n 2) + 2 =

M (n 2) = M (n 3) + 1

= [M (n 3) + 1] + 2 =
= M (n 3) + 3.
, , (,
?), :
M (n) = M (n i) + i.
, .
, , .
.
n = 0, , ( ) ,
i = n:
M (n) = M (n 1) + 1 = = M (n i) + i = = M (n n) + n = n.
- ,
. ,
, , . , n
,
.
.
, n! . 2.1,
, n.
, .

, n!,
.

2.4

111


1. ( ),
.
2. .
3. ,
. ,
, , .
4. , ,
.
5. , ,
.
2.
, . .
, ( , ) n .

, .. ,
. ,
. ,
, ,
.
, . 2.4. n > 1 (
),
n 1 (
). , ,
n 1 (
). , n = 1,

.

. ,
n, . ,
M (n) n. n > 1

112

2.

. 2.4.

:
M (n) = M (n 1) + 1 + M (n 1) .
M (1) = 1,
M (n):
M (n) = 2M (n 1) + 1

n > 1,

(2.3)

M (1) = 1.

:
M (n) = 2M (n 1) + 1 =

M (n 1) = 2M (n 2) + 1

= 2 [2M (n 2) + 1] + 1 =
= 22 M (n 2) + 2 + 1 =
2

= 2 [2M (n 3) + 1] + 2 + 1 =
= 23 M (n 3) + 22 + 2 + 1

M (n 2) = 2M (n 3) + 1

2.4

113

, :
24 M (n 4) + 23 + 22 + 2 + 1.
,
i. :
M (n) = 2i M (n i) + 2i1 + 2i2 + + 2 + 1 = 2i M (n i) + 2i 1.
n = 1, , ,
i = n 1.
(2.3):
M (n) = 2n1 M (n (n 1)) + 2n1 1 =
= 2n1 M (1) + 2n1 1 = 2n1 + 2n1 1 = 2n 1.
, . ,
n (. 2.4.5).
, . ,
.
, , .
:
, .

,
. .
(, ,
), .
, . 2.5. ,
, :
C (n) =

n1


2l = 2n 1,

l=0

l , . 2.5.

114

2.

, ,
.

n
n1

n1

n2

n2

...

n2

...

2
1

n2

...

2
1

2
1

. 2.5. ,
.

3.
, 2.3.
BinRec (n)
// : n
// :
//
n
if n = 1
return 1
else
return BinRec(n/2) + 1

A (n), .
, BinRec (n/2),
A (n/2). ,

BinRec . n > 1
:
A (n) = A (n/2) + 1.
(2.4)
n = 1
,
:
A (1) = 0.
BinRec n/2 ,
n, 2.

2.4

115

n = 2k , (smoothness
rule), . , n = 2k n. ( ,
n = 2k , , n.) ,
,
n = 2k :



A 2k = A 2k1 + 1 k > 0,
 
A 20 = 0.
:







A 2k1 = A 2k2 + 1
A 2k = A 2k1 + 1 =



= A 2k2 + 1 + 1 =






A 2k2 = A 2k3 + 1
= A 2k2 + 2 =



= A 2k3 + 1 + 2 =


= A 2k3 + 3 =



= A 2ki + i =



= A 2kk + k.
,

A 2k = A (1) + k = k.
n = 2k , , k = log2 n.
A (n) = log2 n (log n) .
(. 2.4.6),
n , A (n) =
= log2 n.


116

2.

.

, . , .
,
, .

2.4
1. :
) x (n) = x (n 1) + 5, n > 1, x (1) = 0
) x (n) = 3x (n 1), n > 1, x (1) = 4
) x (n) = x (n 1) + n, n > 0, x (0) = 0
)
x (n) = x (n/2) + n, n > 1, x (1) = 1 (
n = 2k )
) x (n) = x (n/3) + 1, n > 1, x (1) = 1 (
n = 3k )
2.
F (n) n! .
3.
n S (n) = 13 + 23 + + n3 .
S (n)
// : n
// : n
if n = 1
return 1
else
return S(n 1) + n n n
) .
) .
4. .
Q (n)
// :
if n = 1
return 1

2.4

117

else
return Q(n 1) + 2 n 1
) ,
,
.
) , , .
) /, , .
5. ) , 1890 (Edouard Lucas), 64
. ,
, ,
. ( ,
, , ..
.)
) i-
(1  i  n)?
)
.
6. ) , ,
BinRec (n) n, log2 n.
) ,
(. 2.3, 4), .
7. ) 2n
n,
2n = 2n1 + 2n1 .
) , , .
) ,
.
) , ?

118

2.
8. .
M in1 (A [0..n 1])
// : A[0..n 1]
if n = 1
return A[0]
else
temp M in1(A[0..n 2])
if temp  A[n 1]
return temp
else
return A[n 1]
) ?
) .
9. 8, .
: M in2 (A [0..n 1]).
M in2 (A [l..r])
if l = r
return A[l]
else
temp1 M in2(A[l..(l + r)/2])
temp2 M in2(A[(l + r)/2 + 1..r])
if temp1  temp2
return temp1
else
return temp2
) .
) M in1 M in2 ?
,
?
10. () n n

a11 a1n

a21 a2n
A=
..
..

..
.
.
.
an1 ann

2.5

119

det A, .
n = 1 a11 . n > 1
det A =

n


sj a1j det Aj .

j=1

sj +1, j , 1, j
; a1j j- , Aj (n 1) (n 1),
A j- .
) ,
.
) ,
n! ?

2.5

, .. :
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .

(2.5)


F (n) = F (n 1) + F (n 2)

n > 1

(2.6)


F (0) = 0,

F (1) = 1.

(2.7)

1202 (Leonardo Fibonacci)


.
;
,
.
, . ,

( ).
n- F (n),
.

120

2.

n-

(2.6) , .
,
:
ax (n) + bx (n 1) + cx (n 2) = 0.

(2.8)

a, b ( a = 0), , x (n) ,
. (. 1 ),
(2.8) ,
. ,
, (2.8):
ar 2 + br + c = 0.

(2.9)

, (2.9) (2.8).
. (2.6)
:
F (n) F (n 1) F (n 2) = 0.
(2.10)
:
r 2 r 1 = 0.
:


1 1 4 (1)
1 5
r1,2 =
=
.
2
2
, (2.10) , 1:


n
n
1 5
1+ 5
+
.
F (n) =
2
2

2.5

121

(2.7). ,
.
0 1 (.. n, )
, , 0
1 (.. F (0) F (1) (2.7)):

0
0
1 5
1+ 5
+
= 0,
F (0) =
2
2


1
1
1 5
1+ 5
+
= 1.
F (1) =
2
2


,
,
:


+
= 0
 
 

1 5 /2 = 1
1 + 5 /2 +

(, = ),
:
1
= ,
5

1
= .
5

,
1
F (n) =
5


n
n

1+ 5
1 5
1
1

= n n
2
2
5
5

1+ 5
1.61803
=
2

(2.11)

1
5
=
= 0.61803.6
2

, (2.11), ,
n- (2.5),
!
6

(golden ratio).

. .

122

2.

(2.11) :
, F (n) (
?), .. F (n) (n ).
, 1 0.
n, , n
. , (2.11)
n F (n) ,
1
5
. , n :
1
F (n) = n , .
5

(2.12)


,
. ,
, ,
. ,
, . ,
, , .
, , ,
.
(2.6) (2.7)
F (n).
F (n)
// n-
// ,
//
// : n
// : n-
//

if n  1
return n
else
return F (n 1) + F (n 2)

2.5

123

,
, ? ,
. , . A (n)
, F (n). ,
F (n 1) F (n 2), , , A (n 1)
A (n 2). , . , A (n)
:
A (n) = A (n 1) + A (n 2) + 1 n > 1,
A (0) = 0,

(2.13)

A (1) = 0.

A (n) A (n 1) A (n 2) = 1 (2.10), . (inhomogeneous recurrences).


(
),
.
, (2.13) :
[A (n) + 1] [A (n 1) + 1] [A (n 2) + 1] = 0.
B (n) = A (n) + 1, :
B (n) B (n 1) B (n 2) = 0,
B (0) = 1,

B (1) = 1.

(2.10),
F (n). , ,
B (n) , F (n), ,
, .. F (n) . B (n) = F (n + 1)

1
A (n) = B (n) 1 = F (n + 1) 1 = n+1 n+1 1
5
, A (n) (n ). n, b = log2 n + 1, ,
,
.

124

2.

,
(2.13). , ,
n. ( ?) ,
,
, . , n = 5 . 2.6.
,
. ,
.
F (5)
F (4)

F (3)

F (3)

F (2)

F (1)

F (2)

F (1)

F (1)

F (2)

F (0)

F (1)

F (1)

F (0)

F (0)

. 2.6. , n = 5

, , .
F ib (n)
// n-
//
//
// : n
// : n-
//
F [0] 0
F [1] 1
for i 2 to n do
F [i] F [i 1] + F [i 2]
return F [n]
, n1 . , ,
n, ,

2.5

125

b n. ,
:
(. 2.5.6).
n- (2.12). ,
, n .

 b  n 1 ,
(n) = 2 . , . , 5 6
(log n) =
= (b). , n-
.
, , ,
.
, n-
(log n), .

 
n
F (n 1)
F (n)
0 1
, n  1,
=
F (n)
F (n + 1)
1 1
.

2.5
1. Web-, ,
.
2. ,
n )
1 (n
5
(2.6) n > 1 (2.7) n = 0 1.
3. , Java int long , , 231 1 263 1.
n, n-
, :
) int
) long

126

2.
4. , n , , , . , ,
, : 111,
12 21 [118].
5. n- F (n), .
C (n) Z (n) F (1) F (0),
. ,
) C (n) = F (n)
) Z (n) = F (n 1)
6. F ib (n),
(1) .
7. , n  1

 
n
F (n 1)
F (n)
0 1
.
=
F (n)
F (n + 1)
1 1
8.
,
F (n) F (n 1)?
9. ) : F (n + 1) F (n 1)[F (n)]2 = (1)n
n  1.
) , . 8 8 (,
, F (n) F (n), [F (n)]2 ). ,
.
, . 8 8 = 64 ,
13 5 = 65 . .

2.6

127

10.
n- , ) F (n); )
F ib (n) ) F ib (n)
. n,
1 .

2.6
2.3 2.4 ,
.
([45, 46, 93] [105]),
. , ,
. 2.2,
.
.
.

1. .
2. M (
).
3. ( ,
..).
4. , ( ),
.
5. .
6. ( ) .
7. .
. , .
, ,

128

2.

, ,
, , . ,
,
.
, , , ,
.
, ,
( ), . ,
, , . ,
,
,
.
, . , time UNIX.
, (tstart )
(tf inish ),
(tf inish tstart ).7 C C++ clock,
Java currentTimeMillis() System.
. -,
,
. ,
. -, ,
( ).
, , . -,
, (, , UNIX), , , , ,
. ,
, ( UNIX
7
(ticks),
, .

2.6

129

(user time)
time).
,
(
, ), ,
. ,
,
, , , . , , , ,
. , ,
;
.
,
,
. , . ,
, , ,
.
,
. , (
), ( ,
), . (,
1000, 2000, 3000, . . . , 10 000 500, 1000, 2000, 4000, . . . , 128 000), (,
).
,
. , ,
M (2n)/M (n) M ( ) ,
(. 2.2). ,

130

2.

. ,
, ,
.
, : .
,
, ,
. (
,
.) ,
.
. , ,
. , ,
, . ,
.
.
() ,
0 1. () , . , x ,
0  x < 1, y = l + x (r l) ,
l r 1 (l < r).
,
() . (linear congruential method).
Random (n, m, seed, a, b)
// n
//
// : n
//
m, seed, a, b
// : r1 , . . . , rn
//
,

2.6

131

//
0 m 1
// :
0 1
//

//

//

r0 seed
for i 1 to n do
ri (a ri1 + b) mod m
, . , (. [66]): seed
(
); m ( m
2w , w ); a
0.01m 0.99m , ,
a mod 8 = 5; b 1.
, , , . , .
,
.
. , M (n)/g (n),
g (n) . (g (n)), , ,
n. (, - , , , ,
(g (n))).
M (2n)/M (n) ,
. 2.2, , ,
2, 4 8, .
,
. (. 2.7).
.
, , (. 2.7). (n lg n)

132

2.

 
n2 (. 2.7), . ,
. ,
.. loga M (n) M (n) (
2 10). ,
M (n) can logb M (n) logb c + n logb a.

n
)

. 2.7. : ) , ) , )

, . ,
, M (n)/g (n) c
, ,
M (n) cg (n) n.

2.6

133

, ,
n, . ( (extrapolation)
(interpolation), .) , .
, , ,
, .
.
, ,
. , ,
,
.

2.6
1. (
), .
SortAnalysis (A [0..n 1])
// : A[0..n 1] n
//

// :
//

count 0
for i 1 to n 1 do
v A[i]
j i1
while j  0 and A[j] > v do
count count + 1
A[j + 1] A[j]
j j1
A[j + 1] v
return count
? , ; ,
.

134

2.
2. ) 1
( )
20 1000, 1500, 2000, 2500, . . . , 9000,
9500.
) .
) ,
10 000 .
3. 2,
, .
4.
:

1000

2000

3000

4000

5000

6000

7000

8000

9000 10000

11966 24303 39992 53010 67272 78692 91274 113063 129799 140538
5. ?
6. (lg lg n) (lg n)?
7. ) , gcd (m, n) 1  n 
m  100.
) k 1  n  m  100,
k gcd (m, n).
8. n Davg (n),
gcd (n, 1), gcd (n, 2), . . . ,
gcd (n, n). ,
Davg (5) =

1
(1 + 2 + 3 + 2 + 1) = 1.8.
5

Davg (n)
.
9.
(. 1.1).

2.6

135

10. gcd (m, n), 1.1.



.
. , ,
, .
(, ,
..)
. :

, .
. , ,
. , , , .
1970- . 1981 . (Ronald
Baecker) . (D. Sherman) [9, 10]. (
)
.

. ,
, (. 2.8). , ,
.

, , .

136

2.


, (. 2.9). ,
, .
. ,
(, ) (,
), . BALSA [24],
TANGO [112] ZEUS [23]; ( ) [91].
, .
, ,
.
Java World Wide Web
. Internet . Web ,
algorithm animation algorithm visualization.

, (Peter Gloor) [42], Animated Algorithms
.
1. .
2. .
3. .
4. .
5. .
6. .
7. .
8. .
9. ( )
.
10. .

2.6

137

Applet Viewer: SortDemo.class


Applet
PROBLEM

Sort

Sort

ALGORITHM

Step

Back

Reset

Selection Sort

Exit

Applet Viewer: SortDemo.class


Applet
PROBLEM

Sort

Sort

ALGORITHM

Step

Back

Reset

Selection Sort

Exit

Selection Sort has completed $ Number of comparisons: 45, Number of exchanges: 8

. 2.8.

138

2.

Applet Viewer: SortDemo.class


Applet
PROBLEM

Sort

Sort

ALGORITHM

Step

Back

Reset

Selection Sort

Exit

Applet Viewer: SortDemo.class


Applet
PROBLEM

Sort

Sort

ALGORITHM

Step

Back

Reset

Selection Sort

Exit

. 2.9.

139

.
.

. ,
,
.
,
. . ,
, ,
. ,
, .

. , ; ,
.

, . ,
.
.

, , .
,
.
,
, O-, - .

140

2.
, , ,
n log n, , .

.


.
.
, . .

, ,
(
). . ,
.
.
( ).


, .
(Edward Lytton) (1803-1873)
(Leila), II, I
- .
(Robert Byrne),

,
.
.
,
. .


,
.

, ,
.. .
: , !.
.
: an
a n.
,
, . ( ,
an mod m .)
an = a a . . . a .
n

142

3.

an a , 1, n .
:
gcd (m, n) ( 1.1) , ( 2.3).
. ( ,
?)
,
,
. -, , . (, , ,
.) , , ,
n , . -, (, , ,
, ) . -,
, , , ,
. -, ,
. , , ,
,
.

3.1

, : n
(, , , ),
. 1.3,
, . , , . .
, , , :
? , ,
, .

3.1

143

, ,
.



( , ).
, ,
n1 , .. . , i- (0  i  n 2)
ni Ai :

n 1 .
, ,
.
SelectionSort (A [0..n 1])
//
// : A[0..n 1]
//

// : A[0..n 1],
//

for i 0 to n 2 do
min i
for j i + 1 to n 1 do
if A[j] < A[min]
min j
A[i] A[min]
. 3.1
: 89, 45, 68, 90, 29, 34, 17.
.
n . A [j] < A [min].
:
C (n) =

n2
 n1

i=0 j=i+1

1=

n2

i=0

[(n 1) (i + 1) + 1] =

n2

i=0

(n 1 i).

144

3.
| 89

45

68

90

17 | 45

29

34

17

68

90

29

34

89

17

29 | 68

90

45

34

89

17

29

34 | 90

45

68

89

17

29

34

45 | 90

68

89

17

29

34

45

68 | 90

89

17

29

34

45

68

89 | 90

. 3.1. 89, 45, 68, 90, 29,


34, 17.
, ..
.
, .

2 2.3 ( ).
, , , ,

n2
n2
 n1


(n 1) n
.
1=
(n i 1) =
C (n) =
2
i=0 j=i+1

i=0

,
 
n2 . , ,
(n), , n 1 i.
.


, . ,
.
, , n 1 . i- 0  i  n 2)
:

3.1

145

BubbleSort (A [0..n 1])


//
// : A[0..n 1]
//

// : A[0..n 1],
//

for i 0 to n 2 do
for j 0 to n 2 i do
if A[j + 1] < A[j]
A[j] A[j + 1]
89, 45, 68, 90, 29, 34, 17 . 3.2.
89

45

45

89

45

68

68

90

68
89

90
?

90

29

34

17

29

34

17

29

34

17

45

68

68

29

90

45

68

89

29

34

90

45

68

89

29

34

17

|90

34

17

|90

34

17

|90

17

|90

89

|90

45

45

68
68

89

29

29
89

45

68

29

34

89

45

68

29

34

17

34

17
?

17

. 3.2. 89, 45, 68, 90, 29, 34, 17.



.

n. ,
:
C (n) =

n2
 n2i

i=0

n2

i=0

j=0

1=

n2


[(n 2 i) 0 + 1] =

i=0

(n 1 i) =

 
(n 1) n
n2 .
2

146

3.

.
:
Sworst (n) = C (n) =

 
(n 1) n
n2 .
2

. ,
, :
, , ,
( 3.1.9). ,

, 

2
n .
, ,
.
:
, .

3.1
1. ) ,
.
) , , .
2. ) an ,
, n?
n?
) an mod m, a > 1, n ,
an ?
3. 46 2.3
?
4. )
p (x) = an xn + an1 xn1 + + a1 x + a0

3.2

147

x0 .
 
) n2 ,
.
) ,
?
5. E, X, A, M , P , L, E
.
6. ? ( 1.3.)
7.

 2
n , ?
8. E, X, A, M , P , L, E
.
9. ) ,
,
.
) .
) , .
10. ?

3.2


. .
, .


, ,

(. 2.1).
,
( ) , ( ).

148

3.

: ,
, ,
. ; , .
SequentialSearch2 (A [0..n] , K)
//
//
// : A n K
// :
//
A[0..n 1],
//
K; ,
//
1
A[n] K
i0
while A[i] = K do
i i+1
if i < n
return i
else
return 1
, : , , .

, () ( ) . , 2.1
, ,
, .
, .


1.3: n, , m (m  n),
(pattern); , . , i ,
ti = p0 , . . . , ti+j = pj , . . . , ti+m1 = pm1 :

3.2
t0 . . .

ti . . . ti+j


p0 . . . pj

149

. . . ti+m1 . . . tn1 T

. . . pm1
P

,
.
, , :
, , m (
), .
,
, . , ,
n m (
0 n 1). , .
,
.
BruteF orceStringM atch (T [0..n 1] , P [0..m 1])
//
// : T [0..n 1] n ,
//
;
//
P [0..m 1] m ,
//

// : ,
//

//
, ;
//
, 1
for i 0 to n m do
j0
while j < m and P [j] = T [i + j] do
j j+1
if j = m
return i
return 1
. 3.3.
, .
: m , n m + 1 ( 3.2.6

150

3.
N
N

O
O
N

B
T
O
N

O
T
O
N

T
O
N

T
O
N

T
O
N

T
O
N

T
O

D H

. 3.3. .
, ,

). ,
(nm).
,
(
). , . :
,
, .. (n + m) = (n). .
. (R. Boyer) . (J. Moore)
7.2 , . (R. Horspool).

3.2
1. ,
) ;
) p (0  p  1).
2. 2.1, , ( ,
),

p (n + 1)
+ n (1 p)
Cavg (n) =
2
p . n p 0  p  1),
Cavg (n).

3.2

151

3.
, 2.1 3.2.
4. , , ,
GANDHI
THERE_IS_MORE_TO_LIFE_THAN_INCREASING_ITS_SPEED
( , 47
.)
5. ( ) , ,
, 1000 ?
) 00001
) 10000
) 01010
6. n m,
, .
?
7. , , .
8. ,
(, Q Z ),
, ,
?
9. ,
, . ,
.
, , .
10. ,
.
. (
).
10 10 , ,
. 5 , : (2 ),
(3 ), (3 ), (4 -

152

3.
) (5 ).1
, . , .
. , .
(
, ).

3.3


, .
, , , .


,
n , . ,
. (x, y),
Pi = (xi , yi ) Pj = (xj , yj ) ( )
!
d (Pi , Pj ) = (xi xj )2 + (yi yj )2 .

:
. ,
,
(Pi , Pj ), i < j.
1

, , :
4 , 3 ,
. ,
. . .

3.3 . . .

153

BruteF orceClosestP oints (P )


// : P , n  2
//
P1 = (x1 , y1 ), . . . , Pn = (xn , yn )
// : index1 index2
//

dmin
for i 1 to n 1 do
for j i + 1 to n do
// sqrt
d sqrt((xi xj ) 2+
// ,
+(yi yj ) 2)
if d < dmin
//
dmin d; index1 i; index2 j;
return index1, index2

.
, , , ,
. . , ,
. , . :
! (
, ?) ,
(xi xj )2 +
+(yi yj )2 . , ,
, . ,
.
,
d sqrt((xi xj ) 2 + (yi yj ) 2)

d (xi xj ) 2 + (yi yj ) 2
. :
C (n) =

n1


n


i=1 j=i+1

2=2

n1

i=1

2


= (n 1) n n

(n i) = 2 [(n 1) + (n 2) + + 1] =
.

4 , n log n.

154

3.


.
, .
1. ( ) , P Q,
, P Q
.


. 3.4. ) . ) ,

. 3.4 ; , , (
2 ), , . , . 3.4,
, , , , .
.
n , .
,
. (D. Harel) [48]: n
? ,
2
, .. .

3.3 . . .

155

, ?. . . , . , , , .
, , , . ,
(. 3.5).

. 3.5.


, , ,
.
2. S , S. ( , S ,
S.)

S , , S
S. S , , , .
, , ;
, , ,
. . 3.6
.
.
1. S, n > 2
( ), S. ( ,
,
S.)


156

3.
P6

P7
P2

P8

P3

P4

P5

P1

. 3.6. 8 ,

S, n . , . (extreme
points). , , . , ;
; , . 3.6, P1 , P5 , P6 , P7 P3 .
,
. - (simplex method).
, .. n ,
(. 3.3.9
6.6). , .
,
,
. ,
.
? , :
. , ,
, : -

3.3 . . .

157

, Pi Pj , n ,
,
, .3 ( , . 3.6.)
, .
. -, ,
(x1 , y1 ) (x2 , y2 ) , ax + by = c, a = y2 y1 , b = x1 x2 c = x1 y2 y1 x2 .
-, :
ax + by > c, ax + by < c
(, ax + by = c).
, , , ,
ax + by c ,
.
.

O n3 : n (n 1)/2
ax + by c n 2
. ; .

3.3
1. ,
, n x1 , . . . , xn
?
2. )
P1 = (x1 , y1 ) P2 = (x2 , y2 ). ,

dM (P1 , P2 ) = |x1 x2 | + |y1 y2 | .
, dM ,
:
1) dM (P1 , P2 )  0 P1 P2 , dM (P1 , P2 ) = 0
, P1 = P2 ;
2) dM (P1 , P2 ) = dM (P2 , P1 );
3

, . ,
, .

158

3.
3) P1 , P2 P3
dM (P1 , P2 )  dM (P1 , P3 ) + dM (P3 , P2 ).
) ,
(0, 0) 1.
.
) :
, , dE , , dM ?
3. k , P  =
= (x1 , . . . , xk ) P  = (x1 , . . . , xk )
"
# k

    #
(xs xs )2 .
d P ,P = $
s=1


k- , ?
4. ( ):
) ;
) ;
) ;
) .
5.
n ?
6.
n .
7. ,
, ,
?
8. , .
9. :

159

3.4

3x + 5y
x+y 4
x + 3y  6
x  0,

y  0.

) (feasible region) , .. ,
.
) .
) :
,
.

3.4

, ( )
. , , , .
: ,
, , ,
.
(exhaustive search) .
, ,
, ,
(, ). , , ,
. 5,
, . :
, .


100 ,

160

3.

. :
n , ,
. , ,
. (Hamiltonian circuit)
. ( , .
(William Rowan Hamilton) (18051865),
.)
, n + 1 vi0 , vi1 , . . . , vin1 , vi0 ,
, n 1
. , ,
( , , ?) , ,
n 1 , . . 3.7
.
. 3.7 ,
. ,
. , ,
, , B C, , B C (
). . (n 1)!/2,
n, . , ,
, , ,
. , , ,
n .


. n
w1 , . . . , wn v1 , . . . , vn , , W .
, ,
.
, ,

161

3.4
2

d
1

a > b > c > d > a

I = 2 + 8 + 1 + 7 = 18

a > b > d > c > a

I = 2 + 3 + 1 + 5 = 11

a > c > b > d > a

I = 5 + 8 + 3 + 7 = 23

a > c > d > b > a

I = 5 + 1 + 3 + 2 = 11

a > d > b > c > a

I = 7 + 3 + 8 + 5 = 23

a > d > c > b > a

I = 7 + 1 + 8 + 2 = 18

. 3.7.

,
, . . 3.8
.
n ,
, , (..
),
. , . 3.8, . 3.8.
n- 2n , (2n ), ,
.
,
.
NP- (N P -hard problems). N P - ,
. , -

162

3.

W=7
V = 42

W=3
V = 12

W=4
V = 40

W=5
V = 25

{1}
{2}
{3}
{4}
{1, 2}
{1, 3}
{1, 4}
{2, 3}
{2, 4}
{3, 4}
{1, 2, 3}
{1, 2, 4}
{1, 3, 4}
{2, 3, 4}
{1, 2, 3, 4}


0
7
3
4
5
10
11
12
7
8
9
14
15
16
12
19
)


0
42
12
40
25
36

52
37
65

. 3.8. ) . ) (
)

, , . ,
11.2 11.2, ( )

163

3.4

( ) , .
,
11.3.


, , n , n , (.. ,
).
i- j- C [i, j] i, j = 1, . . . n.
:
, .
C [i, j]:

1
2
3
4

1
9
6
5
7

2
2
4
8
6

3
7
3
1
9

4
8
7
8
4

, C.
,
,
. , . ,
, . ,
.
.
n

j1 , . . . , jk , i- i = 1, . . . , n) ,
i- (.. , i- ). ,

2, 3, 4, 1 ,
, ,
. , n . ,

164

3.

1, 2, . . . , n,

.
. 3.9. .

C =

9
6
5
7

2
4
8
6

7
3
1
9

8
7
8
4

<1,
<1,
<1,
<1,
<1,
<1,

2,
2,
3,
3,
4,
4,

3,
4,
2,
4,
2,
3,

4>
3>
4>
2>
3>
2>

c = 9 + 4 + 1 + 4 = 18
c = 9 + 4 + 8 + 9 = 30
c = 9 + 3 + 8 + 4 = 24
c = 9 + 3 + 8 + 6 = 26
c = 9 + 7 + 8 + 9 = 33
c = 9 + 7 + 1 + 6 = 23

..

. 3.9.

n!, ,
, n. , ,
(Konig) (Egervary)
(., , [69]).
. , ( )

.
. , .
, (
, ),
. , , ,
.

3.4
1. ) ,
, ?
) , , 1 . ,

3.4

165

1) ;
2) ;
3) ;
4) .
2.
.
3. , ,
, , .
?
4. , .
5. , .
6. .
7. (partition problem): n
,
(,
).
.
, .
8. (clique problem): G k , k,
.. k .
.
9. ,
.
10. n 1
n2 , n n,
,
, .
) ,


 n ,
n n2 + 1 /2.
)
n.
) Internet
.

166

3.
)
,
.

.
;
.
,
.
:
,
;
;
;
.
.
, ,
, .
, , .
, , , .


, .
, ,
.
. (18181883)

( ), , .
. ,
,
.
1.
, .

2. ( , - ).
3. .
. 4.1, , , ,
( ,
).
a0 , . . . , an1 .
n > 1, : n/2 n/2
(, n = 1 a0 ).
( , .. ), ,
:

 

a0 + + an1 = a0 + + an/21 + an/2 + + an1 .

168

4.

1
n/2

2
n/2

. 4.1.

n ?
, , ( ),
.
,
, . , , , , - .
. .
, , ,
.
: n n/2.
n
n/b, a ( a b -

4.1

169

; a  1 b > 1). , n
b,
T (n):
T (n) = aT (n/b) + f (n) ,
(4.1)
f (n) ,
. ( a = b = 2 f (n) = 1.) (4.1) (general divide-andconquer recurrence). , T (n)
a b f (n).
, ,
(. ).
 
. (4.1) f (n) nd ,
d  0,
 
d

a < bd
 n

T (n) nd log n a = bd

 logb a 
n
a > bd
( O .)

, A (n) n = 2k
A (n) = 2A (n/2) + 1.
, a = 2, b = 2 d = 0. ,
a > bd ,




A (n) nlogb a = nlog2 2 = (n) .
, ,
. , , ,
, .

4.1

(mergesort)
.
A [0..n 1] , A [0.. n/2 1] A[n/2 ..
n 1], .

170

4.

M ergesort (A [0..n 1])


// A[0..n 1]
// : A[0..n 1]
//

// :
//
A[0..n 1]
if n > 1
A[0..n/2 1] B[0..n/2 1]
A[n/2..n 1] C[0..n/2 1]
M ergesort(B[0..n/2 1])
M ergesort(C[0..n/2 1])
M erge(B, C, A)
. ( )
. , ,
, .
, ,
. ,
, .
M erge (B [0..p 1] , C [0..q 1] , A [0..p + q 1])
//
// : B[0..p 1]
//
C[0..q 1]
// : A[0..p + q 1],
//
B C
i 0; j 0; k 0
while i < p and j < q do
if B[i]  C[j]
A[k] B[i]; i i + 1
else
A[k] C[j]; j j + 1
k k+1
if i = p
C[j..q 1] A[k..p + q 1]
else
B[i..p 1] A[k..p + q 1]
8,3,2,9,7,1,5,4 . 4.2.
? , n
2.

4.1

171


8 3 2 9 7 1 5 4

8 3 2 9

8 3

7 1 5 4

2 9

3 8

7 1

2 9

5 4

1 7

4 5

1 4 5 7

2 3 8 9

1 2 3 4 5 6 7 8

. 4.2.

C (n) :
C (n) = 2C (n/2) + Cmerge (n)

n > 1, C (1) = 0.

Cmerge (n),
. ,
1.
, (,
). ,
Cmerge (n) = n1,
Cworst (n) = 2Cworst (n/2) + n 1 n > 1, Cworst (1) = 0.
, Cworst (n) (n log n) (?). n = 2k :
Cworst (n) = n log2 n n + 1.

172

4.

, , 1
, .
, .
,
,
.

4.1
1. ) n .
) , ?
) , .
) , .
2. ) n .
) , .
) , .
3. ) an , a > 0, n .
) ( n = 2k )
, .
) , .
4. 2,
, , , .
, ?
10.2, log2 n!
n log2 n 1.44n.
1

4.1

173

5. .
) T (n) = 4T (n/2) + n, T (1) = 1.
) T (n) = 4T (n/2) + n2 , T (1) = 1.
) T (n) = 4T (n/2) + n3 , T (1) = 1.
6. E, X, A, M ,
P , L, E .
7. ?
8. ) , . ( , n = 2k .)
)
, , n = 2k .
)
, 4.1
. , ?
9. , ,
..
.
10. . L, . 2n 2n .
, , .

174

4.2

4.

(quicksort) , . ,
,
. A [0..n 1]
(partition), s
A [s], s A [s]:
A [0] . . . A [s 1]


A[s]

A [s]

A [s + 1] . . . A [n 1] .


A[s]

, A [s] ,
A [s] (, ).
Quicksort (A [l..r])
//
// : A[l..r] A[0..n 1],
//

//
l r
// : A[l..r],
//

if l < r
s P artition(A[l..r]) // s
Quicksort(A[l..s 1])
Quicksort(A[s + 1..r])
A [0..n 1] , , A [l..r]
(0  l < r  n 1) . , . (pivot). (
).
: p = A [l].
. ,
, .
. , ,
, , , , , ,

4.2

175

.
. , , ,
, ,
, , ,
.
, ,
. i j , .. i < j,
A [i] A [j]
i j:
i
p

j
p

. . .

, .. i > j,
, A [j]:
p

, , ,
.. i = j, p (?). , :
i=j
p

=p

(i > j),
A [j] i  j.
, .
P artition (A [l..r])
//
//
// : A[l..r] A[0..n 1],
//

//
l r (l < r)
// : A[l..r];
//

//

p A[l]
i l; j r + 1
repeat
repeat

176

4.

ii+1
until A[i]  p
repeat
j j1
until A[j]  p
swap(A[i], A[j])
until i  j
swap(A[i], A[j]) // i  j
swap(A[l], A[j])
return j
, i
. i ,
, A [0..n 1] , i n. ,
.
. 4.3.
,
, , n + 1, , n, (?).
,
. , Cbest (n),
:
Cbest (n) = 2Cbest (n/2) + n n > 1, Cbest (1) = 0.
, Cbest (n) (n log2 n). n = 2k
Cbest (n) = n log2 n.
, , 1 .
, , , .. , ! , A [0..n 1]
A [0],
A [1], A [0],
0:
i
[0] [1]

j
. . .

[n-1]

, n + 1 A [0]
, , A [1..n 1].

4.2
0

177

5
5
5

3
3
3

1
1
1

5
2

3
3

3
i

1
2

3
3

2
1

4
4
4

1
1

4
4

2
5

8
8

9
9

7
7
I=0, r=7

s=4

4
4

I=0, r=3

I=5, r=7

s=1

s=6

4
4
I=0, r=0

I=7, r=7

s=2

ij

I=5, r=5

I=2, r=3

I=2, r=1

I=3, r=3

4
i

8
7
7

7
8

9
9

9
)

. 4.3. Quicksort. ) ( ). ) Quicksort l r s

A [n 2..n 1].
Cworst (n) = (n + 1) + n + + 3 =

 
(n + 1) (n + 2)
3 n2 .
2

, .
,
n, Cavg (n). ,

178

4.

s (0  s  n1)
1/n, :
1
[(n + 1) + Cavg (s) + Cavg (n 1 s)] n > 1,
n
n1

Cavg (n) =

s=0

Cavg (0) = 0,
Cavg (1) = 0.
,
, ,
. ,
:
Cavg (n) 2n ln n 1.38n log 2 n.
,
38% , . ,
,
, ( n log n, 6),
, (C. A. R. Hoare)2 .
,
. , , (, , ,
), ,
( ).
(R. Sedgewick) [103],
2025%.
, , . ,
, 5.6.
2


. :
,
.
: .
! [51]

4.2

179

4.2
1. E, X, A, M ,
P , L, E . .
2. , 4.2:
) ,
, .. i = j,
p.
) , j
,
, i.
)
, , ?
3. ?
4. n ,
, . ? , .
5. :
) , , ?
)
, , ?
6. , .
)
, ,
?
) .
7.
.
8. n , .
, .
9. ,
. -

180

4.
,
.
10. . n
n . , ( )
,
. , ,
.
(n log n) [94].

4.3


.
K A [m]. , .
, K < A [m], , K > A [m]:

K = 70,

3

14

27

31

39

42

55

70

74

81

85

93

98

10

11

12

14

27

31

39

42

55

70

74

81

85

93

98

1
2
3

, , . .

4.3

181

BinarySearch (A [0..n 1] , K)
//
// : A[0..n 1],
//
, K
// : , K,
//
1,
l 0; r n 1;
while l  r do
m (l + r)/2
if K = A[m]
return m
else if K < A[m]
r m1
else
l m+1
return 1
. ,
, , ,
.. K A [m] , K,
A [m].

n ? , , n,
. Cw (n) .
,
( , ).
, , , , ,
Cw (n):
(4.2)
Cw (n) = Cw (n/2) + 1 n > 1, Cw (1) = 1.
( , n/2
, (4.2)).
2.4, (4.2) ,
n = 2k
- .


(4.3)
Cw 2k = k + 1 = log2 n + 1.

182

4.

, (4.3) n = 2k n:
Cw (n) = log2 n + 1 = log2 (n + 1) .

(4.4)

,
Cw (n) = log2 n + 1 (4.2)
n. ( n
4.3.3.) n , n = 2i,
i > 0. (4.2) n = 2i
Cw (n) = log2 n + 1 = log2 2i + 1 = log2 2 + log2 i + 1 =
= (1 + log2 i) + 1 = log2 i + 2.
(4.2) n = 2i
Cw (n/2) + 1 = Cw (2i/2) + 1 = Cw (i) + 1 = (log2 i + 1) + 1 = log2 i + 2.
, .
(4.4) . -, ,
(log n) ( , , ,
). -, , : ,
n
log2 n. -, 2.1,
,
%
& n. , (4.4), log2 103 + 1 = 10
(
% , &
) 1000 , log2 106 + 1 = 20 !
?
,
:
Cavg (n) log2 n.
(
yes
no (n) log (n + 1), (n) log2 n 1 Cavg
Cavg
2
.)

4.3

183

(. 10.2),
,
(. 5.6 7.3)
, ()
, ! ,
, . :
, ,
(., , [15]). ,
( 11.4).
, .
, . ,
. , ,
, , .
. ,
, .
, 5.5. ? ,
, .

4.3
1. 3 14 27 31 39 42 55 70 74 81 85 93 98 .
) ?
) ,
.
) , ; , .
) , ; , 14 ,
, .

184

4.
2. Cw (n) = Cw (n/2) + 1 n > 1,
Cw (1) = 1 n = 2k .
3. )
log2 n + 1 = log2 (n + 1)

n  1.

) , Cw (n) = log2 n + 1
(4.2) n.
4. , 100 000
.
5.
. ? (, , .)
6. , ..
, L U (L  U )?
?
7. .
8. , ,  =.
(
).
9.
, 8.
10. 42 , 6 . ,
, .
, , .

4.4

,
. T , ,

4.4

185

TL TR , , , .
(. 4.4). ( 1.4.)

TL

TR

. 4.4.

, , , .
. ,
. , ,
1 ( ). ,
1. ,
:
Height (T )
//
// : T
// : T
if T =
return 1
else
return max{Height(TL ), Height(TR )} + 1
n (T ) T . , , , A (n (T )),
, . A (n (T ))
:
A (n (T )) = A (n (TL )) + A (n (TR )) + 1

n (T ) > 0, A (0) = 0.

186

4.

( , ?), ,
. ? , (, , ).
, T = ,
.
, , .
, , ,
. , . 4.5
, (external); ( ) (internal). .

. 4.5. ) . ) . ,

, ,
.
, ,
n . . 4.5 ,
( ),
x n:
x = n + 1.

(4.5)

n  0.
, n = 0
. , x = k + 1
0  k < n . T

4.4

187

n x , nL xL
, , T ,
nR xR T ,
. n > 0, T ,
, , , n = nL + nR + 1.
,
T :
x = xL + xR = (nL + 1) + (nR + 1) = (nL + nR + 1) + 1 = n + 1,
.
Height, , , ,
C (n) = n + x = 2n + 1,

A (n) = n.
(preorder traversal), ,
(inorder traversal), (postorder traversal).
, ..
, .
, ( ).
, .

( ).
( ).
, ,
.
, , , . ,
. , ,
,
5.6.

188

4.

4.4
1.
( , 0 1 ).
?
2. , . .
a
b
d

c
e

3.
( , ).
, , .
4. , ?
,
.
5. , ,
,
, ?
6. ?
.
7. ,
.
. (,
, 0.)
8. .

4.4

. . .

189

Leaf Counter (T )
//
// : T
// : T
if T =
return 0
else
return Leaf Counter(TL ) + Leaf Counter(TR )
? , ; ,
.
9. (internal path length) I
, . ,
(external path length) E ,
. , E = I + 2n, n
.
10. .
.

4.5

: .
; .


, ,
, 100 .
, ,

190

4.

,
.
. ,
n- , n
n ,
n2 . ( ,
, ,
.) , ,
n2 , .
.
, ,
, , 23 14. :
23 = 2 101 + 3 100 14 = 1 101 + 4 100 .
:
 


23 14 = 2 101 + 3 100 1 101 + 4 100 =
= (2 1) 102 + (3 1 + 2 4) 101 + (3 4) 100 .
, , 322, , . ,
, , (2 1) (3 4),
:
3 1 + 2 4 = (2 + 3) (1 + 4) (2 1) (3 4) .
, , , .
a = a1 a0 b = b1 b0 c

c = a b = c2 102 + c1 101 + c0 ,

c2 = a1 b1 ;
c0 = a0 b0 ;
c1 = (a1 + a0 ) (b1 + b0 ) (c2 + c0 ) a
b c2 c0 .

4.5

. . .

191

n- a b,
n . ,
, .
a a1 , a0 . b
b1 b0 .
a = a1 a0 a = a1 10n/2 + a0 , b = b1 b0 b = b1 10n/2 + b0 .
, ,
a b



c = a b = a1 10n/2 + a0 b1 10n/2 + b0 =
= (a1 b1 ) 10n + (a1 b0 + a0 b1 ) 10n/2 + (a0 b0 ) =
= c2 10n + c1 10n/2 + c0 ,

c2 = a1 b1 ;
c0 = a0 b0 ;
c1 = (a1 + a0 ) (b1 + b0 ) (c2 + c0 ) a
b c2 c0 .
n/2 , c2 , c1 c0 . , n 2,
n- . , n ,
n .
? n- n/2- , M (n)
M (n) = 3M (n/2)

n > 1, M (1) = 1.

n = 2k








M 2k = 3M 2k1 = 3 3M 2k2 = 32 M 2k2 =




= = 3i M 2ki = = 3k M 2kk = 3k .
k = log2 n,
M (n) = 3log2 n = nlog2 3 n1.585 .
( , alogb c = clogb a .)

192

4.

, , ,
, . (Brassard) (Bratley)
([22], .7071) , 600 .
Java, C++ Smalltalk, ,
.


, ,
, .
1969 (V. Strassen) [113].
, , ,
C A B 2 2
7, 8 ,
, 3 2.3. ,
:
 
 


a00 a01
b00 b01
c00 c01
=

=
c10 c11
a10 a11
b10 b11


m3 + m5
m1 + m4 m5 + m7
=
m2 + m4
m1 + m3 m2 + m6

m1 = (a00 + a11 ) (b00 + b11 )


m2 = (a10 + a11 ) b00
m3 = a00 (b01 b11 )
m4 = a11 (b10 b00 )
m5 = (a00 + a01 ) b11
m6 = (a10 a00 ) (b00 + b01 )
m7 = (a01 a11 ) (b10 + b11 ) .
, 22 18 /,
. ,
2 2 ,
.

4.5

. . .

193

A B nn, n . ( n
,
, .) A B C
n/2 n/2
:
 
 


A00 A01
B00 B01
C00 C01
=

.
C10 C11
A10 A11
B10 B11
, . , C00
A00 B00 + A01 B10 M1 + M4 M5 + M7 , M1 , M4 , M5 M7 ,
. , n/2 n/2
.
. M (n) ,
n n ( n ),
M (n):
M (n) = 7M (n/2)

n > 1, M (1) = 1.

n = 2k ,








M 2k = 7M 2k1 = 7 7M 2k2 = 72 M 2k2 = =




= 7i M 2ki = = 7k M 2kk = 7k .
k = log2 n, :
M (n) = 7log2 n = nlog2 7 n2.807 ,
, n3 , .
, A (n),
. n > 1 18 n/2 n/2;
n = 1 , .
:
A (n) = 7A (n/2) + 18 (n/2)2

n > 1, A (1) = 0.

194

4.

(. 4.5.8),
.
 , , A (n)

nlog2 7 . , ,
. ,

, 

  nlog2 7 , ,
n3 .

n n O (n ) .

(Winograd) [31]

 2.376(Coopersmith)
.
n
. -
, . , ,
n2 ,
. ,

, , , .

4.5
1.
n- ?
2. 2101 1130
.
3. ) alogb c = clogb a , 4.5.
) nlog2 3 3log2 n
M (n)?
4. ) 10n M (n) ?
) , n 2, M (n)
, ,
( ). ?
5. n- ? ( .)

4.6
195

6. ,
2 2.
7.

1 0 2 1
0 1 0 1

4 1 1 0 2 1 0 4

0 1 3 0 2 0 1 1 .

5 0 2 1
1 3 5 0
m = 2 , ..
2 2
.
8. , ( , n 2).
9. (V. Pan) [85] ,
70 70
143 640 .
( ) .
10. , ,
.
.

4.6

3.3 : . ,



 
n2 O n3 .
, .

196

4.


P1 = (x1 , y1 ) , . . . , Pn = (xn , yn ) S, n
( , n ).
, x ( ,
O (n log n), , ).
S1 S2 , n/2 , x = c , n/2 (
c
x ).

S1 S2 . d1 d2 S1 S2 , .
d = min {d1 , d2 }. , d
S1 S2 ,

. , . , ,
2d,
d (. 4.6). C1 C2
, .
P (x, y) C1 C2 ,
P , d.
, y
[y d, y + d]. ,
, C2
d . (, d  d2 , d2
.)
. 4.6.
, C1 C2 y (
). ,
, (.
Merge 4.1). C1 ,
C2 2d,
, P
C1 . M (n), ,
O (n).

4.6

. . .

197

x=c

d1

d2

d
)

d
P

. 4.6. ) . ) , P

T (n)
n :
T (n) = 2T (n/2) + M (n) .
O- ( a = 2, b = 2 d = 1, ,
T (n) O (n log n).
, O (n log n). -

198

4.

, ,
(n log n) (. [90], . 188).


, , n ,
, .
,
(quickhull) , .
P1 = (x1 , y1 ) , . . . , Pn (xn , yn ) n .
, x,
x
y. , ,
P1 ) Pn ),

(. 4.7). P1 Pn , P1 Pn .
: S1 , , S2 ,

p
. ( , p3
1 p2 ,
p1 p2 , p1 p2 p3 .
,
, .)

P1

Pn

. 4.7.

S1
P1 Pn , ,
, .. ,
S1 . (upper hull).
, , S2 ,
(lower hull). ,
S ,

4.6

. . .

199

,
.
, ; . -,
Pmax S1 , P1 Pn (. 4.8).
, Pmax , Pmax P1 Pn
. (, ,
P1 Pn , S1 ,
Pmax .) , S1

P1 Pmax ; P1 Pmax S1,1 .

S1 , Pmax Pn , Pmax Pn
S1,2 . , ,
.  P1 Pmax Pn
. , S1,1 S1,2 ,
S1 .

Pmax

Pn

P1

. 4.8.

, . , : p1 = (x1 , y1 ), p2 = (x2 , y2 ) p3 = (x3 , y3 )


,  p1 p2 p3

'
'
' x1 y 1 1 '
'
'
'
'
'x2 y2 1' = x1 y2 + x3 y1 + x2 y3 x3 y2 x2 y1 x1 y3 ,
'
'
' x3 y 3 1 '
, p3 = (x3 , y3 )


p
1 p2 . , -

200

4.

, ,
, .
,

: (n log n) , n2 .
, (
3.3), , , ,
(n log n) (., , [90]).

4.6
1. ) , ..
n .
.
) ?
2.
.
C1 C2
y . , ,
n = 2k .
3.
.
4. Web .
?
5. P S ,
P ,
S.
S S.
) ?
) Web . ,
?

201

6. , Pmax .
7.
, S1 .
?
8.
?
9. ,
.
10. .

( ) , (
), .
, ,
, ,
.
T (n) T (n) = aT (n/b) + f (n).
.
, .
,
. (n log n)
;
. .
, . -

202

4.
.
n log n ,
.
O (log n).
,
.
, , ,
.
.
, n- n1.585 .
22 7 ,
, , .
n n n2.807 .
,
.

, , ,
, .
,
-,
,
.
(E. Cobham Brewer),
(Dictionary of Phrase and Fable), 1898.

( ) . ,
(), (
). :
;

;
.
.
(. 5.1), , -
.
an
. n n 1 an = an1 a.
, f (n) = an -

204

5.

n1

. 5.1. ( )


(
f (n 1) a n > 1
f (n) =
a
n = 1

(5.1)

a n 1 (, ,
,
).
5.15.4.

.
. (
?) . 5.2.
. n an
, .. an/2 ,

205

n/2

. 5.2. ()


2
an = an/2 .
,
n. n , an1
,
a. , :


n/2 2

n ,
 a

2
n
(5.2)
a =
a(n1)/2 a n, 1,

a
n = 1.
an (5.2) ,
, O (log n),
, .
,
-

206

5.

n/2:

(
an/2 a n/2

an =
a

n > 1,
n = 1.

(5.3)

, (5.3), (?),
(5.2) .

5.5 .
, .
, . .
,
gcd (m, n) = gcd (n, m mod n) .

( , ),
, .
5.6.

5.1


A [0..n 1]. , , , A [0..n 2],
n 1: A [0]   A [n 2].
, A [n 1]? , ,
,
A [n 1].
. -, ,
, A [n 1],
. -, , ,
A [n 1], A [n 1] . ,
; ,

(?). (straight insertion sort), .

5.1

207

A [n 1] .
(binary insertion sort).
.
, , .. . . 5.3,
A [i] ( A [1] A [n 1]) i (
). ,
.

. 5.3. :
A [i]

.
InsertionSort (A [0..n 1])
//
// : A[0..n 1] n
//

// : A[0..n 1],
//

for i 1 to n 1 do
v A[i]
j i1
while j  0 and A[j] > v do
A[j + 1] A[j]
j j1
A[j + 1] v
. 5.4.
A [j] > v. (
j  0? . ,
: (. 5.1.5) .)

208

5.
89 | 45

68

90

29

34

17

45

89 | 68

90

29

34

17

45

68

89 | 90

29

34

17

45

68

89

90 | 29

34

17

29

45

68

89

90 | 34

17

29

34

45

68

89

90 | 17

17

29

34

45

68

89

90

. 5.4. .
;

, , . A [j] > v , .. j = i 1, . . . , 0. v = A [i],


, A [j] > A [i] j = i 1, . . . , 0. (, , i-
, A [i], i ,
.) , A [0] > A [1] ( i = 1), A [1] > A [2]
( i = 2), . . . , A [n 2] > A [n 1] ( i = n 1). ,

.
Cworst (n) =

i1
n1


1=

i=1 j=0

n1

i=1

i=

 
(n 1) n
n2 .
2

, ,
(. 3.1).
A [j] > v
. , A [i 1] 
 A [i] i = 1, . . . , n 1, ..
. ( , , ; , ,
4.) ,

Cbest (n) =

n1

i=1

1 = n 1 (n) .

5.1

209

,
, ,
. ,
. , , (, 10 ).
,
, .
10%.
(. 5.1.8). ,
, , ..
 
n2
n2 .
Cavg (n)
4


. ,
(shellsort), . (D.L. Shell) [107],
(. 5.1.10).

5.1
1. n
, . , ,
, . ?
?
2. )

n .
)

.

210

5.
3.
n .
( (power set) S S, S.)
4. E, X, A, M , P ,
L, E .
5. )
,
j  0
?
)
, ?
6.

 ? O n2 ,
?
7. .
InsertSort2 (A [0..n 1])
for i 1 to n 1 do
j i1
while j  0 and A[j] > A[j + 1] do
swap(A[j], A[j + 1]) // A[j] A[j + 1]
j j1
? , .
8. A [0..n 1] n . ( , .) (i, j)
(inversion), i < j A [i] > A [j].
) n ,
?
.
) ,
Cavg (n)

n2
.
4

5.2

211

9. A [i]
A [0]   A [i 1] . .
10. ,

.
hi ,
h1 > > hi > > 1; 1. (
, . , 1, 4, 13, 40, 121,
. . . , ,
.)1
) S, H, E, L, L, S, O, R,
T , I, S, U , S, E, F , U , L.
) ?
) , , ,
102 , 103 , 104
105 ,
.

5.2

,
. , (, ,
) (
1

, 16
8,4,2,1. 8
2 , 8: (a1 , a9 ) , . . . , (a8 , a16 ), . 4 4
(a1 , a5 , a9 , a13 ) , . . . , (a4 , a8 , a12 , a16 ), . 8 (a1 , a3 , . . . , a15 ) , (a2 , a4 , . . . , a16 ),
. . .

212

5.

), ,
. 1.4.
1.3, . , , .

(depth-rst search, DFS) (breadth-rst search, BFS).
( ),
.


,
. , . ( , . , .
.)
,
.
( , )
. , ,
.
. , ,
.
.
, (.. ), , (..
).

(depth-rst search forest). . , ,
. (tree edge),
.
, , (..
). (back edge),
,

213

5.2
a

g
a

e
c

d3, 1
c2, 5
a1, 6

f
b

e6, 2
b5, 3
f4, 4

j10, 7
i9, 8
h8, 9
g7, 10

e
)

. 5.5. . ) . ) (
, ( ), ( )). )
( ,
)

. . 5.5 ,
.
.
DF S (G)
//
// : G =
V, E
// : G,
//

//

V 0 ( )
count 0
for () v V do
if v 0
dfs(v)
dfs (v)
// ,
// v,
// count
count count + 1; v count
for () w V , v do
if w 0
dfs(w)

214

5.

DF S , , . ,
, , (
, . 5.5, ).
? ,
, . ,
(|V |2 ),
(|V | + |E|), |V | |E| , .
, ,
. , .
,

(
). , . ,
. , .
, .

.2 ,
: ,
( ), , ( ). ,
.

.
, ,
. , . , , . ,
2

1970- (John
Hopcroft) (Robert Tarjan) [53, 117]; ( )
.

5.2

215

(?).

. , , .
u v (, d a . 5.5),
, v u
, u v.
,
, , ,
. ( (articulation point),
.)


(
), .
, , ,
, , ..,
,
.
( !).
, .
, , ;
.
,
(breadth-rst search forest).
.
, , ,
(tree edge). , (.. ),
(cross edge). . 5.6 ,
.
.

216

5.
h

a
a

e
c

a1 c2 d3 e4 f5 b6
g7 h8 j9 i10

f
i

j
)

. 5.6. . ) . ) ,
(..
( )). ) (
, )

BF S (G)
//
// : G =
V, E
// : G,
//

//
,
//

V 0,
count 0
for () v V do
if v 0
bfs(v)
bfs (v)
// , v
//
// count
count count + 1
v count v
while () do
for () w V , ,
do
if w 0
count count + 1
w count
w

217

5.2

, : (|V |2 )
(|V | + |E|)
.
,
FIFO (rst-in-rst-out,
), , . ,
, ,
. ,
;
, , , ,
.
,
, . , , , ,
. , ,
, ,
, .
, .
. ,
, . 5.7, a b c g a g.
, ,
,
(., , [32]).
a
a

d
e

d
)

g
)

. 5.7. . ) . )
,
a g

218

5.

. 5.1 .
5.1. (DFS) (BFS)
DFS

BFS

(
)

, ,



2
|V |

, ,


2
|V |

(|V | + |E|)

(|V | + |E|)

5.2
1.
f

) , (, ,

).
) a
, . ,
( ), , ( ).
2. , |E| O (|V |),
?

219

5.2

3. G n m . .
) ( ,
) ?
)
.
4. a
, 1 .
5. ,
.
6. ) ,
.
) , ? ,
. ,
, .
7. ,
) ;
) .
8. (bipartite), X Y ,
X Y . (
, ,
, ; 2- (2colorable). , ,
.
x1

y1

x3

y2

x2

y3

) ,
, .

220

5.
) ,
, .
9. , 1) ; 2) ,
.
10. , , , , ,
, .
) .

) ,
, ?

5.3

, . ,
. (directed graph, digraph) ,
(., , . 5.8). :
. : 1) 2)
( )
.
,
, . , ,
. 5.8, , . 5.8,
,
. (tree edge)

5.3

221

e
)

. 5.8. ) . )
; a

(ab, bc, de), (back edge) (ba) ,


(forward edge) (ac) , , (cross edge) (dc),
.
, .
, . (
,
, .)
, (directed
acyclic graph, dag).
,
.
.
. {C1, C2, C3, C4, C5}, .
: C3 C1 C2, C4 C3, C5
C3 C4. .
?
,
,
(. 5.9). -

222

5.
C1

C4
C3

C2

C5

. 5.9.
,

, ,
, , , (
?).
(topological sorting).
, , .
, . ,
, ; .. ,
. , ,
, ( )
.
:
,
(.. ).
,
. ,
, .
? v ,
u,
u v ( (u, v) ).
, u
v, .
. 5.10 , . 5.9. . 5.10,
: , .
.

5.3

223


C1

C4
C3

C2

C5

C51
C42

:
C5, C4, C3, C2, C1

C33

C14 C25

C2

C1

C3

C4

C5

. 5.10. ) , . ) ;
. )

:
(source) , ,
,
. ,
. ,
(. 5.3.6). .
. 5.11.
C1

C1

C3
C2

C4

C4

C4

C2

C5

C3

C2

C3

C4

C5

C5

C4

C5

C3

C5

C5

: C1, C2, C3, C4, C5

. 5.11.
.
,

, ,
, , . , ,
.

. -

224

5.

, , , . , , .
.
, , , . ,
,
,
(Critical Path Method, CPM) .. (
) (Program Evaluation and Review Technique, PERT).

5.3
1.
.
a
c

d
f

2. ) ,
, .
) n
?
3. ) ?
)
, , ?
4. ,
?
5.
1.

5.3

225

6. ) ,
.
) ,
( , ), , ?
?
) ( ,
) , ?
?
7. , ,
, O (|V | + |E|)?
8. .
.
9. (strongly connected), u v v u.
,
. (strongly connected components). ,
, . ( ) .
1.
( ).
2. .
3. ,
(, , ) .
, .
)
.

226

5.
a

c
e

) ?
.
) ?
10. (transitive closure) n T n n , T [i, j] = 1,
i j, T [i, j] = 0
(1  i, j  n).
.

5.4


. , . ,
. , 3 . , . , ,
,
, ,
. ( ,
, .)
,
.

5.4

227


. , 1 n.
n- {a1 , . . . , an }.
n! {1, . . . , n}?
(n 1)! . ,
, n n
n 1 . (?), n (n 1)! =
= n!. ,
{1, . . . , n}.
n
. , n 1 2 . . . (n 1)
{1, . . . , n 1}.
n = 3 . 5.12.

12

21

123 132 312 321 231 213


. 5.12. ( )

,
(minimal-change requirement):

( ,
. 5.12).
, ,
. , 3.4 . , ,

228

5.

, (?).
n n. ,
k .
, :

3 2 4 1.
k (mobile), . ,

3 2 4 1 3 4 , 2 1 . ,
(JohnsonTrotter algorithm) .
JohnsonT rotter (n)
//
//
// : n
// : {1, . . . , n}

1 2 . . .
n
while () k do
k
k ,
k

, k
n = 3 (
):

1 2 3

1 3 2

3 1 2

3 2 1

2 3 1
2 1 3.


,
, .. (n!). , n,
, , ,
.
, , , ; , , n (n 1) . . . 1.
,

229

5.4

(lexicographic order), .. , ,
:
123

132

213

231

312

3 2 1.

, a1 a2 . . .
an1 an ? an1 < an , (, 1 2 3 1 3 2). an1 > an ,
an2 . an2 < an1 ,
, (n 2)- , .. an2 , an1 an ,
(n 1) n an2 , an1
an . , 1 3 2 2 1 3, 2 3 1 3 1 2.

ai ai+1 , ai < ai+1 (, ,
ai+1 > > an ). ,
ai , .. min {aj | aj > ai , j > i}, i; i + 1-
n- ai , ai+1 , . . . , an ,
i, .
.


, 3.5 , , .
, . 2n
A = {a1 , . . . , an } (
(power set)).

. A = {a1 , . . . , an }
, an , , .
, {a1 , . . . , an1 }, an {a1 , . . . , an1 }.
,
{a1 , . . . , an1 }, {a1 , . . . , an },

230

5.

an .
{a1 , a2 , a3 } . 5.2.
5.2.
n
0
1
2
3

{a1 }
{a1 }
{a1 }

{a2 }
{a2 }

{a1 , a2 }
{a1 , a2 }

{a3 }

{a1 , a3 }

{a2 , a3 }

{a1 , a2 , a3 }

,
. 2n
n- A = {a1 , . . . , an } 2n b1 , . . . , bn n. , bi = 1, ai
, bi = 0 (
1.4, ). ,
000 , 111 , .. {a1 , a2 , a3 }.
, 110 {a1 , a2 }.
, n,
0 2n 1, . , n = 3
:
000 001 010
011
100
101
110
111
{a3 } {a2 } {a2 , a3 } {a1 } {a1 , a3 } {a1 , a2 } {a1 , a2 , a3 }
, , , ( {0, 1}), . , (squashed order), , aj ,
, a1 , . . . , aj1
(j = 1, . . . , n), , , ,
. 5.2. , ,
(. 5.4.6).

, ,

231

5.4


( , ,
, ). ; ,
n = 3
000

001

011

010

110

111

101

100.

(Gray code). ,
[25].

5.4
1. , 25- , ? ?
2. {1, 2, 3, 4}
) , ;
) ;
) .
3.
.
4. , (B. Heap) [50].
HeapP ermute (n)
//
// : n A[1..n]
// : A
if n = 1
write A
else
for i 1 to n do
HeapP ermute(n 1)
if n
A[1] A[n]

232

5.
else
A[i] A[n]
) n = 2, 3 4.
) .
) HeapP ermute.
5. A =
= {a1 , a2 , a3 , a4 } .
6.
.
7. 2n
n.
8. 2n
n,
.
9. )
n = 4.
)
n.
10. k , n-
(.. k- n-
).
?

5.5

, ,
, .
( 4.3) ( ).
,
.
. -

5.5

. . .

233

, ,
. , .



, . n
. , (
), . , .
, , , ,
.3 (
, .)

n n/2 , ,
n . ,
;
. , ,
, .
,
,
(), .
W (n),
:
W (n) = W (n/2) + 1

n > 1, W (1) = 0.

. ,
( ).
,
.
: W (n) = log2 n.
, .
: , .
, ,
3

,
, , n . ,
10.2.

234

5.

n/3 . ( . ! 5.5.3,
, .)

, ,
log3 n, , log2 n ( ,
?).

-

, - (multiplication
a` la russe), (Russian peasant method). n
m , ,
n. , n ,
, n/2,

n
n m = 2m.
2
n , :
n1
2m + m.
nm=
2
1 m = m
,
n m. 50 65 . 5.13. ,
( ) ,
. , ( . 5.13) ,
m,
n, . 5.13.
, ,
, . ,
, ( ) XIX ,
. (, 1650 . .. . [26],
. 16.) ,
, .

5.5

235

. . .
n
50
25
12
6
3
1

m
65
130
260
520
1040
2080
2080

(+130)

(+1040)
+ (130 + 1040) = 3250
)

n
50
25
12
6
3
1

m
65
130
260
520
1040
2080

130

1040
2080
3250

. 5.13. 50 65 -


(Josephus problem),
(Flavius Josephus), , 6670 .
47 (Jotapata), ,
, 40 . , . ,
(.. )4 ,
, . ,
, .
, n , 1 n, .
1 ,
. ,
J (n). , n = 6 (. . 5.14),
2, 4 6, 3
1, J (6) = 5. n = 7,
2, 4, 6 1 ( 1
), 5 3 ( 3
), J (7) = 7.
n . n ,
.. n = 2k, ,
. : , 4
, .
. , . . . .: , 1986
(. 4347). . .

236

5.
12
61
5
41
)

11
21

32

61

21
32
41

52
)

. 5.14.
) n = 6 ) n = 7. , .
J (6) = 5
J (7) = 7

3 2, 5 3
.. , , ,
2 1.
, , ,
J (2k) = 2J (k) 1.
n > 1, .. n = 2k + 1.
.
1, k. ,
,
2 1 (. . 5.14). , n

J (2k + 1) = 2J (k) + 1.
( J (1) = 1)? ,
, . , , J (n),
.
.
[45] (
). , n:
J (n) n ! ,
J (6) = J (1102 ) = 1012 = 5 J (7) = J (1112 ) = 1112 = 7.

5.5

. . .

237

5.5
1. log2 n,
, .
2. (ternary search)
A [0..n 1]: n = 1,
K ; K A [n/3] , K
, A [2n/3] ,
.
) ?
)
( , n = 3k ).
) n = 3k .
) .
3. )
. (, n, 3.)
)

n = 3k .
) n? (
n.)
4. - 26 47.
5. ) ,
- n m m n?
) -.
6. -.
7. J (40) n = 40.
8. , 1 n,
2.
9.
) J (n) n = 1, 2, . . . , 15.
) n .

238

5.
) J (n) n.

5.6

,
, .
.
.


(selection problem) k- n . k-
(order statistic). , k = 1 k = n
.
, k = n/2, .. , , . ,
(median), . , k- ,
k-
. .
O (n log n).
, , ,
- . , ( )
: ,
p, , p:
ai1 . . . ais1 p ais+1 . . . ain .
 


p

p

, 4.
? s
. s = k, p . (-

5.6

239

0, , ,
s = k 1.) s > k, k-
, s < k, (k s)-
. , ,
,
, .. .
.
k ,
s = k.
1. : 4, 1, 10, 9, 7, 12, 8,
2, 15. k = 9/2 = 5,
. , ,
1 9.
,
4, .. :
4 1 10 9 7 12 8 2 15
2 1 4 9 7 12 8 10 15
s = 3 < k = 5, :
9 7 12 8 10 15
8 7 9 12 10 15
s = 6 > k = 5, :
8 7
7 8
s = k = 5, : , 8
2, 1, 4 7, 9, 12, 10 15.

? , , ,
,
. ,
,
:
C (n) = C (n/2) + (n + 1) ,

240

5.

, (. 4), (n).
( , , ),
,
,
. , 
.

2
n . ,
[19], .
,
, : k n k
, k-
.


, (interpolation search). ,
( ,
),
, . .
, ,
, .
. A [l]
( ) A [r] ( ), ,
( , ). , v ,
( ) x ,
(l, A [l]) (r, A [r]), y v (. . 5.15).
,
(l, A [l]) (r, A [r]), y v x,

*
)
(v A [l]) (r l)
.
(5.4)
x=l+
A [r] A [l]
, , . , ( , ) A [l] A [r], ,
. ( -

5.6

241

A[r]

A[l]

. 5.15.

); (5.4)
, v. , v
A [l] A [r], (5.4) (?).
v A [x] ( ),
l
x 1, x + 1 r, , v A [x]
. , ,
, .
,
log2 log2 n+1 n . ,
n (. 5.6.6).
, (?).
(R. Sedgewick) [102], ,
, , ,
,
, . , 11.4
,
, .

242

5.


. , ,
, ,
,
. (, v) ,
. .
, v K (r).
, , .
, , v < K (r),
, v > K (r). ,

.
; ,
.
. ,
,
(. 5.16).
a0

a0
a1

..

.
an2

a1

an2
an1

..

an1
)

. 5.16. ) )

, an1 n , (n) . , (log n). ,


, , n
, 2 ln n 1.39 log 2 n.
,

5.6

243

,
.

5.6
1. ) m n n,
gcd (m, n) ?
) ,
.
2. )
.
) .
3. , .
4. , .
5. .
6. ) n, log2 log2 n + 1 6.
) , :
1) log log n o (log n)
2) log log n (log n)
3) log log n (log n)
7. )
. ?
)
?
8. ) . ?
)
?

244

5.
9. . . n . 1
4 . , . ,
( ).
10. . n . , , .

,
. ( Interactive Mathematics Miscellany and Puzzles [20]).
.



.
,
(), ( ).

:
, (, );
, (, );
(, ).
.
(n2 ) ,
, , .
.

.
.
: (|V |2 ) -

245
(|V | + |E|)
.
, . ,
.
,
, .. .
. ;

.

.
, .
,
.
, - ,
. ,
.

.
, ,
,
.


. . . .
(Charles M. Schulz) (19222000),

, .
,
. , ,
,
,
.
, (. 6.1).
.

.
, .. ,
.

. 6.1.

. 6.1 ,
. ,
, , . ,
; -

248

6.

. 6.2
.

, , .
6.3
. AVL- ;
2-3-.
6.4 () .

, . 6.5
. ,
.
,
.
. ,
. ,
( 10). .
-, , . , ,
,
,
. , ..
, .

6.1
. ,
, . , ,
, . ,

6.1

249

,
.

, , , ,
, (n log n),
,
(n log n), .
? 1.3 (. 10.2),
, ,
, n log n .1
.
.
1 ( ).
2.3 ( 2).
, ,
,

.
 2
n .
-
, :
,
.
P resortElementU niqueness (A [0..n 1])
//
// : A[0..n 1]
// : true, A ,
//
false,
A
for i 0 to n 2 do
if A[i] = A[i + 1]
return false
return true
1

(radix sort)
.
, . , n log n,
n
log 2 n.

250

6.

, , .
n log n , n 1, . ,
, .
, , (n log n),

(n log n):
T (n) = Tsort (n) + Tscan (n) (n log n) + (n) = (n log n) .

2 ( ). (mode) ,
. , 5, 1, 5,
7, 6, 5, 7 5 (
, ).

,
. .
i- . i-
,
; , ,
1.
,
. i-
i 1 ,
. ,
,

C (n) =

n

i=1

(i 1) = 0 + 1 + + (n 1) =

 
(n 1) n
n2 .
2

, n 1 ,
.
, .
,

6.1

251

.
P resortM ode (A [0..n 1])
//
//
// : A[0..n 1]
// :
A
i0
// i
modef requency 0 //
while i  n 1 do
runlength 1; runvalue A[i]
while i + runlength  n 1 and A[i + runlength] = runvalue do
runlength = runlength + 1
if runlength > modef requency
modef requency runlength; modevalue runvalue
i i + runlength
return modevalue
1: ,
(?). , , n log n,
.

3 (). v n . (. 3.1) n .
,
log2 n + 1 . n log n

T (n) = Tsort (n) + Tsearch (n) = (n log n) + (log n) = (n log n) ,
, .
. ,
, ( 6.1.6 ,
).


252

6.

, , ( )
, .
,
, .. ,
, 4.6.

6.1
1. , n n/2
(.. ).

.
2. n ( x y
|x y|).
) .
) (. 1.2.9).
3. A = {a1 , a2 , . . . , an } B = {b1 , b2 , . . . , bm }
. , .. C
, A, B.
)
.
) .
4.
n .
) .
) : 1) , 2)
3) (. 4.1.2).

6.1

253

5. , ,
, ,
.
6. , , ,
103 , , (
, , ). 106 ?
7. ? .
) n m n 
m). , ,
( ,

.)
) n , ,
, .
.
8. n  3 x y. ,
, :
P3

P3
P1

P6
P2

P1

P6
P2

P4

P4
P5

P5

) ? ?
) .

254

6.
9. n s. ,
, s. (,
5, 9, 1, 3 s = 6 , s = 7, .)
, .
10. ) , , ,
[15]. , eat, ate tea .
) , .

6.2
:
a11 x + a12 y = b1
a21 x + a22 y = b2
,
, .

, ,
.
, .
n n , n :
a11 x1 + a12 x2 + + a1n xn = b1
a21 x1 + a22 x2 + + a2n xn = b2
..
.
an1 x1 + an2 x2 + + ann xn = bn
, , (
?),
.
, , (Gauss eli-

255

6.2

mination)2 . n n (.. , ) , .. ,
:
a11 x1 + a12 x2 + + a1n xn = b1

a11 x1 + a12 x2 + + a1n xn = b1

a21 x1 + a22 x2 + + a2n xn = b2


..
.

a22 x2 + + a2n xn = b2


..
.

ann xn = bn

an1 x1 + an2 x2 + + ann xn = bn


,
Ax = b

A x = b

a11 a12 . . . a1n

a21 a22 . . . a2n

A= .

.
an1 an2 . . . ann


b1

b2

b=
..
.
bn

a11 a12 . . . a1n

0 a22 . . . a2n


A = .

.
.


0
0 . . . ann


b1

b2

b =
..
.
bn

( ,
.)
?
,
. xn ;
xn1 . ,
n xn x1 .
,
A
A ?
:
2

(Carl Friedrich Gauss) (17771855), ,


, (Isaac Newton)
(Leonard Euler), ,
.

256

6.
;
;


, .

, , , ,
. , .
a11 ,
x1 . , , a21 /a11 ,
x1 . ,
, , a31 /a11 , a41 /a11 ,
. . . , an1 /a11 , x1 0. x2 ,
,
. n 1 ,
.
,
, , n + 1- .
, + =.
1. (
).
2x1 x2 + x3 = 1
4x1 + x2 x3 = 5

x1 + x2 + x3 = 0

2 1
1
1

4
1 1
5
1
1
1
0

2 1
1
1

0
3 3
3
3
1
1
0
2
2 2

2
3

4
2
1
2

1
1

1
2

6.2

257

2 1
1
1

3 3
3
0
0
0
2 2
x3 = (2)/2 = 1,
x2 = (3 (3) x3 )/3 = 0 x1 = (1 x3 (1) x2 )/2 = 1.

.
GaussElimination (A [1..n, 1..n] , b [1..n])
//
// A,
// b
// : A[1..n, 1..n] b[1..n]
// :
//
A
//
n + 1- ,
//

//

for i 1 to n do
A[i, n + 1] b[i] //
for i 1 to n 1 do
for j i + 1 to n do
for k i to n + 1 do
A[j, k] A[j, k] A[i, k] A[j, i]/A[i, i]
.
-, : A [i, i] = 0,
, , i-
i- . i-
, i- ( ( ),
).
, : ,
A [i, i] (, ,
A [j, i]/A [i, i]), A [j, k]
, .3
3

10.4.

258

6.

, i- i- ,
i- . , (partial pivoting), ,
1.
-, , . , , ,
?
BetterGaussElimination (A [1..n, 1..n] , b [1..n])
//
//
// : A[1..n, 1..n] b[1..n]
// :
//
A
//
n + 1- ,
//

//

for i 1 to n do
A[i, n + 1] b[i] //
for i 1 to n 1 do
pivotrow i
for j i + 1 to n do
if |A[j, i] |>| A[pivotrow, i]|
pivotrow j
for k i to n + 1 do
swap(A[i, k], A[pivotrow, k])
for j i + 1 to n do
temp A[j, i]/A[i, i]
for k i to n + 1 do
A[j, k] A[j, k] A[i, k] temp
.
A[j, k] A[j, k] A[i, k] temp
. , , ,
,
.4 , 2.3 (. 4

2.1,
.

259

6.2

) ,
:
C (n) =

n1


n n+1



1=

i=1 j=i+1 k=i

n1


n1


n


(n + 1 i + 1) =

i=1 j=i+1

(n + 2 i) (n (i + 1) + 1) =

i=1

n1


n


(n + 2 i) =

i=1 j=i+1
n1


(n + 2 i) (n i) =

i=1

= (n + 1) (n 1) + n (n 2) + + 3 1 =

n1


(j + 2) j =

j=1

n1

j=1

j2 +

n1

j=1

2j =

(n 1) n
(n 1) n (2n 1)
+2
=
6
2

 
n (n 1) (2n + 5) 1 3
n n3 .
=
6
3

( )


n2 ( 6.2.5), ,
.
( ), , .
,
. ,
(. 10.4). ,
,
, .

LU-
, LU-, LU- (LU-decomposition)
. ,
.
, (,
, ).

260

6.

2. ,

2 1 1

A = 4 1 1
1 1
1
L,
,
:

1
0 0

L= 2
1 0
1/2 1/2 1
U ,

2 1 1

U = 0 3 3 .
0

, LU A ( L U , , , ,
).
, Ax = b
LU x = b. .
y = U x, Ly = b. Ly = b, ,
L . U x = y,
, U . ,
Ly = b:

1
1
0 0 y1

1 0 y2 = 5 .
2
1/2 1/2 1

y3


y1 = 1,

y2 = 5 2y1 = 3,

1
1
y3 = 0 y1 y2 = 2.
2
2

261

6.2
U y = x, ..


1
2 1 1
x1


0 3 3 x2 = 3 .
x3
2
0 0
2

x3 = (2)/2 = 1,
x2 = (3 (3) x3 )/3 = 0,
x1 = (1 x3 (1) x2 )/2 = 1.

, LU - A,
Ax = b b. LU -
, . , LU - , U
A ( ),
L A.


,
.
, (matrix inverse). A n n
n n A1 :
AA1 = I,
I n n (.. ,
0, , 1).
,
, . A, , (singular). ,
,
( ) .
, , :
, ;
. , .

262

6.

, ,
. ,
ax = b, x = a1 b ( a 0), n n Ax = b
x = A1 b ( A ), b, , ,
.
, ,
A n n, n2 xij ,
1  i, j  n ,

x11 x12 . . . x1n


1 0 ... 0
a11 a12 . . . a1n

a21 a22 . . . a2n x21 x22 . . . x2n 0 1 . . . 0


.
= .
.
.
.
.

.
.
.

.
an1 an2 . . . ann

xn1 xn2 . . . xnn

0 0 ... 1

, n A, xj
j- ,
ej j- (1  j  n):
Axj = ej .
,
A, nn. LU - A LU xj = ej ,
j = 1, 2, . . . , n, .


, , , (determinant)
. A n n, det A
|A|, , .
n = 1, .. A a11 , det A = a11 .
n > 1, det A
det A =

n


sj a1j det Aj ,

j=1

sj +1, j , 1, j (.. sj = (1)j+1 ), a1j


j- , Aj

6.2

263

(n 1) (n 1), A
j- .
, 22
:


a11 a12
= a11 det [a22 ] a12 det [a21 ] = a11 a22 a12 a21 .
det
a21 a22
, 2 2 .
3 3







a11 a12 a13
a22 a23
a21 a23
a21 a22

a12 det
+ a13 det
=
det a21 a22 a23 = a11 det
a32 a33
a31 a33
a31 a32
a31 a32 a33
= a11 a22 a33 + a12 a23 a31 + a21 a32 a13 a31 a22 a13
a21 a12 a33 a32 a23 a11 .
, ; , 4.6 .
? (
, ,
.) ,
n! .
. , ,
, , ( ,
, , ). n n .
.
, n n Ax = b
, det A 0. , ,
(Cramers rule):
det Aj
det A1
det An
, . . . , xj =
, . . . , xn =
,
det A
det A
det A
det Aj , j- A b. ( 6.2.10 ,
.)
x1 =

264

6.

6.2
1.
:
x1 + x2 + x3 = 2
2x1 + x2 + x3 = 3
x1 x2 + 3x3 = 8
2. ) 1 LU .
) LU -
?
3. 1
.
4.
?
C (n) =

n1


n n+1



1=

i=1 j=i+1 k=i

n1

i=1
n1

i=1

n1


(n + 2 i) (n i) =

i=1


(n + 2) n i (2n + 2) + i2 =

(n + 2) n

n1

i=1

(2n + 2) i +

n1


i2 .

i=1

 


(n + 2) n n3 , s2 (n) = n1
s1 (n) = n1
i=1
i=1 (2n + 2) i
 3

 3
n1 2
n s3 (n) = i=1 i n , s1 (n)s2 (n)+s3 (n) n3 .
5. 


2
, n .
6. ,
. , BetterGaussElimination GaussElimination (, ,
GaussElimination ).

6.3

265

7. ) , , .
) , , .
) , , .
8. (GaussJordan elimination) ,
, .
) 1.
) ?
)
n n ?
,
( , ).
9. n n Ax = b , det A = 0.
?
10. ) 1.
) , n
n
(, ).

6.3
1.4 4.4
.
, , , -

266

6.

,
. ,
.
, ,
? , (log n),
. (n),
, ,
n 1.

,
, ,
. .

.
, . AVL-
(AVL tree) ,
1. - (red-black tree)
,
.
, ,
, (rotation) . ( AVL-.
, , (splay tree),
.)
:
. 2-3-, 2-3-4 B-. ,
, . (
, 2-3-,
B- 7.)

6.3

267

AVL-
AVL- 1962 .
.. .. [1];
.
1. AVL- , (balance factor) , , 0, +1 1
( 1).

, . 6.2 AVL-,
. 6.2 .
1

10

10

20

20

12

. 6.2. ) AVL-. ) , AVL-.

AVL- , . AVL-
, , +2 2; ,
, . ,
.
. 6.3.
(single right rotation),
R- (R-rotation) ( ,
. 6.3, ). . 6.4
R- . ,

, +1.

268

6.
2

0
2

3
1
R

0
3

0
1

0
1
)
0
2

2
1
1
2

0
3

0
1

0
3
)
2
3
1
1

0
2
LR

0
3

0
1

0
2
)
2
1

0
2
1
3

RL

0
1

0
3

0
2
)

. 6.3. AVL- . ) R-.


) L-. ) LR-. ) RL-

(single left rotation), L (L-rotation), R. , -

6.3

269


R$
r

c
r

T3
T1

T1
T2

T2

T3

. 6.4. R- AVL-.

1 (
L- ).
- (double left-right rotation), LR- (LR-rotation).
: L- r,
R- , r (. 6.5). ,
+1.
LR$
r

g
r

c
g
T4
T1

T2

T3

T2

T3

T1

T4

. 6.5. LR- AVL-. . ,


- (double right-left rotation), RL-


(RL-rotation), LR-
.

270

6.

, , ,
, .
, . ,
. 6.4 T1 c,
T2 , , , r,
T3 . ,
, .
. 6.6 AVL- . , ,
, , 2,
,
.
AVL-? , . , AVL , . ,
h AVL- n
log2 n  h < 1.4405 log 2 (n + 2) 1.3277.
( , . 2.5.)
,
(log n).
AVL-,
, , ,
AVL- 1.011 log 2 n+0.1, n [67]. , AVL-
, . AVL- ,
, , , ,
, .. .
. AVL- ,
, . AVL-
.

.

6.3

271

0
L(5)

0
6

0
8
1
6
1
5

2
6
0
8

0
3

R(5)

1
3

8
0

0
2

0
2

2
6
1

5
0
8

LR(6)

1
5

0
2

1
6

0
2

0
4

1
5

0
0
4

2
6

3
0
2

1
8

RL(6)

3
0
2

7
0
4

0
6

0
8

0
7

. 6.6. AVL- 5, 6, 8, 3, 2, 4, 7.

2-3-
, ,
. 2-3-, 1970 . . (John Hop-

272

6.

croft) [4]. 2-3- ,


2- 3-. 2- K
: ,
K, , K. ( , 2- ,
.) 3- K1 K2 (K1 < K2 )
. , K1 , , K1
K2 , , K2
(. 6.7).

<K

2-

3-

K1,K2

>K

< K1

(K1,K2)

> K2

. 6.7. 2-3-

2-3- ,
, .. 2-3-
(height-balanced): .
.
K 2-3- . . 2-, , , , K
, ,
, K, , .
3-,
, ( K 3-)
.
2-3- .
K , . , K.
2-, K ,
, K, , .
3-, : ( ) , ,

6.3

273

, (
, ). ,
( 3-) , ,
.
8
9

5, 9

5, 8, 9

3, 8

2, 3, 5

3, 5

3, 8
9

4, 5

5
3, 8

4, 5, 7

3, 5, 8

3
7

8
4

. 6.8. 2-3- 9, 5, 8, 3, 2, 4, 7

2-3- . 6.8.

. . 2-3- h
2- (, . 6.8 h = 2). ,
2-3- h n
n  1 + 2 + + 2h = 2h+1 1,
,
h  log2 (n + 1) 1.
, 2-3- h
, 3-,
. , 2-3- n


n  2 1 + 2 3 + + 2 3h = 2 1 + 3 + + 3h = 3h+1 1,
,
h  log3 (n + 1) 1.

274

6.

h
log3 (n + 1) 1  h  log2 (n + 1) 1
, , , (log n). 7.4
2-3- B-.

6.3
1. AVL-?

8
3

7
)

67
2

1
7

9
)

2. ) n ( n = 1, 2, 3, 4, 5),
AVL-.
) 4,
AVL-
.
3. L- RL- .
4. AVL-
.
) 1, 2, 3, 4, 5, 6
) 6, 5, 4, 3, 2, 1
) 3, 6, 5, 1, 2, 4
5. ) AVL-, (..
)
.

6.4

275

) : AVL-
?
6. AVL- n .
7. ) 2-3- : C, O,
M, P, U, T, I, N, G (
).
)
.
8. TB T23 , ,
2-3-,
, . :
T23
, TB ?
9. 2-3-, , (..
) .
10. 2-3- n
.

6.4
(heap) , . ,
(priority queue) , (priority) ,
:
(.. ) ;
;
.
. , -

276

6.

(heapsort). ,
.


1. (heap)
, ( ),
.
1. .
(essentially complete) (complete), ..
, , , ,
.
2. .
(
).5

10

10

5
4

7
2

10

7
2

5
6

7
2

. 6.9. :

, , . 6.9. . ,
. ,
5.

..
(, ). , ..
, ,
.
, (
, . 6.10.
1. n .
log2 n.
2. .
5

, .

277

6.4

10
5
4

7
2

1
10

2
5

3
7

4
4

5
2

6
1

. 6.10.

3. .
4.
. 1 n, H [0] , ,
.

) n/2 , n/2 .
) i
(1  i  n/2) 2i 2i + 1, ;
i (2  i  n)
i/2.
, H [1..n], i
2i 2i + 1, ..
H [i]  max {H [2i] , H [2i + 1]}

i = 1, 2, . . . , n/2 .

(, 2i + 1 > n, H [i] 
 H [2i].) ,
,
, .
?
. (bottom-up heap construction) . 6.11.
n , .
, . ,

278

6.
2

9
6

7
5

9
6

8
5

2
8
5

9
6

8
5

9
6

2
6

8
5

6
2

8
5

. 6.11. 2, 9, 7, 6, 5, 8

K K .
, K (
, ).
, ,
. .
,
.
,
.
(
. .) ,
.
HeapBottomU p (H [1..n])
//
//
// : H[1..n]
// : H[1..n]
for i n/2 downto 1 do
k i; v H[k]
heap = false
while not heap and 2 k  n do
j 2k
if j < n
//
if H[j] < H[j + 1]

6.4

279

j j+1
if v  H[j]
heap true
else
H[k] H[j]; k j
H[k] v
?
, n = 2k 1, , ..
. h
; , h = log2 n ( log2 (n + 1) 1 = k 1
n).
i
h. ( , ,
), , i, 2 (h i). ,

Cworst (n) =

h1 

i=0
i

2 (h i) =

h1


2 (h i) 2i = 2 (n log2 (n + 1)) ,

i=0


h
i

i=1 i2 (. ),
h. ,
n
2n .
( )
;
(top-down heap
construction). ? K
, K . K :
K, (
). K
. , K
(
. 6.12). -

280

6.

,
K.
9

6
2

87
5

10

10
7

10

6
8

97
5

. 6.12. 10 , . 6.11

, , . n
log2 n, O (log n).
9
8
2

6
5

1
1

2
8
2

6
5

8
2

6
5

5
2

6
1

. 6.13.

?
,
( ,
?). ,
( . 6.13).
1. .
2. 1.
3. K
,
, .. : , ,
K -

281

6.4

, K .

, ,
1.
, ,
O (log n).


(heapsort) , . (J. W. J. Williams) [122].
.
1 ( ). .
2 ( ).
n 1 .
. , , , .
. 6.14 ( . 6.11 ,
).
, O (n), . C (n),
n 2 ,
:
C (n)  2 log2 (n 1) + 2 log2 (n 2) + + 2 log2 1  2

n1


log2 i 

i=1

2

n1


log2 (n 1) = 2 (n 1) log2 (n 1)  2n log2 n.

i=1

, C (n)
O (n log n). , (n log n) , . , , , ,

282

6.
1.

2.

5 |9

2 | 8

5 | 7

2 | 6

2 |5
2

. 6.14. 2, 9, 7, 6, 5, 8

, , . , , ,
,
.

6.4
1. ) 1, 8, 6, 5, 3, 7, 4 .
) 1, 8, 6, 5, 3, 7, 4 .
) ?
2. ,
H [1..n], .
3. ) ,
h.
) , n log2 n.

283

6.4

4. , :
h1


2 (h i) 2i = 2 (n log2 (n + 1)) ,

n = 2h+1 1.

i=0

5. ) .
) v
.
6. :
) 1, 2, 3, 4, 5 ( )
) 5, 4, 3, 2, 1 ( )
) S, O, R, T, I, N, G ( )
7. ?
8.
?
9. , n = 102 , 103 , 104 ,
105 , 106 .
) [1..n];
) 1, 2, . . . , n;
) n, n 1, . . . , 1.
10. , , .
) , .
) (. [34])

?

284

6.5

6.


p (x) = an xn + an1 xn1 + + a1 x + a0

(6.1)

x xn . ,
, , .
, 50
. ,
(fast Fourier transform, FFT).
,
, , .
,
[102] [32].


(Horners rule) , .
. (W. G. Horner), 19 . , . (D. Knuth) ([66]), 150
. (I. Newton).
,
(. 1 2
).
, p (x) ,
(6.1). (6.1) x
:
(6.2)
p (x) = (. . . (an x + an1 ) x + . . .) x + a0 .
, p (x) = 2x4 x3 + 3x2 + x 5 :
p (x) = 2x4 x3 + 3x2 + x 5 =


= x 2x3 x2 + 3x + 1 5 =


 
= x x 2x2 x + 3 + 1 5 =
= x (x (x (2x 1) + 3) + 1) 5.

(6.3)

6.5

285

x (6.2). , ,
,
, , , :
, , .
,
. ( ,
0, ),
an a0 .
( , an ).

, x, .
, , .
1. p (x) = 2x4 x3 + 3x2 + x 5 x = 3.
2
1
3
1
5
x=3
2 3 2 + (1) = 5 3 5 + 3 = 18 3 18 + 1 = 55 3 55 + (5) = 160

, p (3) = 160. ( (6.3), ,


3 2 + (1) = 5 2x 1 x = 3; 3 5 + 3 = 18
x (2x 1)+3 x = 3; 318+1 = 55 x (x (2x 1) + 3)+1
x = 3 , , 3 55 + (5) = 160 x (x (x (2x 1) + 3) + 1) 5 =
= p (x) x = 3.)

,
.
Horner (P [0..n] , x)
//
//
// : P [0..n]
//
n (
//
) x
// : x
p P [n]
for i n 1 downto 0 do
p x p + P [i]
return p

286

6.

:
M (n) = A (n) =

n1


1 = n.

i=0

, ,
n: an xn .
n , , , n 1
! ,
.
. , p (x) x0 ,
p (x) x x0 , , p (x0 ),
p (x) xx0 . , ,
2x4 x3 +3x2 +x5 x3 , , 2x3 +5x2 +18x+55
160. , (synthetic
division), , (
x c, c ).


, an , .. xn x = a.
, , a , 0
. an ( an mod m)
, an ,
. n,
, .
n = bI . . . bi . . . b0 ,
n . , n

p (x) = bI xI + + bi xi + + b0

(6.4)

x = 2. , n = 13, 1101
13 = 1 23 + 1 22 + 0 21 + 1 20 .

6.5

287

,
an = ap(2) = abI x

I ++b xi ++b
0
i

p (2)
p 1 // 1 n  1
for i I 1 downto 0
p 2p + bi

an = ap(2)
ap a1
for i I 1 downto 0
ap ap a2p+bi

(
p 2

a2p+bi = a2p abi = (a ) abi =

(ap )2
bi = 0,
.
2
p
(a ) a bi = 1.

, -
a , ,
, 1, a. an
(left-to-right binary exponentiation).
Lef tRightBinaryExponentiation (a, b (n))
// an
//
// : a b(n) bI , . . . , b0
//
n
// : an
product a
for i I 1 downto 0 do
product product product
if bi = 1
product product a
return product
2. a13
. n = 13 = 11012 . ,
n

1
a

1
2

a a= a

0
 3 2
a
= a6

1
 6 2
a
a = a13

288

6.


, M (n) an
(b 1)  M (n)  2 (b 1) ,
b , n. ,
b 1 = log2 n, ,
,
, ,
( n 1 ).
(right-to-left binary exponentiation) p (2) (. (6.4)), n,
:
an = abI 2

I ++b 2i ++b
0
i

= abI 2 . . . abi 2 . . . ab0 .


I

, an
( i
a2 bi = 1,
i
abi 2 =
1
bi = 0,
.. a2 , , i
bi 0. , a2
i1 2
i
.
, i, a2 = a2
a, ( ), ,
1. .
i

RightLef tBinaryExponentiation (a, b (n))


// an
//
// : a b(n) bI , . . . , b0
//
n
// : an
i
term a // a2
if b0 = 1
product a
else
product 1
for i 1 to I do
term term term
if bi = 1

6.5

289

product product term


return product
3. a13
. n = 13 = 11012 . ,
1
a8
5
a a8 = a13

1
a4
a a4 = a5

0
a2

1
a
a

n
i
a2

,
, . - ,
n. 8
, .

6.5
1. ,
.
BruteF orceP olynomialEvaluation (P [0..n] , x)
// P x
// ,
//
// : P [0..n]
//
n,
//
, x
// : x
p 0.0
for i n downto 0 do
power 1
for j 1 to i do
power power x
p p + P [i] power
return p
,
.

290

6.
2. ,
,
.
, .
3. ) , 2, 1)
; 2) .
)
?
4. )
p (x) = 3x4 x3 + 2x + 5 x = 2.
)
p (x) x + 2.
5. /,
p (x) = an xn + an1 xn1 + +
+ a0 x c, c ,
.
6. ) a17 .
)
, ?
7. a17 .
8. an ,
, n.
9. ,
, p (x) = xn + xn1 + + x + 1?
10. ,

p (x) = an xn + an1 xn1 + + a0



p (x) = an (x x1 ) (x x2 ) . . . (x xn ) ,

6.6

291

x1 , x2 , . . . , xn (
). ,
:
) ;
) ;
) .

6.6
, ,
? ,
, , , 5
, ,
. . . ,
,
. . . , ,
. , .
, , , , (problem
reduction). , ,
(. 6.15).

1
( )

A
2
( A)

. 6.15.

,
( 10).
. ,
, . , , ,
, .
, . , 6.5 ,
. 4.6 :
p1 = (x1 , y1 ), p2 = (x2 , y2 ) p3 = (x3 , y3 ) -

292

6.

x1 y1 1

det x2 y2 1 = x1 y2 + x3 y1 + x2 y3 x3 y2 x2 y1 x1 y3
x3 y 3 1
, p3

p
1 p2 , p1 p2 . ,

. ,
,
(Rene Descartes) (15961650). ,
.


, (least common multiple) m n ( lcm (m, n)) , m, n. , lcm (24, 60) = 120
lcm (11, 5) = 55. ; , ,
. m n ,
lcm (m, n) m n, m,
n, n, m. ,
24 = 2 2 2 3
60 = 2 2 3 5
lcm (24, 60) = (2 2 3) 2 5 = 120
, 1.1

.

.
, m n.
, lcm (m, n) gcd (m, n)? , lcm (m, n) gcd (m, n)

6.6

293

m n, ,
m n.
lcm (m, n) =

mn
,
gcd (m, n)

gcd (m, n) .



. ,
k > 0 i- j- ( ) (i, j)- Ak , A
(, ).
,
.
. 6.16.
A A2 (, , 1 2) . ,
2, a: a b a, a c a a d a,
2 a c: a d c.
a

A =
c

a
0

b
1

c
1

d
1

A2 =

a
3

b
0

c
1

d
1

. 6.16. , A2 .
A A2 1 2,


.
, , (maximization problem);
(minimization problem). ,
f (x), , . ?
:
min f (x) = max [f (x)] .

294

6.

, ,
, ,
.
. 6.17.
y

f(x)

f(x*)
x*

f(x*)

f(x)

. 6.17. :
min f (x) = max [f (x)]

, , :
max f (x) = min [f (x)] .


D. ,
.
.
, ,

. f  (x) f  (x) = 0 .
, . ,
, .
.

6.6

295

, , .
,
.


, ,
(linear programming),
.
1. . 100 000$,
, .
10%, 7%, 3%.
,
, . ,
25% , ,
. , ?
. x, y z , , , , .

:
0.10x + 0.07y + 0.03z

x + y + z = 100
x  13 y
z  0.25 (x + y)
x  0, y  0, z  0.

, ,

( )

c1 x1 + + cn xn
ai1 x1 + + ain xn  ( , =) bi
i = 1, . . . , m
x1  0, . . . , xn  0.

( , , ,

296

6.

ai1 x1 + + ain xn  bi ;
.)
, ,
, . .
- (simplex method);
. (G. Dantzig) 1940- [33].
,
. 50 , ,
.
, ;
(Narenda Karmarkar) [57].
; , ,
, , .
, , - ,
, . , (integer linear programming). , ,
( 10, ,
). ,
11.2.
2. . 3.4: n
w1 , . . . , wn v1 , . . . , vn , , W . ,
.
(continuous), (fractional), ,
.
xj , j = 1, . . . , n , j,
. , xj 0  xj  1.

6.6

297

n

j=1 wj xj , n
j=1 vj xj . ,
:

n


vj xj

j=1

n


wj xj  W

j=1

0  xj  1,

j = 1, . . . , n.



, 11.3
( ? ).

.
(discrete), 0-1,
, . ,
:

n


vj xj

j=1

n


wj xj  W

j=1

xj {0, 1} ,

j = 1, . . . , n.


, .
0-1 , ,
, . , ,
, [77].



1.3, . , ,

298

6.

. ,
.
, ,
( ).
(state-space graph). ,
.
3.
, 1.2.1:
, , .
.
(.. , , ).
, .
, .
Pwgc ||

Pg

wc || Pg

Pwc || g

Pw

c || Pwg

Pc

Pg

Pc

Pg

Pgc || w

Pwg || c

w || Pgc

Pw

g || Pwc

P
Pg || wc

Pg

|| Pwgc

. 6.18.

. 6.18. ,
: P, w, g c , , ,
( ,
. . . . .);

6.6

299

() . , ,
.
, Pwgc, Pwgc.
,
(?). ,
, .
, ,
.

, .
, ,
.
11.1
11.2.

6.6
1. ) ,
lcm (m, n):
mn
.
lcm (m, n) =
gcd (m, n)
) , O (log n).
gcd (m, n), lcm (m, n)?
2. , (
). (, 6.4) ?
3. , k > 0 i- j- ( ) (i, j)- Ak , A .
4. ) , , , n
3 [76].
) . ,

300

6.
,
, . ,
;
. ?
5. n > 3 P1 = (x1 , y1 ), . . . , Pn =
= (xn , yn ). ,
,
. (
,
.)
6. n
, n .
.
7. , 3.4,
. n ,
n , (.. ,
). i- j- C [i, j] i, j = 1, . . . n.
, .
0-1 .
8. ,
6.6:
0.10x + 0.07y + 0.03z

x + y + z = 100
1
x y
3
z  0.25 (x + y)
x  0, y  0, z  0.

9. : ,
. :
, , ,

301

. , .
10. . n  2 , . ,
. , ,
,
, , .
?
) n = 2.
) n = 3 (
).
) n  4? , , ; ,
, .

( ), .
, .
:
, .
, . , AVL-
.

.
, ,
2-3-, ,
.
, . -

302

6.

.
, , . , AVL-
2-3-, .

( ), . , .
,
.

,
, .
(n log n) ,
; ,
.
AVL- ,
,
. , .
AVL- (log n);
, .
2-3- ,

. B-,
.

.
, .
n3 /3 .
n n . , -

303
, , ,
.
an 6.5. n, :
.
. ,
, .
, .

"" -
"Books.Ru - "
. (ISBN 5-8459-0987-2)
- "Books.Ru - ".
- ,
. ,
"" .


.
(Johann Wolfganh von Goethe)
(17491832)

, .. ,
, .
. ,
. ,
(,
- ).
,
. ,
.
1 (input enhancement)
, :
( 7.1);

,
( 7.2).
1

, , (preprocessing). , ,
, (. 6). ,
, .

306

7. -


, / .
(prestructuring).
-
: , ; , .
:
( 7.3);
B-.
, - , (dynamic programming).
,
.
, .
. -,
.
, , ,
. , ,
, , , . , , . ,

 2  , :
n , (n + m)
, n m , . , ..
(, m O (n)),

,
. : ,
, ,
.
-, - ,
, .
, ,

7.1

307

.
; , , [101], .

7.1
.
, , , .
: , 10,
( 10, 0)
. , ,
, .
(comparison counting) (. 7.1).
A[0..5]

i = 0
i = 1
i = 2
i = 3
i = 4

S[0..5]

Count[]
Count[]
Count[]
Count[]
Count[]
Count[]
Count[]

62

31

84

96

19

47

0
3

0
0
1

0
1
2
4

0
1
2
3
5

0
0
0
0
0
0
0

0
0
1
1
1
2
2

19

31

47

62

84

96

. 7.1.

ComparisonCountingSort (A [0..n 1])


//
// : A[0..n 1]
// : S[0..n 1] A,
//

for i 0 to n 1 do
Count[i] 0
for i 0 to n 2 do
for j i + 1 to n 1 do
if A[i] < A[j]

308

7. -

Count[j] Count[j] + 1
else
Count[i] Count[i] + 1
for i 0 to n 1 do
S[Count[i]] A[i]
return S
?
, n- . , ,
A [i] < A [j], , :
C (n) =

n2
 n1

i=0 j=i+1

1=

n2

i=0

[(n 1) (i + 1) + 1] =

n2

i=0

(n 1 i) =

n (n 1)
.
2

, , , .
, . ,
, 1 2.
, . ,
, , ,
1, 2. ,
l (
) u ( ),
F [0..u l].
F [0] l,
F [1] l + 1, . , ,
, .

, ,
. S [0..n 1]. A,
l, F [0] S,
.. 0 F [0] 1, l + 1 F [0] (F [0] + F [1]) 1, ..
,
(distribution counting).

7.1

309

1.
13

11

12

13

12

12

, , {11, 12, 13}


. :

11
1
1

12
3
4

13
2
6

, (
0,
1).
. , 12, 4,
12 41 = 3 S,
. 12 1 ( ) .
. 7.2.

D[0..2]
A[5]=12
A[4]=12
A[3]=13
A[2]=12
A[1]=11
A[0]=13

1
1
1
1
1
0

4
3
2
2
1
1

6
6
6
5
5
5

S[0..5]
12
12
13
12
11
13

. 7.2. .

.
DistributionCounting (A [0..n 1] , l, u)
//
//
// : A[0..n 1]
//
l u (l  u)
// : S[0..n 1] A,

310

7. -

//

for j 0 to u l do
D[j] 0
//
for i 0 to n 1 do
D[A[i] l] D[A[i] l] + 1
//
for j 1 to u l do
D[j] D[j 1] + D[j]
//
for i n 1 downto 0 do
j A[i] l
S[D[j] 1] A[i]
D[j] D[j] 1
return S
, ,
A. , (, ).
,
, ( ).

7.1
1. ?
2. ?
3. , {a, b, c, d},

:
b, c, d, c, b, a, a, b.
4. ?
5. n, 1 n.
6.
, , ( ,

7.1

311

) , , , . ,
.
7. (ancestry problem) ,
u v (,
, ) n .
O (n),

.
8.
(virtual initialization) A [0..n] ,

, , , .
counter,
A,
, B [0..n 1] C [0..n 1],
. B [0] , . . . , B [counter 1]
A, : B [0]
A, , B [1] A,
, .. , A [i] ,
k- 0  k  counter 1), C [i] k.
) A [0..7], B [0..7] C [0..7]
A [3] x; A [7] z A [1] y.
) ,
A [i], , ?
9. )
A p q, B q p.
)
p (x) q (x) m n, .
10. , - ,
3 3
.

312

7. -

7.2

, . ,
m ( , (pattern)) n ( (text)).
3.2:

. n m + 1
m ,
m (n m + 1),
(nm). , , , ; ,
(n).
, .
, , .
, .
,
: ,
, .
[68] [21].
:
, .
, .
(, ; , .
, .) ,
, , .
,
. (R. Horspool) [55]. ,

313

7.2

, .


BARBER :
s0

c sn1
B A R B E R

,
R . , (
, , ). ,
. ,
, .
, c
, . .
1. c (, c
S),
( c
, , c, ):
s0

sn1

B A R B E R
B A R B E R

2. c , (,
B ), , c
:
s0

B
sn1

B A R B E R
B A R B E R

314

7. -

3. c m 1 ,
1 m:
s0

L E A D

sn1

E R
 
E R
L E A D E R

4. , , c
m 1 ,
2 c
m 1 c :
s0

O R
sn1

R E O R D E R
R E O R D E R

,
,
.
, .
,
.
. , ,
, , , (, ,
, ). . , c

m, c

m 1

t (c) =
(7.1)

c m 1


, BARBER 6, E, B, R A, 1, 2, 3 4,
.

7.2

315

. m, m 1 :
j- 0  j  m 2)
m 1 j,
. , , , ,
, .. , .
Shif tT able (P [0..m 1])
// ,
//
// : P [0..m 1]
//

// : T able[0..size 1],
//

//
,
//
(7.1)
T able m
for j 0 to m 2 do
T able[P [j]] m 1 j
return T able
.

1. m , , .
2. .
3. ,
, .
,
,
m ( ) .
t (c) , c ,
, t (c)
.
.

316

7. -

HorspoolM atching (P [0..m 1] , T [0..n 1])


//
// : P [0..m 1] T [0..n 1]
// :
//
1,
//

Shif tT able(P [0..m 1])
// T able
im1
//
while i  n 1 do
k0
//
while k  m 1 and P [m 1 k] = T [i k] do
k k+1
if k = m
return i m + 1
else
i i + T able[T [i]]
return 1
1.
BARBER ,
( ). ,
, :
c

...

...

t (c)

:
J I M _ S AW _ M E _ I N _ A _ B ARB E RS H OP
B A R B E R
B A RB E R
B A R B E R
B A RB E R
B A R B E R
B ARB E R

, (nm) (. 7.2.4).
(n).
, , , , ,
. , ,
, ,
. (R. Boyer) . (J. Moore).

7.2

317


.
c
, , , , .. ,
( ,
). , k (0 < k < m)
, :
s0 . . .
p0

. . . pmk1

sik+1 . . .
si
. . . sn1


pmk . . . pm1

, . c,
.
(bad-symbol shift).
, .
c , , c
. t1 (c) k,
t1 (c) , ,
k :
s0 . . .
p0

. . . pmk1

sik+1 . . .
si
. . . sn1


pmk . . . pm1

...
pm1
p0

, BARBER
S,
t1 (S) 2 = 6 2 = 4 :
s0 . . .

S E R
. . . sn1
 
B A R B E R
B A R B E R

, c t1 (c)k > 0. , BARBER

318

7. -

A, t1 (A) 2 = 4 2 = 2
:
A E R
. . . sn1
s0 . . .
 
B A R B E R
B A R B E R
t1 (c) k  0, ,
.
. , d1 , , t1 (c) k,
, 1, .
:
d1 = max {t1 (c) k, 1} .

(7.2)

k > 0
. k
suff (k). ,
(good-sufx shift). ,
c, 1, . . . , m 1.
, , suff (k), ; , suff (k), , ,
( ,
suff (k)
).
d2 suff (k) (
, ) ,
. , ABCBAB k = 1 2
, , 4 6:
k
1
2

ABCBAB
ABCBAB

d2
2
4

319

7.2

,
suff (k), , , suff (k)?
m. , DBCBAB k = 3
6 :
s0 . . .

c B A B
  
D B C B A B

. . . sn1

D B C B A B
, m,
suff (k), ,
, suff (k), . ,
ABCBAB k = 3 6 ,
AB, :
s0 . . .

c B A B C B A B
. . . sn1
  
A B C B A B
A B C B A B

, 6 DBCBAB,
ABCBAB, AB, .
k, l < k, l. , d2 ; d2
m. d2
ABCBAB:
k
1
2
3
4
5

ABCBAB
ABCBAB
ABCBAB
ABCBAB
ABCBAB

d2
2
4
4
4
4

, .

320

7. -


1. .
2. .
3. .
3. ,
, .
,
,
m ( ) k  0 .
t1 (c) , c . k > 0,
, , d2 . ,

(
k = 0,
d1
(7.3)
d=
max {d1 , d2 } k > 0,
d1 = max {t1 (c) k, 1}.
.
( ,
), .
, .
2. BAOBAB , .
:
c
t1 (c)

A
1

B
2

C
6

D
6

...
6

O
3

...
6

Z
6

_
6

321

7.2
:
k
1
2
3
4
5

BAOBAB
BAOBAB
BAOBAB
BAOBAB
BAOBAB

d2
2
5
5
5
5


. 7.3. B
K ,
t1 (K) = 6 d1 = max {t1 (K) 0, 1} = 6 .
, t1 (_) = 6
d2 = 5 max {d1 , d2 } = max {6 2, 5} = 5 . ,
,
.
B E S S _ K N E W _ A
B A O B A B
d1 = t1 (K) 0 = 6 B A O B A
d1 = t1 (_) 2 = 4
d2 = 5
d = max {4, 5} = 5

B O U T

_ B A O B A B S

B
B A O B A B
d1 = t1 (_) 1 = 5
d2 = 2
d = max {5, 2} = 5
B A O B A B

. 7.3.

B.
t1 (_) = 6,
d2 = 2
max {d1 , d2 } = max {6 2, 5} = 5 . , ,
, .

.


322

7. -


.
, (
), , , , .

7.2
1. BAOBAB
BESS_KNEW_ABOUT_BAOBABS.
2. .
{A, C, G, T },
.
) TCCTATTCTT.
)
TTATAGATCTCGTATTCTTTTATAGATCTCCTATTCTT.
3.
,
1000 ?
) 00001
) 10000
) 01010
4. m n (n  m)

)
)
5. , ,
?
6. ,
?
7.
,
1000 ?
) 00001
) 10000
) 01010

7.3

323

8. )
?
)
?
9. ) ,
?
) .
10. ,
3.2

) ;
)
.

7.3
. , , , ,
.
, , .. ( , , ..).
, ,
. ,
, , , , ..
, (key) , (,
). ,
n K1 , K2 , . . . , Kn .
(hashing) H [0..m 1], - (hash table).
h, - (hash function).

324

7. -

- (hash address),
0 m 1. , , -
h (K) = K mod m (, m 0 m 1). ,
( ord (K)),
, . (
, K c0 c1 . . . cs1 ,

ord(c
) ( s1
i )) mod m;
i=0
h (K) 2
h 0; for i 0 to s 1 do h (h C + ord (ci )) mod m,
C , , ord (ci ).
- :
- -
. ( m
. - ,
.)
- .
Kj

Ki
.
0

.
b

.
m1

. 7.4. : h (Ki ) = h (Kj )

, , - m , n, (collision) , ( )
- (. 7.4). , m n (. 7.3.5).
-. , - .
.
ord (ci )
C,
m.
2

7.3

325


(open hashing) (
(separate chaining)) (closed hashing) ( (open addressing)).

( )
, -. , . , , :
A, F OOL, AN D, HIS, M ON EY, ARE, SOON, P ART ED.
- ,
, .. ,
, 13.
. A; - h (A) =
= 1 mod 13 = 1. F OOL ( (6 + 15 + 15 + 12) mod 13 = 9), .. . 7.5. ARE SOON ( , h (ARE) = (1 + 18 + 5) mod 13 = 11
h (SOON) = (19 + 15 + 15 + 14) mod 13 = 11).

FOOL

AND

HIS

MONEY

ARE

SOON

PARTED

10

11

11

12

10

11

12

AND

MONEY

FOOL

HIS

ARE

PARTED

SOON

. 7.5. -

?
, .
: . 7.5 -
KID, -: h (KID) = 11.
, 11, ,
. - ,
, . KID

326

7. -

ARE, SOON ,
.
,
, , . - n m
( ), n/m . = n/m (load factor) -
. ,
S U ,

(7.4)
S 1+ U =
2
( , ).
, ,
, ,
m .
, 1.
;
. 1, ,
. , -,
, n m. ,
, .
. ( 7.3.6
).
.
, ,
(1), n
- m.

( )
-, ( , ,
- m n).
.

7.3

327

(linear probing),
. , ; , . ,
, . . 7.6
,
( -, ).

FOOL

AND

HIS

MONEY

ARE

SOON

PARTED

10

11

11

12

10

11

12

A
A

PARTED

FOOL

AND

AND

FOOL

HIS

AND

MONEY

FOOL

HIS

AND

MONEY

FOOL

HIS

ARE

AND

MONEY

FOOL

HIS

ARE

SOON

AND

MONEY

FOOL

HIS

ARE

SOON

FOOL

. 7.6. -,

K h (K), h , .. h (K) ,
. , K ,
. , ; ,
( ) ( ).
, LIT . 7.6, h (LIT ) =
= (12 + 9 + 20) mod 13 = 2 , 2 , .
KID h (KID) = (11 + 9 + 4) mod 13 = 11,
KID ARE, SOON , P ART ED,
.

, . , ARE . 7.6,
SOON . : h (SOON ) = 11, , , .
, .. -

328

7. -

,
, .
, .3
,
-





1
1
1
1
1+
U
1+
(7.5)
S
2
1
2
(1 )2
( -). : ,
:

50%
75%
90%

1
2


1+

1
1

1.5
2.5
5.5

1
2


1+

1
(1)2

2.5
8.5
50.5

- - . (cluster)
( ). , . 7.6 .
, . ,
, . , ,
.

. (double hashing).
l = h (K) - s (K):
(l + s (K)) mod m, (l + 2s (K)) mod m, . . . .

(7.6)

3
1962 (Donald Knuth),
.
[65, 66, 67]
.

7.3

329

, (7.6), s (K) m , .. , 1 (
, m ( s (K) = m)).
, : s (k) = m 2 k mod (m 2),
s (k) = 8 (k mod 8) s (k) = k mod 97 + 1
(. [102, 103]).
.
, - ,
.
.
: ,
.
1950- IBM
. , , . , , ;
(extensible hashing).
, , . , , - , , (bucket),
b .
,
.
B-, .

7.3
1. 30, 20, 56, 75, 31, 19 - h (K) =
= K mod 11
) -;
)
;
) .

330

7. -
2. 30, 20, 56, 75, 31, 19 - h (K) =
= K mod 11
) -;
)
; .
3. -, (,
) ?
4. , n - m, -
.
5. .
, , ,
( ),
, .
( ) . ?
6. .
) , ,
?
?
) .
? , ?
7. , ,
?
?
8.
:
- -


7.4 B-

331

9. ,
- . . ?
10. ,
. . ( ).
.

7.4 B-

,
, . ,
. , (
, , , ),
B- (B-tree), . (R. Bayer) . (E. McGreight) [12].
2-3- (. 6.3),
.
B- ( ) , , .
, n 1
K1 < < Kn1 ( ). n , T0 K1 ,
T1 K1 K2 , K1 T1 , Tn1 ,
Kn1 , Kn1 Tn1 (. 7.7)4 .
, B- m  2 .
, 2 m .
, , m/2 m
(, , m/2 1 m 1 ).
4

, . 7.7, n- (n-node). , 2-; 2-3-, 6.3,


2- 3-.

332

7. -

p0

K1

T0

. . .

p1

T1

pi 1

. . .

pi

Ki

Ti 1

Ti

pn 2

Kn 1 pn 1

Tn 2

Tn 1

. 7.7. B-

() , ..
.
B- 4 . 7.8.
B- ,
2-3-. , ,
.
. , ,
, ,
, ,
.

.

B- . ,
, ,
.

11

4, 7, 10

15

11, 14

15, 16, 19

20, 24

20

51

25

34

25, 28

40

34, 38

60

40, 43, 46

. 7.8. B- 4

51, 55

60, 68, 80

333

7.4 B-

B-
? , . ,
B- m h.
. 1 m/2 1
, ..
2 (m/2 1). 2 m/2 (, ) m/2 1
, 2 m/2 (m/2 1). i- (1  i  h1)
2 m/2i1 (m/2 1) . , , h
2 m/2h1
. , B- m n
h > 0 :
n1+

h1


2 m/2i1 (m/2 1) + 2 m/2h1 .

i=1

(. 7.4.2)
n  4 m/2h1 1,
, , h B-
m n :
*
)
n+1
+ 1.
(7.7)
h  log m/2

4
, B- , O (log n).
,
, . 100
B- m:
m
h

50
6

100
5

250
4

,
. 3,
B-,
.
,
, O (log n).

334

7. -

;
(, [5] [32]).
B-
2-3-, 6.3.
K, .
, (
, , ), . ,
, .
K 
( ).
. , ,
, .
. 7.9 65 B-,
. 7.8 ( , B- ).

11

4, 7, 10

15

11, 14

15, 16, 19

20, 24

20

51

25

34

25, 28

40

34, 38

60

40, 43, 46

51, 55

68

60, 65

68, 80

. 7.9. B-, 65 B- . 7.8

,
B-. ,
, .
. , 65 B . 7.8 60,
, 51 55, 65 ( ).
.
B-
. , AVL-

335

7.4 B-

2-3-, B-
( B). , B-, ,
B+ -.

7.4
1. , .
2. )
1+

h1


2 m/2i1 (m/2 1) + 2 m/2h1 = 4 m/2h1 1,

i=1

B- (7.7).
) (7.7), B-.
3. B- m, , 100 3. ,
.
4. B-, 30 31 B-
. 7.8, ,
.
5. B-.
6. ) 2-3-4- (top-down 2-3-4-tree)
B- 4 .
(.. ),
(
,
). 2-3-4-
10, 6, 15, 31, 20, 27, 50, 44, 18.
)
2-3-? ?

336

7. -
7. ) , B.
)
B-.

- , .

,
.
- . .
, , .

.

. .
,
.
- /
. B-.
.
. , , . ,

337
( -), , (
-). , , (1).
B- , 2-3-,
. B-
, . B- ,

.

, ,. . . ,
.
(Charles Dickens) (18121870)


.
(Richard Bellman) 1950- .
, .
, , , ,
, .
.

. ,
.
,
, , .
, 2.5 ( ,
; , ,
, ).

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . ,

340

8.


F (n) = F (n 1) + F (n 2)

n  2

(8.1)


F (0) = 0,

F (1) = 1.

(8.2)

n-
F (n) (8.1) ,
(. . 2.6). , F (n)
F (n 1) F (n 2). , n + 1
F (n), (8.2)
(8.1). , F (n).
2.5.
, , (. 2.5.6). , .
,
,
.
n-
(. 2.5). , , .
,
. , 8.4,

.
,
, : ,
() .
(8.1) n- .

8.1

341

(
).
(, [8])
(, [111])
(, [14, 18]).

8.1

, . ,
(binomial coefcient), C (n, k), Cnk (nk ), () k n-
0  k  n).
:
(a + b)n = Cn0 an + + Cnk ank bk + + Cnn bn .

:
k1
k
+ Cn1
n > k > 0
(8.3)
Cnk = Cn1

Cn0 = Cnn = 1.

(8.4)

(8.3),
k1
Cn1
k
Cn1

.
n+1 k+1 , , ,
0 n 0 k (. 8.1).
Cnk . 8.1 ,
0 n. i (0  i  n) , 1, Cn0 = 1.
0 k- 1, Cii = 1 (0  i  k). (8.3)
( ,
Cnk

342

8.

0
1
2
..
.
k
..
.
n1
n

0
1
1
1

1
2

k1

1
1
1

1
k1
Cn1

k
Cn1
Cnk

. 8.1. Cnk

, : ). ,
.
Binomial (n, k)
// C(n, k)
//
// : n  k  0
// : C(n, k)
for i 0 to n do
for j 0 to min(i, k) do
if j = 0 or j = i
C[i, j] 1
else
C[i, j] C[i 1, j 1] + C[i 1, j]
return C[n, k]
? ,
, A (n, k)
, Cnk . , (8.3) .
, k + 1 , n k , ,

8.1

343

A (n, k), :
A (n, k) =

k 
i1

i=1 j=1

1+

k
n 

i=k+1 j=1

1=

k


(i 1) +

i=1

n


k=

i=k+1

(k 1) k
+ k (n k) (nk) .
=
2
.
,
, .

8.1
1. ) ?
) ?
2. ) C63 .
) Cnk , , ?
3. ,
Cnk :
(k 1) k
+ k (n k) (nk) .
2
4. ) Binomial Cnk ?
) ( , ).
5. ) :
2) Cn2
3) Cn n
1) Cn1
) Cnk
) ?
n/2

6. , , (8.3) (8.4):

344

8.
BinomCoeff (n, k)
if k = 0 or k = n
return 1
else
return BinomCoeff(n 1, k 1) + BinomCoeff(n 1, k)
7. ?
)
Cnk =

n!
.
k! (n k)!

)
Cnk =

n (n 1) (n k + 1)
.
k!

)
k1
k
+ Cn1
Cnk = Cn1

n > k > 0, Cn0 = Cnn = 1.

) .
8. ,

Cnk = Cnnk

n  k  0,

,
Cnk .
9. . , A B, , n .
, A p, q = 1 p (, ). P (i, j)
A , A i , B
j .
) P (i, j),
.
) , A 7 ,
0.4.
)
.

345

8.2

8.2
:

. , , ,
.


, A = {aij } , i-
j- 1 ,
i- j-. ,
.
1. (transitive closure)
n T = {tij }
n n, i- (1  i  n) j-
(1  j  n) 1,
(.. ) i j;
tij 0.

. 8.2 ,
.
a

a
0

b
1

c
0

d
0

A =
c

d
)

T=

a
1

b
1

c
1

d
1

. 8.2. ) , )
)

. ,
i- , , , , , i-
. , -

346

8.

,
.

, .
(Warshalls algorithm)
, . (S. Warshall) [119].
n n n:
R(0) , . . . , R(k1) , R(k) , . . . , R(n) .

(8.5)

(k)
. , rij
i- j- R(k) (k = 0, 1, . . . , n) 1
, ( ) i-
j- , , ,
k. , R(0) , , R(0) , . R(1) ,
1, , , ,
R(0) . (8.5)
, ,
, ( ) .
R(n)
n , ,
, .
,
R(k) (k)
R(k1) (8.5). rij , i-
j- R(k) , 1. , i-
vi j- vj ,
k:
vi  , k  vj .

(8.6)

, . : k-. vi vj ,
(k1)
1.
k 1, , rij
: (8.6) k-
vk . , vk

347

8.2

( , vi vj ,
vk ).
(8.6) :
vi   k 1, vk ,  k 1  vj .
, vi vk ,
, k 1
(k1)
= 1), , vk
(, rik
vj , ,
(k1)
= 1).
k 1 (, rkj
(k)

(k1)

, rij = 1, rij

(k1)
rik

= 1,

(k1)
1, rkj

=
= 1. , ,
. ,
R(k) R(k1) :
(k)

(k1)

rij = rij

(k1)

or rik

(k1)

and rkj

(8.7)

(8.7) .
R(k)
R(k1) , , ,
.
rij 1 R(k1) , 1 R(k) .
rij 0 R(k1) , 1 R(k)
, R(k1) i- k-
, k- j- 1 (
. 8.3).
j

R (k1) =

R (k) =

. 8.3.

,
. 8.2, . 8.4.
.

348

8.

a
b
R (0) =
c
d

a
0
0
0
1

b
1
0
0
0

c
0
0
0
1

d
0
1
0
0

a
(1) = b
R
c
d

a
0
0
0
1

b
1
0
0
1

c
0
0
0
1

d
0
1
0
0

a
(2) = b
R
c
d

a
0
0
0
1

b
1
0
0
1

c
0
0
0
1

d
1
1
0
1

a
b
R (3) =
c
d

a
0
0
0
1

b
1
0
0
1

c
0
0
0
1

d
1
1
0
1

a
b
R (4) =
c
d

a
1
1
0
1

b
1
1
0
1

c
1
1
0
1

d
1
1
0
1


(R (0) );
R (1) .


, 1, ..
a ( d b);

R (2) .

, 2,
.. a b ( );

R (3) .

, 3,
.. a, b c ( );

R (4) .

, 4
.. a, b, c d ( ).

. 8.4. .

Warshall (A [1..n, 1..n])


//
//
// : A
//
n
// :
//

R(0) A
for k 1 to n do
for i 1 to n do
for j 1 to n do
R(k) [i, j] R(k1) [i, j] or R(k1) [i, k] and R(k1) [k, j]
return R(n)

349

8.2

. ,
  . -,
n3 , ,
, , , (?). ,
(. 8.2.4). or,
.
,
, : .
,
( 8.2.3 , ). , , ,
,
.



(all-pairs
shortest-paths problem)
( )
.
D n n, (distance
matrix): dij i- j- i- j- (1  i, j  n).
. 8.5.
2

a
3

6
c

b
7

W =
d

a
0

b
c
d

3
0

D =

a
0

b
10

c
3

d
4

b
c

16

. 8.5. ) , ) )

350

8.

, .
(Floyds algorithm) . (R. Floyd) [36]. , ,
(,
).
n
D(0) , . . . , D(k1) , D(k) , . . . , D(n) .

(8.8)


(k)
. , dij i- j-
D(k) k = 0, 1, . . . , n)
i- j-, (
) , k. ,
D(0) ,
(.. D(0) ).
, D(n) ,
, n
, D(n) .
, D(k)
(k)
D (k1) (8.8). dij i-
(k)

j- D(k) . , dij i- vi j- vj ,
k:
vi  , k  vj

(8.9)

: ,
k- vk , ,
.
k1,
(k1)
, , dij .
?
,
, vk
( vk
). :
vi   k 1, vk ,  k 1  vj .

351

8.2

, vi vk ,
k 1, vk vj ,
k 1.
. 8.6.
dij(k1)
vi

vj

dkj(k1)

dik(k1)

vk

. 8.6. ,

vi vk , (k1)
k 1, dik ,
vk vj , (k1)
k 1, dkj , vi
(k1)

(k1)

vj vk dik
+ dkj .
:
+
,
(k)
(k1) (k1)
(k1)
(0)
+ dkj
(8.10)
k  1, dij = wij .
dij = min dij , dik
.. i- j- D (k1) i-
k- , k- j- ,
.
, . 8.5,
. 8.7.
. ,
(8.8) .
F loyd (W [1..n, 1..n])
//
//
// : W
// :
D W // , W

352

8.
2

3
c

7
1

a
b
(0)
D =
c
d

a
b
D (1) =
c
d

a
0
2

c
3

0
7

a
0
2

c
3
5
0
9

1
0


(D (0)
)


, 1,
.. a (
b c d c)

0
7

a
0
2
9
6

a
b
D (2) =
c
d

c
3
5
0
9

a
(3) = b
D
c
d

a
0
2
9
6

b
10
0
7
16

c
3
5
0
9

d
4
6
1
0


, 3,
.. a, b c (
a b, a d, b d d b)

a
b
D (4) =
c
d

a
0
2
7
6

b
10
0
7
16

c
3
5
0
9

d
4
6
1
0


, 4,
.. a, b, c d ( c a)

0
7

1
0

1
0


, 2,
.. a b ( c a)

. 8.7. .

for k 1 to n do
for i 1 to n do
for j 1 to n do
D[i, j] min{D[i, j], D[i, k] + D[k, j]}
return D
, ,
.
.
,
,
. (Richard Bellman) (principle of optimality).
, -

8.2

353

.
, (
). , , ,

. , , , .

.

8.2
1.
, :

0 1 0 0

0 0 1 0

0 0 0 1

0 0 0 0
2. ) ,
.
) ,
, .
3. , .
4. ,
Warshall , .
5. ,
,
(or).
6. ) , ,

354

8.
.
?
) ?
7.

0 2 1 8

6 0 3 2

0 4

2 0 3
3 0
8. , (8.8)
.
9.
, .
10. ,
, .

8.3
. ,
, .
(, ),
,
(
.
).
A, B, C D,
0.1, 0.2, 0.4 0.3, . . 8.8
, .
0.1 1 + 0.2 2 + 0.4 3 + 0.3 4 = 2.9,
0.1 2 + 0.2 1 + 0.4 2 + 0.3 3 = 2.1.
. ( , ?)

355

8.3
B

A
A

B
C

D
D

. 8.8. A, B, C D

, 14 .
: n n-
(Catalan number):
Cn
n > 0, c (0) = 1,
c (n) = 2n
n+1
n 4n /n1.5 (. 8.3.7).
, a1 , . . . , an ,
, p1 , . . . pn . C [i, j] , Tij , ai , . . . , aj , i j
, , 1  i  j  n. , , C [i, j]
, C [1, n].
,
, ak
ai , . . . , aj . (. 8.9)
ak , Tik1 ai , . . . , ak1 ,
j
ak+1 , . . . , aj ( Tk+1
).
, 1 ( , ),
:

356

8.
(
C [i, j] = min

ikj

pk 1 +

k1

s=i

ikj

pk +

k1


ikj



j
ps as Tk+1
+1

ps as Tik1 +

k1



=

( k1


ps +

s=1

s=i

= min

j

s=k+1

(
= min



ps as Tik1 + 1 +

j


j


j
ps as Tk+1
+

s=k+1


ps

s=k+1

ps as Tik1 +

s=i

j


ps as

j
Tk+1

j



ps

s=i

s=k+1

= min {C [i, k 1] + C [k + 1, j]} +


ikj

j


ps .

s=i

,
C [i, j] = min {C [i, k 1] + C [k + 1, j]} +
ikj

j


ps

1  i  j  n. (8.11)

s=i

ak



ai, ..., ak1



ak+1, ..., aj

. 8.9. ak j
Tik1 Tk+1

357

8.3

(8.11) , C [i, i 1] = 0 1  i  n + 1,
. :
,
C [i, i] = pi

1  i  n,

ai .

p1
0

p2
C[i,j]

pn
n+1

. 8.10.

. 8.10 , C [i, j] (8.11): i


j, j i. , ,
C [i, j].
,
pi , 1  i  n, ,
.
C [1, n]
. , , ,
k,
(8.11). , . 8.10,
, R [i, i] = i 1  i  n.

358

8.

, ,
.
1. ,
, :

A
0.1

B
0.2

C
0.4

D
0.3

1
2
3
4
5

0
0


1
2
3
4
0.1
0
0.2
0
0.4
0
0.3
0

1
2
3
4
5


0 1 2 3 4
1
2
3
4

C [1, 2]:
C [1, 2] = min


k = 1 : C [1, 0] + C [2, 2] + 2s=1 ps = 0 + 0.2 + 0.3 = 0.5
= 0.4.

k = 2 : C [1, 1] + C [3, 2] + 2s=1 ps = 0.1 + 0 + 0.3 = 0.4

, ,
, A B, 2 (.. B),
0.4.
. :

1
2
3
4
5

0
0


1
2
3
0.1 0.4 1.1
0
0.2 0.8
0
0.4
0

4
1.7
1.4
1.0
0.3
0

1
2
3
4
5


0 1 2 3 4
1 2 3 3
2 3 3
3 3
4

,
1.7. R [1, 4] = 3,

359

8.3
C
B

. 8.11.

1

, .. C. A B,
D (?). ,

. R [1, 2] = 2, , A
B, B, A (
: R [1, 1] = 1). R [4, 4] = 4,
D.
. 8.11.

.
OptimalBST (P [1..n])
//
//
// : P [1..n]
//
n
// :
//

//

//

//
R
for i 1 to n do
C[i, i 1] 0
C[i, i] P [i]
R[i, i] i
C[n + 1, n] 0
for d 1 to n 1 do
//
for i 1 to n d do
j i+d
minval
for k i to j do
if C[i, k 1] + C[k + 1, j] < minval

360

8.

minval C[i, k 1] + C[k + 1, j]; kmin k


R[i, j] kmin
sum P [i];
for s i + 1 to j do
sum sum + P [s]
C[i, j] minval + sum
return C[1, n], R
, , n; (?). ,
, .
R [i, j]
 R [i, j 1] , . . . , R [i + 1, j]
n2 .

8.3
1. ,
.
2. ) OptimalBST ?
) OptimalBST
?
3. ,
.

4. js=i ps ,
.
5. : .
6. n , ?
, n = 2k ?
7. ) ,
b (n), n -

8.4

361

,
b (n) =

n1


b (k) b (n 1 k) n > 0, b (0) = 1.

k=0

) ,
. n = 1, 2, . . . , 5.
) b (n).
?
8.



n2 .
9. .
10. . , n
A1 A2 . . . An ,
d0 d1 , d1 d2 , . . . , dn1 dn , .
, (
).
) ,
(A1 A2 ) A3 A1 (A2 A3 )
1000 .
) n ?
) n .

8.4

: n
w1 , . . . , wn v1 , . . . , vn W .
, . (
3.4, .) ,

362

8.

;
.
,
. ,
i , 1  i  n, w1 , . . . , wi ,
v1 , . . . , vi 1  j  W . V [i, j]
, .. i , j.
i ,
j, : , i- , ,
. .
1. , i- ,
V [i 1, j].
2. , i- (,
jwi  0),
i 1 , j wi .
vi + V [i 1, j wi ].
, i . , i- , , i , ,
, i 1 .
:
(
max {V [i 1, j] , vi + V [i 1, j wi ]} j wi  0,
(8.12)
V [i, j] =
V [i 1, j]
j wi < 0.
:
V [0, j] = 0 j  0, V [i, 0] = 0 i  0.

(8.13)

, V [n, W ] n , W ,
.
. 8.12 , (8.12) (8.13). i, j > 0
i- j- V [i, j]

vi , wi
, .
, .

363

8.4

wi , vi

0
0

j wi
0

j
0

i1
i

0
0

V [i 1, j wi ]

V [i 1, j]
V [i, j]

W
0

. 8.12.

1. , .
W = 5.

1
2
3
4

2
1
3
2

12
10
20
15


(8.12) (8.13) . 8.13.

w1
w2
w3
w4

= 2,
= 1,
= 3,
= 2,

v1
v2
v3
v4

= 12
= 10
= 20
= 15

i
0
1
2
3
4

0
0
0
0
0
0

1
0
0
10
10
10

j
2
3
0
0
12 12
12 22
12 22
15 25

4
0
12
22
30
30

5
0
12
22
32
37

. 8.13.

, V [4, 5] = 37. , . V [4, 5] = V [3, 5], 4 , 5 2 = 3 . V [3, 3].


V [3, 3] = V [2, 3], 3 . -

364

8.

, V [2, 3] = V [1, 3], 2


, V [1, 3 1] . , V [1, 2] = V [0, 2], ,
1 , { 1, 2, 4}.

,
(nW ). , , (n + W ). .


,
, . , ,
, (
). ,
:
, .
, . ,
. ,
,
. ; (memory
functions) [22].
, , , ,
.
NULL, , ( (virtual initialization), 7.1.8). , , , .
NULL,
;
, .

8.4

365

.
i = n (
) j = W ( ).
MFKnapsack (i, j)
//
//
// : i,
//
,
//
j
// :
//
i
// :

//
Weights[1..n], Values[1..n]
//
V [0..n, 0..W ] (
//
1,
//
0 0,
//

//
0)
//

if V [i, j] < 0
if j < Weights[i]
value MFKnapsack(i 1, j)
else
value max(MFKnapsack(i 1, j),
Values[i] + MFKnapsack(i 1, j Weights[i]))
V [i, j] value
return V [i, j]
2. , 1. . 8.14 .
, 10 20 (.. ) . ,
V [1, 2], .

.


.
,
, (?). -

366

8.

w1
w2
w3
w4

= 2,
= 1,
= 3,
= 2,

v1
v2
v3
v4

= 12
= 10
= 20
= 15

i
0
1
2
3
4

0
0
0
0
0
0

1
0
0

j
2
3
4
0
0
0
12 12
12 22
22

5
0
12
22
32
37

. 8.14.

, .
,
,
.

8.4
1. ) .
W = 6.

1
2
3
4
5

3
2
1
4
5

25
20
15
40
50

) )?
) , ,
,
.
2. ) .

8.4

3.

4.

5.

6.

7.

8.

9.

10.

367

) , , .
:
) (nW ),
) (nW ),
) , , (n + W ).
) :

.
) :

.

1. , 1) ; 2)
.
, ,
(. 3).
( ).
,

.

(change-making problem): n
d1 , d2 , . . . , dm . , n,
, .
:
) .

368

8.
) .
) .


. , . , ,
,
.
, :

.
, .


,
.


.
.

.
,
.


, , ! !
(Michael Douglas),
, (Gordon Gecko)
- (Wall Street), 1987

(change-making problem), : n
d1 > d2 > > dm , . ,
d1 = 25 (quarter), d2 = 10 (dime ()), d3 = 5 (nickel ()) d4 = 1 (penny).
, , 48 ? ,
25 , 10 1 , , ,
, .
. ,
25 ,
23 . 25 , ,
. 10 , 13 .
10 3 ,
1 .
? .
, , .
(, d1 = 7,
d2 = 5, d3 = 1), ( 8.4.9 , -

370

9.

,
).
(greedy).
,
, .
, , .

, .. ;
, ..
, ;
, .. , .
:
, ()
. , ( , ,
: ).
, . , ,

.
, ;
,
.

: .
,
,
. 9.3 . 9.4

,
. , 11.3 , .
, . , ,
, (matroid).

9.1

371

;
, [32].

9.1
: n ,
, . ,
.
,
.
1. (spanning tree)
(.. ), . (minimum spanning tree)
,
.
.

. 9.1 , .
a

5
c

2
d

3
w(T 1 ) = 6

5
d

3
w(T 2 ) = 9

5
c

w(T 3 ) = 8

. 9.1. . T1

, . ,
( ). -,
;

.
(Prims algorithm), 1957 [92].

372

9.

. ,
V .
, , ( ,
.
). , .
,
n 1, n . , , ,
.
.
P rim (G)
//
// : G =
V, E
// : ET , ,
//
G
//
VT {v0 }
//
ET
for i 1 to |V | 1 do
e = (v , u )
(v, u) , v VT u V VT
VT VT {u }
ET ET {e }
return ET
,
, , . , :
() . ,
,
,
. (
, , . ,
, , .
,
, .) -

9.1

373

T =
VT , ET

V VT .
.
u , , .
u V VT
VT .
V VT
u , , u ,
u .1
. 9.2
.
?
. , Ti , i = 0, . . . , n 1, , (.. ) . (, , ,
, Tn1 , , n .)
, T0 , ,
.
, Ti1 T . , Ti ,
Ti1 , . , ,
, Ti . ei = (v, u)
Ti1 , Ti1 Ti1 Ti .
ei , T . , ei T ,
(. 9.3).
ei = (v, u) (v  , u ), v  Ti1 u
/ Ti1 (v  v

u u, ).
(v  , u ) , , T , ei (v  , u ).
1


, u ,
.

374

9.
1

b
3
a

4
5

c
6

4
5

2
6


a(,)

b(a,3) c(, ) d(, )


e(a,6) f(a,5)

b
3

4
5

c
6

4
5

2
6
b(a,3)

c(b,1) d(, ) e(a,6)


f(b,4)

b
3

4
5

c
6

4
5

2
6
c(b,1)

d(c,6) e(a,6) f(b,4)

b
3

4
5

c
6

4
5

2
6
f(b,4)

d(f,5) e(f,2)

b
3

4
5

c
6

4
5

2
6
e(f,2)

d(f,5)

b
3
a

4
5

c
6

4
5

2
6

d(f,5)
* , ;

. 9.2.

9.1

375

u'

v'

ei

T i1

. 9.3.

, ,
,
, Ti . .
? ,
V VT (
; . 9.2 , ).
, , ,
(|V |2 ). , |V | 1 (,
, ).
,
, 6.4 (
). , .
, . ,
, . n
, O (log n),
(. 9.1.10).
,
,
O (|E| log |V |),
|V | 1 |E| (, ,

376

9.

)
|V |. , , O (log |V |). ,

(|V | 1 + |E|) O (log |V |) = O (|E| log |V |) ,


|V | 1  |E|.

,
, .

9.1
1. ,
.
2. n d1 > d2 > > dm
.
n?
3. n
t1 , . . . , tn .
.
, ,
(, , , ,
).
) .
) ?
4. ) (. 3.4).
) ?
5. . n {w1 , w2 ,
. . . , wn }, 1 W , ,

) ;
) .

9.1

377

6. ) . , .
5

2
7

4
c

3
6

5
d

) . ( ,
).
a
5
c

4
d

3
1

4
g

f
5

4
6

5
8

7. . ,
?
8. ) ?
)
?
9. , ,
, .
10.
.
?

378

9.

9.2
,
, . ,
,
. (Kruskals
algorithm) [71], (Joseph Kruskal), , -.
G =
V, E |V | 1 , (
, ). , , , .

. , ,
,
; .
Kruskal (G)
//
//
// : G =
V, E
// : ET ,
//
G
E
w (ei1 )   w(ei|E| )
//
ET
ecounter 0
//
k0
//
while ecounter < |V | 1 do
k k+1
if ET {eik }
ET ET {eik }; ecounter ecounter + 1
return ET
,
, . ,
ET ,
, , .
. 9.4 9.1 , .

379

9.2
1

b
3

4
5

c
6

4
5

2
6

bc ef ad bf
1 2 3 4

cf
4

af
5

df ae cd de
5 6 6 8

b
3

4
5

c
6

4
5

2
6

bc
1

bc ef ad bf
1 2 3 4

cf
4

af
5

df ae cd de
5 6 6 8

b
3

4
5

c
6

4
5

2
6
ef
2

bc ef ad bf
1 2 3 4

cf
4

af
5

df ae cd de
5 6 6 8

b
3

4
5

c
6

4
5

2
6

ab
3

bc ef ad bf cf
1 2 3 4 4

af
5

df ae cd de
5 6 6 8

b
3

4
5

c
6

4
5

2
6
bf
4

bc ef ad bf
1 2 3 4

cf
4

af
5

df ae cd de
5 6 6 8

b
3
a

4
5

c
6

4
5

f
2

6
df
5
*

. 9.4.

380

9.

.


. , , .
, , , , .. ,

(. 9.5). , , , , .
v

. 9.5. , , ) , )

.
,
. |V | ,
.
, . (u, v)
, , u
v, , ,
(u, v).
, ,
,
. (union-nd algorithm)
.

, . ,
, , (|E| log |E|).

381

9.2

, n- S S1 , S2 , . . . , Sk . n , S,
(, n 1,
1, S n ). ,
,
:
makeset (x) {x}. ,
S ;
f ind (x) , x;
union (x, y) Sx
Sy , , , x y,
Sx Sy , .
, , S = {1, 2, 3, 4, 5, 6}. make (i) {i},

{1} ,

{2} ,

{3} ,

{4} ,

{5} ,

{6} .

union (1, 4) union (5, 2)


{1, 4} ,

{5, 2} ,

{3} ,

{6} .

union (4, 5) union (3, 6)


{1, 4, 5, 2} {3, 6} .

(representative). - ; , ,
. ,
, ( ) .

382

9.

. , (quick nd), ; (quick union), , .


,
S; , . ,
(first) (last)
(size) (
. 9.6).
size last first
1

null

null null

null

null null

null null

null null

1
2
3
4
5
6

1
1
3
1
1
3

. 9.6. {1, 4, 5, 2} {3, 6}


. union (1, 4), union (5, 2), union (4, 5) union (3, 6).

makeset (x) , x,
x. ,
, (1), , n (n) . f ind (x) (1):
, , x .
union (x, y) .
y x,

9.2

383

y y
. , ,

union (2, 1) , union (3, 2) , . . . , union (i + 1, i) , . . . , union (n, n 1)
 
n2 ,
.
union ,
, . ,
, (,
). (union by size).
union ( (n)), union-by-size O (n log n).2
. ai S,
, Ai ai union-bysize. Ai , S
n ? ai ai , ,

, , ai . ,
ai ; .
Ai
2Ai . S n , 2Ai  n , , Ai  log2 n. ,
n S
n log2 n.
, union-by-size
n1 m O(n log n+m).
. ,
; . (. 9.7).
2

, 2. n union-by-size
, n .

384

9.

, , ,
, .
1

. 9.7. ) {1, 4, 5, 2} {3, 6} .{1, 4, 5, 2} {3, 6} )


union (5, 6)

makeset (x)
(1); , n (n) . union (x, y)
y x ( y ,
). ,
(1). f ind (x)
(
). ,
f ind O (n), , ,
n .
.
, union ,
.
( (union by size)),
( (union by rank)).
, , ,
. , , O (log n).
, n 1 m O (n + m log n).
,

(path compression). ,
, (. 9.8).

385

9.2

x
T1

T1
T2

T2

T3

T4

T3
T4

. 9.8.

,
(. [115]), n 1 m ,
.

9.2
1. .
1
)
b
c
5
a

a
5
c

b
3

5
3

6
2

f
5

4
6

4
g

5
8

2. ?
) e ,

.

386

9.
) e ,
.
) , .
) ,
.
3. Kruskal (
), ? ( ,
.)
4. ,
?
5.
..
.
6. .
7. .
8. , f ind (x)
union-by-size O (log n).
9. Web-
. .
10.

.

9.3

(single-source shortest-paths problem):
, (source),
. , ,
. (
, 3.4 ).

9.3

387

,
, , ,
.

.
,
,
8.
,
(Dijkstra algorithm).3
.
,
.
.
, ..
i-
i 1 , . ,
, Ti (. 9.9). ,
, Ti . , Ti ,
; , . (
, .) i-
u
v ( (u, v)) dv
v ( ), .
, .
.
d , . , .. (
,
3

. (Edsger W. Dijkstra) (19302002),


, 1950- . : , , .
, .
.

388

9.

v0
v*
u*

. 9.9. . . , v0 , u
, ,

).
u d.
.
u ,
.
u
.
u, u
w (u , u) , du + w (u , u) < du , u,
u du + w (u , u), .
. 9.10 .
(. 9.1).
,
. .
, , , :
,
.
.
: VT ,

9.3

389

b
3
a


a(,0)

2
7

c
6

4
d

b(a,3) c(, ) d(a,7) e(, )


3
a

b(a,3)

c(b,3+4) d(b,3+2) e(, )

c(b,7) e(d,5+4)
3
a

c(b,7)

3
a

c
6

c
6

4
d

1
2

2
7

e(d,9)

d(b,5)

e(d,9)
(
) ( ):
a b:
a d:
a c:
a e:

ab
abd
abc
abde

3
5
7
9

. 9.10. .

, Q, . (, VT
, 0.)
Dijkstra (G, s)
//
//
// : G =
V, E
//
s
// : dv s v

390

9.

//
pv
//
v V
Initialize(Q)
//
for () v V do
dv ; pv NULL
//
Insert(Q, v, dv )
//
ds 0;
// s ds
Decrease(Q, s, ds )
VT
for i 0 to |V | 1 do
//
u DeleteM in(Q)
//
VT VT {u }
for () u V VT , u do
if du + w (u , u) < du
du du + w (u , u); pu u
Decrease(Q, u, du )
,
. , 9.1, (|V |2 ) , , . ,
, , O (|E| log |V |). , , ,
(Fibonacci heap) (., , [121]).
.

9.3
1. , ( )
/ .
)
.
) (

9.3

391

(single-pair shortest-path problem)).


) ( (singledestination shortest-paths problem)).
)
,
( , ).
2.
a .
1
)
b
c
3
a

a
5
c

b
3

5
3

6
2

4
6

5
8

5
3

4
g

3. , ,
.
4. T , G.
?
) T G.
) T G.
5. , (.. ,

392

9.
) ,
.
6. .
7.
,
.
8. . (
, ,
, .)
9. , , ( ),
( ).
) , .
) ,
.
10. 1.3.6 ,
, .
.

9.4
, , n- , , (codeword). ,
(xed-length encoding),
m
(m  log2 n). , ASCII. ,
,
,
, . ( , ,
, XIX (Samuel Morse).

9.4

393

, e () a ( ),
, , q ( )
z ( ), .)
(variable-length encoding),
, , , : ,
(, ,
i-) ? , (prex code).
. , ,
, , ,
.
,
,
0, 1 ( ).

. ,
, , ,
; , .
, , ,

? , (David Huffman)
[56].

1. n . . ( ,
.)
2. ,
. (
, . 9.4.2) ,
.
, ,
(Huffman tree), , , (Huffman code).

394

9.

1. {A, B, C, D, _}
:

A
0.35

B
0.1

C
0.2

D
0.2

_
0.15


. 9.11. :

A
0.35
11

B
0.1
100

C
0.2
00

D
0.2
01

_
0.15
101

, DAD 011101, 10011011011101


BAD_AD.

2 0.35 + 3 0.1 + 2 0.2 + 2 0.2 + 3 0.15 = 2.25.
,
.
, (compression ratio),
, (3 2.25)/3 100% = 25%. ,
, ,
, 25% , ( ,
, , 20% 80%,
).

.
,
(.. ) ( , ).

. ,
. ,
. (dynamic Huffman encoding),

395

9.4
0,1

0,15

0,2

0,2

0,35

0,2

0,2

0,35

0,25

A
0,1

0,15

0,35

0,25

0,4

A
0,1
B

0,15

0,2

0,2

0,4

0,4

0,2

0,2

0,35

0,25

A
0,1

0,15

0,4

0,4

0,4

0,2

0,2

1
0,35

0,25
0

0,1

0,15

. 9.11.

,
(., , [101]).
, . , n w1 , w2 , . . . , wn ,
n ,
 .
ni=1 li wi , li

396

9.

i- , ? ,
( li wi , , i- ).
, . , , n (, 1 n)
, .
, ,
(decision trees)4 . n = 4
. 9.12.
n>2

n>1

n=1

n=2

n=3

n=3

n=2

n=4

n=4

n=3

n>3

n=4

n=1

n=2

. 9.12. 1 4

, ,
. i

pi , ni=1 li pi ( li
i- ) , ,
.
, 1/n,
( ) ,
. pi (, n = 4, p1 = 0.1, p2 = 0.2, p3 = 0.3 p4 = 0.4
,
. 9.12 ). ,
.
, , . 8.3
( ), 4

10.2.

397

9.4

. .
: ,

.

9.4
1. ) :

A
0.4

B
0.1

C
0.2

D
0.15

_
0.15

) ABACABAD.
) ,
) 100010111001010.
2.
( ). ,
:

A
0.1

B
0.1

C
0.2

D
0.2

E
0.4

3. ,
.
) .
)
.
4.
n ?
5. ) .
) n ?
6. , ,
.

398

9.
7.
?
n ?
8. ,
.
9. ) ,
(, )
.
) ,
.
)
1000 .
) ,
.
10.
, [39].
, , , ..
, 45 . . , ,
.


, ,
.
, , .
.
, , .


.
, -

399
.
.

() . , , .
.
, ,
.
.

,
.
,
,
.

10


; . .
(Max Born) (18821970)
(My Life and My Views), 1968

.
: ,
. ,
.
, . , .
,
.
10.1 , .. .
,
, . ,
. , .
,
, , ,
, 1895 ,
. . .
10.2 .
, , . ,
, ,

402

10.

( ?). ,
, (. 5.5).
10.3 : , . .

, P , N P N P -,
P
NP.
. , ,
sin x ln x, ..
. -, .
-, ,
.
. ,

.

10.1
. (, )
, (. 2.2). , , ,
, . ,
, . ,
, .
,
O (n log n); ,

10.1

403

,
.
,
,
, ,
. (lower bound) ,
. (tight), ..
, ,
,
.
, :
, ,
.
. ,
. ,
, . , , n
(n) (?), , , ,
3 (n 1)/2 ( n).



, .
,
, , (trivial lower bound). , n (n!),
n!. ,

, (. 5.4).
n
p (x) = an xn + an1 xn1 + + a0

404

10.

x an , an1 , . . . , a0 . ,

. , , x. ,
(n). ,
( 6.5.2)
( 6.5) .



2
n n n ,
2n2 n2
. , .
,  
. , n2 ,
n (n 1)/2 ,
n + 1 , .
, ,
.

. ,
. ,
(?).
,
. ,
n (n 1)/2 ,
.

-
, -
, .
, ,
1 n ,
. , , log2 n ,
n .
(, )
( ). ,

10.1

405

log2 n ,
.
, , - (information-theoretic argument) -
. , - (information-theoretic
lower bounds) ,
. , , (decision
trees). - , 10.2.


, - . , , ,
log2 n , , , . 1 n. (,
, , .) ,
,
. ( ,
.)
, ,
, . ,
log2 n n-
. ,
log2 n .
(adversary method) . , ,
, ,
.
,

.
n :
a1 < a2 < < an b1 < b2 < < bn

406

10.

2n. , a
b , .
4.1. ,

.
, ,
2n 1.
, ? . [67]
, 2n 1 ,
, , . :
ai < bj , i < j.
, :
b1 < a1 < b2 < a2 < < bn < an .
, 2n 1 , .. b1 a1 , a1 b2 ..
, , a1
b2 ,
b1 < b2 < a1 < a2 < < bn < an ,
, , . , 2n 1 ,
.


6.6.
P
Q, . . ,
P , Q
, Q P ( P Q). ,
, Q ( ) P , ,
P , Q.
Q P . . 10.1
, .

10.1

407


10.1



n-


(n log n)
(log n)
(n log n)
(n)
 
n2

. , n
( 2.3 6.1).

, (., , [90]). ,
, . 10.1, , ,
.
: , n
. .
x1 , x2 , . . . , xn n n
, 0 y: (x1 , 0),
(x2 , 0) , . . . , (xn , 0). T , . T , ,
T , . ,
(n log n).
, . ,
(x + y)2 (x y)2
x2 = x x
4
, n- n-
,
.
. ,
,
. xy =

408

10.

,
, n, ,
A B,




0 BT
0 A
Y =
,
X=
B 0
AT 0
AT B T A B (.. ATij = Aji
T = B , 0 n n (
Bij
ji
0). ,

 


AB
0
0 A 0 BT
=
,
XY =
0 AT B T
AT 0 B 0
AB (, ,
, ).
, 10.3
.

10.1
1. ,
( 2.4) , .
2.
, .
) .
) , .
) n- .
) , n .
3. n .
- ,
, , log2 n
?

10.2

409

4. , n
n 1 .
5. ,  ,


2
n , n , ,
. ?
6. ,
, ,
n n + 1 ? .
7. A B ,




1 1
0 1
A=
B=
.
2 3
1 2
8. ) ,
,
?
) , n 2n.
9. n x1 , x2 , . . . , xn .
10. , n m .
nm . , .
.

10.2
, ,
.

(decision tree). . 10.1 .

, k < k . , , k < k ;

410

10.

k > k (
, ).
n. , , ,
(
. 10.1). ,
.
n
;
. ,
.

a<c

a<b

b<c

. 10.1.

, , , ,
. , , l
h
(10.1)
h  log2 l .
, h
(?). ,
2h . , 2h  l,
(10.1).
(10.1)
, , , , . --

10.2

411

(. 10.1).
: .


,
.. . ,
(. 5.1.9), . ,
, ,
.

, . , a < c < b, a, b, c (. . 10.2), 1, 3, 2. ,
n-
n!.
abc

a<b

abc

a<c

abc

a<b<c

b<c

abc

bac

cba

a<c<b

b<a

b<c

c<a<b

b<a<c

a<c

cba

b<c<a

b<a

c<b<a

. 10.2. .
.
b < a ,

(10.1) ,
( ,
, )

412

10.

log2 n!:
Cworst (n)  log2 n! .

(10.2)

n!,

log2 n log2 2
+
n log2 n.
log2 n! log2 2n (n/e)n = n log2 n n log2 e +
2
2
, n log2 n n- , . ,
, ,
.
n log2 n, , .
, , log2 n! n. , log2 12! = 29, ,
12 ( )
30 .
, , .
, ..
. , , . 10.3,
(2 + 3 + 3 + 2 + 3 + 3)/6 = 2 23 .
, n! ,
, n- :
Cavg (n)  log2 n!.

(10.3)

, n log2 n. ,
. , ,
,
. , , .

10.2

413


abc

a<b

abc

a<b<c

bac

b<c

acb

a<c<b

a<c

c<a<b

b<a<c

a<c

bca

b<c<a

b<c

c<b<a

. 10.3.

n A [0] < A [1] < < A [n 1]. .


4.3, ,
bc
(n),
, Cworst
bc
(n) = log2 n + 1 = log2 (n + 1) .
Cworst

(10.4)

, ,
.
, K A [i]
K < A [i], K = A [i] K > A [i],
. . 10.4
n = 4. , .
, ,
, .

, . 10.4. n
2n + 1 (n n + 1
). h l log3 l,
:
Cworst (n)  log3 (2n + 1) .

414

10.

A[1]
<
A[0]
<
<A[1]

=
A[1]

>

A[1]

A[2]

>

<

(A[0], A[1])

(A[1], A[2])

>

A[2]

A[3]
<

(A[2], A[3])

>

A[3]

>A[3]

. 10.4.

,
, log2 (n + 1) n ( log2 (n + 1) n . 10.2.7).
?
, , , . 10.5.
, ,
. ,
, , n + 1
n- .
A[1]
<

>

A[0]
<
<A[1]

A[2]
>
(A[0], A[1])

<

>

(A[1], A[2])

A[3]
<
(A[2], A[3])

>
>A[3]

. 10.5.

10.2

415

. 10.4 10.5, ,

. (10.1)

Cworst (n)  log2 (n + 1) .

(10.5)


, ,
log2 (n + 1). (., , [67],
6.2.1) , .
log2 n 1
log2 (n + 1) .

10.2
1. ,
) h  log2 l h
l.
) h  log3 l h l.
2.
{a, b, c}.
) - , ?
) ,
.
) - ,
, ,
? ( ,
.)
3.

) ;
) ( ,
).

416

10.
4. .
5.
.
6.
.
7.
log3 (2n + 1) log2 (n + 1) ,
) log3 (2n + 1)  log2 (n + 1) n;
) log3 (2n + 1) < log2 (n + 1) n  n0 .
8. . n  3 ; ,
. , , . ,
. ..
, , , , .
, , ,
, ,
.
) ,
log3 (2n + 1)
.
) , n = 3 .
9. ) , ,
8 n = 4 .
) , 8 n = 4
, ,
.
) , 8 n = 12
.
10. , :
n , , -

10.3

P , N P N P -

417

, .
, .
) , ?
) ?
) , , , .
?

10.3

P , N P N P -

, , , .
1. ,
, O (p (n)), p (n) n. ( , , O, , ,
, , .)
, ,
(tractable), ,
, (intractable).

. , . 2.1 2.1 ,
,
. -,
O (P (n)),
. , , ,
. -,
; , ,
. -,
(computational complexity),
. ,
.
.

418

10.

, , (, [110, 86]).

P N P -
, ,
. , ,
( ), ,
.
( .) , ,
, , - P .
P (decision problems),
, .
2. P , ()
. .

P
. -, ,
-
. ,
n , ,
. -, , ,
, . , , ,
, , , m
m = 1, 2, . . . . ( m m (m-coloring problem).) m ,
m- ,
.
: ? , .
, . (undecidable).

10.3

P , N P N P -

419

(Turing) 1936 .1 (halting problem):


,
.
.
, A , ,
.. P I
(
1, P I;
A (P, I) =
.
0, P I.
P A (P, P ) Q
:

, A (P, P ) = 0, .. P

P ;
Q (P ) =

, A (P, P ) = 1, .. P

P .
, P Q,

, A (Q, Q) = 0, .. Q

Q;
Q (Q) =

, A (Q, Q) = 1, .. Q

Q.
, Q
, .
, ? ,
, , ,
.
, ,
. [40]
1

,
(Alan Turing) (19121954). ACM (Association for Computing Machinery ), ,
, , .
(Richard Karp) [59]
.

420

10.

, . ,
.
. , (,

).
. n (
).
. n
,
.
. n ;
, .
. n ,
, 1.
1.
.
( ,
,
).
.
( )
/ .
, ,
, (, , m- ).
, ( ) n. , ,
, . , , .. ,

, 
 2
O n ,

10.3

P , N P N P -

421

. : ,
( ) ,
( ).

, ,
,
(
-
). , ,
n . ,
, , n + 1 , n
, ,
.
.
3. (nondeterministic algorithm) , I .
(): S, I (
).
(): I S , S
I. ( S I, , .)

,
,
( , ,
, . , , , ,
). , (nondeterministic polynomial),
.
N P -.
4. N P ,
.
(nondeterministic
polynomial).


422

10.

N P .
P : P N P . ,
P , , , , S,
. , , N P
, , , ,
[40]. , , , N P .
: P N P
?
?

P = N P.
. P = N P ,
,
. ,
NP- (N P complete). , N P - N P ,
, ,
N P
( . 10.6).
.
5. D1 (polynomially reducible) D2 ,
t, D1 D2 ,
1. t D1 D2 , D1 D2 ;
2. t
.

, D1 D2 , , D1
(?).

10.3

423

P , N P N P -
NP$

NP$

. 10.6. N P - . N P - N P -

N P -
N P .
6. D NP- (N P -complete),

1. N P ;
2. N P D.

, , . , ,
.
,
m. G G ,
, , 1, G,
2 , G.
m n = m, n G ( G ). ,
.
G . G , G n,

424

10.

. : G n,
n (?), , , G,

. .
N P - , ,
N P , , , .
,
N P - . (Stephen Cook)
.2 [30], 1971 , , CNF- N P . .
, , x1 , x2 x3
2 x
3 :
, x
1 , x
2 x
2 ) & (
x1 x2 ) & (
x1 x
2 x
3 ) .
(x1 x
CNF- ,
true f alse ,
true. ( ,
: x1 = true, x2 = true x3 = f alse,
true.)
N P - ,
, N P - . ,
( ), ,
, , ,
N P - . ,
, P = N P , N P -,
P -, N P - .
, . 2002
(Manindra Agrawal)
(Neeraj Kayal) (Nitin Saxena)
2

, .
, N P -, ,
[73] .

10.3

425

P , N P N P -

[3]. , ,
,
,
RSA [96].
, N P -,
. , N P -,
..
.
. , , N P
.
, N P -
(. . 10.7). , ,
N P . , , N P ,
, N P - ( ,
N P ).
NP$

NP$

NP$

. 10.7. N P -

N P - ,
N P - ,
N P , , P = N P . , N P -

426

10.

,
.
, P = N P ,
. , 15 - [106]
,
, , P = N P .
?
P = N P , , N P -, . , , N P - ,
,
.
.
.

10.3
1. : , ,
, . ?
2.



O nlog2 n . ?
) .
) .
) .
3. ,
:
) , ;
) , ;
) , ;
) , ,
, .
4. .

10.3

427

P , N P N P -
)

a
e

b
d

f
d

c
c

5. 2- : ,
,
.
6.
: 2
n/2 n. n
, (.. ),
.
P ?
7.
, .
(, .)
)
)
8. ,
.
9. ,
.
G = (V, E) m  |V |,
G (clique) m . ( k k .)
G = (V, E) m  |V |,
(vertex cover) G
m. ( k G = (V, E)
V  V , |V  | = k
(u, v) E u v V  .)

428

10.
G = (V, E) m  |V |,
G (independent set) m. ( k
G = (V, E) V  V , |V  | = k
u, v V  u v G.)
10.
P , N P N P C (N P - )?
P = NP
)
)
P = NP = NPC
NPC

)
P

NP

NP

NPC

NPC

NP

)
P

NPC

11. 150 .
, (
, ). , , ,
.
)
?
) ,
,
75 .

10.4
, .
:
, -

10.4

429

, sin x ln x, ..
, , , ,
. ,
, . ,
. ,
,
30 . , ,
. ,
,
.
.
, ,
? ,
.3 ,
. , ex
x
x = 0 ,
n- :
ex 1 + x +

xn
x2
+ +
.
2!
n!

(10.6)


, :
b
a



n1

h
f (a) + 2
f (x) dx
f (xi ) + f (b) ,
2

(10.7)

i=1

h = (b a)/n, xi = a + ih i = 0, 1, . . . , n (. 10.8).
4 (truncating error).
3

, 6.2 6.5 , .
4
, . . .

430

10.

h
a

h
x1

x i1

h
xi

h
x i+1

x n1

. 10.8.

. . , (10.6) :
'
.'
2
n '
' x
M
'e 1 + x + x + + x ' 
|x|n+1 ,
(10.8)
'
2!
n! ' (n + 1)!
M = max e 0 x.
, (10.6).
, e0.5 (10.6)
104 , . , M (10.8):
M = max e  e0.5 < 2.
00.5

104 , (10.8)
2
M
|0.5|n+1 <
0.5n+1 < 104 .
(n + 1)!
(n + 1)!

2n
2
0.5n+1 =
,
(n + 1)!
(n + 1)!
n, : 5.

10.4

431

, (10.7)

'b

''
'
n1

' (b a) h2
'
' f (x) dx h f (a) + 2
M2 ,
f (xi ) + f (b) '' 
(10.9)
'
2
12
'
'
a

i=1

M2 = max |f  (x)| a  x  b.
.
, (round-off error),
, . (
), .

(10.10)
.d1 d2 . . . dp B E ,
B , 2 16 (, 10), d1 , d2 , . . . , dp (0  di < B i = 1, 2, . . . , p d1 > 0,
0), (mantissa); E ,
(exponent) , 0.

(signicant digits) p (10.10). : (single precision) ( 6 7 ),
(double precision) ( 13 14 )
(extended precision) ( 19 20
). , ,
.
.
,
(absolute error) (relative error)
:
= | | ,
| |
=
.
| |
( = 0.)

(10.11)
(10.12)

432

10.

- , (overow) , (underow).

. ,

(,

 29
30
30
29
10 11 /12 = 10 (11/12)30 ), (2 100!/(2! (100 2)!), (100 99)/2)
, C100
.
, ,
. ,
, , .
,
, , . ,
.
(subtractive cancellation).
1.
= = 3.14159265 . . . = 6 107 = 3.14159205 . . . ,
= 0.3141593101 = 0.3141592
101 . :
4
0.0000003 . . .
| |
< 107
=

0.00000005 . . .
1
| |
=
< 107

6 10
3

. = =
| |
106 6 107
2
=
= ,

6 107
3
,
.


10.4

433

, , ( 6.2
). ,
,
.
. (instability).
, .
(ill-conditioned).
2.
:
1.001x + 0.999y = 2,
0.999x + 1.001y = 2.
x = 1, y = 1. ,
,

:
1.001x + 0.999y = 2.002,
0.999x + 1.001y = 1.998.
x = 2 y = 0,
. , (?). ,
,
.

(, [41]).


ax2 + bx + c = 0

(10.13)

434

10.

a, b c (a = 0).
, (10.13)
, D = b2 4ac ,

b b2 4ac
.
(10.14)
x1,2 =
2a
(10.14) , .

. D D ,
.
, , , ( ,
, 11.4). {xn } D, D ,



1
D
xn +
n = 0, 1, . . . ,
(10.15)
xn+1 =
2
xn
x0 (
) x0 = (1 + D)/2. ,
(10.15)
( D = 1) D.
> 0
xn xn+1 < ,
x2n+1 D.
(10.15) D D. , , 0.25  D < 1,
, ,
'
''
'

D' < 4 1015 ,


'xn
d [0.25, 1)
d = D2p , p .

3. 2 ( ).

10.4

435

.
= .
x0 =
x1 =
x2 =
x3 =
x4 =

1
(1 + 2) = 1.500000,
2

1
2
.
x0 +
= 1.416667,
2
x0


1
2
.
x1 +
= 1.414216,
2
x1


1
2
.
x2 +
= 1.414214,
2
x2


1
2
.
x3 +
= 1.414214.
2
x3

.
, x4 = x3 = 1.414214,, . 2 1.41421356 . . . . 
, , (10.14)? , .

. b2 4ac, b2 4ac
|b|, , (10.14),
.
4. (George Forsythe)5 [38]

x2 105 x + 1 = 0.

.
.
x1 = 99999.999990 x2 = 0.000010000000001.
(10.14) , , ,
.
(b)2 = 0.1000000 1011 ,
.
4ac = 0.4000000 101 ,
.
D = 0.1000000 1011 ,
.
D = 0.1000000 106 ,
5
(19171972), ,
.
.

436

10.

b + D
x1 =
2a
b D
x2 =
2a

.
= 0.1000000 106 ,
.
= 0.

x1 x1 ,
:
|x2 x2 |
= 1,
x2
.. 100%.

, , :

b + b2 4ac
=
x1 =
2a



b + b2 4ac b b2 4ac


=
=

2a b b2 4ac
=

2c

,
b b2 4ac

, b > 0.
x2 ,

b b2 4ac
x2 =
2a
b.
b < 0 :

b + b2 4ac
x1 =
2a

2c

.
b + b2 4ac
( b = 0 .)
(10.14),
: a ,
; , b2 4ac,
, ..
x2 =

10.4

437

(William Kahan) ,
.
,
, .
.

10.4
1.
k,

| |
< 5 10k .
| |

2.

3.

4.
5.

, ,

) 3.1415?
) 3.1417?
, = 1.5
, 102 .
) ;
) .

e = 1.648721 . . . ,
0,
.
, ?
(10.7).
n = 4 .

(10.9).
1
3
) x1 dx
) x2 dx
0

6.

1
0 esin x dx ,
104 ? 106 ?
7. ,
.

438

10.
)

2x + 5y = 7
2x + 5.000001y = 7.000001

2x + 5y = 7
2x + 4.999999y = 7.000002

8. ax2 + bx +
+ c = 0.
9. ) , D
, D,
D

x0 > D.
) , 0.25  D < 1 x0 = (1 + D)/2,
, ,

'
''
'
'xn D' < 4 1015 .

10. 3 .


, .
, , , .
-
. ,
. , log2 n! n log2 n
, log2 (n + 1) .
, .
,
.. .

439
.
,
. , .. ,
/.

, .. .
P ,
. N P ,
,
.
N P ( ) N P -: N P . N P - . CNF.
, P N P
P = N P .
. N P - ,
P = NP.
, .
( ), . , .

,
( ,
).
ax2 + bx + c = 0 .
;
,
( ,
b) b2 4ac .

11



. ,
.
(Thomas Edison) (18471931)

, . ,
.
.
11.1 11.2
(backtracking) (branch-and-bound)

. , 3.4.
: , , ,
.
,
, .
(statespace-tree), , . , ,
, , . ,
. , . ,
, .

442

11.

.
( ).
;
, 11.2.
11.3 .
, . ,
.
, .
,
,
.
11.4 .

: ,
.

11.1

( , 3.4 10.3) , ,
( ) : ,
.. 10.3
, -
. , 3.4 ,
( ) .
- ( )
.

.
, . ,

. , .

11.1

443


.
,
(state-space tree).
. ,
,
..
(promising), ,
;
(unpromising). , , .
. ,
,
. ,
; ,
. ,
, (
), .

n
n . n n n , ,
, . n = 1 ; , n = 2 n = 3
.
.
, , , ,
. 11.1.
1
; 1. 2
( 1 2) 3, (2, 3). ,
,
. ,
(2, 4).

444

11.
1

. 11.1.

(3, 2), .
1 (1, 2). 2 (2, 4),
3 (3, 1), 4 (4, 3), .
. 11.2.
(
?),
, .
.


, . 11.3.
, ,
a. , a (. 11.3). ,
, . , a, b.
b c, d, e , , f ,
. c
, . c e ,
b. b f , e, c d,
a,
a, b, f , e, c, d, a. - ,
, .

11.1

445

1
Q

2
1
x

2
x

3
1
x

Q
Q

2
x

3
x

4
1
x

2
x

3
x

4
x

1 Q
x

3
x

Q
Q

7
Q

4
x

8
1
x

2
x

3
x

4
x

1
x

2
x

Q
Q
Q
Q

. 11.2. .
.



(subset-sum problem):
S = {s1 , . . . , sn }, n , , d. ,
S = {1, 2, 5, 6, 8} d = 9 {1, 2, 6} {1, 8}.
, .
.
,
s1  s2   sn .

446

11.
0
a
1
b
2
a

b
c

3
d

e
5

10
11
12

. 11.3. ) . )
.

,
. 11.4 S = {3, 5, 6, 7} d = 15.
,
. , , s1 .
, s2 ,
. , i- ,
i ,
.
s , , . s d,
. , , , . s d,

:
s + si+1 > d ( s ),
n

sj < d ( s ).
s +
j=i+1

11.1

447


0
3

8
6
14
x
14+7>15

8
7
15

9
x
9+7>15

5
5
6
3
x
3+7<15

6
11
x
11+7>15

0
x
0+13<15

5
x
5+7<15

8
x
8<15

. 11.4. , S = {3, 5, 6, 7}
d = 15. ,
, .


.
n (x1 , x2 , . . . , xn ), xi Si . , n
Si ( )
1 n. , (,
n ). -
(, , n )
( ).
, , ;
i , . (x1 , x2 , . . . , xi )
, Si+1 , (x1 , x2 , . . . , xi ) ,
(i + 1)- . ,
xi , ..

i = 0; X [1..0] .

448

11.

Backtrack (X [1..i])
//
// : X[1..i], i
//

// : ,
//

if X[1..i]
write X[1..i]
else
// . 8
for () x Si+1 ,
X[1..i] do
X[i + 1] x
Backtrack(X[1..i + 1])


, .

( )
. ,
,
( , ).
, ,
.
, . . , n
,
. , , ,
n/2 , (1, i), n/2  i  n,
(?) ,
(1, n i + 1).
.
, .
.

. , , . [62]

11.1

449

,
, . , c1
x1 , . (
1/c1 ) c1
. c2 x2 , x1 , c2
.
, x1 , x2 , . . . , xn
. , i ci
,
1 + c1 + c1 c2 + + c1 c2 . . . cn .
, ,
.
.
-, , , , . -, ,
,
, .
, .
, . , -,

,
, .

11.1
1. ) ,
, .
) ,
.
2. ) ,
?
) .

450

11.
3. ) n
.
n .
-, .
) n,
a ,
, 11.1,
.
4. :
a
c

b
e

d
f

5. 3- ,
. 11.3.
6. {1, 2, 3, 4}
.
7. )
: S = {1, 3, 4, 5} d = 11.
) ?
8. ,
11.1, ,
, .
,
.
9. ,
) ;
) m- .
10. Hi-Q. - 32 ,
, .
, ; ,

11.2

451

, .
31 , .

11.2

,
, , , .
, , ( , ,
..). ,
(feasible solution)
,
(, ,
, ),
(optimal solution) (, ,
, ).
:

1 ,
, ;
, .
1

452

11.

,
, : ..
,
( , ), , .
.
.
, .
- , .
, ,
(, )

, .


, , n , n ,
.
3.4, . ,
C nn,
: , ,
.

, 3.4:

1 2 3 4
9
2
7
8
6
4
3
7
5
8
1
8
7
6
9
4

a
b
c
d

11.2

453


? . , , , ,
. 2 + 3 + 1 + 4 = 10. ,
(3 1
), , . ( )
. , , 9,
9 + 3 + 1 + 4 = 17.
, . .
, ,
(, ..
(live)). , ?
, . , , ,
, .
(best-rst branchand-bound).
, , , -
. ,
( lb) 10. () ,
(. 11.5).
, ( 1 4), . 2,
. , ,
. ,
, , , b (. 11.6).
( 1, 3, 4, 5, 6 7), , , 5. c
(.. 3 c) , 4 d, 8 (. 11.7),

454

11.
0
start
lb=2+3+1+4=10

1
a

lb=9+3+1+4=17

lb=2+3+1+4=10

lb=7+4+5+4=20

lb=8+3+1+6=18

. 11.5. 0 1
, . .
a
lb
0
start
lb=10

1
a

lb=17

lb=10
5
b

lb=13

lb=20

6
b

lb=14

4
a

lb=18

7
b

lb=17

. 11.6. 0, 1 2 ,

{a 2, b 1, c 3, d 4} 13. 9
{a 2, b 1, c 4, d 3} 25.
, 8, 9
( 13, ,
).
,
( 1, 3, 4, 6 7 . 11.7), ,

, ( 13, 8). , , , 8,
.

455

11.2
0
start
lb=10

1
a

lb=17
x

lb=10
5
b

lb=13

lb=14
x

4
a

lb=18
x

7
b

lb=17
x

3
d4
cost = 13

4
d3
cost = 25

lb=20
x

. 11.7. ,

, , , ,
(., , [87]).
, .


.
3.4: n w1 , . . . , wn v1 , . . . , vn , , W .
,
.
( ),
:
v1 /w1  v2 /w2   vn /wn .

, (. 11.8).
0  i  n n , i .

456

11.

: ,
, ,
. w v , ,
ub ,
(, )
.
0
w=0, v=0
ub=100
1

1
w=4, v=40

2
w=0, v=0

ub=76
2

ub=60
x
, 8

3
w=11

4
w=4, v=40
ub=70

5
w=9, v=65

6
w=4, v=40

ub=69

ub=64
x
, 8

4
7
w=12

4
8
w=9, v=65
ub=65

. 11.8.

ub v
W w
, vi+1 /wi+1 :
ub = v + (W w) (vi+1 /wi+1 ) .

(11.1)


, 3.4 -

457

11.2

(
). W = 10.

1
2
3
4

4
7
5
3

40
42
25
12


10
6
5
4

. , , 0.
, (11.1), 100. 1,
, ,
, 1; , , 4 40,
40 + (10 4) 6 = 76. 2 , 1, w = 0, v = 0
ub = 0 + (10 0) 6 = 60. 1 ,
2, ,
1. 3 4 1 2, .
, 3, ,
. 4
, ,
ub = 40+(10 4)5 = 70. 2 4
4 (?) 5 6, , , 3.
, . 5 7,
, 8,
{1, 3}. ( ,
8 .)
2 6 , , 8. , ,
{1, 3} 8 .
.
, (., , , ).
.

.
, 2 6 -

458

11.

8,
5, 65.


, .

n.
, . ( 11.2.8), l
. i- (1  i  n) si i ,
n 2. ,
:
lb = s/2 .

(11.2)

, , . 11.9, (11.2)

lb = [(1 + 3) + (3 + 6) + (1 + 2) + (3 + 4) + (2 + 3)]/2 = 14.


, , ,
(11.2). , . 11.9,
(a, d), ,
( (a, d) (d, a)):
[(1 + 5) + (3 + 6) + (1 + 2) + (3 + 5) + (2 + 3)]/2 = 16.
(11.2)
, . 11.9.
, , 3.4. -, , a. -,
, ,
b c. : n 1 = 4
, .
,
. 11.9, . 11.9.
, ,

459

11.2
3

0
a

lb=14
5
8

2
a, c

1
a, b

lb=14
x
b
c

3
e

8
a, b, c, d,
(e, a)

5
a, b, c

6
a, b, d

7
a, b, e

lb=16

lb=16

lb=19
x
lb > l
11

9
a, b, c, e,
(d, a)

10
a, b, d, c,
(e, a)

l=24

l=19

l=24

3
a, d

4
a, e

lb=16
x
lb l
11

lb=19
x
lb > l
11

11
a, b, d, e,
(c, a)
l=16

. 11.9. ) . ) .
,

. , . , ,
,
, .
, , .
,
.
, , ,
. ,
, , - .

, .

460

11.

, , , , . (, , ,
.)
. , , .
,
.

.

11.2
1.
?
2. ,
,
, ,
, .
3. )
, .
) ,
?
4. . ,
,
.
5. :

1
2
3
4

W = 16.

10
7
8
4

100
63
56
12

11.3 N P -

461

6. ) , .
) , 5.
7.
.
8. ) ,
(11.2)
.
) (11.2)
?
9.
( 3.4
).
2

3
8

10.
, , -.
- .

11.3
N P -
, , .
10.3,
N P -.
NP- (N P -hard) , ,

462

11.

N P - .2 , , ,
, ,
. ,
?
, (. 3.4).
, 8.4.
,
.
,
.
.

. , ,
. , .
.
,
, . (heuristic)
, , .
, , . , ,
.
, , , ,
. sa f

f (sa ) f (s )
,
re (sa ) =
f (s )
s . , re (sa ) =
= f (sa )/f (s ) 1, sa 2

N P -
, N P ,
, (. 5 [40]).

11.3 N P -

463

(accuracy ratio)
r (sa ) =

f (sa )
.
f (s )

,
r (sa ) =

f (s )
,
f (sa )

, , 1.
, r (sa ) 1, . (.. )
r (sa ), , (performance ratio) RA .
,
. , ,
RA 1, , , ,
RA = ). , ;
.
, c- (c-approximation algorithm),
c, ..

f (sa )  cf (s ) .
, , .
, ,
. ,
, .

3.4 . 10.3 , N P - , 11.2 ,


.
.

464

11.


:
.
1. .
2. ,
: ,
( ).
3. .
1. , . 11.10, a ,
( ): sa : a b c d a 10.
1

2
3

3
d

. 11.10.

, ,
s : a b d c a 8. ,

r (sa ) =

10
f (sa )
=
= 1.25

f (s )
8

(.. sa 25% s ).

, ,
. ,
,
. , 1
(a, d) 6 w  6,
a b c d a,
4 + w, a b d c a
8. ,
r (sa ) =

4+w
f (sa )
=
,
f (s )
8

11.3 N P -

465

,
w. , RA = .
, , . , ,
. , ,
. , :

d [i, j]  d [i, k] + d [k, j]

i, j k

( i j i j k);

d [i, j] = d [j, i]

i j

( i j j i).
(., , [95]),
, n  2

1
f (sa )
 (log2 n + 1) ,
f (s )
2
f (sa ) f (s ) , ,
, .

.
.
1. , .
2. , ,
.

466

11.

3. , 2,
, .
, , ,
.
2. , . 11.11. (a, b), (b, c), (b, d) (d, e)
(. . 11.11). , f , a, b, c, b, d, e, d, b, a.
12

9
8

12

8
6

12
c

. 11.11. . ) . )

b, d b,
a, b, c, d, e, a 41.

, 2, .
,
,
. , ,
, f (sa )/f (s ).
, .
1. 2- .
. , ,
, .

11.3 N P -

467

, sa , ,
s , ..
f (sa )  2f (s ) .
s T w (T ),

w (T ),
f (s ) > w (T )  w (T ) .
,
2f (s ) > 2w (T ) = , 2 .
3 ,
sa , , ..
, 2  sa .
,
2f (s ) > f (sa ) ,
,
.


. , (Christodes algorithm), , , , 1.5 [40].


? [100], ,
P = N P .
2. P = N P , c- , .. ,
f (sa )  cf (s )
c.

468

11.

. . ,
A c (
, c ). ,
. , 10.3
. G
n . G
G , 1 cn + 1
, G. G
, G n; , s G . , sa
, G A, f (sa )  cn . G , G cn + 1, , f (sa )  f (s ) > cn.

G , G G ,
A G
cn. N P -, , P = N P .



N P - ,
3.4. n w1 , . . . , wn
v1 , . . . , vn , , W . ,
. , ( 3.4), ( 8.4)
( 11.2).
.

.
; ,
.
, .
, , ? , : vi /wi ,
i = 1, 2, . . . , n
(

11.3 N P -

469

11.2). ,
.
1. ri = vi /wi ,
i = 1, 2, . . . , n.
2. , 1 ( ).
3. , : , ;
.
3. 10 :

1
2
3
4

7
3
4
5

42
12
40
25


, :

3
1
4
2

4
7
5
3

40
42
25
12

10
6
5
4

3 4, 1 7, 4 5 2
3. (. 11.2,
).

, ?
, ,
N P - . ,

.

470

11.

4. W > 2.

1
2

1
W

2
W

2
1

, ;
2. W .
, r (sa )
W/2 , .

, . , ,
:
, ,
(, ). ,
2. , s
sa , , 2 ,
.
.
.
.
1.
ri = vi /wi , i = 1, 2, . . . , n.
2. , 1 ( ).
3. ,
: ,
;
, (
), .
, 3
3 4 6/7 1
7, .

11.3 N P -

471

,
. , .
w1 v1 , w1 ,
v1 . , ,
.. , , .
,

.
, , 11.2.

. ,
, (approximation
schemes), ,
(k)
sa :


(k)
f sa
1
1+
n,

f (s )
k
k 0  k < n.
. (S. Sahni) 1975 [99].
k , , ,
(..
). ,
, .
5. k = 2 . 11.12.
{1, 3, 4}.

,
. , . ,

n. , , ,

472

11.

1
2
3
4

4
7
5
1

40
42
25
4

10
6
5
4

W = 10
)

{1}
{2}
{3}
{4}
{1, 2}
{1, 3}
{1, 4}
{2, 3}
{2, 4}
{3, 4}


1,3,4
3,4
4
1,4
1,3

4
3

69
69
46
69
69
69
69
46
69

1
)

. 11.12.
k = 2. ) . ) ,

k

j=0

Cnj =

k

n (n 1) (n j + 1)
j=0

j!

k

j=0

nj 

k


nk = (k + 1) nk .

j=0

O (n),

 . ,

O knk+1 . : n, k .
, (fully polynomial schemes), . , , . (S. Martello)

11.3 N P -

473

. (P. Toth) [77], ,


.

11.3
1. ) , . 1, , 1 5.

0 14 4 10

14 0 5 8 7

4 5 0 9 16

10 8 9 0 32
7 16 32 0
) .
2. ) . ,
n n.
) ?
3. . 11.11
, a, , . 11.11. ,
. 11.11?
4. , , , .
5. ?
6. , RA 2.
7. , (rst-t algorithm):
,

474

11.
; ,
.
) s1 = 0.4, s2 = 0.7, s3 = 0.2,
s4 = 0.1, s5 = 0.5 ,
.
) .
) , 2- .
8. (rst-t
decreasing algorithm) ,
, .
) s1 = 0.4, s2 = 0.7, s3 = 0.2,
s4 = 0.1, s5 = 0.5 ,
.
) ? .
) , 1.5- .
) ,


.
9. ) 2- (minimal vertex cover)
.
) (maximal independent set)

: 2-
, . ,
2- ?
10. ) .
) , .

11.4

11.4

475


f (x) = 0.

(11.3)

. -, , .
, , (,
, f (x)
, f  (x) = 0). -,
. -,
, ,
.
, ,
. ,
,
, . ,
.

ax2 + bx + c = 0,

b b2 4ac
x1,2 =
2a
, . , 10.4, ,
.
? ,

476

11.

, .
,
, .
1799 (Paolo Rufni) (17651822) (Niels Abel)
(18021829)
(Evariste Galois) (18111832)3 .

. 1801
(Carl Friedrich Gauss) (17771855),
, , [84].
(11.3) ,
f (x) x. , . ,
f (x) (, x3 = 0),
(, sin x = 0)
(ex + 1 = 0). , (11.3) ,
.
, . .
, .. , .


,
a b ,
(. 11.13).
, . (11.3), (bisection method). [a, b],
f (x) ,
xmid = (a + b)/2. f (xmid ) = 0, , .
[a, xmid ],
3


. , . , 21 .
.

477

11.4
f(x)

x
a

x1

. 11.13. : x1 [a, b]

[xmid , b] ,
f (x) .
,
, .
, [an , bn ], x ,
, , x
xn ( )
> 0. xn [an , bn ] x
,
bn an
.
(11.4)
|xn x | 
2
, , (bn an )/2 <
< , , ,
(11.5)
xn an < .
,
|xn x | 

b1 a1
n = 1, 2, . . . .
2n

(11.6)

, {xn }
x
n. , , {xn } x . ,
, ,
(. 10.4), ,
. , ,
,
! . , , ,
, .
.

478

11.

Bisection (f (x) , a, b, eps, N )


//
// f (x) = 0
// : a b, a < b,
//
[a, b] f (x),
//
f (a) f (b) < 0,
//
eps > 0,
//
N
// : ( ) x
//
(a, b)
//
, (
//
)
n 1 //
while n  N do
x (a + b)/2
if x a < eps
return x
fval f (x)
if fval = 0
return x
if fval f (a) < 0
bx
else
ax
nn+1
return , a, b
(11.6) , ( ) . n ,
(b1 a1 )/2n < , ..
b1 a1
n > log2
,
(11.7)

.
1.
x3 x 1 = 0.

(11.8)

(. f (x) = x3
x 1 . 11.14). f (0) < 0 f (2) > 0,

479

11.4

(0, 2). 
= 102 , (11.7)

, n > log2 2/102 , n  8 .
y
f(x)=x 3 x1

. 11.14. f (x) = x3 x 1

. 11.15 (11.8). x8 =
= 1.3203125 x (11.8), ,
|1.3203125 x | < 102 .
, (11.8) a8 , b8 x8 , , 1.3203125
1.328125.

n
1
2
3
4
5
6
7
8

an
0.0
1.0
1.0
1.25
1.25
1.3125
1.3125
1.3125

bn
2.0+
2.0+
1.5+
1.5+
1.375+
1.375+
1.34375+
1.328125+

xn
1.0
1.5
1.25
1.375
1.3125
1.34375
1.328125
1.3203125

f (xn )
1.0
0.875
0.296875
0.224609
0.051514
0.082611
0.014576
0.018711

. 11.15.
(11.8).

f (x) = x3 x 1

480

11.


.
. , .
. ,
( ). f (x),
.
?
, , .
, 2 . :
. ,
, , . ,
, .


4 (method of false position),
regula falsi, ,
. , [an , bn ],
f (x),
an bn . [an , bn ],
, (an , f (an )) (bn , f (bn ))
(. 11.16).
, :
an f (bn ) bn f (an )
.
(11.9)
xn =
f (bn ) f (an )
2. . 11.17
(11.8).
, ,
.

4

,
, . . .

481

11.4

f(x)

xn

an

bn

. 11.16.
n
1
2
3
4
5
6
7
8

an
0.0
0.333333
0.676471
0.960619
1.144425
1.242259
1.288532
1.309142

bn
2.0+
2.0+
2.0+
2.0+
2.0+
2.0+
2.0+
2.0+

xn
0.333333
0.676471
0.960619
1.144425
1.242259
1.288532
1.309142
1.318071

f (xn )
1.296296
1.366909
1.074171
0.645561
0.325196
0.149163
0.065464
0.028173

. 11.17.
(11.8).
f (x) =
= x3 x 1


(Newtons method),
(NewtonRaphson), .
(11.3) . 11.18: xn+1
f (x) xn .
:
xn+1 = xn

f (xn )
n = 0, 1, . . . .
f  (xn )

(11.10)

482

11.

f(x n )

x n+1

xn

. 11.18.

(11.10), x0
( x0 ).
, .

3. a
x2 a = 0. (11.10) f (x) =
= x2 a f  (x) = 2x,


f (xn )
x2 a
x2 + a
1
a
= xn n
xn +
= n
=
,
xn+1 = xn 
f (xn )
2xn
2xn
2
xn
10.4.

4. (11.8),
. (11.10)

x3 xn 1
.
xn+1 = xn n 2
3xn 1
, , x0 = 2.
. 11.19 .

, ,
, .
,

11.4
n
0
1
2
3
4

xn
2.0
1.545455
1.359615
1.325801
1.324719

xn+1
1.545455
1.359615
1.325801
1.324719
1.324718

483

f (xn+1 )
1.145755
0.153705
0.004625
4.7 106
5.0 1012

. 11.19.
(11.8)

. , ,
,
. ,
, , .
. , (11.10)
, , . , f  (x)
x . ,
'
' 
'f (x)'  m1 > 0
xn x , xn x
:
f (xn ) f (x ) = f  (c) (xn x ) ,
c xn x . f (x ) = 0, |f  (c)|  m1 ,

|f (xn )|
.
(11.11)
|xn x | 
m1
(11.11)
,
.
|xn x | <

|f (xn ) < | ,
. xn x ,
(11.11).

484

11.

:

.

11.4
1. ) Internet
ax3 +
+ bx2 + cx + d = 0 .
) ?
2. :
) x ln x = 0
) x sin x 1 = 0
) xex 1 = 0
3. ) , p (x) ,
.
) , x0 p (x) n- ,
:
p (x) = (x x0 ) q (x) ,
q (x) n1.
.
) , x0 p (x) n- ,
p (x0 ) = q (x0 ) ,
q (x) p (x) x x0 .
4. (11.6).
5.
x3 + x 1 = 0
102 .
6. (11.9), .
7.
x3 + x 1 = 0
102 .
8. (11.10), .

485

9.
x3 + x 1 = 0
102 .

10. , , .


,
. , , .
N P - .
,
, . ,
, ,
.

. , , ,
.
.
,
,
.
, ,
.

486

11.

.
.

.
,
.

, 2 .
.

.

.

.

.
, , .
, . ,
, .
,
.
.


, .
(Thomas H. Huxley) (18251895),

, . . , ,
, . , .
, ,
, .
, . ,
.
.
:

, ,
, , , , . , ,
. ,

488

, . , , .
. . ,
, . ,
:

(1)
(log n)

(n)

n log n

(n log n)
 
n2
 
n3
(2n )


( )
(
)
( )



, . ,
, .
. , :
. .
, ,
. . ,
. ,
.

. / ,
.
, , , , (., , [54]).

489


, . ,
.
, .
(R. Motwani) . (P. Raghavan) [81] .
(R. M. Karp) [60].
, (John von Neumann).
. , ,
, . . ,
, .. . ,
, .
, , n , , , A [0..n 1]. , , , ,
n 1 .
A [0] A [1], A [2] A [3] .. ,
. ,
, , log2 n
.
, . , (, [54]) (, [17, 78]).
, ,
, ,
. (. [83])
, .. n ( (qubit)) 2n .
1994 (Peter Shor) AT&T Research Labs
,
[109]. , IBM , 15 3 5.
,

490


.

, -
.
1994 ,
(Len Adleman) [2, 8], RSA, ,

, , ,
. N P -, .
.

, .
.
, - ,
.
,
, , .
!

,

,
. [45, 46, 93] [105].


,
1; . lg x 2, ln x
e = 2.718281828 . . . ; x y .
1. log xy = y log x
2. log xy = log x + log y
x
3. log = log x log y
y
4. loga x = loga b logb x
5. alogb x = xlogb a

1. n- : P (n) = n!
2. k- n- ( k n!
n): Cnk =
k! (n k)!
3. n- : 2n

492

. ,


1.

u

i=l

1 = 1 + 1 + + 1 = u l + 1 (l, u ;

l  u);

n


ul+1

1=n

i=1

2.

n


i = 1 + 2 + + n =

i=1

3.

n


n (n + 1) 1 2
n
2
2

i2 = 12 + 22 + + n2 =

n (n + 1) (2n + 1) 1 3
n
6
3

i3 = 13 + 23 + + n3 =

n2 (n + 1)2 1 4
n
4
4

ik = 1k + 2k + + nk

1
nk+1
k+1

i=1

4.

n

i=1

5.

n

i=1

6.

n

i=0

7.

n


n

an+1 1
(a = 1);
a = 1 + a + + a =
2i = 2n+1 1
a1
i

i=0

i2i = 1 2 + 2 22 + + n 2n = (n 1) 2n+1 + 2

i=1
n

1

1
1
= 1 + + + ln n + , 0.577216 . . . (
i
2
n
i=1
n- Hn ;
)
n

lg i n lg n
9.
8.

i=1


1.

u

i=l

2.
3.

u

i=l
u

i=l

cai = c

u


ai

i=l

(ai bi ) =

u


ai

i=l

(cai + bi ) = c

u

i=l

u


bi

i=l

ai +

u

i=l

bi

4.
5.

u

i=l
u


ai =

m


u


ai +

493

ai , l  m < u

i=m+1

i=l

(ai ai1 ) = au al1

i=l

u
f (x) dx 

1.

u+1


f (i) 

i=l

l1
u+1


f (x) dx 

2.

u


u


u
f (i) 

i=l

f (x) dx f (x)

f (x) dx f (x)
l1


x ( ),
x, , x (,
3.8 = 3, 3.8 = 4, 3 = 3). x
( ), x, ,
x (, 3.8 = 4, 3.8 = 3, 3 = 3).
1. x 1  x  x  x < x + 1
2. x + n = x + n x + n = x + n x n
3. n/2 + n/2 = n
4. lg (n + 1) = lg n + 1

1. n! =

2n

n n

en , n  1

e
n ( )

n n

1
1
< n <
, .. n! 2n
12n + 1
12n
e

2. (n, m , p )
(n + m) mod p = (n mod p + m mod p) mod p
(n m) mod p = ((n mod p) (m mod p)) mod p

1. () (sequence)
.

:

2, 4, 6, 8, 10, 12, . . .
0, 1, 1, 2, 3, 5, 8, . . .
0, 1, 3, 6, 10, 15, . . .

( )
( )
( )

(, x a) ( , n i), ,
{xn }. x (n),
, : n
, x (n) . x (n)
(generic term) .
:
, n,
x (n) = 2n n  0;
, ,
,
x (n) = x (n 1) + n n > 0

(.1)

x (0) = 0

(.2)

(. 2.4).

496

(.1) (recurrence
equation) (recurrence relation) ( (recurrence)), (.2) (initial condition).
n, 0, , n = 1, (,
F (n) = F (n 1) + F (n 2),
. 2.5)
.
, , ,
, . , (.1) (.2)
x (n) =

n (n + 1)
n  0.
2

(.3)

(.1) , n > 0, ..
(n 1) (n 1 + 1)
n (n + 1)
=
+ n,
2
2
(.2) x (0) = 0, ..
0 (0 + 1)
= 0.
2
. , . (general solution) , .
. , (.1)
n (n + 1)
,
(.4)
x (n) = c +
2
c . c ,
(.1), .
(particular solution)
, . , . , (.3) (.1) (.2).

497


,
( ,
f (x) = 0 f (x)).
, .


( )
, ,
. ,
( (.1) (.2))
.
,
x (n) = 2x (n 1) + 1 n > 1

(.5)

x (1) = 1

(.6)

:
x (1) = 1,
x (2) = 2x (1) + 1 = 2 1 + 1 = 3,
x (3) = 2x (2) + 1 = 2 3 + 1 = 7,
x (4) = 2x (3) + 1 = 2 7 + 1 = 15.
, 1 2:
x (n) = 2n 1 n = 1 . . . 4.
, (.5)(.6)
(.5) (.6),
.
,
.


,
: , x (n 1) x (n 2)

498

, x (n) x (n 2).
x (n 2) x (n) x (n 3). x (n) x (n i) i = 1, 2, . . ..
i , n i
,
.

(.1)(.2). ,
x (n) = x (n 1) + n.
n n 1, x (n 1) = x (n 2) + n 1.
x (n 1)
x (n) = (x (n 2) + n 1) + n = x (n 2) + (n 1) + n.
n n 2 x (n 2) = x (n 3) + n 2;
x (n 2)
x (n) = (x (n 3) + n 2) + (n 1) + n = x (n 3) + (n 2) + (n 1) + n.
x (n), ,
i 1
x (n) = x (n i) + (n i + 1) + (n i + 2) + + n.
(.2) n = 0, , n i = 0, .. i = n:
x (n) = x (0) + 1 + 2 + + n = 0 + 1 + 2 + + n = n (n + 1)/2.
.
(., , 2.4 ).



,
, ,
ax (n) + bx (n 1) + cx (n 2) = f (n) ,
1

(.7)

, i. , (,
x (n) = n (n + 1)/2).

499

a, b c , a = 0. (second-order linear recurrence with constant coefcients).


, x (n)
x (n 2) . ,

. ,
a, b c , n.
f (n) 0 n, (homogeneous); (inhomogeneous).
:
ax (n) + bx (n 1) + cx (n 2) = 0.

(.8)

b = c = 0, (.8)
. ,
(.8), . ,
,
(.8):
(.9)
ar 2 + br + c = 0.
(.9) (characteristic equation) (.8).
1. r1 r2 (.9)
(.8).
1. r1 r2 , (.8)
x (n) = r1n + r2n ,
.
2. r1 r2 ,
(.8)
x (n) = r n + nr n ,
r = r1 = r2 , .
3. r1,2 = u iv ,
(.8)
x (n) = n ( cos n + sin n) ,

500

= u2 + v 2 , = arctg (v/u),
.

, ,
n- ( 2.5).

x (n) 6x (n 1) + 9x (n 2) = 0.

r 2 6r + 9 = 0
r1 = r2 = 3. , 2
1,
x (n) = 3n + n3n .
, , , x (0) = 0 x (1) =
= 3, n = 0 n = 1 ,
. = 0 = 1, ,
x (n) = n3n .

.
2. (.7) (.8)
(.7).

1
, 2 (.7) . f (n)
(.7) ,
.
. , f (n) , , .

x (n) 6x (n 1) + 9x (n 2) = 4.

. . .

501

x (n) = c , c

c 6c + 9c = 4,
c = 1.
x (n) 6x (n 1) + 9x (n 2) = 0,
x (n) 6x (n 1) + 9x (n 2) = 4

x (n) = 3n + n3n + 1.
, , ,
1 2, n- (general
linear kth degree recurrence with constant coefcients)
ak x (n) + ak1 x (n 1) + + a0 x (n k) = f (n) .

(.10)

,
k- :
ak r k + ak1 r k1 + + a0 = 0.

(.11)

(.11)
(.10).
, . ,
, (Purdom)
(Brown) [93].



,
. ,
.

502


,
n
n 1.
n! ( 2.4) ( 5.1). ,
,

T (n) = T (n 1) + f (n) ,
(.12)
f (n) ,
. (.12)
T (n) = T (n 1) + f (n) =
= T (n 2) + f (n 1) + f (n) =
= =
= T (0) +

n


f (j).

j=1


,
f (n) nj=1 f (j)

. , f (n) = 1, nj=1 f (j) = n;


f (n) = log n, nj=1 f (j) (n log n); f (n) = nk , nj=1 f (j)
 k+1 
n
. j=1 f (j)
n
, (., ,
).


n n/b ( ,
b = 2). ;
. ;
(. 5), - ( 5.5). ,
,

T (n) = T (n/b) + f (n) ,
(.13)
b > 1 f (n) , . , (.13)

. . .

503

n = bk , k = 0, 1, . . . . n, b, (
). n = bk . ,
n (., , 4.3 4.3.3), (smoothness rule),
4 , n.
n = bk , k = 0, 1, . . . (.13),
:




T bk = T bk1 + f bk =





= T bk2 + f bk1 + f bk =
= =
= T (1) +

k

 
f bj .
j=1

 

f (n) kj1 f bj , . , f (n) = 1,
k

 
f bj = k = logb n.
j=1

f (n) = n, :
k
k

  
n1
bk 1
=b
.
f bj =
bj = b
b1
b1
j=1

j=1

, (.13) (.14), (Master theorem) ( 5  ).


  , , f (n) nd , d > 0, T (n) nd .


, .
, n/b
a , ,
n = bk , k = 1, 2, . . . :
T (n) = aT (n/b) + f (n) ,

(.14)

504

a  1, b  2, f (n) , , .
(.14) (general
divide-and-conquer recurrence)2 .
(.14) :




T bk = aT bk1 + f bk =





= a T bk2 + f bk1 + f bk =





= a2 T bk2 + af bk1 + f bk =







= a2 aT bk3 + f bk2 + af bk1 + f bk =







= a3 T bk3 + a2 f bk2 + af bk1 + f bk =
= =


 
 
= ak T (1) + ak1 f b1 + ak2 f b2 + + a0 f bk =

k



f bj /aj .
= ak T (1) +
j=1

ak = alogb n = nlogb a ,
(.14) n = bk :

logb n
  
(.15)
T (n) = nlogb a T (1) +
f bj /aj .
j=1

, T (n) a b
f (n).
f (n), , (.15),
T (n).


,

n, b ( b = 2,
; b = 3, 5.5,
a = 1
, .
2

. . .

505

b , 2). , , n,
b, n.
1. f (n) , . f (n)
(eventually nondecreasing), n0 , f (n) [n0 , ),
.. f (n1 )  f (n2 ) n2 > n1  n0 .

, (n 100)2 ,
[1, 100]; sin2 n/2 , . , ,
(
).
2. f (n) , . f (n) (smooth), f (2n) (f (n)).

, , , log n, n,
n log n n ,  0, . , f (n) = n log n ,

f (2n) = 2n log 2n = 2n (log 2 + log n) = (2 log 2) n + 2n log n (n log n) .


, an , a > 1 n!, .
, f (n) = 2n ,
/ (2n ) .
f (2n) = 22n = 4n
3. f (n) . b  2
f (bn) (f (n)) ,
.. cb db
n0 ,
db f (n)  f (bn)  cb f (n) n  n0 .
( , , O .)

506

. O-;
. , f (2n)  c2 f (n)
n  n0 ,


f 2k n  ck2 f (n) k = 1, 2, . . . n  n0 .

k = 1 . , ,

 k1
k1
f 2 n  c2 f (n) n  n0 ,






k
f 2k n = f 2 2k1 n  c2 f 2k1 n  c2 ck1
2 f (n) = c2 f (n) .
( b = 2k .)
b  2. k , 2k1  b < 2k .
f (bn) , , , f (n)
n  n0 :


f (bn)  f 2k n  ck2 f (n) .
, ck2
b, .

.
4 ( ). T (n)
, f (n) .
T (n) (f (n)) n, b,
b  2,

T (n) (f (n)) .

( O .)
. O-; . ,
c n0 = bk0 ,


T bk  cf bk bk  n0 ,
T (n) n  n0 , f (bn)  cb f (n) n  n0
3. n  n0 .
b: n0  bk  n < bk+1 .
,






T (n)  T bk+1  cf bk+1 = cf bbk  ccb f bk  ccb f (n) .

507

. . .

, ccb , O (f (n)),
.

4 ,
T (n) ( b),
.
5 ( ). T (n)
,
T (n) = aT (n/b) + f (n) n = bk , k = 1, 2, . . . ; T (1) = c,
 
a  1, b  2, c > 0. f (n) nd , d  0,
 
d

n

T (n) nd log n

 logb a 
n

a < bd ,
a = bd ,
a > bd .

( O .)
. f (n) = nd
(
., , [32]). f (n) = nd , (.15)
n = bk , k = 0, 1, . . .

logb n
logb n
j


bjd /aj = nlogb a T (1) +
bd /a .
T (n) = nlogb a T (1) +
j=1

j=1

,
logb n

j

b /a

j=1


 bd /alogb n 1
d
, bd = a,
= b /a
(bd /a) 1

logb n

j
bd /a = logb n, bd = a.

j=1

a <

bd ,

bd /a

logb n


j=1

> 1 , ,

b /a

j



 bd /alogb n 1
logb n 
d
d
b /a
= b /a
.
(bd /a) 1

508

T (n) = nlogb a T (1) +


j
logb n 
d
log
a
d
b /a n b b /a
=

logb n


j=1


logb n 
logb n 


= alogb n bd /a
=
= nlogb a bd /a





d
= bd logb n = blogb n = nd .


a > bd , bd /a < 1 , ,
logb n


j=1

b /a

j


 bd /alogb n 1
d
(1) .
= b /a
(bd /a) 1

T (n) = nlogb a T (1) +

j


bd /a nlogb a .

logb n


j=1

a = bd , bd /a = 1 , ,

logb n
j

bd /a =
T (n) = nlogb a T (1) +
j=1



= nlogb a (T (1) + logb n) nlogb a logb n =




d
= nlogb b logb n = nd logb n .
f (n) = nd d, 4
.

5

. .


[1] AdelsonVelsky, G.M. and Landis, E.M. An algorithm for organization of information. Soviet Mathematics Doklady, vol. 3, 1962, 12591263.
[2] Adleman, L.M. Molecular computation of solutions to combinatorial problems.
Science, vol. 266, 1994, 10211024.
[3] Agrawal, M., Kayal, N., and Saxena, N. PRIMES Is in P. Preprint, Department
of Computer Science and Engineering, Indian Institute of Technology Kanpur,
Kanpur-208016, India, August 6, 2002.
[4] Aho, A.V., Hopcroft, J.E., and Ullman, J.D. The Design and Analysis of Computer
Algorithms. AddisonWesley, Reading, MA, 1974.
[5] Aho, A.V, Hopcroft, J.E., and Ullman, J.D. Data Structures and Algorithms.
AddisonWesley, Reading, MA, 1983. ( , , , . ., , 2000.)
[6] Albert, R., Hawoong Jeong, and Barbsi, A.-L. Diameter of the World-Wide
Web. Nature, vol. 401, 1999, 130131.
[7] Atallah, M.J., editor. Algorithms and Theory of Computation Handbook. CRC
Press, Boca Raton, FL, 1998.
[8] Baase, S., and Van Gelder, A. Computer Algorithms: Introduction to Design and
Analysis, 3rd ed. AddisonWesley, Reading, MA, 2000.
[9] Baecker, R. (with assistance of D. Sherman). Sorting Out Sorting. 30-minute
color sound lm. Dynamic Graphics Project, University of Toronto, 1981. (Distributed by Morgan Kaufmann, Publishers.)
[10] Baecker, R. Sorting out sorting: a case study of software visualization for teaching
computer science. In Software Visualization: Programming as a Multimedia
Experience, edited by J. Stasko, J. Domingue, M.C. Brown, and B.A. Price. MIT
Press, Cambridge, MA, 1998, 369381.
[11] BaezaYates, R.A. Teaching Algorithms. ACM SIGACT News, vol. 26, no. 4,
Dec. 1995, 5159.
[12] Bayer, R., and McGreight, E.M. Organization and maintenance of large ordered
indices. Acta Informatica, vol. 1, no. 3, 1972, 173189.

510

[13] Bellman, R.E. Dynamic Programming. Princeton University Press, Princeton,


NJ, 1957.
[14] Bellman, R.E., and Dreyfus, S.E. Applied Dynamic Programming, Princeton
University Press, Princeton, NJ, 1962.
[15] Bentley, J. Programming Pearls, 2nd ed. AddisonWesley, Reading, MA, 2000
( . . 2- . .
2002.).
[16] Berlinski, D. The Advent of the Algorithm. Harcourt, Inc., New York, 2000.
[17] Berman, K.A., and Paul, J.L. Fundamentals of Sequential and Parallel Algorithms. PWS Publishing, Boston, 1996.
[18] Berstekas, D.P. Dynamic Programming and Optimal Control, 2nd Edition (2
vols.), Athena Scientic, 2001.
[19] Bloom, M., Floyd, R.W., Pratt, V, Rivest, R.L., and Tarjan, R.E. Time bounds
for selection. Journal of Computer and System Sciences, vol. 7, no. 4, 1973,
448461.
[20] Bogomolny, A. Interactive Mathematics Miscellany and Puzzles. http://
www.cut-the-knot.org, 1996.
[21] Boyer, R.S., and Moore, J.S. A fast string searching algorithm. Communications
of the ACM, vol. 21, no. 10,1977, 762772.
[22] Brassard, G., and Bratley, P. Fundamentals of Algorithmics. Prentice Hall, Englewood Cliffs, NJ, 1996.
[23] Brown, M.R. ZEUS: A system for algorithm animation and multi-view editing.
In Proceedings of the 1991 IEEE Workshop on Visual Languages, Kobe, Japan,
October 1991, 49.
[24] Brown, M., and Sedgewick, R. A system for algorithm animation. In Proceedings
of ACM SIGGRAPH 84, Minneapolis, July 1984, 177186.
[25] Brualdi, R. Introductory Combinatorics, 3rd ed. Prentice Hall, Upper Saddle
River, NJ, 1999.
[26] Chabert, JeanLuc, editor. A History of Algorithms: From the Pebble to the
Microchip. Translated by Chris Weeks. SpringerVerlag, Berlin, 1998.
[27] Chandler, J.P. Patent protection of computer programs. Minnesota Intellectual
Property Review, vol. 1, no. 1, 2000, 3346.
[28] Comer, D. The ubiquitous B-tree. ACM Computing Surveys, vol. 11, no. 2, 1979,
121137.
[29] Computing Curricula 2001. The Joint Task Force on Computing Curricula. IEEE
Computer Society, Association for Computing Machinery, 2001.

511

[30] Cook, S.A. The complexity of theorem-proving procedures. In Proceedings of


the Third Annual ACM Symposium on the Theory of Computing, 1971, 151158.
[31] Coopersmith, D., and Winograd, S. Matrix multiplication via arithmetic progressions. In Proceedings of Nineteenth Annual ACM Symposium on the Theory of
Computing, 1987, 16.
[32] Cormen, T.H., Leiserson, C.E., Rivest, R.L., and C. Stein. Introduction to Algorithms, 2nd ed. MIT Press, Cambridge, MA, 2001. ( ,
, , , .
. ., , 2005.)
[33] Dantzig, G.B. Linear Programming and Extensions. Princeton University Press,
Princeton, NJ, 1963.
[34] Dewdney, A.K. The (New) Turing Omnibus. Computer Science Press, New York,
1993.
[35] Dijkstra, E.W. A note on two problems in connection with graphs. Numerische
Mathematik, vol. 1, 1959, 269271.
[36] Floyd, R.W. Algorithm 97: shortest path. Communications of the ACM, vol. 5,
no. 6, 1962, 345.
[37] Forsythe, G.E. What to do till the computer scientist comes. American Mathematical Monthly, vol. 75, 1968, 454462.
[38] Forsythe, G.E. Solving a quadratic equation on a computer. In The Mathematical
Sciences, edited by COSRIMS and George Boehm, MIT Press, Cambridge, MA,
1969, 138152.
[39] Gardner, M. My Best Mathematical and Logic Puzzles. Dover Publications, New
York, 1994.
[40] Garey, M.R., and Johnson, D.S. Computers and Intractability: A Guide to the
Theory of NP-Completeness. W.H. Freeman and Company, New York, 1979.
[41] Gerald, C.F., and Wheatley, P.O. Applied Numerical Analysis, 6th ed. Addison
Wesley, Reading, MA, 1999.
[42] Gloor, P.A. User interface issues for algorithm animation. In Software Visualization: Programming as a Multimedia Experience, edited by John Stasko, John
Domingue, Marc H. Brown, and Blaine A. Price. MIT Press, Cambridge, MA,
1998, 145152.
[43] Gonnet, G.H., and BaezaYates, R. Handbook of Algorithms and Data Structures
in Pascal and C, 2nd ed. AddisonWesley, Reading, MA, 1991.
[44] Goodrich, M.T., and Tamassia, R. Algorithm Design: Foundations, Analysis, and
Internet Examples. John Wiley & Sons, New York, 2002.

512

[45] Graham, R.L., Knuth, D.E., and Patashnik, O. Concrete Mathematics: A Foundation for Computer Science, 2nd ed. AddisonWesley, Reading, MA, 1994.
(. , . , . . .
. ., , 1998.)
[46] Green, D.H., and Knuth, D.E. Mathematics for Analysis of Algorithms, 2nd ed.
Birkhuser, Boston, 1982.
[47] Gries, D. The Science of Programming. SpringerVerlag, New York, 1981.
[48] Harel, D. Algorithmics: The Spirit of Computing, 2nd ed. AddisonWesley, Reading, MA, 1992.
[49] Hartmanis, J., and Steams, R.E. On the computational complexity of algorithms.
Transactions of the American Mathematical Society, vol. 117, 1965, 285306.
[50] Heap, B.R. Permutations by interchanges. Computer Journal, vol. 6, 1963,
293294.
[51] Hoare, C.A.R. Quicksort. In Great Papers in Computer Science, Phillip Laplante,
ed. West Publishing Company, Minneapolis/St. Paul, 1996, pp. 3139.
[52] Hochbaum, D.S., editor. Approximation Algorithms for NP-Hard Problems. PWS
Publishing, Boston, 1997.
[53] Hopcroft, J.E. Computer science: The emergence of a discipline. Communications of the ACM, vol. 30, no. 3, 1987, pp. 198202.
[54] Horowitz, E., Sahni, S., and Rajasekaran, S. Computer Algorithms. Computer
Science Press, New York, 1998.
[55] Horspool, R.N. Practical fast searching in strings. SoftwarePractice and Experience, vol. 10, 1980, 501506.
[56] Huffman, D.A. A method for the construction of minimum redundancy codes.
Proceedings of the IRE, vol. 40, 1952, 10981101.
[57] Karmarkar, N. A new polynomial-time algorithm for linear programming. Combinatorica, vol. 4, 1984, 373395.
[58] Karp, R.M. Reducibility among combinatorial problems. In Complexity of Computer Communications, edited by R.E. Miller and J.W. Thatcher. Plenum Press,
New York, 1972, 85103.
[59] Karp, R.M. Combinatorics, complexity, and randomness. Communications of the
ACM, vol. 29, no. 2, 1986, 89109.
[60] Karp, R.M. An introduction to randomized algorithms. Discrete Applied Mathematics, vol. 34, 1991, 165201.
[61] Kernighan, B.W, and Pike. R. The Practice of Programming. AddisonWesley,
Reading, MA, 1999. ( .., . .
.-., , 2001.)

513

[62] Knuth, D.E. Estimating the efciency of backtrack programs. Mathematics of


Computation, vol. 29, 1975, 121136.
[63] Knuth, D.E. Big omicron and big omega and big theta. ACM SIGACT News,
vol. 8, no. 2, 1976, 1823.
[64] Knuth, D.E. Selected Papers on Computer Science. CSLI Publications and Cambridge University Press, New York, 1996.
[65] Knuth, D.E. The Art of Computer Programming, Volume 1: Fundamental Algorithms, 3rd ed. AddisonWesley, Reading, MA, 1997. ( . .
, 1. , 3- . .,
, 2000.)
[66] Knuth, D.E. The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd ed. AddisonWesley, Reading, MA, 1998. ( . .
, 2. , 3- .
., , 2000.)
[67] Knuth, D.E. The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd ed. AddisonWesley, Reading, MA, 1998. ( . . , 3. , 2- . .,
, 2000.)
[68] Knuth, D.E., Morris, Jr., J.H., and Pratt, V.R. Fast pattern matching in strings.
SIAM Journal on Computing, vol. 5, no. 2, 1977, 323350.
[69] Kolman, B., and Beck, R.E. Elementary Linear Programming with Applications,
2nd ed. Academic Press, San Diego, 1995.
[70] Kordemsky, B.A. The Moscow Puzzles. Charles Scribnerss Sons, New York,
1972.
[71] Kruskal, J.B. On the shortest spanning subtree of a graph and the traveling
salesman problem. Proceedings of the American Mathematical Society, vol. 7,
1956, 4850.
[72] Lawler, E.L., Lenstra, J.K., Rinnooy Kan, A.H.G., and Shmoys, D.B., editors.
The Traveling Salesman Problem. John Wiley & Sons, New York, 1985.
[73] Levin, L.A. Universal sorting problems. Problemy Peredachi Informatsii, vol. 9,
no. 3, 1973, 115116 (in Russian). English translation in Problems of Information Transmission, vol. 9, 265266. ( ..,
. , 9, 1 3, 1973, . 115116)
[74] Levitin, A. Do we teach the right algorithm design techniques? In Proceedings
SIGCSE 99, New Orleans, 1999, 179183.
[75] Levitin, A., and Papalaskari, M-A. Using puzzles in teaching algorithms. SIGCSE
02, Cincinnati, 2002.

514

[76] Manber, U. Introduction to Algorithms: A Creative Approach. AddisonWesley,


Reading, MA, 1989.
[77] Martello, S., and Toth, P. Knapsack Problems: Algorithms and Computer Implementations. John Wiley & Sons, Chichester, England, 1990.
[78] Miller, R., and Boxer, L. A Unied Approach to Sequential and Parallel Algorithms. Prentice Hall, Upper Saddle River, NJ, 2000.
[79] Moret, B.M.E., and Shapiro, H.D. Algorithms from P to NP, Volume I: Design
and Efciency. Benjamin Cummings, Redwood City, CA, 1991.
[80] Moscovich, I. 1000 Play Thinks: Puzzles, Paradoxes, Illusions, and Games.
Workman, New York, 2001.
[81] Motwani, R., and Raghavan, P. Randomized Algorithms. Cambridge University
Press, New York, 1995.
[82] Neapolitan, R., and Naimipour, K. Foundations of Algorithms: With C++ Pseudocode, 2nd ed. Jones and Bartlett, Sudbury, MA, 1998.
[83] Nielsen, M.A., and Chuang, I.L. Quantum Computation and Quantum Information. Cambridge University Press, Cambridge, UK, 2000.
[84] OConnor, J.J., and Robertson, E.F. The MacTutor History of Mathematics
Archive, June 1998, http://www-history.mcs.st-andrews.ac.uk/
history/Mathematicians/Abel.html.
[85] Pan, V.Y. Strassens algorithm is not optimal. In Proceedings of Ninteenth Annual
IEEE Symposium on the Foundations of Computer Science. 1978, 166176.
[86] Papadimitriou, C.H. Computational Complexity. AddisonWesley, Reading, MA,
1994.
[87] Papadimitriou, C.H., and Steiglitz, K. Combinatorial Optimization: Algorithms
and Complexity. PrenticeHall, Englewood Cliffs, NJ, 1982.
[88] Parberry, I. Problems on Algorithms. PrenticeHall, Englewood Cliffs, NJ, 1995.
[89] Polya, G. How to Solve It. Doubleday, Garden City, NY, 1957.
[90] Preparata, EP, and Shamos, M.I. Computational Geometry: An Introduction.
SpringerVerlag, New York, 1985.
[91] Price B., Baecker, R., and Small, I. A principled taxonomy of software visualization. Journal of Visual Languages and Computing, vol. 4, no. 3, 1993, 211266.
[92] Prim, R.C. Shortest connection networks and some generalizations. Bell System
Technical Journal, vol. 36, no. 1, 1957, 13891401.
[93] Purdom, P.W., Jr., and Brown, C. The Analysis of Algorithms. Holt, Rinehart and
Winston, New York, 1985.

515

[94] Rawlins, G.J.E. Compared to What? An Introduction to the Analysis of Algorithms. Computer Science Press, New York, 1991.
[95] Reingold, E.M., Nievergelt, J., and Deo, N. Combinatorial Algorithms: Theory
and Practice. PrenticeHall, Englewood Cliffs, NJ, 1977.
[96] Rivest, R.L., Shamir, A., and Adleman, L.M. A method for obtaining digital
signatures and public-key cryptosystems. Communications of the ACM, vol. 21,
no. 2, 1978, 120126.
[97] Rosen, K.H. Discrete Mathematics and Its Applications, 4th ed. McGrawHill,
New York, 1999.
[98] Rosenkrantz, D.J., Steams, R.E., and Lewis, P.M. An analysis of several heuristics
for the traveling salesman problem. SIAM Journal of Computing, vol. 6, 1977,
563581.
[99] Sahni, S. Approximation algorithms for the 0/1 knapsack problem. Journal of
the ACM, vol. 22, 1975, 115124.
[100] Sahni, S., and Gonzalez, T. P-complete approximation problems. Journal of the
ACM, vol. 23, 1976, 555565.
[101] Sayood, K. Introduction to Data Compression, 2nd ed. Morgan Kaufmann Publishers, San Francisco, CA, 2000.
[102] Sedgewick, R. Algorithms, 2nd ed. AddisonWesley, Reading, MA, 1988.
[103] Sedgewick, R. Algorithms in C, 3rd ed. Parts 14: Fundamentals, Data Structures,
Sorting, Searching. AddisonWesley, Reading, MA, 1998.
[104] Sedgewick, R. Algorithms in C, 3rd ed. Part 5: Graph Algorithms, Addison
Wesley, Boston, 2002.
[105] Sedgewick, R., and Flajolet, P. An Introduction to the Analysis of Algorithms.
AddisonWesley, Reading, MA, 1996.
[106] Shasha, D., and Lazere, C. Out of Their Minds: The Lives and Discoveries of 15
Great Computer Scientists. Copernicus, New York, 1998.
[107] Shell, D.L. A high-speed sorting procedure. Communications of the ACM, vol. 2,
no. 7, July 1959, 3032.
[108] Shen, A. Algorithms and Programming: Problems and Solutions. Birkhuser,
Boston, 1997.
[109] Shor, P.W. Algorithms for quantum computation: Discrete algorithms and factoring. Proceedings of the 35th Annual Symposium on Foundations of Computer Science (Sha Goldwasser, editor). IEEE Computer Society Press, 1994,
124134.
[110] Sipser, M. Introduction to the Theory of Computation. PSW Publishing, Boston,
1997.

516

[111] Skiena, S.S. Algorithm Design Manual. SpringerVerlag, New York, 1998.
[112] Stasko, J. TANGO: A framework and system for algorithm animation. Computer,
vol. 23, no. 9, 1990, 2739.
[113] Strassen, V. Gaussian elimination is not optimal. Numerische Mathematik,
vol. 13, 354356.
[114] Tarjan, R.E. Data Structures and Network Algorithms. Society for Industrial and
Applied Mathematics, Philadelphia, 1983.
[115] Tarjan, R.E., and van Leeuwen, J. Worst-case analysis of set union algorithms.
Journal of the ACM, vol. 31, no. 2, 1984, 245281.
[116] Tarjan, R.E. Amortized computational complexity. SIAM Journal on Algebraic
and Discrete Methods, vol. 6, no. 2, 1985, 306-318.
[117] Tarjan, R.E. Algorithm design. Communications of the ACM, vol. 30, no. 3,
1987, 204212.
[118] Tucker, A. Applied Combinatorics. John Wiley & Sons, New York, 1980.
[119] Warshall, S. A theorem on boolean matrices. Journal of the ACM, vol. 9, no. 1,
1962, 1112.
[120] Weide, B. A survey of analysis techniques for discrete algorithms. Computing
Surveys, vol. 9, no. 4, 1977, 291313.
[121] Weiss, M.A. Data Structures and Algorithm Analysis, Benjamin/Cummings,
1991.
[122] Williams, J.W.J. Algorithm 232 (heapsort). Communications of the ACM, vol. 7,
no. 6, 1964, 347348.
[123] Wirth, N. Algorithms + Data Structures = Programs. PrenticeHall, Englewood
Cliffs, NJ, 1976. ( . + = .
.: , 1985.)
[124] Yao, R Speed-up in dynamic programming. SIAM Journal on Algebraic and
Discrete Methods, vol. 3, no. 4, 1982, 532540.


1
1.1
1. , Web,
.
2. , .
( , ?).
?
3. , , .
, .
([65], . 6) .
4. ) .
) , .
5. , d m, n (.. m = sd n =
= td s t),
n, r = m mod n, . m = qn + r
(0  r < n) , d u v,
u + v u v (?).
6. m < n.
7. ) ; )
, 1 < m <
< n  10.
8. ) gcd (m, n) = gcd (m n, n) m  n > 0.
) , ,
, m > n  1.

518

).
, , n = 1
n = 2 .

9. ,  n.

1.2
1. ,
.
2. ,
.
3. .
4.
, .
5.
. , ,
.
6. , ,
.
7. ) , , 1760- (Johann Lambert),
. ) .
8. , .
9. ,
( ) ,
, .

1.3
1. . , .
2. , . (
.)
3. ,
.

519

4. , Web
. .
5. . (
).
6. ) ,
. ,
.
) .
.
7. ) ?
) .
8. , . , .
9. , ,
. n  2.
10. -
.

1.4
1. ) , .
) 1.1.
2. ) , ,
, .
) .
3. ) push(x) x , pop
.
) enqueue(x) x , dequeue
.
4. .
5. ,
. , .

520


,
!

6. h  n 1
. 2h+1 1  n,
, , h.
7. , .
8. - ( ) .
9. ( Internet).
10. , . , .

2
2.1
1. , ,
. , ,
.
2. ) , -.
)
. ?
3.
?
4. ) : .
) .
.
5. ) , n
b ,
2b1  n < 2b .

521

2 .
) , .
)
?
6. , .
7. .
8. f (4n) f (n), . ( , , n.)
9. , , . (
; .)

10. ni=0 2i = 2n+1 1.

2.2
1. (.
2.1) O, .
2. n (n + 1)/2, O, (
).
3. , .
4. ) .
) .
5. .
. 2.2 .
,
.
6. ) , , .
) lim an1 /an2 .
n

7. ), ) ) . ) (,
, -
.

522

8. ) . , .
9. , ,
.
10. , , .

2.3
1. , .
,
.
2. , , ,
. ,
.
3. .
4. ) n
(, n = 1, 2, 3) .
) .
) , .
) ). ,

n (?).
) - ,
?
5. ) n
(, n = 1, 2, 3) .
) .
) , ,
( ).
) ).
) ?
.
, .
, ?

523

6. ) A [i, j] A [j, i] .
) .
) .
) ).
) , .
7. n n1 . -?
8. , n
.
9.
n

i=1

i=

n1


i + n.

i=1

, 1 + 2 + + 99 + 100
50 , .
10. ) .
,
.
) .

2.4
1. .
2. ,
.
3. ) .
) .
4. ) ,
, .

524


. (. ).
) .
) ,
n.

5. ) ,
.
) , ,
.
n .
) , : , - .
Web.
6. ) n ,
log2 n
.
) .
7. ) 2n = 2n1 + 2n1 , .
.
) 2.4.
) 2.4.
) ,
. ,
, .
, , , .
8. ) n = 1 n = 2.
) .
9. n = 1 n = 4.
10. ) ,
, .
) . M (n)
.

525

2.5
2. n 3n . ?
3. F (n), n, F (n) .
4. .
.
5. C (n) Z (n), , .
6. , ,
. , .
7. .
8. ; , gcd (13, 8).
9. ) .
) . , .
. [45],
(Lewis Carroll). , :
, . . . ,
. ,
.
10. k N N mod 10k .
10k (. ) .
, 2.6 .

2.6
1. 2?
2. .
3.
, . 2.6
, .

526

4. , .
5. .
6. lg lg n lg n n = 2k .
7. , , .
8. n ,
, 102 104 105
(,
).

3
3.1
1. ) ,
/ .
, .
) , - . , .
2. ) , , .
, ,
.
) (ab) mod m?
3. .
4. ) , x0 p (x), .
) .
) n?
, ?
5.
( ).
6. , .

527

7. , ,

.
8.
( ).
9. ) ,
. ?
) .
) ,
.
10. ?

3.2
1. 2.1.
2. Cavg (p)?
3. . 2.6.
4. , , .
5. .
6. , .
7. , . ,
?
8.
, , .
9. ,
, .
10. ()
, , , .
? ( , ,

528


, .)
?

3.3
1. n O (n log n) .
2. ) 2) 3)
.
)
|x 0| + |y 0| = 1. ,
(x, y  0), .
) . , P1 (0, 0) P2 (1, 0),
P3 , .
3. : n k.
k = 2 .
4. .
5. n
n? ?
6. , .
7. , Pi Pj ,
,
?
8. n , ,
.
9. ) , ax + by  c, ,
ax + by = c, .
.
) , ) .
) .

529

3.4
1. )
.
)
n, .
2. ?
3. ,
.
4. 4! 6 = 18 ,
.
5. .
6. , . ,
.
7. ,
, .
8.
.
9. .
10. )
.
)
?

4
4.1
1.
. 2.4.
2. 1 , ,
.
3. a8 4? a9 ?

530

4. , .
5. .
6. ,
.
7. ?
8. ) , .
) , ? ?
) ,
.
10. ,
.

4.2
1.
.
2. .
) .
) .
) , .
3. 1.3. ,
, .
4. , , i .
5. , . , .
6.
?
7.
.
, .
8. , .

531

9. , , .
10. , .

4.3
1. ) , .
) , .
:
6(55)

2(27)

9(81)

( m ,
, , .. A [m]).
2. , 2.4,
, .
3. ) , n
, .. 2k  n < 2k+1 .
) n (n = 2i) . n (n = 2i + 1) .
). .
4.
.
5. ?
6. , L, ,
U . , L, U .
7. , .
8. K  A [m], m (l + r)/2, l
r. , .

532

9.
.
10. .3

4.4
1. .
2. , , , . ,
, , .
3. .
4. ?
?
5. .
6. ,
.
:
k1 k2 , k1 < k2 ?
7. .
8. .
9. ( x = n + 1 ).

4.5
1. n = 2,
n.
2. .
,
.
3. ) .
) ?
4. ) 10?
) , , 98 76.
3


. . .

533

5. , ,
.
6. .
7. ( ,
n = 2, n = 1).

.
8. .
9. .
.

4.6
1. ) ?
) ,
.
2. (. 4.1), , Cworst (n) = n log2 n n + 1
n = 2k . .
5. .
6. , .
7. S1 .
8. , (n). (?)
9. n ,
.

5
5.1
1. n = 1.

534

2. n , n 1 ? , ,
2.4.
3. , n- S = {a1 , . . . , an } ,
an , , .
4. ,
(. . 5.4).
5. )
.
) , , .
6. ,
.
7. ,
.
8. )
.
) , A [i]
i + 1
. .
9. ( 4.3)
. ?
10. ) , , .. A [0] A [hi ], A [1] A [1 + hi ] ..
) , , , , , .

535

5.2
1. )
, 1.4.
) ,
(. . 5.5).
2. .
3. ) ?
) .
4. ,
(. . 5.6).
5. ,
.
6. ) ? (
.)
) . , .
7. ,
, ,
(.. )?
8. , , .
9. .
10. ) .
)
.

5.3
1. ,
(. . 5.10).
2. ) : 1) ,
; 2)
,
.
) .

536

3. )

?
) , ?
, ,
, ?4
4. - .
5. , ,
(. . 5.11).
6. ) .
) ,
,
, .
) .
7. ,
.
-.
9. ) , .
) ,
. ,

.
10. ,
.

5.4
1. . , , , , .
2. ( ) .
3. .
4

. . .

537

4. ) n = 2;
n = 3, n = 4.
) , n! . .
) , .  .
e ni=0 (1/i!).
5. ( ) .
6. . . .
7. ,
n n 1 .
8. .
9. n = 3 . ,

n = 4. , n- .
n = 1, 2 3.
n.
10. , . ,
. (, , ) , .
. ( ),
k1
k-,
{1, 2, . . . , n}. Cni
i (i = 1, . . . , n k + 1).

5.5
1. n log2 n,
n/2?
?
2. , , .

?

538

3. , n mod 3 = 0
n mod 3 = 1; n mod 3 = 2?
4. ,
(. . 5.13).
5. ?
6. , .
7. n = 40 , n (. 5.5).
8. n.
9. ) (. )
, .
) 15 , ) , .
) n, J (n), ) .

5.6
1. ) ,
.
) r = m mod n. r
n.
2. .
3. ,
, ,
.
4. , (l, A [l]) (r, A [r]),
x , y v.
5. , .
6. ) log2 log2 n + 1 > 6.
) lim logloglogn n . , n
.
7. ) .

539

) ?
?
8. ) : 1) ;
2) ; 3) .
) , .
9. ( ) n = 1, 2, . . . ,
10 .
10. .
, .

6
6.1
1. .
.
2. .
3. ) .
)
: ,
.
4. ) ?
) , .
5. .
6. , n log2 n .

.
7. )
.
) ,
? : ,
,
.

540

8. ) . ,
.
) , .
9. ,
. , :
s = 0, , .
10. .

6.2
1. ,
.
2. ) , .
) .
?
3.
, 3 3, LU , 2.
4. , ,
.
5. . , .
,
.
6. .
7. )
.
) ,
.

541

)
( ).
8. ) ,
.
) ?
)
,
.
9.
,
?
10. ) .
) ?

6.3
1. AVL-. , AVL- .
2. n.
3. L- RL- R- LR-,
.
4. , ,
, .
5. ) , AVL-.
) , .
7. ) (. . 6.8).
) , ,
2-3-, , ,
.
8. ; .
9. ?

542

6.4
1. ) .
) .
) .
2. , ,
.
3. ) h
? ?
) ) .
4. h.
 i

i2
, h.
5. ) ?
)
.
6. (. . 6.14).
7. , , , .
8. .
10. .

6.5
1. .
.
2. , xi
xi1 .
3. ) ( )
.
) ?
4. ,
.

543

5. x c,
.
6. ) ,
.
) . . ?
7. ,
.
8. n .
9. .
10. , , .

6.6
1. ) lcm (m, n) gcd (m, n) m n.
) lcm (m, n).
2. .
3. k.
4. ) :
3 , i
j, 2.
) , .
5. ,
. , .
6. .
7. xij , j- i- .
8. ,
.
9. .

544

10. , ) ,
, .
) ).

7
7.1
1. , . ?
2. ,
.
3. (. . 7.2
).
4. , .
5. A [i]?
6. , ..
( 4.2.8).
7. .
8. ) B C .
) , , B [C [3]] ).
9. )
.
) ,
i ai
ai xi .
10. ,
.

7.2
1. ,
.
2. ,
.

545

3.
( , ) ,
.
4. m n
(n  m) ,
) ;
) .
5. .
6. ?
7. ( , )
, .
8. .
9. .

7.3
1. ( )
,
(. . 7.5).
.
2. ( ) ,
(. . 7.6).
.
3. -? ?
4.
n .
5. , n .
, ?
6. ) ,
.
) ? -

546


,
?

7. ,

( 2.3) ( 6.1).
8. : 7.3,

( ,
).
9. , .

7.4
1.
.
2. ) ,
.
) m/2.
3. B, .
4. , .
5. B-.
6. ) , . ,
, ,
.
) ? ,
?

8
8.1
1. .

547

2. ) Binomial n = 6 k = 3
, . 8.1.
) ,
Cnk .
3. , c1 c2
n0 , n k (n  n0 0  k  n)
c2 nk 

(k 1) k
+ k (n k)  c1 nk.
2

4. )
, 8.1.
) .
5. Cnk
.
6. A (n, k) A (n, k) = B (n, k) 1.
7. .
8. Cnk ,
.
9. ) , A B i j
, , A
.
) 5 (0  i  4) 5 (0  j  4)
) .
) ) . , .

8.2
1. ,
.
2. ) ,
(.. ).

548


)
,
?

3. , R(k1)
R(k) - .
4. , R(k1) [i, k] = 0?
5. , (8.7) (
3)
rij = rij or rik and rkj

if rik

rij rij or rkj .

6. ) ? ?
) 1? ?
7. .
.
(k)

8. D(k1) dij
i- j- D(k) ? ?
9. .
10. P , .
, , 1.

8.3
1. (8.11), .
2. )

.
) , ?

549

3. k = R [1, n] ,
k- a1 , a2 , . . . , an .
R [1, k 1] R [k + 1, n], .
4. - .
5. ,
?
6. . .
7. ) n n (?), .

.
) .
) n- n!.
8. OptimalBST , ,
8.3.
9. , a1 , . . . , an , ; p1 , . . . , pn , q0 , q1 , . . . ,
qn (, a1 ), (a1 , a2 ),
. . . , (an , ), ; (p1 + + pn ) + (q0 + + qn ) = 1.
, (8.11), ,
.
10. ) , (A1 A2 ) A3 A1 (A2 A3 ) A1
d0 d1 , A2 d1 d2 A3 d2 d3 ,
,
1000 .
) , ,
.
)
Ai . . . Aj
, ai , . . . , aj .

550

8.4
1. ) (8.12) (8.13) ,
.
)
max{V [i 1, j], vi + V [i 1, j wi ]}?
2. ) . 8.12 (,
) (8.12) (8.13).
)
.
3. ?
?
, ?
4. V [i, j]
) V [i, j 1]  V [i, j] 1  j  W ;
) V [i 1, j]  V [i, j] 1  i  n.
5. MFKnapsack (i, j) (
).
6. (8.12) . , (nW )?
7. 8.3 , 8.4.
8. , .
9. C [i, j],
i
(1  i  m) j (0  j  n).
C [i, j] +.

9
9.1
1. , : d1 = 7, d2 = 5, d3 = 1.

551

2. .
3. . , , , , ,
.
4. ,
( ).
5.
n = 1, 2 3.
.
6. ) .
.
) ,
,
.
7.
.
8. ) ,
.
) , ?
9. , : ,
.
.

.
10. : ( ) .

9.2
1. ,
.
2. , .
3. ;
.

552

4. . ,
,
; , .
5. 6.6?
6.
makeset (x), f ind (x) union (x, y)
, .
7. , 9.1 .
8. ,
union-by-size .
9. , 2.7.

9.3
1. ;
.
2. ,
.
3.
.
4. .
.
5. , ,
.
6. ,
, .
7. .
8. .
9. .
10. , , . ,

553

, .

9.4
1. . , .
2.
.
.
3.
, .
4.
. n n , ,
.
5. ) ,
?
) ,
.
6. : ,
.
7. .
8. .
10. 9.4.
, ,
(, , , : ,
?).

10
10.1
1. , ,
, 2n 1, (,
), ,
, M (n), , 2n 1. ,

554


M (n) , M (n)

M (n) = 2M (n) + 1 n > 1 M (1) = 1,
2n 1.

2. . , ,
.
3. 5.5,
; .
4. , .
5.
U W , n/2 n/2 , , ,

(u, w), u U w W .
6. n- , .
.
7. , .
8. ) ,
.
) , , ,
.
9. ? , ,
, .
10. n = 1.

10.2
1. ) , 2h  l h.
) , 3h  l h.
2. ) ?
) , .
) a, b c .

555

3. . ,
. ( ,
.)
4. , , .
5. . . ,
.
6. . ,

, , .
7. ) .
) ,
log2 (n + 1)
> 1.
n log3 (2n + 1)
lim

, ,
f (n)
f (n) + 1
f (n) 1
<
<
,
g (n) + 1
g (n)
g (n) 1
f (n) = log2 (n + 1) g (n) = log3 (2n + 1), ,
lim

f (n) 1
f (n) + 1
= lim
> 1.
g (n) + 1 n g (n) 1

8. ) ?
) .
9. ) , (
) ( )

.
.5
) , .
, .
) . ) .
5

- (Brassard)
(Bratley) [22].

556

10. ) .
) ( , n-
).
) ,
?

10.3
1. .
2. , nlog2 n . .
3. , .
4. . 5 ( ).
5. .
6.
?
7.
.
8.
xi , i = 1, . . . , n, 0
1.
9. , , ,
, 4.
. ,
, .
10.
.
11. 10.3.

557

10.4
1. .
.
2. .

3. 5i=0 0.5i /i!

e = 1.648721 . . . .
4.
.
5. (10.7) (10.9) .
6. esin x (10.9) n, .
7. .
8. a, b c. ,

.
9. ) ,
xn 1) ,
2) D ( xn+1 D) 3) (
xn+1 xn ). (10.15)
n, .
2

(xn D)
.
) xn+1 D =
2xn

10. 2.

11
11.1
1. ) , .
) ,
?
2. .
3. ) .
,
, .

558


,
, , . ,
n n n n,
( ),
.
)

. ,
, ,
.

4. .
5. , , a
1, .
6. .
7. ) .
) ,
.
8. .
10. .

11.2
1.
?
2. , .

1 .
3. ) n n ,
.
)
) .
5. .

559

6. ,
.
8. .
9. .

11.3
1. )
.
) ( -
).
2. ) , .
.
)
(
, ).
3. .
4. k  1
.
5. .
6. .
1) f (s )  2f (sa ) , f (sa ) , , f (s )
.
2) 2 , .
1) .
2) , ( W/2,
, W/2).

560

7. ) ,
,
.
) ? ?
)
BF F < 2

n


si BF F > 1,

i=1

BF F ,
s1 , s2 , . . . , sn . , ,
, .
8. ) ,
,
.
) , .
) .
1) , , .. B ,
1/3.
2) , , B 1 (B ).
) .
?
9. ) , , ,
.
) , N P -
.
10. ) , .
) Gn ,
a (Gn )
(Gn )

561

( a (Gn ) (Gn ) , , , )
.

11.4
1. ,
(Girolamo Cardano).
2. ,
f1 (x) = f2 (x) f1 (x) f2 (x).
3. ) , .
) p (x) xx0 , .. p (x) = q (x) (x x0 ) + r, x0 p (x), q (x)
r , .
) x0
.
4. , |xn x | xn , [an , bn ], x .
5.
, . 11.4 . ,
.
6. , (an , f (an ))
(bn , f (bn )), .
7. .
(an , bn ) (11.11).
8. (xn , f (xn ))
.
9. . ,
x0 .

10. , , f (x) = 3 x.

2-3-4-, 266
2-3-, 266 ; 272

A
Abel, Niels, 476
Absolute error, 431
Abstract data type, 69
Acyclic graph, 63
Adjacency matrix, 59
Adleman, Len, 490
ADT, 69
Adversary method, 405
Agrawal, Manindra, 424
Algorithm design technique, 36
Algorithmics, 23
All-pairs shortest-paths problem, 349
Amortized efciency, 84
Analysis, 73
Ancestors, 64
Ancestry problem, 311
Approximation algorithm, 35
Approximation schemes, 471
Array, 55
Articulation point, 215
Average-case efciency, 82
AVL tree, 266 ; 267

B
B-tree, 266 ; 331
Back edge, 212 ; 221
Backward substitution, 109
Baecker, Ronald, 135
Basic operation, 77
Bayer, Rudolf, 331
Bellman, Richard, 339 ; 352
Berlinski, David, 23

Binary insertion sort, 207


Binary search tree, 65
Binary string, 55
Binary tree, 65
Binomial coefcient, 341
Bipartite graph, 219
Bisection method, 476
Bit string, 55
Bit vector, 68
Boyer, Robert, 48 ; 150 ; 316
Breadth-rst search, 212 ; 215
Breadth-rst search forest, 215
Brewer, E. Cobham, 203
Brown, C., 501
Burks, A., 34
Byrne, Robert, 141

C
C-approximation algorithm, 463
Cardano, Girolamo, 561
Carroll, Lewis, 525
Catalan number, 355
Change-making problem, 367 ; 369
Characteristic equation, 499
Child, 64
Class, 69
Clique, 427
Clique problem, 165
Closed hashing, 325
Cluster, 328
Collision, 324
Comparison counting, 307
Complete graph, 59
Complete tree, 276
Compression ratio, 394
Computational complexity, 417
Connected component, 62

563


Connected graph, 62
Cook, Stephen, 424
Cost matrix, 61
Cramers rule, 263
Cross edge, 215 ; 221
Cycle, 63

D
Dantzig, George, 296
Data structure, 54
Decision problem, 418
Decision tree, 396 ; 409
Dense graph, 59
Depth, 65
Depth-rst search, 212
Depth-rst search forest, 212
Dequeue, 57
Descartes, Rene, 292
Descendants, 65
Determinant, 262
Dickens, Charles, 339
Dictionary, 69
Digraph, 58 ; 220
Dijkstra algorithm, 387
Dijkstra, Edsger W., 387
Directed acyclic graph, 221
Directed graph, 58 ; 220
Distance matrix, 349
Distribution counting, 308
Double hashing, 328
Doubly linked list, 56
Douglas, Michael, 369
Dynamic Huffman encoding, 394
Dynamic programming, 306

E
Edison, Thomas, 441
Einstein, Albert, 73
Enqueue, 57
Essentially complete tree, 276
Euler, Leonard, 52 ; 255
Exact algorithm, 35
Exhaustive search, 159
Exponent, 431
Extensible hashing, 329
External path length, 189
Extrapolation, 133

Extreme point, 156

F
Fast Fourier transform, 284
Feasible region, 159
Feasible solution, 451
FFT, 284
Fibonacci heap, 390
Fibonacci, Leonardo, 119
FIFO, 57
First-in-rst-out, 57
Fixed-length encoding, 392
Flavius, Josephus, 235
Flowchart, 38
Floyds algorithm, 350
Floyd, R., 350
Forest, 63
Forsythe, George, 14 ; 435
Forward edge, 221
Free tree, 63
Front, 57

G
Galois, Evariste, 476
Gauss elimination, 255
Gauss, Karl Friedrich, 107 ; 255 ; 476
GaussJordan elimination, 265
General linear kth degree recurrence with
constant coefcients, 501
Generality, 39
Generic term, 495
Gloor, Peter, 136
Goethe, Johann Wolfganh von, 305
Golden ratio, 121
Goldstine, H., 34
Graph
Acyclicity, 61
Connectivity, 61
Graph-coloring problem, 49
Gray code, 231

H
Halting problem, 419
Hamilton, William Rowan, 53 ; 160
Hamiltonian circuit, 53
Harel, David, 23 ; 154

564

Hash address, 324


Hash function, 323
Hash table, 323
Hashing, 323
Header, 56
Heap, 58 ; 275 ; 276
Bottom-up construction, 277
Top-down construction, 279
Heapsort, 281
Height-balanced tree, 272
Hopcroft, John, 271
Horners rule, 284
Horner, William George, 284
Horspool, R., 150 ; 312
Huffman code, 393
Huffman tree, 393
Huffmans algorithm, 393
Huffman, David, 393
Huxley, Thomas H., 487

I
Icosian Game, 53
Ill-conditioned problem, 433
In place algorithm, 47
Independent set, 428
Information-theoretic lower bounds, 405
Inhomogeneous recurrence, 123
Initial condition, 108 ; 496
Inorder traversal, 187
Input enhancement, 305
Instability, 433
Integer linear programming, 296
Internal path length, 189
Interpolation, 133
Interpolation search, 240
Inversion, 210

J
JohnsonTrotter algorithm, 228
Josephus problem, 235

K
Kahan, William, 437
Karmarkar, Narenda, 296
Karp, Richard, 419 ; 489
Kayal, Neeraj, 424

Key, 46 ; 323
Knuth, Donald Ervin, 284 ; 328
Kruskals algorithm, 378
Kruskal, Joseph, 378

L
Lambert, Johann, 518
Last-in-rst-out, 57
Leaf, 65
Least common multiple, 292
Left-to-right binary exponentiation, 287
Lexicographic order, 229
LIFO, 57
Linear congruential method, 130
Linear probing, 327
Linear programming, 295
List, 57
Load factor, 326
Loop of graph, 59
Lower bound, 403
Lower hull, 198
LU-, 259
Lucas, Edouard, 117
Lytton, Edward, 141

M
M-coloring problem, 418
Mantissa, 431
Martello, S., 472
Matroid, 370
Maximization problem, 293
McGreight, Edwarg Meyers, 331
Median, 238
Memory functions, 364
Mergesort, 169
Method of false position, 480
Minimal-change requirement, 227
Minimization problem, 293
Minimum spanning tree, 371
Mode, 250
Moore, J., 48 ; 150 ; 316
Morse, Samuel, 392
Motwani, R., 489
Multiplication a` la russe, 234
Multiset, 68
Multiway search tree, 66

565

N
Neumann, John von, 34 ; 489
Newtons method, 481
Newton, Isaac, 255 ; 284
Node, 56
Nondeterministic algorithm, 421
NP-complete, 422 ; 423
NP- , 161

O
Open addressing, 325
Open hashing, 325
Optimal solution, 451
Optimality, 42
Order of growth, 78
Order statistic, 238
Ordered tree, 65
Overow, 432

P
Parallel algorithms, 35
Parent, 64
Parental vertex, 65
Particular solution, 496
Partition, 174
Partition problem, 165
Path, 61
Pattern, 148
Pivot, 174
Pointer, 56
Polya, George, 45
Postorder traversal, 187
Power set, 210 ; 229
Prex code, 393
Preorder traversal, 187
Prestructuring, 306
Prims algorithm, 371
Prim, R.C., 371
Principle of optimality, 352
Priority, 275
Priority queue, 57 ; 275
Problem reduction, 291
Problems instance, 34
Proper ancestor, 64
Pseudocode, 37
Purdom, P.W., 501

Q
Queue, 57
Quickhull, 198
Quicksort, 174

R
Radix sort, 249
Raghavan, P., 489
RAM, 34
Random-access machine, 34
Rear, 57
Recurrence, 108
equation, 496
General solution, 496
relation, 108 ; 496
Red-black tree, 266
Regula falsi, 480
Relative error, 431
Representative, 381
Right-to-left binary exponentiation, 288
Root of tree, 63
Rooted tree, 64
Rotation, 266
Round-off error, 431
Rufni, Paolo, 476
Russian peasant method, 234

S
Sahni, S., 471
SaintExupery, Antoine de, 40
Saxena, Nitin, 424
Search key, 47
Second-order linear recurrence with constant
coefcients, 499
Selection problem, 238
Separate chaining, 325
Sequence, 495
Sequential algorithms, 35
Set, 67
Shell, D.L., 209
Shellsort, 209
Sherman, D., 135
Shor, Peter, 489
Sibling, 65
Signicant digits, 431
Simplex method, 156 ; 296

566

Single-destination shortest-paths problem,


391
Single-pair shortest-path problem, 391
Single-source shortest-paths problem, 386
Singly linked list, 56
Singular matrix, 261
Smoothness rule, 115 ; 503
Space efciency, 39 ; 75
Spanning tree, 371
Sparse graph, 59
Splay tree, 266
Squashed order, 230
Stable algorithm, 47
Stack, 57
State-space graph, 298
State-space tree, 64 ; 443
Straight insertion sort, 206
Strassen, V., 192
String, 48 ; 55
Binary, 55
Bit, 55
Matching, 48
Strongly connected components, 225
Strongly connected graph, 225
Subgraph, 62
Subset-sum problem, 445
Subtractive cancellation, 432
Subtree, 65
Synthetic division, 286

T
Tarjan, Robert, 84
Ternary search, 237
Time efciency, 39 ; 75
Top, 57
Topological sorting, 222
Toth, P., 473
Transitive closure, 226 ; 345
Traveling salesman problem, 35
Tree, 63
Tree edge, 212 ; 215 ; 220
Truncating error, 429
Turing, Alan, 419

U
Undecidable problem, 418
Underow, 432

Universal set, 68
Upper hull, 198

V
Variable-length encoding, 393
Vertex cover, 427
Virtual initialization, 311; 364

W
Warshalls algorithm, 346
Warshall, S., 346
Weight, 61
Weight matrix, 61
Weighted graph, 61
Williams, John William Joseph, 281
Worst-case efciency, 81

, , 476
, 69
, , 424
, .., 267
, , 490
, 25 ; 31; 71
c-, 463
RSA, 425
, 135
, 77
, 317
, 198
, 135
, 346
, 75
, 387
, 476
, 228
, 27 ; 243
, 32
-
, 405
, 86 ; 254
, 258
, 265
, 296
, 40
, 38

567


, 378
, 480
, 36
, 37
, 421
, 98
, 433
, 403
, 481
, 39
, 42
, 77
, 35 ; 489
, 28
, 35 ; 489
, 35 ; 461
, 371
, 39
, 75
, 488
, 107
, 480
, 51
, 47
, 47
, 35
, 403
, 39
, 350
, 393
, 231
, 480
, 313
, 122
, 192
, 33
, 39
, 23
, 31
, 73
, 135
, 432

, 77

, , 331
, ., 34
, , 135
, , 339 ; 352
, , 23
, 207

, 287

, 288
, 184
, 186
, 187
, 187
, 187
, 186
, 187
, 65 ; 242
, 180
, 341
, , 141
, 68
-, 38
, , 48 ; 150 ; 316
, ., 501
, , 203
, 284

, 258

, 68
, 164
, 198
, 39
, 61
, 135
, , 281
, 311; 364
, 200
, 200
, ., 346
, 155
, 154
, 417
, 25

568

, , 476
, , 53 ; 160
, 53 ; 160
, , 107 ; 255 ; 476
, 254
, 265
, , 305
, , 136
, ., 34

, 254
, , , 43
Hi-Q, 450
-, 311
, 244
, 53
, 220
, 165
, 151
, 52
, 180
, 376
, 126
, 209
, 298
, 409
, 301
, 87
, 428
, 330
, 244
, 43
, 237
, 173

, 416
, 111
, , 284
, 284
, 48
2-, 219
, 61; 63
, 48 ; 58
, 427
, 61

, 219
, 427
, 61
, 59
,
474
, 371
, 428
, 58
, 58 ; 220
, 221
, 371
, 59
, 59
, 212
, 215
, 59
, 59
, 298
, 61
, 62
, 59
, 48 ; 58
, 60
, 61; 62
, 62
, 225
, 225
, 215
, 226 ; 345
, 63
, 231

, , 296
, 328
, ., 387
, , 292
, 286
, 63
2-3, 266 ; 272
2-3-4, 266
AVL, 266 ; 267
B-, 266 ; 331
, 65 ; 184
, 65 ; 242


, 65
, 65
, 65
, 189
, 189
, 63
, 63
, 266
-, 266
, 371
, 371
, 266 ; 267
, 66
, 267
, 276
, 276
, 64
, 396 ; 409
, 64 ; 443
, 113
, 272
, 65
, 393
, 118 ; 262
, 228
, 200
, , 339
, 394
, 306
, 32
, 189
, 189
, 159
, 451
, , 369

, 26
, 27
, 32
, 152 ; 158
, 261

, 56

CNF-, 424

569
m-, 418
NP-, 422 ; 423
NP-, 161
, 238
, 154 ;
156
, 250
, 235
, 50
, 35 ; 49 ; 159 ; 463
, 295

, 296
, 293
, 293
, 418
n , 443
, 198
, 444
, 165
, 371
, 196
, 310
, 367 ; 369
, 311
, 160 ; 361
, 445
, 419
, 361
, 433
, 47 ; 147

, 391

, 391

, 386

, 349
, 152
, 50
, 291
, 418

, 249
, 165

570

, 49
, 45 ; 142
, 325 ; 326
, 345
, 431
, 121

, 210
, 55
, 311; 364
, 240
, 133
, 64
, 159

, , 437
, , 561
, , 296
, , 419 ; 489
, 126
, 355 ; 361
, , 424
, 69
, 46 ; 47 ; 323
, , 284 ; 328

, 231
, 393
, 393

, 393
, 392
, 394
, 324
, 50
, 25
, 38
, 266
, 263
- , 266
, , 378
, , 424
, , 525

, , 518

, .., 267
, , 424
, 229
, 63
, 212
, 215
, 327
, 156 ; 295
, 55
, 130
, 65
, , 141
, 93
, , 117

, 165
, , 331
, 431
, 157
, ., 472

, 55
, 55
, 55
, 248

, 255
, 61
, 118 ; 262
, 261
, 260
, 261
, 118 ; 262
, 349
, 261
, 61
, 101; 192
, 59
, 370
, 34
, 238

, 451
, 453

571


, 141
, 168
, 476
, 159
, 480
, 481
, 109
, 442
, 247
, 36
, 405
, 234
, 480

, 204
, 206

, 203
, 480
, 371
, 227
, 66
, 200
, 67
, 210 ; 229
, 68
, 250
, , 392
, ., 489
, 47 ; 68
, ., 48 ; 150 ; 316

, 81
, 292
, 81
, 108 ; 496
, , 34 ; 489
, 381
, 381
, 260
, 198
, , 255 ; 284

, 47

,
495
, 48
, 261
, 212 ; 221
, 174
, 118 ; 262
, 451
, 432
, 220
, 221
, 77
, 371
, 325
, 57
, 57
, 57
, 57
, 57
, 57
, 275

, 431
, 431
, 431
, 429

, 68
, 330
, 35
, 341
, 432
, 227
, 58 ; 275 ; 276
, 277
, 375
, 279
, 390
, 281
, 230
, 62
, 65
, 47
, 180
, 240
, 48 ; 148

572

, 147
, 442
, , 45
, 267
, 431
, 210 ; 229
, 429
, 215 ; 221
, 249
, 238
, 78
, 495
, 35
, 432
, 64

, 503
, 263
, 115
, 306
, 64
, 64
, 64
, 393
, 35
, 291
, ., 371
, 352
, 275
, 206
, 221
, 37
, 130
, .., 501

, ., 489

, 152 ; 158
, 157
, 329
, 212 ; 215 ; 220
, 496
, 496
, 504

,
499

n-
,
501
, 496
, 499
, 496
, 108 ; 496
, 108

, 123
, 29
, 64
, 65
, 64
, , 476

, , 424
, ., 471
, , 40
-, 156 ; 296
, 261
, 286
, 69
, 45
, 174
, 176
, 174
, 206
, 207
, 143
, 281
, 308
, 307
, 249
, 144
, 169
, 170
, 222
, 209 ; 211
, 57
, 56
, 56
, 56
, 56

573


, 57
, 57
, 394
, 93
, 48 ; 55
, 55
, 55
, 55
, 48
, 54
, 55
, 284
, 471

, , 84
, 148
, 48
, 237
, 126
, 222
, ., 473
, 35
, 345
, 227
, 341
, , 419

, 156
, 56
, 186
, 186
, 56
, 56
, 305
, 192
-, 234
, 58 ; 68
, 58 ; 68

, 32
, 499
, 47

, 107

, 390
, 116 ; 119 ; 339
, , 119
, , 235
, ., 350
, 429
, , 14 ; 435
, 364
, 284

, , 487
, 120 ; 499
, , 23 ; 154
, , 393
-, 324
-, 323
-, 323
, 323
, 328
, 325 ; 326
, 328
, 326
, 327
, 325
, 329
, 325 ; 326
, 325
, , 271
, ., 150 ; 312

, 160

, 355 ; 361
, 116 ; 119 ; 339

, 148
, ., 209
, ., 135
, , 489
, ., 192
, 192

574

, 462
, , 441
, , 52 ; 255
, , 73
, 34

, 431
, 133
, 29

, 94

..
..
..
.. ,
..


101509, . , . , . 43, . 1
10.03.2006. 70100/16
Times.
. . . 46,4. .-. . 30,1
2000 .
.

. ..
.
197110, -, ., 15.

"" -
"Books.Ru - "
. (ISBN 5-8459-0987-2)
- "Books.Ru - ".
- ,
. ,
"" .