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

7

.

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

7.1.
. =
(X, U, ), X ; U (); ;
X U = . (x, u, y), x, y X, u U ,
( ), (
) :
1. u U x, y X (x, u, y) .
2. (x, u, y) (x0 , u, y 0 ) (x = x0 y = y 0 ) (x = y 0 y = x0 )
u x, y.


1. x X .
2. (x, u, y) (y, u, x)
, .
3. (x, u, y) (y, u, x)
.
4. (x, u, x) .


1. x .
2. x
y
.
3. x
y
.
4. x .

. 1 (X1 , U1 , 1 ) 2 (X2 , U2 , 2 )
(1
= 2 ), : X1 X2 : U1 U2 , : 2 ((x1 ), (u1 ), (y1 )) = 1 (x1 , u1 , y1 ).

264

7.1.

r
6
r

-r

. 7.1.

,
(. 7.2).
r

r
r

r
r

r
r

. 7.2.

. (), :x 6= y Xu U
(x, u, y) (y, u, x). .
. (X, U, ) 0 (X 0 , U 0 , ), X 0 X, U 0 U . 0 .
. ,
, . .
. (. 7.3).
r

?
r

r

-r

r

. 7.3. () ()

. ,
.
. ,
. n Cn2 (. 7.4).

7.1.

265

r
r

r
r

. 7.4.

.
, , (. 7.5).
r
r

r
r

. 7.5.

. (),
, .
r

r
r

. 7.6. 1 , 2

. x y u,
, x, y , u
x y. ,
.
. , . ,
0, ,
1, .
. ( ), (. 7.7).

266

7.1.

x2 r

rx
3

x4

x1 r

. 7.7. 1 , 2

. () (X, U, )

x1 u1 x2 u2 x3 . . . xn un xn+1 , xi X, ui U . ui
xi xi+1 , . .
(xi , ui , xi+1 ).
, .
, x 1 = xn+1 .
.
, .
.
,
.
, .

r

r

1
?
r

j
-r

?
r

. 7.8. 1 , 2

. (X, U, ) ,
x, y X x
y ( x y ). .
,
( ) (. 7.8).
.
,
r
r
r
(. . 6.8). r
1
r

2
r

. 7.9. 1 , 2

7.2.1.

267

, , . . . ,
.

.

7.2.
()
() . .
. , ,
, .
.
, . .
, .
7.2.1.
.
n A = [a ij ],
i, j = 1, 2, . . . , n,

1, (xi , xj ),
aij =
0, xi , xj (xi , xj )
.
. 7.10 . 7.11. ,
. , 1, ,
.

268

7.2.2.

2
r


4
- r
I
r6

?
r 
1

?
- r
3

R?
r

5

jR r?
7

. 7.10.

1
2
A= 3
4
5
6
7

0
1
0
1
0
0
0

1
0
0
0
0
0
0

1
1
0
0
0
0
0

0
1
0
0
0
1
0

0
1
1
1
0
1
1

0
0
0
0
0
0
0

0
1
0
1
0
1
0

. 7.11. (. 7.10)

7.2.2.
. n m
B = [bij ], i = 1, 2, . . . , n, j = 1, 2, . . . , m, , .
bij =

1, xi uj ;
0, xi uj .

. n m
B = [bij ], i = 1, 2, . . . , n, j = 1, 2, . . . , m, , .
bij =

+1, uj xi ;
1, uj xi ;
0, xi uj .

7.2.5.

269

.
. 7.12 . 7.10.
1
2
1
B= 2
3
4
5
6
7

1
3

2
1

2
3

2
4

2
5

2
7

3
5

4
1

4
5

4
7

6
4

6
5

6
7

7
5

+1 +1 1 0 0 0 0 0 1 0 0 0 0 0 0
1 0 +1 +1 +1 +1 +1 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 +1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 +1 +1 +1 1 0 0 0
0 0 0 0 0 1 0 1 0 1 0 0 1 0 1
0 0 0 0 0 0 0 0 0 0 0 +1 +1 +1 0
0 0 0 0 0 0 1 0 0 0 1 0 0 1 +1

. 7.12. (. 7.10)

7.2.3.
. , .
W = [w ij ], wij
, i, j = 1, 2, . . . , n. 0
. ,
.
7.2.4.
. r = (r 1 , r2 , . . . , rm )
t = (t1 , t2 , . . . , tm ), m . , i-
ri ti . ,
. 7.10 :
r = ( 1, 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 6, 6, 6, 7),
t = ( 2, 3, 1, 3, 4, 5, 7, 5, 1, 5, 7, 4, 5, 7, 5).
,
.
7.2.5.

.
Adj[x] ,

270

7.3.

x. Adj[x]
. . 7.13
. 7.10.
n (
) .
, , . , . ,
.

7.3.

Adj[x]

1 2, 3

2
1, 3, 4, 5, 7

3
5
1, 5, 7
4
,
5
6
4, 5, 7
(. . 4.1).
7
5
(X, U, )
.
. 7.13
x X,
(x, y), y X.
y ( ), x . y ,
y. , x,
, (s, x),
x, ,
s X. ,
, .
. , , x,
(x, y), x. , y, x ,
y , .
,
.

7.1. Depth(x, w)
(X, U, ),
x X, T ,

7.3.

271

0 (X, T, ) (
, 0 ); w X
x X , x .
7.1.

for v X do M ark[v] = 0;
count = 0;
T = ;
B = ;
for v X do if M ark[v] = 0 then Depth(v, 0);
Procedure Depth(x, w);
count = count + 1;
M ark[x] = count;
for v Adj[x] do begin
if M ark[v] = 0 then begin
T = T {(x, v)}; { }
Depth(v, x);
end
else if M ark[v] < M ark[x] and v 6= w then
B = B {(x, v)}; { }
end;
end.

Adj[x], Adj[x] , x X. T
, B , 0 ,
. , .
,
M ark[x] ,
1 |X| , .
M ark[x] = 0 x X ,
, x , M ark[x]
. (x, v) T , M ark[v] = 0. M ark[v] 6= 0, ,
(x, v) B ,
M ark[v] < M ark[x] v 6= w. M ark[v] < M ark[x] , v x.
(x, v) B ,
T , w x, . . v 6= w.
. , ,
Depth , |X|.

272

7.3.

, . O(|X| + |U |).
7.1 7.2. 7.1. Init, Depth,
W ayDepth, W ayDepth ; Depth ,
7.1; Init
. .
1 |X|.
, ,
.
Adj[x] . for v Adj[x] do for i = 1 to N br[x] do
v = Adj[F st[x] + i] , N br[x] x X; Adj[x] , ; F st[x] + 1
x X, F st[x] + i i-
x X. ,
Adj[x] :
x
1
2
3
4
5
6
7

Adj[x]
2, 3, 5
1, 3, 4
1, 2, 4, 5
2, 3
1, 3, 6, 7
5, 7
5, 6


N br[x]

F st[x]

10

12

16

18

Adj[x]

2 3 5

1 3 4

1 2 4 5

2 3

1 3 6 7

5 7

5 6

7.2

Adj[x]:

7.3.

273

, ;

,
.
7.2 , . 7.14.
,
, .
2
r

1
r

r
4

r
3

6
r

5
r

r
7

. 7.14.

. 7.14 Depth.in:
7
1
2
3
4
5
6
7

3
3
4
2
4
2
2

2
1
1
2
1
5
5

3
3
2
3
3
7
6

5
4
4 5
6 7


Depth.out :
1
2
1

2
3
1

3
1
0

3
4
1

4
2
0

3
5
1

5
1
0

5
6
1

6
7
1

7
5
0

xi
yi
T B

(xi , yi ) , 1 0, ,
.
7.2.

Program PgmWayDepth; { }
uses CRT,DOS;
Const

274

7.3.

nVertex=100; { }
nAdjacent=1000; { }
Type
TypeVertex=array[1..nVertex] of Integer;
TypeAdjacent=array[1..nAdjacent] of Integer;
Var
f
:Text;
{ }
n
:Integer;
{ }
nT
:Integer;
{ }
Adj :TypeAdjacent; { }
Fst :TypeVertex;
{ }
Nbr :TypeVertex;
{ }
Vtx :TypeVertex;
{ }
Mark :TypeVertex;
{ }
T
:TypeVertex;
{ }
B
:TypeVertex;
{ }
Procedure Init( Var yes :Boolean );
{ }
{ }
{ yes - }
Var i,j,m :Integer;
begin
for i:=1 to n do
for j:=1 to Nbr[i] do begin
yes:=FALSE;
for m:=1 to n do
if Adj[Fst[i]+j]=Vtx[m] then begin
yes:=TRUE;
Adj[Fst[i]+j]:=m;
break;
end;
if not yes then exit;
end;
end;
Procedure Depth( x,u:Integer; var count :Integer);
Var i,v :Integer;
begin
count:=count+1;
Mark[x]:=count;
for i:=1 to Nbr[x] do begin
v:=Adj[Fst[x]+i];
if Mark[v]=0 then begin
nT:=nT+2; T[nT-1]:=x; T[nT]:=v;
B[nT div 2]:=1; { }
Depth(v,x,count);
end
else if (Mark[v]<Mark[x]) and (v<>u) then begin
{ }
nT:=nT+2; T[nT-1]:=x; T[nT]:=v;
B[nT div 2]:=0; { }
end;
end;

7.4.

275

end;
Procedure WayDepth; { }
Var v,count :Integer;
begin
nT:=0; {T - }
count:=0;
for v:=1 to n do Mark[v]:=0;
for v:=1 to n do if Mark[v]=0 then Depth(v,0,count);
end;
Var {Main}
i,j :Integer;
yes :Boolean;
begin {Main}
Assign(f,Depth.in);
Reset(f);{ }
{ }
Read(f,n); { }
Fst[1]:=0; { }
for i:=1 to n do begin
Read(f,Vtx[i]); { }
Read(f,Nbr[i]); { }
for j:=1 to Nbr[i] do
Read(f,Adj[Fst[i]+j]); { }
Fst[i+1]:=Fst[i]+Nbr[i]; { }
end;
Close(f);
Assign(f,Depth.out);
Rewrite(f); { }
Init(yes);
if not yes then begin
WriteLn(f, !);
Close(f);
exit;
end;
WayDepth;
for i:=1 to nT div 2 do Write(f,Vtx[T[2*i-1]]:3);
Writeln(f);
for i:=1 to nT div 2 do Write(f,Vtx[T[2*i]]:3); Writeln(f);
for i:=1 to nT div 2 do Write(f,B[i]:3); Writeln(f);
Close(f);
end. {Main}

7.4.
, S, ,
, (. 1.7.8):
1. s1 S s1 s1
2. s1 , s2 S s1 s2 s2 s1
3. s1 , s2 , s3 S s1 s2 s2 s3 s1 s3

276

7.4.

si , sj S, s i
sj , .
. ,
si S, Sbi = {s S | si s}
, s i .

7.4.1. S
. s i S Sbi = {s S | si s}.
Sbi :
1). si S , si Sbi .
2). , s i sj ,
Sbi = Sbj .
3). ( Sbi = Sbj ), : Sbi Sbj 6= .
.
1). Sbi = {s S | si s}
si S. si si , si Sbi .
2). z Sbj = {s S | sj s}
. , sj z. si sj . si sj sj z si z,
z Sbi . Sbj Sbi . Sbj Sbi . ,
Sbj = Sbi .
3). z Sbi Sbj 6= .
, si z sj z.
sj z z sj , si z z sj
si sj . , si sj . ,
, Sbi = Sbj (. 2).
. S (). S
S = Sbk1 Sbk2 . . . Sbkm , Sbki Sbkj = ,
ki 6= kj . Sbki , . Sbki , i = 1, 2, . . . , m,
S S/ .
. S .

7.5.

277

S () . , 4a, 4b S 4a 4b, . , 13 .
.
. S n- . S () . ,
a, b S a b, .
,
1 3 . .
. S = {1, 2, . . . , n}. S
() . , p, q S
p q, m. . S

m.
. ()
S. si , sj S si sj ,
. .
. S . S () si sj = si sj
, si , sj S.
.
, ,
.

7.5.
(X, U, ) .
x1 , x2 X , x1 x2 . X
() . x1 , x2 X , . . x1 x2 , .
. , x1 x2 , x2 x3 ,

278

7.5.

, x1 x3 . ,
[
X=
Xi
i

,
Xi , Xi .

[
b i , Ui , )
=
(X
i

(X, U, )
b i , Ui , ). ,
(X
,
. , .

7.5.1.
.


.
7.5.2. (X, U, )
n k .
2
C nk+1
= (n
k + 1)(n k)/2.
. =
k
S
b i , Ui , ) .
(X
i=1

, X i
k
P
Cn2i . ni ,
i=1

, . ,
b i , Ui , ) ,
(X
, n 2 > n1 > 1.

7.6.

279

b1
b2 . ,
k
. , , k 1
n k + 1 .
. n ,
2
Cn1
= (n 1)(n 2)/2, .

7.6.
,
. . 7.3

(X, U, ). 7.3 7.1
. 7.3 M ark[x] x X .
M ark[x] ,
x X. 7.3,
7.1, O(|X| + |U |).
7.3.

for v X do M ark[v] = 0; { }
count = 0; { }
for v X do if M ark[v] = 0 then begin
count = count + 1;
Component(v, count);
end;
Procedure Component(x, count);
M ark[x] = count;
for v Adj[x] do if M ark[v] = 0 then begin
Component(v, count);
end;
end.


7.4, 7.3. 7.4
, . 7.15.

280

7.6.

2 r

r3

14 r

r 15

8 r

1 r

r4

12 r

r 21

7 r

r9

. 7.15.

7.4
Adj[x] . 7.15
Connecnt.in. ()
,
7.2.
Connect.in . 7.15:
11
1
2
9
8
12
14
3
4
7
15
21

3
2
1
1
3
2
3
2
2
2
1

2
3
7
7
14
12
2
1
9
14
12

3
1

15 21
15
1 4
3
8
12

Connect.out
:
1 2 9 8 12 14 3 4 7 15 21 ;
1 1 2 2 3 3 1 1 2 3 3 .

7.4.

Program ConnectComponent; { }
uses CRT,DOS;
Const nVertex=100; { }
nAdjacent=1000; { }
Type TypeVertex=array[1..nVertex] of Integer;
TypeAdjacent=array[1..nAdjacent] of Integer;
Var
f
:Text;
{ }
n
:Integer;
{ }
Adj :TypeAdjacent; { }
Fst :TypeVertex;
{ }
Nbr :TypeVertex;
{ }
Vtx :TypeVertex;
{ }

7.6.

Mark :TypeVertex;
{ }
Procedure Init( Var yes :Boolean );
{ }
{ }
{ yes - }
Var i,j,m :Integer;
begin
for i:=1 to n do
for j:=1 to Nbr[i] do begin
yes:=FALSE;
for m:=1 to n do
if Adj[Fst[i]+j]=Vtx[m] then begin
yes:=TRUE;
Adj[Fst[i]+j]:=m;
break;
end;
if not yes then exit;
end;
end;
Procedure Component( x,count :Integer);
Var i,v :Integer;
begin
Mark[x]:=count;
for i:=1 to Nbr[x] do begin
v:=Adj[Fst[x]+i];
if Mark[v]=0 then Component(v,count);
end
end;
Procedure Connect; { }
Var v,count :Integer;
begin
for v:=1 to n do Mark[v]:=0;
count:=0; { }
for v:=1 to n do begin
if Mark[v]=0 then begin
count:=count+1;
Component(v,count);
end;
end;
end;
Var {Main}
i,j :Integer;
yes :Boolean;
begin {Main}
Assign(f,Connect.in);
Reset(f);{ }
{ }
Read(f,n); { }
Fst[1]:=0; { }
for i:=1 to n do begin

281

282

7.7.

Read(f,Vtx[i]); { }
Read(f,Nbr[i]); { }
for j:=1 to Nbr[i] do
Read(f,Adj[Fst[i]+j]);{ }
Fst[i+1]:=Fst[i]+Nbr[i];{ }
end;
Close(f);
Assign(f,Connect.out);
Rewrite(f); { }
Init(yes);
if not yes then begin
WriteLn(f, !);
Close(f);
exit;
end;
Connect;
for i:=1 to n do Write(f,Vtx[i]:3); Writeln(f);
for i:=1 to n do Write(f,Mark[i]:3);
Close(f);
end. {Main}

7.7.
C r
.
. A D,
rD
A r
C B ,
. 7.16. : r
B
, ,
, . . 7.16.


.
. (X, U, )
. ,
. .
.

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

7.7.

283

7.7.1 (.). (X, U, )


, :
1. .
2. (
).
3. a b a 6= b,
.
4. a b a = b,
.
. () ,
au1 x2 u2 . . . xn un b, a, b, xi X, ui U , .
, . . .
14: 1)
, , ; 2) ui1 xi ui xi ,
ui ,
; 3) 4)
2).
() 14. . 3) 4),
u , a b.
3) .
A X (. 7.17). , ,
. ( 1 )
A, , ,
, (
).
A r

1


r B

12

2


. 7.17.

: 1) 1
, ; 2) 1 . \ 1 ,
, 1 . \ 1

284

7.7.

( ).
, 1 \ 1 .
B X. 2 ,
1 . 12
1 2 , . 7.17, ..
1 2 . 12 = 1 2
, 1 .
,
: Au 1 x2 . . . au b . . . un A.
u ,
aw1 t2 . . . tn wn b, ti X, wi U .
7.5.
v X; { }
Z = {v}; { }
R = ; { }
repeat
Cycle(v, R);
Z = Z R; { }
until Not v Z |Adj[v]| > 0
Procedure Cycle(v, R)
R = {v}; { }
repeat
w = Adj[v];
R = R {w};
Adj[v] = Adj[v] \ {w}; { (v, w)}
if v 6= w then Adj[w] = Adj[w] \ {v}; { (w, v)}
until Not |Adj[v]| > 0 { }
end.

7.5 .
Adj[x], Adj[x] ,
x X.
Z.
, Z, R. , R ( , Z),
Adj[x] (
). R

7.7.

285

,
.
. Cycle ,
|X|.
,
. Cycle U . O(|X| + |U |).
6.6, 6.5.
7.6.
Program EilerWay; { }
uses CRT,DOS;
Const
nVertex=100; { }
nAdjacent=1000; { }
Type
TypeVertex=array[1..nVertex] of Integer;
TypeAdjacent=array[1..nAdjacent] of Integer;
Var
f
:Text;
{ }
ks :Integer;
{ }
n
:Integer;
{ }
Adj :TypeAdjacent; { }
Fst :TypeVertex;
{ }
Nbr :TypeVertex;
{ }
Vtx :TypeVertex;
{ }
Deg :TypeVertex;
{ }
kz :Integer;
{ }
z
:TypeAdjacent; { }
r
:TypeAdjacent; { }
Procedure Init( Var yes :Boolean );
Var i,j,k,m :Integer;
begin
{ }
{ }
{ yes - }
for i:=1 to n do
for j:=1 to Nbr[i] do begin
yes:=FALSE;
for m:=1 to n do
if Adj[Fst[i]+j]=Vtx[m] then begin
yes:=TRUE;
Adj[Fst[i]+j]:=m;
break;
end;

286

7.7.

if not yes then exit;


end;
{ }
for i:=1 to n do begin
k:=1;
for j:=1 to Nbr[i] do if Adj[Fst[i]+j]=i then begin
Adj[Fst[i]+j]:=Adj[Fst[i]+k];
Adj[Fst[i]+k]:=i;
k:=k+1;
end;
end;
{ }
for i:=1 to n do begin
Deg[i]:=0;
for j:=1 to Nbr[i] do begin
Deg[i]:=Deg[i]+1;
if Adj[Fst[i]+j]=i then Deg[i]:=Deg[i]+1; {}
end;
end;
{ ks }
k:=0; ks:=1;
for i:=1 to n do if ( Deg[i] mod 2 ) > 0 then begin
k:=k+1; ks:=i;
end;
if ( k<>2 ) and ( k<>0 ) then yes:=FALSE; { }
end;
Procedure Cycle( v :Integer; var count :Integer );
{ r[] }
Var i,j,w :Integer;
begin
count:=1; r[count]:=v;
repeat
w:=Adj[Fst[v]+1]; { }
count:=count+1; r[count]:=w;
{ (v,w) v}
Fst[v]:=Fst[v]+1;
Nbr[v]:=Nbr[v]-1;
{ (w,v) ,
w }
if v<>w then
for i:=1 to Nbr[w] do if Adj[Fst[w]+i]=v then begin
for j:=i+1 to Nbr[w] do
Adj[Fst[w]+j-1]:=Adj[Fst[w]+j];
Nbr[w]:=Nbr[w]-1;
break;
end;
v:=w;
until Not( Nbr[v]>0 );
end;
Procedure Eiler; { z[] }
Var v,w
:Integer;
i,j,kt
:Integer;

7.7.

287

count
:Integer;
yes
:Boolean;
begin
v:=ks; kz:=1;
kt:=kz; z[kz]:=v;
Write(f,Z=);{ }
for i:=1 to kz do Write(f,Vtx[z[i]]:3); WriteLn(f);
repeat
Cycle(v,count);
Write(f,R=);
for i:=1 to count do Write(f,Vtx[r[i]]:3); WriteLn(f);
for i:=1 to count-1 do begin
z[kz+i]:=z[kt+i];
z[kt+i]:=r[i+1];
end;
kz:=kz+count-1;
Write(f,Z=);{ }
for i:=1 to kz do Write(f,Vtx[z[i]]:3); WriteLn(f);
yes:=FALSE;
for i:=kz downto 1 do if Nbr[z[i]]>0 then begin
v:=z[i];
kt:=i;
yes:=TRUE;
break;
end;
until Not yes;
end;
Var {Main}
i,j :Integer;
yes :Boolean;
begin {Main}
Assign(f,Eiler.in);
Reset(f);{ }
{ }
Read(f,n); { }
Fst[1]:=0; { }
for i:=1 to n do begin
Read(f,Vtx[i]); { }
Read(f,Nbr[i]); { }
for j:=1 to Nbr[i] do
Read(f,Adj[Fst[i]+j]);{ }
Fst[i+1]:=Fst[i]+Nbr[i];{ }
end;
Close(f);
Assign(f,Eiler.out);
Rewrite(f); { }
Init(yes);
if not yes then begin
WriteLn(f, );
WriteLn(f,
!);
Close(f);
exit;

288

7.7.

end;
Eiler;
Write(f,=);
for i:=1 to kz do Write(f,Vtx[z[i]]:3); WriteLn(f);
Close(f);
end. {Main}

7.6 , . 7.18.
3
r
1
r

7 r
2 r
6 r

r
5

r8
r4
r9

. 7.18.

7.6
Adj[x] . 7.18
Eiler.in. () ,

7.2.
Eiler.in . 7.18:
9
1
2
3
4
5
6
7
8
9

4
4
4
4
4
2
2
2
2

2
1
1
1
1
2
2
3
4

3
3
2
5
4
5
3
4
5

4
6
7
8
6

5
7
8
9
9

Eiler.out
:
Z=
R=
Z=
R=
Z=
=

1
1
1
5
1
1

2
2
6
2
2

3
3
2
3
3

1
1
7
1
1

4
4
3
4
4

5
5
8
5
5

1
1
4 9 5
6 2 7 3 8 4 9 5 1
6 2 7 3 8 4 9 5 1.

Z, R . O .

7.8.

289

7.8.
. (X, U, ) 0 (X, U0 , ), (.
7.19).

. 7.19.

7.8.1. n n 1 .
.
. , , ( ). ,
, > 2,
, .
. , . > 2, ,
, .
. , ,
,
.
n = 2 , . ,
,
n. n .
. ,
, . .
n 1 . n 2 , ,
n 1.
. n , . . ,
? ,
.
10 ..

290

7.8.1.

. (X, U, ), X ,
U . u U (u) u. , 0 (X, U0 ,P
), ,
W (0 ) =
(u). , 0 uU0

, ,
0 .
. ()
()
.
7.8.1.

(X, U, ) ,

. ,
, . ,
|X| 1 . ( ).
. X = {x 1 , x2 , . . . , xn } (X, U, )
Ti (Xi , Ui , ), Xi = {xi }, Ui = , Xi Xj = , i 6= j,
|X|
S
X=
Xi , i, j = 1, 2, . . . , |X|. Ti i=1
S
, T = Ti
i

0 (X, U0 , ).
0
Ti = Ti Tj (Xi = Xi Xj Ui = Ui Uj ) Ti
Tj , ,
U i = Ui Uj .
S
T =
Ti
i

0 (X, U0 , ) , |X| 1 .
.

7.8.1.

291

7.8.1. (X, U, ) 0 (X, U0 , )


. :
1) x1 , x2 X 0 ;
2) 0 U \ U0 ,
.
. 1 , 0 , 0 .
2 ,
, , .
7.8.2. (X, U, ) , u U (u),
Ti (Xi , Ui , )
, S
T = Ti , , i

0 (X, U0 , ), i = 1, 2, . . . , k k > 1.

S
= (x, y) U \ Ui
i

x X1 y
/ X1 .
0
S
(X, U, ), Ui {},
i
S
, Ui .

. . S
0 (X, U00 , ) , Ui

U1

,
S
, Ui {}. i

x r
x0 r

X1

r y
r

y0

. 7.20
1 7.8.1,
00 . 0 = (x0 , y 0 ),
, x0 X1 y 0
/ X1 , . . U1
(x, y) y
/ X, U 1 (.
7.20).
S
, () 6 ( 0 ),
Ui U 0
i
S

Ui U00
i
S
U \ i Ui ,

0 , . .
S
Ui .
i

10*

292

7.8.1.

0 (X, U0 , ),
00 0 0 . 0 ,
0 . 0 ,
x0 y 0 . , 0 . , () 6 (0 ), S
0
00 . 0 Ui ,
i

00 , .

7.7.
, . M ark[x] x X U i ,
. M ark[xi ] x i X.
M ark[xi ] U i , .
U ,
U0 .
7.7.
( )
for xi X do M ark[xi ] = i;
Sort(U ); { }
U0 = ;
while |U0 | < |X| 1 do begin
(x, y) U ; { min }
if M ark[x] 6= M ark[y] then begin
U0 = U0 {(x, y)}; { }
z = M ark[y]; { Ux Uy }
for v X do if M ark[v] = z then
M ark[v] = M ark[x];
end;
U = U \ {(x, y)}; { min }
end.

. .
O(|U | 2 ). 7.7
Ux Uy .
(

7.8.1.

293

7.7) O(|X| 2 ). , O(|X|2 + |U |2 ).



7.8,
7.7.
7.8.
Program HungryOstov; { . }
uses CRT,DOS;
Const nVertex=50; { }
nRib=1000; { }
Type TypeVertex=array[1..nVertex] of Integer;
TypeRib=array[1..nRib] of Integer;
Var
f
:Text;
{ }
nX
:Integer;
{ }
nU
:Integer;
{ }
Mark :TypeVertex; { }
X
:TypeVertex; { }
U
:TypeRib;
{ }
nUo :Integer;
{ }
Uo
:TypeRib;
{ }
We
:TypeRib;
{ }
Wt
:LongInt;
{ }
Procedure Init; { }
Var i,j,m :Integer;
begin
for i:=1 to 2*nU do Uo[i]:=1;
for i:=1 to 2*nU do
for j:=i+1 to 2*nU do if Uo[j]=1 then
if U[j]=U[i] then Uo[j]:=0;
nX:=0;
for i:=1 to 2*nU do
if Uo[i]=1 then begin
nX:=nX+1;
X[nX]:=U[i];
end;
for i:=1 to 2*nU do { }
for m:=1 to nX do
if U[i]=X[m] then begin U[i]:=m; break; end;
end;
Procedure Sort; { }
Var i,j,k,w :Integer;
begin
for i:=1 to nU do
for j:=1 to nU-i do
if We[j]>We[j+1] then begin
w:=We[j]; We[j]:=We[j+1]; We[j+1]:=w;
w:=U[2*j-1]; U[2*j-1]:=U[2*(j+1)-1]; U[2*(j+1)-1]:=w;
w:=U[2*j]; U[2*j]:=U[2*(j+1)]; U[2*(j+1)]:=w;

294

7.8.1.

end;
end;
Procedure Ostov; { }
Var i,x,y,z,sU :Integer;
begin
for i:=1 to nX do Mark[i]:=i;
Sort; { }
nUo:=0; { Uo}
sU:=1; { U}
while nUo<nX-1 do begin
x:=U[2*sU]; { }
y:=U[2*sU-1];
if Mark[x]<>Mark[y] then begin
nUo:=nUo+1;
Uo[nUo]:=sU;{ }
z:=Mark[y]; { Ux Uy}
for i:=1 to nX do if Mark[i]=z then Mark[i]:=Mark[x];
end;
sU:=sU+1; { (x,y) U}
end;
end;
Var {Main}
i,j :Integer;
begin {Main}
Assign(f,Hungry.in);
Reset(f);{ }
Read(f,nU); { }
for i:=1 to nU do Read(f,U[2*i-1]); { }
for i:=1 to nU do Read(f,U[2*i]); { }
for i:=1 to nU do Read(f,We[i]); { }
Close(f);
Assign(f,Hungry.out);
Rewrite(f); { }
Init;
Sort;
WriteLn(f,nU =,nU:3);
WriteLn(f,nX =,nX:3);
Write(f,X =);for i:=1 to nX do Write(f,X[i]:3);
WriteLn(f); Write(f,u1 =);
for i:=1 to nU do Write(f,X[U[2*i-1]]:3);
WriteLn(f); Write(f,u2 =);
for i:=1 to nU do Write(f,X[U[2*i]]:3);
WriteLn(f); Write(f,We =);
for i:=1 to nU do Write(f,We[i]:3);
Ostov;
WriteLn(f); Write(f,uo1=);
for i:=1 to nUo do Write(f,X[U[2*Uo[i]-1]]:3);
WriteLn(f); Write(f,uo2=);
for i:=1 to nUo do Write(f,X[U[2*Uo[i]]]:3);
WriteLn(f); Write(f,Woe=);
for i:=1 to nUo do Write(f,We[Uo[i]]:3);
WriteLn(f); Wt:=0;

7.8.1.

295

for i:=1 to nUo do Wt:=Wt+We[Uo[i]];


Write(f,=,Wt:3);
Close(f);
end. {Main}


, . 7.21, 7.8.
x1

15

36

x7

9
16

28

x6 r

r x3

x7

25
x5

23

x6 r

x2

23

x1

x2

20

r x3
3

x5
x4
17
17
. 7.21.

x4


. 7.21
Hungry.in :
(12);
: ,
;
.
12
7 7
1 2
1 4

7
3
9

7 7 7 1 2 3
4 5 6 2 3 4
16 25 36 20 15 3

4 5 6
5 6 1
17 28 23


Hungry.out :
nU
nX
X
u1
u2
We
uo1
uo2
Woe

=
=
=
=
=
=
=
=
=
=

12
7
7
7
1
1
7
1
1
57.

1
3
4
3
3
4
3

2
7
2
4
7
2
4

3
7
3
9
7
3
9

4
2
3
15
4
5
17

5
7
4
16
6
1
23

6
4 1 6 7 5 7
5 2 1 5 6 6
17 20 23 25 28 36

296

7.8.2.

Hungry.out:
nU
nX
X
(u1, u2)
We
(uo1, uo2)
Woe

;
;
;
;
;
;
;
.

7.8.2.


, .
1. 0
x1 .
2. , x 1 , (x1 , x2 )
0 .
3. , , x1 x2 x3 .
4. ,
0 .
.
, 14
, .

7.9, (X, U, )
W e = [wij ], +. .
0 (X0 , U0 , ) U0 X0 .
X = {1, 2, . . . , n}.
x X ,
,
X0 .
dist[x] prev[x], dist[x] x X prev[x] X 0 .
dist[x] prev[x]
X0 .

7.8.2.

297

7.9.
X = {1, 2, . . . , n}; { }
nX = |X|; { }
v = rand(1, |X|); { v X}
X0 = {v}; { }
X = X \ {v}; { v X}
U0 = ; { }
WT = 0; { }
for x X do begin
dist[x] = W e[x, v]; { x v X0 }
prev[x] = v; { (v, x), dist[x]}
end;
while |X0 | 6= nX do begin
dist[v] = min dist[x]; {v X }
xX

X0 = X0 {v}; { }
X = X \ {v}; { }
U0 = U0 {(prev[v], v)}; { }
WT = WT + dist[v]; { }
{ dist[x] }

for x X do if dist[x] > W e[v, x] then begin


dist[x] = W e[v, x];
prev[x] = v;
end;
end.

.

.
. , O(|X| 2 ).
7.10, 7.9.
7.10.
Program NearOstov; { . }
uses CRT,DOS;
Const
nVertex=50; { }
nRib=1000; { }
Type
TypeVertex=array[1..nVertex] of Integer;
TypeRib=array[1..nRib] of Integer;
TypeWeight=array[1..nVertex,1..nVertex] of Integer;
Var
f
:Text;
{ }
nX
:Integer;
{ }

298

7.8.2.

nXo :Integer;
{ }
nUo :Integer;
{ }
X
:TypeVertex; { }
Xo
:TypeVertex; { }
Uo
:TypeRib;
{ }
Prev :TypeVertex; { }
Dist :TypeRib;
{ }
We
:TypeWeight; { }
Wt
:LongInt;
{ }
{ v - }
Procedure minDist( Var v :Integer );
Var i,d :Integer;
begin
v:=X[1];
d:=Dist[v];
for i:=2 to nX do
if d>Dist[X[i]] then begin
v:=X[i];
d:=Dist[v];
end;
end;
{ dist[] - }
Procedure newDist( v :Integer );
Var i :Integer;
begin
for i:=1 to nX do
if Dist[X[i]]>We[X[i],v] then begin
Dist[X[i]]:=We[X[i],v];
Prev[X[i]]:=v;
end;
end;
Procedure Ostov; { }
Var i,nStop,v :Integer;
begin
nStop:=nX;
for i:=1 to nX do X[i]:=i;
v:=1;
for i:=1 to nX do begin
Dist[i]:=We[i,v];
Prev[i]:=v;
end;
nXo:=0; { Xo - }
nUo:=0; { Uo - }
nXo:=nXo+1;
Xo[nXo]:=X[v]; { Xo=Xo+v - v }
X[v]:=X[nX]; nX:=nX-1; { X=X\v - v X }
Wt:=0;
while nXo<>nStop do begin
minDist(v);
nXo:=nXo+1;
Xo[nXo]:=v; { Xo=Xo+v - v }
for i:=1 to nX do { X=X\v - v X }

7.8.2.

299

if X[i]=v then begin X[i]:=X[nX]; break; end;


nX:=nX-1;
nUo:=nUo+1;{Uo=Uo+(Prev[v],v)- }
Uo[2*nUo-1]:=Prev[v];
Uo[2*nUo]:=v;
Wt:=Wt+Dist[v]; { }
newDist(v);
end;
end;
Var {Main}
i,j :Integer;
begin {Main}
Assign(f,Near.in);
Reset(f);{ }
Read(f,nX); { }
for i:=1 to nX do begin
for j:=i to nX do begin
Read(f,We[i,j]); { }
if We[i,j]=0 then We[i,j]:=$7fff; {+}
We[j,i]:=We[i,j];
end;
end;
Close(f);
Assign(f,Near.out);
Rewrite(f); { }
nUo:=0;{ }
for i:=1 to nX do
for j:=i+1 to nX do begin if We[i,j]=$7fff then continue;
nUo:=nUo+1;
Uo[2*nUo-1]:=i;
Uo[2*nUo]:=j;
end;
WriteLn(f,nU =,nUo:3); WriteLn(f,nX =,nX:3);
Write(f,X =);for i:=1 to nX do Write(f,i:3);
WriteLn(f); Write(f,u1 =);
for i:=1 to nUo do Write(f,Uo[2*i-1]:3);
WriteLn(f); Write(f,u2 =);
for i:=1 to nUo do Write(f,Uo[2*i]:3);
WriteLn(f); Write(f,We =);
for i:=1 to nUo do Write(f,We[Uo[2*i-1],Uo[2*i]]:3);
Ostov;
WriteLn(f); Write(f,uo1=);
for i:=1 to nUo do Write(f,Uo[2*i-1]:3);
WriteLn(f); Write(f,uo2=);
for i:=1 to nUo do Write(f,Uo[2*i]:3);
WriteLn(f); Write(f,Woe=);
for i:=1 to nUo do Write(f,We[Uo[2*i-1],Uo[2*i]]:3);
WriteLn(f); Wt:=0;
for i:=1 to nUo do Wt:=Wt+We[Uo[2*i-1],Uo[2*i]];
Write(f,=,Wt:3);
Close(f);
end.{Main}

300

7.9.

7.10 ,
. 7.21.
Near.in :
;
( ) .
7
0

20
0

0
15
0

0
0
3
0

0
0
0
17
0

23
0
0
0
28
0

1
4
9
16
25
36
0

Near.out
:
nU
nX
X
u1
u2
We
uo1
uo2
Woe

=
=
=
=
=
=
=
=
=
=

12
7
1
1
2
20
1
7
1
57.

2
1
6
23
7
2
4

3
1
7
1
7
3
9

4
2
3
15
3
4
3

5
2
7
4
4
5
17

6
3
4
3
1
6
23

7
3
7
9

4 4 5 5 6
5 7 6 7 7
17 16 28 25 36

Near.out Hungry.out
7.8 (. . 7.8.1).

7.9.

. . , ,

( ) ;
. , ,
.

7.9.

301

(X, U, ) , u U
(u) > 0. x0 z (. 7.22).
. .
wij = (u) u = (xi , xj ). , x0 , ,
xj
x0 . .
xj x0 xj ,
x0 xj . xj
x0 xj . ,
, . , z
, . . x0 z.
r z
*

10

x1
r
>6
7

- r=
x2

6
3

x0 r

x4
- r
}
6

3
5

~r
x3

N
- r x5

. 7.22.

x0 0
( ), |X| 1 ().
.
.
1. xj , , (wij + xi ), xi ,
.
2. ,
.
.

302

7.9.

.1+.2 ,
z . ,
x0 .
, . 7.22.

. 7.23.
, . .
x0 . z x0 , .
x0

x1

x2

x3

x4

x5

10

15

13

15

12

11

3
4
5

. 7.23.


7.11, (X, U, ) W e = [wij ],
. M ark[x] x X (TRUE)
(FALSE) . dist[x]
. prev[x]
.
P rev[x] , x.
:
z, prev[z], prev[prev[z]], prev[prev[prev[z]]], . . . , x 0 ,
dist[z] x 0 z. , ,
y.

7.9.

303

. while |X| 1 , ,
, O(|X|).
O(|X|2 ).
7.11.

for x X do begin
M ark[x] = F ALSE;
dist[x] = ;
end;
y = x0 ;
M ark[x0 ] = T RU E;
dist[x0 ] = 0;
while not M ark[z] do begin
for x X do begin
if not M ark[x] and dist[x] > dist[y] + w[y, x] then begin
dist[x] = dist[y] + w[y, x];
prev[x] = y;
end;
end;
{ y X }

dist[y] =

min

xX and M ark[x]=F ALSE

dist[x];

M ark[y] = T RU E;
end.

, x0 , 7.11
while not MWark[z] do begin
while xX not M ark[x] do begin.
.
7.11 .
7.9.1. (X, U, )
(u) > 0 u U . , 7.11,
x 0 X
z X. x X dist[x]
x0 x.

. , ()
.

304

7.9.

. x 0 X
.
dist[x0 ] = 0 x 0 x0 .
.
. , (X, U, ) x X k, x
dist[x] x 0 .
. M =
{x0 , x1 , x2 , . . . , xk1 } X
, k .
dist[x i ] x0 xi M .
y X , (k + 1)- . , dist[y]
x y. W s (y). . W t (y) x0 y,
dist[y], . . W t (y) < Ws (y).
Wt (y)
.
1. Wt (y) (x2 , y),
x2 M (. 7.24 a). dist[x2 ] x0 x2 .
Ws (y) Ws (y) =
dist[x1 ] + w[x1 , y] = min {dist[x] + w[x, y]} 6 dist[x2 ] + w[x2 , y] =
xM

Wt (y), x1 , x2 M . W t (y) <


Ws (y), Wt (y)
Ws (y) = dist[y].
x1 r

a)

x0
r x2 r

ry

x1 r

b)

x0
r x2 r

ry
r
x3

. 7.24. a) b)

2. Wt (y) . , ,
Wt (y) (x2 , x3 ), (x3 , y), x2 M ,
x3
/ M , x3 6= y (. 7.24 b).

7.9.

305

dist[x2 ] x0 x2 .
, Wt (y) x0 y
x3
/ M , Wt (x3 ) < Wt (y), w[x3 , y] > 0.
, W s (y)
(k + 1)- y X x3 X, Wt (x3 ) = dist[x2 ] + w[x2 , x3 ] <
Wt (y) < Ws (y) = dist[x1 ] + w[x1 , y], x1 , x2 M . , W t (y)
(. 7.24 b) ,
Ws (y) .
7.12 Pascale,
7.11.
7.12.
Program Short; { }
uses CRT,DOS;
Const
nVertex=50; { }
Type
TypeMark=array[0..nVertex] of Boolean;
TypeDist=array[0..nVertex] of LongInt;
TypePrev=array[0..nVertex] of Integer;
TypeWeight=array[0..nVertex,0..nVertex] of Integer;
Var
f
:Text;
{ }
nX
:Integer;
{ }
Mark :TypeMark;
{ }
Dist :TypeDist;
{ }
Prev :TypePrev;
{ }
We
:TypeWeight; { }
x0
:Integer;
{ }
z
:Integer;
{ }
y
:Integer;
{ }
Var
i,j,x :Integer;
weight :LongInt;
begin
Assign(f,Short.in);
Reset(f);{ }
{ }
Read(f,x0); { }
Read(f,z); { }
Read(f,nX); { }
nX:=nX-1; (* X={0,1,2,...,nX} - *)
for i:=0 to nX do begin
for j:=0 to nX do begin
Read(f,We[i,j]); { }

306

7.9.

if We[i,j]=0 then We[i,j]:=$7fff; {+}


end;
end;
Close(f);
Assign(f,Short.out);
Rewrite(f); { }
for x:=0 to nX do begin
Mark[x]:=FALSE;
Dist[x]:=$7fffffff;
end;
y:=x0; { }
Mark[y]:=TRUE;
Dist[y]:=0;
while not Mark[z] do begin
{ }
for x:=0 to nX do
if not Mark[x] and ( Dist[x]>Dist[y]+We[y,x] ) then
begin
Dist[x]:=Dist[y]+We[y,x];
Prev[x]:=y;
end;
{ }
weight:=$7fffffff;
for x:=0 to nX do
if not Mark[x] then if weight>Dist[x] then begin
weight:=Dist[x];
y:=x;
end;
Mark[y]:=TRUE;
end;
Write(f, =);
x:=z;
while x<>x0 do begin Write(f,x:2); x:=Prev[x]; end;
WriteLn(f,x:2);
WriteLn(f, = ,Dist[z]);
Close(f);
end.

7.12 , . 7.22.
Short.in :
x0 ;
z ;
nX ;
nX
[wij ] .
0
6

7.10.

7
0
0
0
0
0
0
0

7
0
3
0
0
0
0

2
0
0
0
8
0
0

0
1
5
0
0
0
0

0
5
0
3
0
5
0

0
0
0
7
3
0
0

307

0
10
0
0
2
6
0

Short.out
:
= 6
= 11.

7.10.
.
(X, U, ) x0 z (. 7.25). x0 , . z
, .
u U c(u) > 0, c(u)
.
1
1

*


x1
r

1
0

x0 r
3
0

3
0
1
1

s
r
x2

rz

Ux+

j r
*
x

*
j

Ux

1
1

. 7.25.

.
(u) > 0,
u U :
X
X
u U (u) 6 c(u)
(u) =
(u),
(7.1)
uUx+

uUx

x , . . x 6= x 0 , x 6= z;
Ux+ , x;
Ux , x (. 7.25).

308

7.10.

. 7.25 ,
,
. (7.1) , , ,
( ).

X
X
(z) =
(u) (x0 ) =
(u),
(7.2)
uUz+

uUx0

(z) , z;
(x0 ) , x0 .
7.10.1. (z) = (x0 )
P P
(u)
[
. ,
xX uUx+
P
(u)] = 0, u U (u) -

uUx

+ .
i
Xh X
X
(u)
(u) =
xX

uUx+

xX\{x0 ,z}

uUx

h X

uUx+

(u)

uUx

i
(u) + [ (z) (x0 ) ] = 0,

(7.1).
. A X : x0
/ A, z A. UA+
, A.

.
P
C(A) =
c(u) . +
, A
uUA
.
P
(u) , A, . (A) =
+
, (A) 6 C(A), u U (u) 6
uUA
6 c(u).
R
- x0
/A


R
?


zA

. 7.26.

7.10.

309

7.10.2. (z) 6 C(A).


. . 7.26 , ,
A, z.
A. , (z) 6 (A), (A) 6 C(A), (z) 6 C(A).
7.10.1 ( ).
:
(7.3)

max (z) = min C(A).


. .
1. . ,
x0 z u U ,
(u) = c(u). .
1.1. . ,
: u U (u) = 0.
1.2. x0 z. ,
1.3. , 1.5.
1.3. , .
1.4.
1.2, x0 z.
1.5. .
2. . , ,
. 7.27. xi .
2.1. x0 0.
1
1

*


x1
r

3
1

1
1

1
1

x0 r

3
1
1
1

s
r
x2

3
1
1

rz

2 xr1

1
1

x0 r

1
1

3
1

3
1

s
r

3
1
1

+0 x2
. 7.27.

+1

rz

310

7.10.

2.2. xi ; y , x i . y
+i, xi y(+i), i,
xi y(i). : z ,
.
2.3. z . , x 0 z. , . 7.27 7.28.
, ,
x 0 z,
,
, . 7.28.
0

r
x0

3
1

+0

- r
x2

1
1

r
x1

3
1

+1

- r

r
x0

3
2

- r
x2

1
0

3
2

r
x1

- r

. 7.28.

, () () , ().
( 2.1).
z. , z , ,
, . . , z
.
1
1

*


x1
r

1
1

x0 r
0

3
2

s
r
+0 x2

3
2

s
1
0

3
1
1

rz

1
1
1
1
1
0
1
1

q xr1
-

. 7.29.

2.4. z . . 7.29. A
. ,

7.10.

311

, , . A = {x1 , z}. A ,
x0
/ A , z A . , A , (A ) = C(A ). , A , (A )
z (. 7.10.1), . . (z) = C(A ).
, , A
.
A (z) 6 C(A),
(z) 6 max (z) 6 min C(A) 6 C(A ).

(z) =
C(A ). , max (z) = min C(A).


A , .
(z) = 3 C(A ) = c(x0 , x1 ) +
c(x2 , x1 ) + c(x2 , z) = 1 + 1 + 1 = 3.
. 7.25
.
A1
A2
A3
A4

=
=
=
=

{z}, C(A1 ) = c(x1 , z) + c(x2 , z) = 3 + 1 = 4.


{z, x1 }, C(A2 ) = c(x0 , x1 ) + c(x2 , x1 ) + c(x2 , z) = 1 + 1 + 1 = 3.
{z, x2 }, C(A3 ) = c(x0 , x2 ) + c(x1 , x2 ) + c(x1 , z) = 3 + 1 + 3 = 7.
{z, x1 , x2 }, C(A4 ) = c(x0 , x1 ) + c(x0 , x2 ) = 1 + 3 = 4.

. ,
. . n = |X|2, n
x0 z. Cnk , k xi z.
Cn0 + Cn1 + . . . + Cnn = 2n ,
. , O(2 |X| ).
. , ,
, . . O(|X|).
. . |U |, ,
O(|X| |U |)
. .

312

7.11. ,

7.11. ,

.
(X, U, ) .
D X , D
,
D. , ,
, , , , D
,
D .
. ,
. () , . ()
. , ,
. 5 ; .
() = 5,
;
, , ,
.
1
1

a)

2r

r3

r6

t
r

2r

r3

r6

t
r

b)

c)

2r

r3

r6

d)

. 7.30

, 33 (. 7.30(a)). .
7.30(b) . 1 9
, 1 9. 1 8 , 1
8 . . 1 8 (. 7.30(c)) D = {1, 8},

. 5 (. 7.30(d))

7.11. ,

313

D = {5} . ,
() = 1.
.
(X, U, ) .
I X ,
. . Q
, .
, Q, .
.
, . ,

. () , , () .
.

, , . .
u ().
,
.
, .
() = 8.
7.11.1. , , ,
() > () ()
.
. () I X , x I, I ,
{x} I , I . I .
() I .
k I I , I X
, , I .

314

7.11. ,

. , .
.
2

r5
r

r5

. 7.31.

, , .
. 7.31 .
. , (X, U, ) , ()
, .
5

r
{}

{2}

{1}

{3}

{12}

{21}

{23}

{31}

{32}

{123} {123}

{213}

{231}

{312}

{321}

{13}

{5}

{4}

{34}

{45}

{43}

{54}

. 7.32. T


, . ,
. () .

7.11. ,

315

S = . S
.
S {x}, x
/ S x
S. S S{x} , x. . 7.32
T ,
.
, :
{1, 2, 3} 3! , {3, 4}, {4, 5} 2!.
k k! .
. 7.32 ,
.
( ),

.
7.11.2. (X, U, ) ,
S T (S X
). S , , S {x},
. 7.33, x S.
S {x} T , S {x}. S {v i },
vi
/ Adj[x](. 7.33).
S

S {x}
t

S {v1 }

S {vt }

. 7.33. S {vi } ( S)

7.11.3. S T ,
Se S S T . Se {x}
, ,
Se {x}, S {x}
.

316

7.11. ,

7.13

. CLIQU E N D.
S N D , S, . . N D
, S. N
, S
, . . N = {x X | (x, s) U s S Se S
Se {x} }.
7.13.

S = ; { T }
N = X;
D = ;
Z = X;
CLIQU E(N, D, Z); { T }
Procedure CLIQU E(N, D, Z);
if N D = then S,
else if N 6= then begin
x N ; { }
V iew(x); { }
{ }

Z = Z \ {x};
Z = Z \ Adj[x];
while Z 6= do begin
v Z;
V iew(v); { }
Z = Z \ {v};
end;
end;
end;
Procedure V iew(v); { }
N = N \ {v};
S = S {v};
CLIQU E(N Adj[v], D Adj[v], N Adj[v]);
S = S \ {v};
D = D {v};
end.

CLIQU E x X,
N S = S {x},
V iew. , 7.11.2 7.11.3,
S = S

7.11. ,

317

{v}, v N, v
/ Adj[x], v Z.
Z ,

, .
D CLIQU E
, S, , S
. D = {x X | (x, s) U s S
Se {x} Se S}.
S X
(X, U, ) N D ,
S.
S ,
N D = .
N = D 6= , , S, .
N 6= , S, .
S {x}, x N .
7.13, ,
N D, , , .
7.14 Pascale,
7.13. , N, D, Z
kN, nN, kD, nD, kZ, nZ,
kN, kD, kZ , , N, D, Z, nN, nD, nZ
.
7.14.
Program PgmClique; { }
uses CRT,DOS;
Const
nVertex=100; { }
nAdjacent=1000; { }
Type
TypeVertex=array[1..nVertex] of Integer;
TypeAdjacent=array[1..nAdjacent] of Integer;
Var
f :Text;
{ }

318

7.11. ,

m :Integer;
{ }
Adj:TypeAdjacent;{ }
Fst:TypeVertex; { }
Nbr:TypeVertex; { }
Vtx:TypeVertex; { }
S :TypeVertex; { }
nS :Integer;
{ S }
N :TypeVertex; { }
{kN, nN - N}
D :TypeVertex; { }
{kD, nD - D}
Z :TypeVertex; { }
{kZ, nZ - Z}
Procedure PrintClique; FORWARD;
Procedure Subtract( x:Integer; Var kZ,nZ:Integer ); FORWARD;
Procedure InterSection( v,kM,nM:Integer;
Var kMw,nMw:Integer; Var M:TypeVertex ); FORWARD;
Procedure View( v:Integer; Var kN,nN, kD,nD,
kZ,nZ:Integer ); FORWARD;
Procedure Clique( kN,nN,kD,nD,kZ,nZ:Integer ); FORWARD;
Procedure Init( Var yes :Boolean );
{ }
{ }
{yes - }
Var i,j,k :Integer;
begin
for i:=1 to m do
for j:=1 to Nbr[i] do begin
yes:=FALSE;
for k:=1 to m do
if Adj[Fst[i]+j]=Vtx[k] then begin
yes:=TRUE;
Adj[Fst[i]+j]:=k;
break;
end;
if not yes then exit;
end;
end;
Procedure PrintClique;
{ }
Var i :Integer;
begin
for i:=1 to nS do Write(f,Vtx[S[i]]:3); WriteLn(f);
end;
Procedure InterSection( v,kM,nM:Integer;
Var kMw,nMw:Integer;Var M:TypeVertex );
{ M*Adj[v] M Adj[v] }
Var i,j,k :Integer;
yes :Boolean;
begin

7.11. ,

319

{kMw - M*Adj[v]}
{nMw - M*Adj[v]}
kMw:=kM+nM; nMw:=0;
for i:=1 to nM do
for j:=1 to Nbr[v] do
if M[kM+i]=Adj[Fst[v]+j] then begin
nMw:=nMw+1;
M[kMw+nMw]:=M[kM+i];
break;
end;
end;
Procedure Subtract( x:Integer; Var kZ,nZ:Integer );
(* Z=Z\{x} Z=Z\Adj[x] *)
Var i,j,k :Integer;
yes :Boolean;
begin
(* Z=Z\{x} *)
for i:=1 to nZ do
if Z[kZ+i]=x then begin
nZ:=nZ-1;
for k:=i to nZ do Z[kZ+k]:=Z[kZ+k+1];
break;
end;
(* Z=Z\Adj[x] *)
for j:=1 to Nbr[x] do
for i:=1 to nZ do
if Z[kZ+i]=Adj[Fst[x]+j] then begin
nZ:=nZ-1;
for k:=i to nZ do Z[kZ+k]:=Z[kZ+k+1];
break;
end;
end;
Procedure Clique( kN,nN,kD,nD,kZ,nZ:Integer );
{ }
Var i,j,x :Integer;
begin
if (nN=0) and (nD=0) then { }
PrintClique { }
else if nN<>0 then begin
{ }
x:=N[kN+nN]; { N}
{ }
View(x,kN,nN,kD,nD,kZ,nZ);
Subtract(x,kZ,nZ); (* Z=Z\{x} Z=Z\Adj[x] *)
{ }
while nZ<>0 do begin
x:=Z[kZ+nZ];
View(x,kN,nN,kD,nD,kZ,nZ);
nZ:=nZ-1; (* Z=Z\{x} *)
end;
end;
end;

320

7.11. ,

Procedure View( v:Integer; Var kN,nN,kD,nD,kZ,nZ:Integer );


{ }
Var i,k :Integer;
kNw,nNw :Integer;
kDw,nDw :Integer;
kZw,nZw :Integer;
begin
(* N=N\{v} *)
for i:=1 to nN do
if N[kN+i]=v then begin
nN:=nN-1;
for k:=i to nN do N[kN+k]:=N[kN+k+1];
break;
end;
(* S=S+{v} *)
nS:=nS+1;
S[nS]:=v;
(* *)
InterSection(v,kN,nN,kNw,nNw,N); {N Adj[v]}
InterSection(v,kD,nD,kDw,nDw,D); {D Adj[v]}
(* Z=N *)
kZw:=kZ+nZ;
nZw:=nNw;
for i:=1 to nNw do Z[kZw+i]:=N[kNw+i];
(* *)
Clique(kNw,nNw,kDw,nDw,kZw,nZw);
(* S=S\{v} *)
nS:=nS-1;
(* D=D+{v} *)
nD:=nD+1;
D[kD+nD]:=v;
end;
Var kN,nN, kD,nD, kZ,nZ :Integer;
i,j :Integer;
yes :Boolean;
begin
Assign(f,Clique.in);
Reset(f); { }
{ }
Read(f,m); { }
Fst[1]:=0; { }
for i:=1 to m do begin
Read(f,Vtx[i]); { }
Read(f,Nbr[i]); { }
{ }
for j:=1 to Nbr[i] do Read(f,Adj[Fst[i]+j]);
{ }
Fst[i+1]:=Fst[i]+Nbr[i];
end;
Close(f);
Assign(f,Clique.out);
Rewrite(f); { }

7.11. ,

321

Init(yes);
if not yes then begin
WriteLn(f, !);
Close(f);
exit;
end;
{ : S, D, N, Z}
nS:=0; {S - }
kD:=0; nD:=0; {D - }
kN:=0; nN:=m; {N - }
for i:=1 to m do N[i]:=i;
kZ:=0; nZ:=m; {Z - }
for i:=1 to m do Z[i]:=i;
Clique(kN,nN,kD,nD,kZ,nZ); { }
Close(f);
end.

, ,
.
. 1r
2r
.

.
4
5
r
r
r6
, - 3 r
7
, r
,
. . 7.34.

. .
. , . ,
,
.
. 7.34.
7.14 Adj[x] Clique.in. ()
,
7.2.
Clique.in . 7.34:
7
3 3
1 4

1 4 7
3 4 5 2

11 ..

322

7.12. ,

4
7
2
5
6

4
2
4
4
3

3
3
1
4
7

1
6
4
1
5

2 5
5 6
2 6
2


Clique.out :
6
6
4
4
3

5 2
7
5 2 1
3 1
7

Clique.out . 7.34. ,

.

7.12. ,
, .
,
. .
0 (X, U0 , ) (X, U, ). , U0 , . . U \ U0 ,
U 0 .

0 . |X|1 ,

|U | |X| + 1 .

, : .
F = {C1 , C2 , . . . , C|U ||X|+1 } , Ci Ci U .
(( (Ci1 Ci2 ) ) Cir ),
, Cp Cq = {u | u Cp Cq u
/ Cp Cq }.
, . 7.35
, . (x 1 , x2 , x5 , x3 )

7.12. ,

323

C1 C3 C4 (x1 , x2 , x4 , x3 ) C1 C2 . ,

.
;
. ,
,
. , ,
. 7.15,
F = {C1 , C2 , . . . , C|U ||X|+1 } (X, U, ).
x1
r

x1
r

r
x2
r

C1
x2 r

x4
r
r
x5

r x3

x2 r
x2 r

r
x4

r x3
r x3
C2

C4

x4
r
r

C3
r

r
x3

r
x5

. 7.35. ,

7.16.
7.15.

11*

for v X do M ark[v] = 0; { }
count = 0;
jC = 0; { }
nC = 0; { }
for v X do if M ark[v] = 0 then begin
nC = nC + 1;
C[nC] = v; { }
Cycle(v, 0);
nC = nC 1;
end;
Procedure Cycle(x, y);
count = count + 1;
M ark[x] = count; { }
for v Adj[x] do begin

324

7.12. ,

nC = nC + 1;
C[nC] = v; { }
if M ark[v] = 0 then Cycle(v, x)
else if M ark[v] < M ark[x] v 6= y then begin
jC = jC + 1; { (x, v), }
W riteCycle(v, C, nC); { }
end;
nC = nC 1; { }
end;
end;
Procedure W riteCycle(x, C, nC);
print jC; { }
repeat
print C[nC]; { }
nC = nC 1;
until C[nC] = x;
end.

7.16.
Program GraphCycle; { }
uses CRT,DOS;
Const
nVertex=100; { }
nAdjacent=1000; { }
Type
TypeVertex=array[1..nVertex] of Integer;
TypeAdjacent=array[1..nAdjacent] of Integer;
Var
f
:Text;
{ }
n
:Integer;
{ }
nC
:Integer;
{ }
Adj :TypeAdjacent; { }
Fst :TypeVertex;
{ }
Nbr :TypeVertex;
{ }
Vtx :TypeVertex;
{ }
Mark :TypeVertex;
{ }
C
:TypeVertex;
{ }
B
:TypeVertex;
{ }
jC
:Integer;
{ }
count:Integer;
{ }
Procedure Init( var yes :Boolean );
{ }
{ }
{ yes - }
Var i,j,m :Integer;
begin
for i:=1 to n do
for j:=1 to Nbr[i] do begin

7.12. ,

325

yes:=FALSE;
for m:=1 to n do
if Adj[Fst[i]+j]=Vtx[m] then begin
yes:=TRUE;
Adj[Fst[i]+j]:=m;
break;
end;
if not yes then exit;
end;
end;
Procedure PrintCycle(x:Integer;var C:TypeVertex;nC:Integer);
{ }
begin
Write(f,jC,));
repeat
Write(f,Vtx[C[nC]]:3);
nC:=nC-1;
until C[nC]=x;
Writeln(f);
end;
Procedure Cycle( x,y:Integer );
Var i,v :Integer;
begin
count:=count+1;
Mark[x]:=count;
for i:=1 to Nbr[x] do begin
v:=Adj[Fst[x]+i];
nC:=nC+1; C[nC]:=v;
if Mark[v]=0 then Cycle(v,x)
else if (Mark[v]<Mark[x]) and (v<>y) then begin
{ - }
jC:=jC+1;
PrintCycle(v,C,nC);
end;
nC:=nC-1;
end;
end;
Procedure DepthCycle; { }
Var v:Integer;
begin
jC:=0; { }
nC:=0; {C - c }
count:=0; { }
for v:=1 to n do Mark[v]:=0;
for v:=1 to n do if Mark[v]=0 then begin
nC:=nC+1; C[nC]:=v;
Cycle(v,0);
nC:=nC-1;
end;
end;
Var {Main}
i,j :Integer;

326

7.13.

yes :Boolean;
begin {Main}
Assign(f,Cycle.in);
Reset(f);{ }
{ }
Read(f,n); { }
Fst[1]:=0; { }
for i:=1 to n do begin
Read(f,Vtx[i]); { }
Read(f,Nbr[i]); { }
{ }
for j:=1 to Nbr[i] do Read(f,Adj[Fst[i]+j]);
{ }
Fst[i+1]:=Fst[i]+Nbr[i];
end;
Close(f);
Assign(f,Cycle.out);
Rewrite(f); { }
Init(yes);
if not yes then begin
WriteLn(f, !);
Close(f);
exit;
end;
DepthCycle;
Close(f);
end. {Main}

7.16
, . 7.35.
Cycle.in:
5
1
2
3
4
5

2
4
4
3
3

3
1
1
2
2

2
3
2
3
4

4 5
4 5
5
3


Cycle.out :
1)
2)
3)
4)

1
3
2
3

2
4
5
5

3
2
4
4 2.

, .
.

327

7.13.1.

7.13.
,
,
. , .
.
7.13.1.
. (X, U, )
. u = (a, b) U ,
. . (
) . , , , ,
.
. u = (x, y) U ( , ) ,
e u, x
,
e
y .

e = 1 2 ,

(7.4)
1 2 = 1 x, 2 y.
7.13.1. u = (x, y) U , .
. () ,
u . x
y , , 7.4 ,
: u .
() u = (x, y) , . . , x y u. u
.
x0 r

r
x1

r
x2

r
xn1

r xn

. 7.36.

. , x 0 xn
, C1 , C2 , . . . , Ck , x0 C1 , xn Ck

328

7.13.1.

.
, . 7.36.
7.13.2.
(. . 7.4)
(X, U, ). . 7.37
.

L11

L12

L1m1

L21

L22

L2m2

Ln1

Ln2

Lnmn

. 7.37. (Lij ) .
(Lk ) .
(Lij ) (Li )

. ,
x,
L(x), x. , x,
. , , ,
x,
.
. (L),
L, .
7.13.3.
(L).
1. (L) , . . C L,
C (L).
2. , L1 L2 ,
L 1 L2 .
L1 L2 .

329

7.13.2.

, , .
3. (L) . (x, y)
(L). , . ,
, , x y L ,
(x, y) L. x y L
, , (x, y) L, , x, y L .
, (x, y) .
4. (L) .
, (L),
,
.
7.13.2.
. (X, U, )
. u, v U , C1 , C2 , . . . , Ck , u C1 , v Ck
Ci , Ci+1 , , .
, . 7.38.
. , u U , (Lb ), , u. Lb ,
u.
.
r

. 7.38. u, v

7.13.1.
, ,
. (. 7.38).

330

7.13.2.

7.13.2. P (x, y) ,
x, y Lb ,
P (x, y) (L b ).
. ,
, P (x, y), (L b ).
, ,
P (x, y). x, y Lb , (Lb ) Q(x, y), x, y.
P (x, y Q(x, y)) .
7.13.1, ,
, P (x, y) (L b ),
.
7.13.4. 7.13.2 ,
(Lb ) . ,
, C , Lb , C (L b ).

(. 7.39, 7.41). .
7.13.5. Lb ,
, (L b )
. , L b L, L
Lb , (L)
(L) =

(Lbi ).

(7.5)

. (X, U, ) x X ( ), (. 7.41)
(X) = (X1 ) (X2 ), X = X1 X2 , X1 X2 = x.

(7.6)

7.13.6. (Lb )
(. . 7.13.4).

7.13.3.

331

7.5 (L) (L b ) , (L) (L b ) , .


7.39, .
S . 7.5
(Lij ) = (Lbijk )
k

(. 7.37): Lij (. 7.39) (L bijk ).


7.13.3.

r
r
r
. ,
;
r
.
,
. 7.39
(. 7.41)
, . ,
. , .
.
, , , ,
()
.
. 7.40
( ). .

(X, U, ). , . 7.41, ,
i , i = 1, 2, . . . , 6, x j , j = 1, 2, 3, 4.
, , s 2 ,
2 1 , x1 .
, 1

332

7.13.3.

, x1 . 1
,
x1 . .
re
c r

rd
r

h r

h r

f
g

ra

f r

c r

ra

ir

rl

ri

rf

a r

rg

b)

j r

b r

j r

h r

rd

a)

re

ri

i r

d)

c)

rl
e)

. 7.40. (); (b); (), ()


(d); : b, f, j, i

6 x4 r

1
x1
r

2 r x2 3

r x3 4

. 7.41.

, , 2 5 , 6
x4 , 6 , 2 , 5 6 . , 6 5
x4 6 .
5
5 . , ,
, , ( ).
,
, .

7.13.3.

333


7.17. M ark[x]
x X .

.
, w

r
.
y
x
r
r

M ark[x],
. 7.42
. . 7.42 , w
. x w.
,
y, w. ,
(y, w) , ,
, M ark[w]
(w, x). ,
(
),
. , .
V irtual[x] x X, M ark[y], y
, x
.
7.17.

count = 0;
Stack = ;
for v X do M ark[v] = 0 { }
for v X do if M ark[v] = 0 then Block(v, 0);
Procedure Block(x, w)
count = count + 1;
M ark[x] = count; { }
V irtual[x] = count; { }
for v Adj[x] do begin

334

7.14.

if M ark[v] = 0 then begin


Stack = Stack {(x, v)};
Block(v, x);
V irtual[x] = min(V irtual[x], V irtual[v]);
if V irtual[v] > M ark[x] begin
x ,
. ,
, (x, v).

end;
end
else if M ark[v] < M ark[x] and v 6= w then begin
(x, v)
Stack = Stack {(x, v)};
V irtual[x] = min(V irtual[x], M ark[v]);
end
end;
end.

7.14.
. (X, U, )
,
(. 7.11)
V1 , V2 , X = V1 V2 V1 V2 = .
(V1 V2 , U, ). .
7.43 .
.

r
r
r
r
r

r
r
r
r
r
V1

V2
. 7.43

7.14.1.
7.14.1 (). (X, U, ) ,
.
. () , .
.
() 1 , 2 , . . . , m .
k (Xk , Uk , ) a Xk . Xk Vk1
Vk2 , Vk1 , a ; Vk2 , a , a V k2 .
Vk1 Vk2 . ,

7.14.3.

335

, b c b, c V k1
b, c Vk2 , , (a b c
a) (a b
c a), .
1 , 2 , . . . , m . . 7.44

Vk1 Vk2
S
S
V1 = Vk1 V2 = Vk2 .
k1

k2

7.14.2.

V11

.
V21
(V1 V2 , U, )

Vm1
(. 7.11) U ,
.
. 7.44
, . 7.45,
s1 r
1 = {(s1 , 1), (s2 , 2)}, 2 =
s2 r
{(s3 , 1), (s2 , 2)} . .
. - s3 r
. 7.45
, .

V12
V22

Vm2

r1
r2

7.14.3.

(V1 V2 , U, )

.
r
r
A1 V1 A2 V2
r
r
r V2
(. 7.46 ). - V1 r
r
r

r
r
C, r
a2 r A2
U ,
r
A1 r a1
. , r
V2
. a1 A1
V1
u1 = (a1 , v2 )
. 7.46
, A1
V2 . C =
(u1 1 u2 2 . . . un1 n1 un ) , a2 A2 . C

336

7.14.3.


e,
, . ,

e , 1 , 2 , . . . , n1
u1 , u2 , . . . , un .
e
, ,
e . ,
e
.
. (V1 V2 , U, ), V1 =
{s1 , s2 , . . . , s6 } V2 = {1, 2, . . . , 6}. V 1 V2
: s1 {4}, s2 {1, 6}, s3 {4, 5, 6},
s4 {1, 3}, s5 {2}, s6 {1, 3}. . 7.47 . .
, si V1 j V2
si . = {(s1 , 4), (s2 , 1), (s3 , 5), (s4 , 3), (s5 , 2)}, || = 5.
s6 6 . u
, .

V1 V2 u
V2 V1 . , s6 {1, 3} , s6

1 3, {1, 3} {s2 , s4 } ,
1 3
s2 s4 .
s6 . ,
u

u
s6 , : s6 {1, 3} {s2 , s4 } {1, 3, 6}.
, r1
s1 r
6
r2
s2 r
V2 , s6 , r3
s3 r
r4
. 6
s4 r
r5
s5 r
s6 , , r
r6
s
6
.
V
V
1
2
6 .
. 7.47

u

u
: 6 s2 1 s6 .
, (s 2 , 1) (6, s2 ), (1, s6 ).
(s1 , 4),(s2 , 6),(s3 , 5),(s4 , 3),(s5 , 2),(s6 , 1) || = 6.
7.14.2 ( ).
(V1 V2 , U, ) .

7.14.3.

337

()
|()| = |V1 | max ( |A| | M A| ),
AV1

(7.7)

M A = { y V2 | x A V1 (x, y) U } V2 .
. () (. 7.48).

: V 2 V1
V2 V1 . , M
M
M: V1 V2 V1 V2
U . Z (. 7.48) , .
. M

(M ()) V1 . M
: M M (M ) V1 . . ,

M M (M ) . , ,
. ,
:
M M (M ) M ( M (M ) ) = A

A |A| = | M A|, A . M
M M A.
A = B. , B (7.7) , . .
() = |V1 | (|B| | M B|). | M B| = | M ( A)| =
| M M A| = | M A|, , |V1 | (|B| | M B|) =
|V1 | (|| + |A| | M A|) = |V1 | || = |()|. ,
A V1 (7.7) :
|()| 6 |V1 | (|A| | M A|). B,
,
(7.7) . A 1 = A \ ( A),
A = ( A) A1 . , A1 V1 \ ( V1 ) |A1 | 6 | M A1 |, V1 \ ( V1 ) . ,

338

7.14.4.

|A| = | A| + |A1 | 6 || + |A1 | 6 || + | M A1 | 6 || + | M A|


|A| | M A| 6 ||. : |()| = |V 1 ||| 6 |V1 |(|A|| M A|).

( r
r
r
r
V1 r
r V2
).
e
r
r
, r
r
r
r Z
r

r
r
V2
V1
Z. . 7.48.



(7.7). ,

e (7.7), ,
e .
7.14.4.

: S1 = {2, 3}, S2 = {1, 2, 4}, S3 =


{1, 2, 5}, S4 = {3, 4, 5}, S5 = {3, 4, 5}.
x1 , x2 , x3 , x4 , x5 , xi Si , i = 1, 2, 3, 4, 5.
x1 = 2, x2 = 1, x3 = 5, x4 = 3, x5 = 4.
T1 = {1, 2}, T2 = {1, 2}, T3 = {1, 2}, T4 = {3, 4, 5},
T5 = {3, 4, 5}, .
S = {1, 2, . . . , n}. M (S)
S1 , S2 , . . . , Sn S. , M (S) , i = 1, 2, . . . , n
xi Si .
7.14.5.

(V1
V2 , U, ), . M (S) = {S1 , S2 , . . . , Sn } S.
S1 , S2 , . . . , Sn V1 ,

V2 (. 7.49).

s1
s2
s3
s4
sn

r1
r2
r3
r4

r
r
r
r

r
V1

. 7.49

rn
V2

7.14.1. (V1 V2 , U, ), -

7.14.6.

339

M (S) = {S1 , S2 , . . . , Sn }, n ,
M (S) . ( , ).
7.14.3 (. ). M (S) = {S 1 , S2 , . . . , Sn }
,

{Si1 , Si2 , . . . , Sik } M (S)


S

k



Sij > k, . . j=1
k k.
. () .
Sij x ij Sij ,
S

k
Sij > k.
,
j=1
() (V1 V2 , U, ) , M (S) = {S 1 , S2 , . . . , Sn }. , , n.
(7.14.1) . (7.14.2) , |()| = |V1 | max ( |A| | M A| ), M A = {y
AV1

V2 | x A (x, y) U } V2 .
k
S
Sij . A = {Si1 , Si2 , . . . , Sik }, |A| = k M A =
j=1

| M A| > k. , A V1 |A| | M A| 6 0, ,
|()| = |V1 | max ( |A| | M A| ) > |V1 |. |()| 6 |V1 |,
AV1

, |()| = |V1 | = n.

7.14.6.
,
. .
. , n . , -

340

7.14.6.

. a ij i- j- .
, .
. n . i- j-
aij .
.
7.14.4 ( ). A = [aij ], i, j = 1, 2, . . . , n, .

max

n
X

(7.8)

aii

i=1

n
n
X
X
vj
ui +
min

(7.9)

ui + vj > aij , i, j = 1, n.

(7.10)

ui +vj >aij

i=1

j=1

ui vj ,

(7.9) ,
ui + vi = aii , i = 1, n.

. .
i 1 n i
{1, 2, . . . , n}. u i + vj > aij , i, j = 1, n,
, j = i ui + vi > aii .
(7.8) (7.9).
n
X
i=1

ui +

n
X
j=1

vj =

n
X
i=1

ui +

n
X
i=1

v i =

n
X
i=1

(ui + vi ) >

n
X

aii .

i=1

, (7.8)
(7.9). , ,
ui vj , (7.8) (7.9) .

7.14.6.

341

u i , vj ,
ui + vi = aii , i = 1, n, .
0. ui vj , ui + vj > aij , i, j = 1, n. vj = 0 ui = max aij
j

i- ; (7.10) u i + vj =
max aij > aij . . 7.50
j
, u i
vj .
v1

v2

v3

v4

u1

u2

u3

u4

. 7.50.

1. vj , j = 1, n ui ,
i = 1, n , u i + vj > aij
ui + vj = aij .
i = 1, n. .
7.51 .
v1

v2

v3

v4

u1

u2

u3

u4

6
6
0

. 7.51.

2. ui , i = 1, n
Si = { j | ui + vj = aij }, i = 1, n.

, S1 = {2}, S2 = {2}, S3 = {1}, S4 =


{1}. Si , i = 1, n : j1 S1 , j2 S2 , . . ., jn Sn , j1 , j2 , . . . , jn

342

7.14.6.

. = 1j1 2j2 .. .. .. n
jn ui + vji = ui + vi = aii , ,
.
3. , S1 , S2 , . . . , Sn
, . . (7.14.3), . . Si1 , Si2 , . . . , Sik , |Si1 Si2 . . . Sik | < k.
|S1 S2 S3 S4 | = 2 < 4.
ui vj ,
ui1 = ui1 1, ui2 = ui2 1, . . . , uik = uik 1
vj = vj + 1, j Si1 Si2 . . . Sik .
ui vj . . . 7.52 ui vj .
v1

v2

v3

v4

u1

4 3

u2

4 3

u3

6 5

u4

6 5

0
1

0
1

0
0

0
0

. 7.52. ui vj
v1

v2

u1

u2

u3

u4

0
1

0
1

v3

v4

4 3

6 5

6 5

0
0

0
0

4 3

. 7.53. ui vj

ui + vj > aij , i, j = 1, n. ,
:

7.14.6.

343

1). j Si1 Si2 . . . Sik , ui + vj = (ui 1) + (vj + 1) =


ui + vj > aij .
2). j
/ Si1 Si2 . . . Sik , , vj = vj ui + vj > aij
ui + vj > aij + 1. ui + vj = (ui 1) + vj > aij + 1 1 = aij .
|Si1 Si2 . . . Sik | = t, (7.9)
n
X
i=1

ui +

n
X
j=1

vj =

n
X
i=1

ui

k+

n
X
j=1

vj + t =

n
n
X
X
=
ui +
vj (k t).
i=1

j=1

k t. 2,

ui vj , ,
2 3 .
n
n
P
P
(7.9)
ui +
vj
i=1

j=1

k t , , ,
.
.
ui vj 1 . , u i vj
, (7.10):
ui + vj > aij , i, j = 1, n.
2,
|S1 S2 S3 S4 | = t = 2 k = 4.
2. . 7.52, 7.53.
S 1 = {2}, S2 = {2, 3, 4},
S3 = {1, 4}, S4 = {1, 3, 4}.
. , ,
4 (V1 V2 , U, ) , V1 = {S1 , S2 , S3 , S4 },
V2 = {1, 2, 3, 4}, S1 {2}, S2 {2, 3, 4},
S3 {1, 4}, S4 {1, 3, 4}. , , :
1 = {(S1 , 2), (S2 , 3), (S3 , 1), (S4 , 4)},
2 = {(S1 , 2), (S2 , 3), (S3 , 4), (S4 , 1)},
3 = {(S1 , 2), (S2 , 4), (S3 , 1), (S4 , 3)}.

344

7.15.

, :


1 = 21 32 13 44 , 2 = 21 32 43 14 , 3 = 21 42 13 34 .
a12 + a23 + a31 + a44 = 18.

7.15.
(X, U, ) . k,
X k (,
) C1 , C2 , . . . , Ck ,
X=

k
[

i=1

Ci , Ci Cj = i 6= j

Ci , . . (.
7.54). (7.11) k . k ,

. Ci . k = () (7.11)
.
.

(7.11)

C
r 2r
r
C1 r
r

r
r C3
r r
Ck

. 7.54

7.15.1. (X, U, ) |X| = n


() = n.
Ci , i = 1, 2, . . . , n, (7.11) , |Ci | = 1.
7.15.2. (X, U, ) , () = 2.
7.15.3.
2, .

7.15.

345

(). ,
, .
?
. (X, U, ), X ; (x, y) U x, y X
. C i , i =
1, 2, . . . , k, (7.11)
( ). ,
Ci .
() .
. r
r
r
r
r
. 7.55
r
r
(7.11) , r
r
r
r
r
() = 3.
(). . 7.55
.
, ( ) .
?
. (X, U, ), X ;
(x, y) U x, y X
( ) .
(X, U, ) () . (. 7.1).
. 7.56 .
Ci , i = 1, 2, . . . , k, (7.11) 4 r
4
(X, U, )
1
. ,
r1
2
3
2
3
Ci r
r
, . 7.56.
.

() .
. 7.56 () = 4.
( ). ,
,
,

346

7.15.

.

().
, , , (). ()

.
7.15.4. () k ,
() > k.
7.15.1. ()
() (X, U, ), |X| = n
n
(7.12)
()() > n () >
()
. (7.11) C i . () ,
k
P
|Ci | 6 (). , n =
|Ci | 6
k (), k = ().

i=1

7.15.2. (X, U, ),
() 6 + 1
(7.13)
.
n = |X| n . n = 1 n = 2.
n n , . . (n ) 6 + 1.
( + 1)- (7.11). ,
n+1 ( + 1)-.
n+1 x X .
n n ,
.
( + 1)-. x X n+1
. x X
+ 1 ,
, . ,
n+1 ( + 1)-,
.
(7.13) ().

7.15.

347

7.15.3 (). ,
; ( > 3),
() 6 .

(7.14)

7.15.4 ( ). , () = k. k : (i) I(G), G = , (ii)


I(G),
G = G \ I(G), (iii) , .
. () = k,
k- C i , i = 1, 2, . . . , k, (7.11) . Ci
. Ci
. (i).
C1 .
C 1 . (ii). C2 .
C2 . (iii). (ii)
Ci , i = 1, 2, . . . , k, k . k
.
.
. () .
, , .

, .
(X, U, ) . () ,

348

7.15.

( ).
.

7.18. Adj[x], Adj[x] ,
x X. 1 |X|,

.
C ;
|X|. C[i], i = 1, 2, . . . , |X| (. . 5.1.2). C[i]
. C[i]
: 0 1. C[i] = 1 ,
i- .
C[i] = 0, i- .
7.18.

for x X do M ark[x] = 0; { }
for x X do begin
{ x X}

for i = 1 to |X| do C[i] = 0; { }


for v Adj[x] do begin
if M ark[v] <> 0 then C[M ark[v]] = 1; { }
end;
{ }

for i = 1 to |X| do if C[i] = 0 then begin


M ark[x] = i; { }
break;
end;
end.
( C[i] ) x X M ark[x].
M ark[x] 0,
C[i].
7.18 C[i], i =
1, 2, . . . , |X|,
x X. C[i]

7.15.

349

x X
.
7.18 7.19. 7.18. Init
SetColor, SetColor
; Init .
.
1 |X|. ,
, .
Adj[x] (.
. 7.3).
7.19 , . 7.57.
2
r

1
r

r
4

r
3

5
r

6
r
r
7

. 7.57.

7.19
Adj[x] Color.in.
() ,

7.2.
Color.in . 7.57:
7
3
4
6
7
5
2
1

4
2
2
2
4
3
3

1
2
5
5
1
1
2

2
3
7
6
3
3
3

6
4
5

Color.out
:
3 4 6 7 5 2 1 - ;
1 2 1 2 3 3 2 - .

350

7.15.

. , . . 7.57
. ,
,
.
7.19.
Program PgmColor; { }
{$APPTYPE CONSOLE}
uses
SysUtils;
Const
nVertex=100; { }
nAdjacent=1000; { }
Type
TypeVertex=array[1..nVertex] of Integer;
TypeAdjacent=array[1..nAdjacent] of Integer;
Var
f
:Text;
{ }
n
:Integer;
{ }
Adj :TypeAdjacent;{ }
Fst :TypeVertex; { }
Nbr :TypeVertex; { }
Vtx :TypeVertex; { }
Mark:TypeVertex; { }
C
:TypeVertex; { - }
Procedure Init( Var yes :Boolean );
{ }
{ }
{ yes - }
Var i,j,m :Integer;
begin
for i:=1 to n do
for j:=1 to Nbr[i] do begin
yes:=FALSE;
for m:=1 to n do
if Adj[Fst[i]+j]=Vtx[m] then begin
yes:=TRUE;
Adj[Fst[i]+j]:=m;
break;
end;
if not yes then exit;
end;
end;
Procedure SetColor(); { }
Var i,j,v,x :Integer;
begin
for x:=1 to n do Mark[x]:=0; { }

7.16. ,

351

for x:=1 to n do begin


for i:=1 to n do C[i]:=0; { }
for j:=1 to Nbr[x] do begin { }
v:=Adj[Fst[x]+j];
if Mark[v]<>0 then C[Mark[v]]:=1; { }
end;
{ }
for i:=1 to n do if C[i]=0 then begin
Mark[x]:=i; { }
break;
end;
end;
end;
Var {Main}
i,j :Integer;
yes :Boolean;
begin
Assign(f,Color.in);
Reset(f);{ }
{ }
Read(f,n); { }
Fst[1]:=0; { }
for i:=1 to n do begin
Read(f,Vtx[i]); { }
Read(f,Nbr[i]); { }
for j:=1 to Nbr[i] do
Read(f,Adj[Fst[i]+j]); { }
Fst[i+1]:=Fst[i]+Nbr[i]; { }
end;
Close(f);
Assign(f,Color.out);
Rewrite(f); { }
Init(yes);
if not yes then begin
WriteLn(f, !);
Close(f);
exit;
end;
SetColor();
for i:=1 to n do Write(f,Vtx[i]:3);Writeln(f, - );
for i:=1 to n do Write(f,Mark[i]:3);Writeln(f, - );
Close(f);
end. {Main}

7.16. ,
I. (X, U, ) . d(x, y) x, y X. d() = max d(x, y) x,yX

352

7.16. ,

II. x X .
r(x) = max d(x, y)
yX

x.
III. r() = min r(x).

x1 r

IV. z X,
r(z) = r(), .

x2 r

xX

. . 7.58
, .
.
d() = max d(x, y) = 3,

x3
r

x4
r

x5
r

x6 r
. 7.58

x,yX

r(x1 ) = 3,
r(x3 ) = 3,
r(x5 ) = 3,

r(x2 ) = 2,
r(x4 ) = 2,
r(x6 ) = 2.

, r() = min r(x) = 2. :


xX
x2 , x 4 , x 6 .

Оценить