Академический Документы
Профессиональный Документы
Культура Документы
ndice
1. Coloreo y repaso de grafos
1.0.1.
1.1.
BFS-DFS
Coloreo
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.1.
Nmero Cromtico . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.2.
1.1.3.
Propiedades de Greedy . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.4.
Heursticas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
. . . . . . . . . . . . . . . . . . .
2. Flujos en networks
2.1.
2.2.
2.5.
2.6.
14
. . . . . . . . . . . . . . . . . . . . . . . . . .
14
Ejemplos de Greedy
2.1.2.
Complejidad de Greedy . . . . . . . . . . . . . . . . . . . . . . . . .
15
15
2.4.
. . . . . . . . . . . . . . . . . . . .
2.1.1.
2.2.1.
2.3.
12
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
16
20
21
23
2.4.1.
Complejidad de Edmonds-Karp
. . . . . . . . . . . . . . . . . . . .
24
2.4.2.
27
2.4.3.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
29
2.5.1.
30
2.5.2.
Ejemplo de Dinic . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
2.5.3.
33
2.5.4.
Complejidad de Dinic . . . . . . . . . . . . . . . . . . . . . . . . . .
35
36
2.6.1.
. . . . . . . . . . . . . . . . . . . . . . . .
36
2.6.2.
Complejidad de Wave . . . . . . . . . . . . . . . . . . . . . . . . . .
38
2.6.3.
Ejemplos de Wave
39
. . . . . . . . . . . . . . . . . . . . . . . . . . .
3. MST
3.1.
3.2.
3.3.
41
Kruskal
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42
3.1.1.
43
3.1.2.
43
3.1.3.
Complejidad de Kruskal
. . . . . . . . . . . . . . . . . . . . . . . .
44
3.1.4.
Kruskal Delete
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
3.1.5.
44
3.1.6.
Ejemplo de Kruskal . . . . . . . . . . . . . . . . . . . . . . . . . . .
45
PRIM
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2.1.
3.2.2.
Correctitud de Prim
3.2.3.
Ejemplo de Prim
46
. . . . . . . . . . . . . . . . . . . . . . . .
46
. . . . . . . . . . . . . . . . . . . . . . . . . .
46
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
. . . . . . . . . . . . . . . . . . . . . . .
48
3.3.1.
48
3.3.2.
. . . . . . . . . . . . . . . . . . . .
48
3.3.3.
. . . . . . . . . . . . . . . . . . . . . . . .
49
4. Matchings
51
4.1.
51
4.2.
Teorema de Hall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
4.3.
. . . . . . . . . . . . . . . . . . .
53
4.4.
53
4.4.1.
Algoritmo de Gross . . . . . . . . . . . . . . . . . . . . . . . . . .
54
4.4.2.
. . . . . . . . . . . . . . . . . . .
55
4.5.
. . . . . . . . . . . . . . . . . . . . . . . . .
60
4.6.
61
4.7.
Minimizar la Suma
62
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.7.1.
. . . . . . . .
62
4.7.2.
Algoritmo Hngaro . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
4.7.3.
63
4.7.4.
. . . . . . . . . . . . . . . . . .
65
4.7.5.
67
. . . . . . . . . . . . . . . . . . . .
68
5.1.
Cota de Hamming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
5.2.
Matriz generadora . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74
5.3.
Matriz de chequeo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
75
5.4.
Cota Singleton
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80
Cdigos Cclicos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80
5.5.1.
87
5.5.
6. P-NP
90
6.1.
La clase P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
90
6.2.
La Clase NP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
90
6.3.
Co-NP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
91
6.4.
SAT
91
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.5.
6.4.1.
Reduccin Polinomial . . . . . . . . . . . . . . . . . . . . . . . . . .
92
6.4.2.
k color P SAT
. . . . . . . . . . . . . . . . . . . . . . . . . . .
92
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
94
NP Completo
6.5.1.
3-SAT es NP completo . . . . . . . . . . . . . . . . . . . . . . . . .
94
6.5.2.
3-color es NP completo . . . . . . . . . . . . . . . . . . . . . . . . .
96
6.5.3.
Ejemplo 3-SAT
6.5.4.
2-SAT
6.5.5.
HORN-SAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
P 3-color
. . . . . . . . . . . . . . . . . . . . . . .
97
P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
102
7.1.
Hill Climbing
7.2.
Simulated Annealing
7.3.
Algoritmos Evolutivos
7.4.
7.5.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Seleccin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Ejemplo de un AG
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
7.5.1.
. . . . . . . . . . . . . . . . . . . . . . . . 104
7.5.2.
7.5.3.
7.5.4.
7.5.5.
7.5.6.
Operadores de Seleccin
7.5.7.
7.5.8.
7.5.9.
. . . . . . . . . . . . . . . . . . . . . 104
. . . . . . . . . . . . . . . . . . . . . . . . 105
. . . . . . . . . . . . . . 107
7.5.10. Crossover
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7.5.11. Mutacin
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
No Free Lunch
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
E {A V : |A| = 2}.
E es el conjuntode lados1 o aristas.
G = {1, 2, 3}, {1, 2}, {1, 3}
es un par
(V, E)
donde
Grco(G)
1<
<<
<<
<<
<
Notacin usual
{x, y} xy
Vecinos de un nodo
Grado
o Valencia de
x: (x) = {y V : xy E}
un vrtice: d(x) = (x)
Menor valencia de
G: = mn{d(x) : x V }
Mayor valencia de
G: = max{d(x) : x V }
Deniciones
Un grafo es
Un
regular si
= ,
x1 , x2 , . . . , xt
xi xi+1 E .
Un grafo es
conexo si
par
de vrtices
Conexo
1 ==
4
==
==
==
_3
En general denimos
Ejercicio: demostrar que
1. Reexividad:
Disconexo
_5
2
xy
4 ==
==
==
==
_3
camino entre
==
==
==
5
x
y.
xx
xyyx
xy
Transitividad:
xz
yz
2. Simetra:
3.
1 ==
1 Edges.
2 Degree.
Notacin
n = |V |.
La cantidad de vrtices:
La cantidad de lados:
m = |E|.
Observacin
n
m
=
2
Si
1.0.1.
n(n1)
2
es conexo
n1m
BFS-DFS
DFS(x)-Depth First Search3
A
z ???
zz
??
z
??
zz
z
?
zz
_
_
D
C
B
y @@ mmm
yy mmmmm@@@
y
y m
@@
yyymymmmm
@
m
m
E
DF S(A)
N ivel 0
N ivel 1
N ivel 2
D@
N ivel 3
@@
@@
@@
@
N ivel 4
N ivel 5
para cada uno de los vecinos se exploran sus respectivos vecinos adyacentes, y as hasta
que se recorra todo el Grafo.
Ejemplo BFS
BF S(A)
A?
??
zz
z
??
z
z
??
zz
?
z
z
_D
_
B
C
y @@ mmm
yy mmmmm@@@
y
y m
@@
yyymymmmm
@
mm
E
A
ss ???
s
s
??
ss
??
sss
s
?
s
ss
C
N ivel 0
N ivel 1
N ivel 2
N ivel 3
1.1. Coloreo
G = (V, E)
xy E C(x) 6= C(y)
Un coloreo (propio) de
tal que si:
con
C : V {1, 2, . . . , k}
Ejemplo de coloreo
A(1)
FF
x
FF
xx
FF
x
FF
xx
x
F
x
x
_ D(2)
_ C(3)
B(2)
FF kkk
x
xx kkkkkFkFF
x
FF
x kk
FF
xxxkxkkkkk
k
E(1)
G(1)
F (2)
1.1.1.
Nmero Cromtico
(G) = mn{k :
un coloreo propio de
Propiedad:
con
G : (G).
k colores }.
1 (G) n
1.1.2.
x1 , . . . , x n .
C(x1 ) = 1
j>1
Inicio:
Para
C(xj ) =
n
o
C(xj ) = mn k : C(xi ) 6= k i < j : xi (xj )
Invariante: en todo momento el coloreo es propio, por lo tanto al terminar da un
coloreo propio.
Ejemplo:
_D
_
@@
~B @@@ C
~
~
@@
~
~
@
~
~
@@
@@
~
~
~
~
@@
@
~
~~
@@@~~~
@
E
A@
~~~ @@@
@@
~~
@@
~~~
@@
~
@@
@@ ~~
~~
@ ~~~
@ ~~
_
Orden alfabtico
Orden alternativo
Vrtices
Color
O d(x1 ) + d(x2 ) + . . . + d(xn ) = O(2m) = O(m)
Teorema: sea
Prueba: Supongamos que
Sea
Sea
A
B
G = C2r+1 (G) = 3
(G) = 2
x1 x2 E A 6= B
(G) = 2 estos son
Como x2 x3 E C(x3 ) 6= B C(x3 ) = A
5
Como x3 x4 E C(x4 ) = B
Como estamos suponiendo
Por induccin:
C(xi ) =
Pero entonces el lado
x1 x2r+1
|{z}
| {z }
A
A
B
cuando
cuando
tiene los
i
i
es Impar
es Par
Absurdo.
5 Este paso es slo ilustrativo no forma parte de una demostracin formal ya que no sabemos si
n = 3.
7
n>3
(G) 3
Y est claro que con
A
B
C(xi ) =
cuando
cuando
cuando
i es Impar < 2r + 1
i es Par
i = 2r + 1
C2r+1 G (G) 3
un grafo G es bipartito si: (G) = 2
Corolario: si
Denicin:
Teorema:
es bipartito
@C2r+1 G
es bi-
partito o no.
(G) = 2,
C2r+1 G
(G) 3
En realidad esto lo haremos para cada componente conexa de
Vamos a suponer
Sea
x V,
G.
conexo.
0 Nivel BF S(x), z es Par
C(z) =
1 Nivel BF S(x), z es Impar
y corramos
propio:
vw E
v, w : vw E : C(v) = C(w)
v fue puesto antes que w en el rbol entonces
debe ser:
1.1.3.
nivel(v) = nivel(w)
pues
Propiedades de Greedy
Analizando a Greedy, podemos decir que en ciertos casos puede andar muy mal
Ejemplo:
Sea
G : V = {1, . . . , 2r}
y con
E = ij
i es Impar
j es P ar
r=4
j 6= i + 1
x1 , . . . , x2r = x8
Vrtice
Pero si denimos
1 i
2 i
C(xi ) =
va a necesitar
x1
x2
x3
x4
x5
x6
x7
x8
es impar
es par
r=4
colores:
(G) = 2
Conclusin:
Greedy(G)
(G) = 2.
Sin embargo . . .
Propiedad:
Prueba: sea
{x1 , . . . , xn } de los
obtiene: (G)
Sean
Sean
Vt
Induccin en
t=1
t = (G)
V1
V2
Sean
Si
un ordenamiento
colores.
t:
obvio.
t1
V1 <
los de
V2 < . . . <
Vt
de los primeros
color
Vt1
t1
colores o el color
los de
Vt , en Vi
lo coloreamos con
t1
colores.
t + 1.
Propiedad:
(G) + 1
xi
estn coloreados y con colores distintos, esto forzara a Greedy() a usar un color distinto
a esos
d(xi )
de esos colores.
Pero
d(xi )
Greedy con
+1
Ms an, si
que:
6= (G)
x V : d(x) =
6
Corramos BF S(x) y sea x1 , . . . , xn el orden inverso al de BFS(x) xn = x
7
En BF S(x) todo vrtice, salvo x es incorporado al rbol por medio de un vecino que
Prueba: sea
ya est en el rbol.
Hay a lo sumo
d(xi ) 1 1
xn = x
d(x) + 1
colores:
(Kn ) = n = (n 1) + 1 = + 1
1.1.4.
(G) = + 1
(C2r+1 ) = 3 = 2 + 1 = + 1
colores.
d(x) = < + 1
Ejemplos donde
1. Los ciclos impares:
posibles conictos.
Heursticas
1. Welsh-Powell
Greedy en el orden:
2. Matul-Marble-Isaacson
xn : d(xn ) =
Sea
Gn1 = Gn xn
Tomo
2
6 O lo mismo
7O
3
q5
>M>MMMM ;;qq;qqqqqqq
q
>> MMqMqqqq ;
> qqqq MM ;;
qqqq>q>> MMMM;;
q
q
q
q
M
qqq
4
3
qqq
qqqqqqqqq
q
qqqqq
qqqqqqqqq
qqq
y as seguimos . . .
3 > qqq5
q>q>qq>qqqqq
qqqq >>>
qqqqqqqqq
>
qq
qqqqq
4
pp5
ppppp
p
p
p
p
ppppp
ppppp
p
p
p
p
pp
ppppp
DF S(x).
DF S(x).
10
x6 = 1 x5 = 6 x4 = 4 x3 = 5 x2 = 3 x1 = 2
2, 3, 5, 4, 6, 1
(G) = 2
color = 1;
R = V;
//
Vrtices
while ( R 6= ) {
5
6
no Coloreados
L = R;
while
(L
6= ) {
Tomar v
Color ( v ) = c o l o r ;
de L
V tenga
10
Remover ( v )
de R ;
11
Remover ( v )
12
( v )
la
mayor
cantidad
de
vecinos
en R
de L ;
13
Color
14
++;
15 }
4. D-Satur
d5 (x) = #de
x.
Corre Greedy con un orden dinmico eligiendo como prximo vrtice el que tenga
d5
ms grande.
11
2. Flujos en networks
(V, E) : E V V con
C : E R.
0
En este contexto siempre pediremos C : E R
y los C(x, y) se llaman capacidades.
xy
xy ).
+ (x) = {y V : xy E}.
(x) = {y V : yx E}.
(x) = + (x) (x).
N = (V, E, C) y
f : E R tal que:
N,
de
es una funcin
y (x)
y+ (x)
3.
s, t V ,
0 f (xy) C(xy) xy E
P
P
Conservacin:
f (xy) =
f (yx) x 6= s, t
1. Feasability :
2.
vrtices distinguidos
Productor:
f (sy)
produce se llama la
fuente9 y
f (ys)
y (s)
y+ (s)
Como
se llamar
resumidero10 .
f (ys) = 0
y (s)
O ms an:
C(ys) = 0 y (s)
Notacin:
y+ (x)
y (x)
Denicin: el
8 Viabilidad.
9 Source.
10 Sink.
12
un
ujo
Propiedad:
Prueba:
Sea
0
x 6= s, t
V (f ) x = s
D x = t
P
xV
Notacin: Si
A, B V
entonces
f (A, B) =
f (xy)
xA
yB
xy E
X
Outf (x) =
X X
f (xy)
(1)
xV y+ (x)
xV
f (xy)
x, y V
xy E
= f (V, V )
( 1) Por denicin de
(2)
Outf (x).
( 2) Notacin.
Similarmente:
Inf (x) =
X X
f (yx)
xV y (x)
xV
f (yx)
x, y V
yx E
= f (V, V )
( 3) Por denicin de
Inf (x).
Nos queda:
f (V, V ) f (V, V ) = V (f ) D
{z
}
|
=0
V (f ) = D
13
(3)
hallar
que maximice
V (f ).
s = x0 , x1 , . . . , xr = t : xi xi+1 E
que se mandan unidades de ujo
Sea
Signica
, la notacin:
11
s, x1 , . . . , xr1 , t :
era
f (xy) =
G( xy)
Si no
2.1.1.
Ejemplos de Greedy
10,
excepto
?A
C@
@
@@
@@
@
s>
>>
>>
>>
>
C(EF ) = 15.
E
~>
~
~
~~
~~
> G >>
>>
~~
~
>>
~
~
>>
~~
?t
@@
@@
@@
@
da: f (EF ) = 15
1000
excepto:
C(CD) = 1
/B
A
G >>
? 00
>>
00
>>
0
>>
00
0
s?
?t
??
000
??
00
??
0
?
/
D
C
Corremos Greedy con el orden alfabtico
exista.
14
C(CB) = 900
2.1.2.
Complejidad de Greedy
Greedy
O(m)
O(n)
f (xy) = C(xy),
no se vuelve a
camino aumentante12 de
O bien:
xi xi+1
O bien:
Backward.
Hasta que no se pueda ms buscar caminos aumentantes y mandar ujo por ellos.
12 Augmenting path.
15
2.2.1.
Si
es ujo. Ms an si se manda
V (f ) + .
Prueba:
s = x0 , x1 , . . . , xr = t un camino
(
f (xi+1 xi )
Sea
Elijo
aumentante.
para los lados forward
para los lados backward
= mn{i }
f es tomar
Aumentar
f (xy) =
f (xi+1 xi ) Si xy = xi+1 xi backward
f (xy)
en caso contrario
Queremos ver que
es ujo y que
V (f ) = V (f ) +
x 6= s, t x = xi : 0 < i < r.
i , .
+
casos:
+
xi1 xi xi+1
Outf (xi ) = Outf (xi ) +
Inf (xi ) = Inf (xi ) +
//
//
xi xi+1
xi1 xi
+
xi1 xi xi+1
Outf (xi ) = Outf (xi )
Inf (xi ) = Inf (xi ) +
Luego:
3. B-F:
+
xi1 xi xi+1
Inf (xi ) = Inf (xi )
Outf (xi ) = Outf (xi ) +
Luego:
16
porque
es ujo.
4. B-B:
xi1 xi xi+1
Inf (xi ) = Inf (xi )
Outf (xi ) = Outf (xi )
Luego:
s:
En el caso de
1.
+
s x1
Outf (s) = Outf (s) +
Inf (s) = Inf (s)
V (f ) = V (f ) +
2.
s x1
Outf (s) = Outf (s)
Inf (s) = Inf (s)
V (f ) = V (f ) ()
= V (f ) +
Denicin: dado
N = (V, E, C)
Un
con vrtices
s=
corte es un subconjunto
fuente,
t=
S V : s S, t 6 S
Ejemplos de cortes:
S = {s}.
S = V {t}.
Cuntos cortes hay?
2n2
Cap(S) = C(S, S) : S = V S
X
=
C(xy)
xS
yS
xy E
17
resumidero.
Lema: sea
1.
V (f ) = f (S, S) f (S, S)
2.
V (f ) Cap(S)
3. Si
V (f ) = Cap(S)
ujo y
corte entonces:
es Maximal, y
network,
entonces
menor
capacidad.
Prueba:
1. Observemos que:
f (A B, C) = f (A, C) + f (B, C) : A, B
disjuntos.
disjuntos.
Adems:
Outf (x) = f {x}, V
Inf (x) = f V, {x}
Adems si
x S:
Outf (x) Inf (x) =
V (f ) Si x = s
0
Si x 6= s
pues
t 6 S
X
Outf (x) Inf (x) = 0 + . . . + 0 + V (f )
xS
= V (f )
X
X
V (f ) =
Outf (x)
Inf (x)
xS
xS
X
X
=
f {x}, V
f V, {x}
xS
(4)
xS
= f (S, V ) f (V, S)
= f (S, S S) f (S S, S)
(5)
S)
S)
=
f (S,
+ f (S, S) f (S, S)
f (S,
(6)
= f (S, S) f (S, S)
( 4) Por la observacin.
( 5)
V = S S.
( 6) Disjuntos.
18
2.
V (f ) = f (S, S) f (S, S)
| {z }
0
| {z }
<0
un ujo cualquiera:
Hip(3)
V (g) Cap(S) = V (f ) f
Similarmente, si
es maximal
Cap(T ) V (g) g
En particular elijo como ujo a
y me queda:
Hip
Cap(T ) V (f ) = Cap(S) S
19
es minimal
es maximal
Demostracin: dado
sS
maximal
13
: V (f ) = Cap(S).
construyamos S recursivamente
corte
Si
Si
entonces agregamos y a
entonces agregamos y a
S.
S.
aumentante de
as:
por denicin.
Es decir
Si
t.
S
estuviera en
t.
tal que:
es
corte.
Slo resta ver que:
Si
V (f ) = f (S, S) f (S, S)
x S f (xy) = C(xy)
y 6 S
xy E
Si
V (f ) = Cap(S)
Def
xS
y 6 S
f (yx) = 0 f S, S = 0
yx E
V (f ) = f (S, S) f (S, S)
= Cap(S) 0
= Cap(S)
Corolario
14
: Ford-Fulkerson es
f,
entonces
es
maximal.
Prueba: el algoritmo aumenta a partir del ujo 0 usando caminos aumentantes hasta
que no encuentra ms caminos.
Sea
20
< F F >15
f entre s y t.
Cuando
para
intent aumentar
no pudo, entonces
S.
V (f ) = Cap(S)
camino aumentante
16
, tenemos que
entonces
es maximal por
2.3.1.
< F F >
<F F >
no termina
/ y1 j
??
??
??
??
/ y2
/2 t
?
x1
~?
~
~~
~~
~
~
/ x2
s@
@@
@@
@@
@
/ y3
x3
Capacidades: todas
2
salvo:
st : 1000 x1 y1 : 1 x2 y2
1+ 5
2
Donde: r =
es raz de: P (r) = r + r 1
2
Observacin:
1 r = r2
r r2 = r3
Satisface:
Corramos
<F F >
1.
s, x1 , y1 , t : 1
2.
s, x3 , y3 , y1 , x1 , x2 , y2 , t : r2
3.
s, x2 , y2 , y3 , x3 , x1 , y1 , t : r3
4.
s, x1 , y1 , y2 , x2 , x3 , y3 , t : r4
Luego
<F F >
repite
con
r5 r6 r7
21
: r x3 y 3 : r 2
...
< F F >
no termina, ms an si
fi
lm V (fi ) = 1 + r + r2 + r3 + . . .
ri r
i=0
1
r
1r
1 r(1 r)
=
=2
1r
=
1002,
mandando
st = 1000.
Observacin: si las capacidades son enteras entonces como el primer valor del ujo es
0 Z,
y aumenta por:
= mn{i } : i =
Entonces
C f
Z
f
f1 , f2 , . . . V (fi+1 ) V (fi ) + 1.
Como hay cota superior, como por ejemplo: Cap({s}), en algn momento debemos parar.
Es decir si las capacidades son enteras < F F > siempre termina, inclusive cuando
termina sabemos que el ujo que devuelve es entero, sumado al hecho de que < F F >
Si les llamamos
Teorema de la
integralidad: si las capacidades son enteras entonces existe un ujo maximal entero.
Ejemplo
? A >>
>>
>>
>>
s?
?t
??
??
??
?
AB : 1, las
< F F >:
Capacidades:
Corremos
1.
s, A, B, t : 1
2.
s, B, A, t : 1
dems:
106
(1 2) 2 106
momento.
Anexo: recordemos la notacin
entre
(u, v)
v.
22
Lema: sea
T = BF S(x)
para algn
x V,
entonces:
induccin en
(z, x).
Caso base:
(z, x) = k + 1
de la forma:
x = x0 , x1 , . . . , xk , xk+1 = z
x = x0 , x1 , . . . , xk
N ivelT (xk ) = k
Entonces
H.Ind
xk (x, xk ) = k
xk z E
T =BF S(x)
N ivelT (z) = j
|j k| 1
N ivelT (xk ) = k
Ya que como son vecinos la diferencia de nivel es
1.
|j k| 1
P ero
Absurdo.
j >k+1
< E K >.
Consiste en correr
<F F >
23
2.4.1.
Complejidad de Edmonds-Karp
17
Teorema: la complejidad
de
<EK >
O(nm2 ).
es
Prueba:
Supongamos que
El paso
<EK >
fk
O = f0 , f1 , . . .
Deno:
dk (x) =
distancia entre
bk (x) =
distancia entre
en el paso
en el paso
k.
k.
Sea
A 6=
A = ,
ya que para
es similar.
n
o
A = z V : dk+1 (z) < dk (z)
supongamos que
tomamos
qA
tal que
no y lleguemos a un absurdo.
Luego como
17 Recordando que:
n = |V |
q,
24
q.
(3)
por (1)
no puede estar en A)
m = |E|.
(4)
(u, v)
Est disponible
Supongamos que
si:
Es Forward con
Es Backward
(x, q)
en el paso
k?
s entonces:
dk (q) dk (x) + 1
(4)
dk+1 (x) + 1
(3)
= dk+1 (q)
Contradice (2)
Por lo tanto
xq
Absurdo!
k,
pero
k,
s lo est en el paso
i.e.
(q, x)
k + 1.
k.
se us en el paso
(3)
dk (x) + 1
(5)
= dk (q) + 1 + 1
= dk (q) + 2
(2)
xz
dk (z) = dk (x) + 1 ?
Pues estoy en
<EK >
entonces
d (x) = d (z) + 1
L
L
dL (t) = dL (x) + bL (x)
= dL (z) + 1 + bL (x)
dk (z) + 1 + bk (x)
= dk (x) + 1 + 1 + bk (x)
= dk (t) + 2
25
(7)
(8)
(9)
dL (t) dk (t) + 2
( 7) Por
Por lo tanto cuando un lado se vuelve crtico recin puede volver a usarse cuando la
distancia a
2.
n1
2
Complejidad de cada bsqueda
= O(n)
Exactamente sera:
BF S +
Aumento
O(n)
veces.
Corolario de la complejidad de
26
<EK >
2.4.2.
Capacidades
Caminos
sA:20
s, A, B, t : 10
Primer Camino:
s
sC:20
20
20
10
10
10
10
s, A, I, t : 3
Segundo Camino:
A
10
20
10
10
10
10
10
10
10
10
10
AE:3
AF:10
s, C, D, t : 10
Tercer Camino:
s
20
AB:20
s
AI:3
Cuarto Camino:
s, A, B, I, t : 7
10
10
Bt:10
s
BI:10
BJ:10
CD:10
Quinto Camino:
s
CK:10
10
10
10
s, C, K, I, B, D, t : 2
B
Dt:22
EG:10
s, C, K, I, A, E, G, D, t : 3
Sexto Camino:
s
EH:10
FG:10
GD:3
Sptimo Camino:
s
HD:10
s, C, K, I, B , A, F, G, E, H, D, t : 3
It:10
Jt:2
KI:10
27
Cap(S) =10 + 10 + 10 + 2 + 3 + 3 = 38
cd + it + bt + dt + ae + gd
=0
z }| {
V (f ) = Outf (s) Inf (s)
= f ( sa) + f ( sc )
= 20 + 18 = 38
2.4.3.
1 EK( Network N)
2 {
// Flujo
// Valor del Flujo
S = V ; // Corte
while ( t S ) { // mientras el vertice t este en el corte
// buscar camino y aumentar
Q = new_queue ( S ) ; //Crear cola
E [ x ] = x V ;
S = { s } ; // corte
F = 0;
v = 0;
5
6
7
8
9
10
11
while ( h e a d ( q ) 6=
12
t ){
x = head ( q ) ;
13
Forward_search ( x ) ;
14
Backward_search ( x ) ;
15
dequeue ( q ) ;
16
if
17
18
21
S){
Aumentar_flujo ( ) ;
19
20
(t
//saco x de la cola
}
}
return
(F , v , S )
22 }
23
24 F o r w a r d _ s e a r c h ( v e r t i c e
25 {
26
27
x)
i f ( f ( xq ) < C( xq ) ) {
//Agregar q a Q
//Ancestro
28
e n q u e u e (Q, q ) ;
29
S = S
30
A[ q ]
= x;
31
E[ q ]
32
b[q]
= 1;
33
34
{q } ;
xq )
// forward
F(
xq ) } ;
35 }
36
37
38
39
28
40
41 B a c k w a r d _ s e a r c h ( v e r t i c e
42 {
43
44
x)
i f ( f ( qx ) >0) {
//Agregar q a Q
//Ancestro
//Backward
45
e n q u e u e (Q, q ) ;
46
S = S
47
A[ q ]
= x;
48
b[q]
E[ q ]
= min {E( x ) , F (
49
50
{q } ;
1;
qx ) } ;
51
52 }
53
54 Aumentar_Flujo ( )
55 {
// pivote
56
p = t;
57
58
v = v +
= E( t ) ;
while (
59
60
;
6=
s ){
q = a[p];
if
61
62
( b [ p ] == 1 ) {
f(
qp )
63
} else {
64
f(
65
pq )
= f(
= f(
qp )
//FORWARD
+
;
pq ) ;
66
p = q;
67
68 }
q + (p) con f (pq) < C(pq) q (p) : f (qp) > 0, pero que q ya est en el network
auxiliar, entonces agregamos un lado pq al network auxiliar si el N ivel(q) = N ivel(p) + 1.
Denicin: un ujo G se dice bloqueante o saturante si para todo camino:
s = x0 , x1 , . . . , xr = t
Dado
ujo en
con
(f g)(x, y) =
f (x, y) + g(x, y)
f (x, y) g(y, x)
29
Forward
Backward
2.5.1.
1 Dinic ()
2 {
3
f = 0;
S t o p = NO;
while ( S t o p
5
6
== NO) {
NA = New_Na( f ) ;
if
7
8
(t
g =
f = f
NA) {
} else {
10
11
g;
S t o p = S ;
12
13
return
14
f ;
15 }
Teorema
La distancia en NA
18
Demostracin:
0
Sea A un NA y A el siguiente NA, denotamos con
0
en A .
0
0
Como A y A se construyen con BFS
la distancia en A y
la distancia
s = x0 , x1 , . . . , xr = t
Caso 1:
un camino en
A0 .
i {1, . . . , r 1} : xi 6 A
(t) (xi )
0 (xi )
= i < r = 0 (t)
(10)
(11)
<EK >
(t) < 0 (t)
Caso 2:
xi A i
A0
s, x1 , . . . , xr1 , t
no
i.
30
Caso 2-a:
Como
(xi+1 ) < i + 1
Luego:
(12)
(13)
?
<EK >
(xi+1 ) = i + 1
Observacin: es por < E K > no hace falta ver >.
Caso 2-b:
s = x0 , x1 , . . . , xi A
(xi ) = i (xi+1 ) = i + 1 = (xi ) + 1
Como los niveles son los correctos la nica razn por la cual
porque no est disponible en
Pero
pues
N,
xi xi+1 6 A
O(b)
xi+1 xi A Absurdo
O(n b).
Dinic = Algoritmo tipo Dinic + Greedy DFS para hallar ujo bloqueante.
2
A simple vista sera: Complejidad(Dinic) = O(nm ) = Complejidad(< E
Parecera que no ganamos nada.
Tendremos
es
Complejidad(Dinic) = O(n2 m)
31
= O(nm).
K >)
2.5.2.
Ejemplo de Dinic
Inicial
sA:8
sC:7
sD:10
sE:12
AF:4
AG:3
AI:8
BI:2
BJ:2
CG:2
CH:3
CJ:5
DF:4
DG:2
EF:3
EG:5
EH:4
Ft:7
Gt:5
GB:4
Ht:4
It:9
Jt:15
Primera Modicacin
sA:0
sC:0
sD:7
sE:11
AF:0
AG:0
AI:7
BI:2
BJ:2
CG:0
CH:0
CJ:3
DF:1
DG:2
EF:3
EG:5
EH:3
Ft:0
Gt:0
GB:4
Ht:0
It:8
Jt:13
caminos (4)
sAFt:4
sAGt:3
sAIt:1
sCGt:2
sCHt:3
sCJt:2
sDFt:3
sEHt:1
I
}> /
}} ///
}
//
}}
}}
//
/
GI >> ///
? A AA }> G
>> /
AA}}
>> //
}}AAA
>>/
}
}
/t
/C
s/?
F
?
G
00AA }> G
//??
00AA}}
// ???
}A
// ?? } }00} AA
0
//
00
// D 00 > H
// }}0}0
// }} 00
}
}
E
Caminos (6)
sD F AIt : 1
sD GAIt : 2
sE F AIt : 3
sE GAIt : 1
sEGBIt : 1
SEGBJt : 2
SE GCJt : 1
sE HCJt : 2
/I
/ F () / A
? D AA
?
G () ~~~>
A
AA
~
AA
~~
~
~
/B
s?
}> G AA
?
??
AA() ???
}}
??
A
}
??
??
AA
? } }}
?
()
/J
/
/
C
E
H
==
==
==
==
@ t
s?
D
??
??
??
?
E
()
()
S = {s, d, e, h, c, g, b, i, a, f }
Cap(S) =
4+ 5+ 5+ 2 + 9+ 7 = 32
ht+ cj+ gt+ bj + it+ f t
32
A
t,
el ltimo.
Se ve que
2.5.3.
1 D i n i c ( Network Na ) {
2
g = 0;
// l a b e l 2 , pivote
i f ( p 6= t ) { // l a b e l 1
p = s;
i f ( + (p) 6= ) {
5
6
avanzar ( ) ;
goto 1 ;
} e l s e i f ( p 6= s ) {
7
8
9
10
11
12
retroceder () ;
goto 1 ;
} e l s e return
} else {
13
g;
Incrementar_flujo () ;
goto
14
15
2;
16 }
17
18 D i n i c ( Network Na ) {
19
g = 0;
20
E( s ) =
21
continuar = 1;
22
\\
Parte
Bloqueante
while ( c o n t i n u a r ) {
23
p = s;
while ( p 6= t && c o n t i n u a r ) {
i f ( + (p) 6= ) {
24
25
26
Av anz ar ( ) ;
} else
27
28
i f ( p 6=
s ){
Retroceder () ;
} else {
29
30
continuar = 0;
31
32
if
33
34
( p == t ) {
Incrementar_flujo () ;
35
36
37
Return
38 }
39
40
41
42
43
44
33
, Version
goto
less ;
6.
45
46
47 A van zar ( p i v o t e
p)
+ (p)
48
Tomar q
49
A( q ) = p ;
50
51
p = q;
// ancestro
de qes p
pq )G( pq ) } ;
52 }
53
54
55 R e t r o c e d e r ( p i v o t e
p)
56 {
57
Borrar
58
p = A( q ) ;
+ (A(p))
de
59 }
60
61 I n c r e m e n t a r _ F l u j o ( )
62 {
63
64
q = t;
65
= E( t ) ;
while ( q 6= s ) {
66
p = a(q) ;
67
G(
68
if
69
pq )
(G(
= G(
pq )
Borrar
70
pq )
pq ) ) {
+ (p)
== C (
q
de
;
71
72
q = p;
73 }
34
2.5.4.
Complejidad de Dinic
O(n2 m)
Prueba: por el teorema general de los algoritmos tipo Dinic basta ver que la com-
O(n m).
Inicializacin
(O(1))
A . . . AX
X = R:
Si
X = I:
Cada
borramos un lado.
borramos al menos un lado.
hay
palabras de la forma
A . . . AX
R = O(1)
Complejidad de
I = O(n);
Complejidad(X)
Complejidad de
hay
vrtices en el camino
s, . . . , t.
= O(n)
A = O(1)
Complejidad(A
. . A} X) = O(r) + O(n)
| .{z
r
Avanzar() antes de un
o un
I r n.
35
= m O(n) = O(n m)
En el caso de Wave
19
2.6.1.
1 Wave ( ) {
2
i n i c i a l i z a r () ;
olaForward ( ) ;
olaBackward ( ) ;
while (N
5
6
!=
2) {
olaForward ( ) ;
olaBackward ( ) ;
return
g;
10 }
11
12
i n i c i a l i z a r () {
13
g = 0;
14
(x 6= s) {
15
B( x ) = 0 ;
16
D( x ) = 0
17
18
A( x ) =
//No bloqueado
//Desbalanceo
// subconjunto de aquellos que le mandaron f l u j o a x
19
20
D( s ) = 0 ;
21
N = 1;
22
23
24
25
26
(x + (s)) {
G( sx ) = C( sx ) ;
D( s ) = D( s ) C( sx ) ;
D( x ) = C( sx ) ;
27
N++;
28
29
// #Vertices balanceados
A( x ) = { s } ;
}
30 }
31
32 o l a F o r w a r d ( )
33 {
34
35
for ( x = s +1;
i f (D( x ) >0)
36
37
To
; t 1) {
//Orden BFS()
ForwardBalance ( x ) ;
}
38 }
36
39
40 o l a B a c k w a r d ( )
41 {
42
43
for ( x = t 1
i f (D( x )>0 &&
44
45
DownTo
s +1) {
B( x ) == 1 )
//Orden BFS()
BackwardBalance ( x ) ;
}
46 }
47
48 B a c k w a r d B a l a n c e ( v e r t i c e
49
x){
50
Tomar y
51
A( x ) ;
m;
m;
52
g ( yx )= g ( yx )
53
D( x ) = D( x )
i f (D( y )
54
55
== 0 ) {
N++;
56
57
D( y ) = D( y ) + m;
i f (G( yx )
58
59
== 0 ) {
A( x ) = A( x )
60
{y } ;
61
62
N;
63 }
64
65
66 F o r w a r d B a l a n c e ( v e r t i c e
67
68
i f (B( y )
== 1 )
+ (x) = + (x)
70
{y}
} else {
71
x){
(+ (x) 6= ) ) {
+ (x) ;
Tomar y
69
&&
72
73
G( xy ) = G(
74
D( x ) = D( x )
i f (D( y )
75
76
xy )
+ m ;
== 0 ) {
N++;
}
78
D( y ) = D( y ) + m;
79
A( y ) = A( y )
{x } ;
i f (G( xy ) == C( xy ) ) {
+ (x) = + (x) { y }
81
82
83
86
87
88
80
85
G( xy ) }
m;
77
84
}
}
i f (D( x ) >0)
B( x ) = 1 ;
else
//Bloqueamos x
N;
89 }
37
2.6.2.
Complejidad de Wave
O(n3 )
Prueba: como es Tipo Dinic slo basta ver que la complejidad del paso bloqueante
2
es O(n ).
Sea
()t partes del algoritmo en donde se satura totalmente un lado o se vaca totalmente
un lado.
Y sea
()p
Compl(W ave_bloqueante)p
Cada ola Forward, salvo quizs la ltima bloquea al menos un vrtice nuevo.
Hay a lo sumo
??
n 2, F orwardBalance(x)
Entonces:
(14)
O(1)
= O(n)
( 14) Pues en cada
F B(x)
a slo
(2)
Luego:
Compl(W ave_bloqueante)t .
Cada vez que un lado se satura completamente no puede volver a saturarse, pues si
xy
#Saturamientos + #V aciamientos 2m
Compl(W ave_bloqueante)t = O(m)
38
2.6.3.
Ejemplos de Wave
Aplicar Wave al siguiente Network Auxiliar
sA:8
sC:7
sD:10
sE:7
AF:4
AG:3
AI:8
CG:2
CH:3
CJ:5
DF:4
DG:2
Nivel 1
Nivel 0
s
-32
8
4
1
7
5
2
10
6
7
4
60
4
8
3
5
1
3
60
2
7
11
4
7
12
13
16
18
0
0
0
7
0
4
-25
3
1
It:9
Jt:15
1
7
3
4
19
3
-19
EH:4
Ht:4
4
11
6
2
6
7
EG:5
Gt:5
Nivel 3
EF:3
Ft:7
Nivel 2
3
0
0
8
13
39
0
0
-32
AF:4
AG:3
AI:8
CJ:5
DF:4
DG:2
EF:3
EG:5
6 8 7
64 65
61 62
67
64
64
8
63
5
4
11
4
7
0
3
2
5
7
Nivel 2
10
66
0
CG:2
CH:3
Nivel 1
Nivel 0
-27
11
6
3
1
Nivel 3
I
Gt:5
Ht:4
It:9
Jt:15
2
27
0
1
0
0
1
11
40
7
0
7
12
13
16
18
25
EH:4
Ft:7
61 63 62
0
3. MST
MST: minimal spanning tree, rbol de expansin o generador minimal.
Denicin: sea
un grafo, un
rbol generador de
es un subgrafo de
que es
rbol y generador.
Es decir
es rbol generador de
1.
subgrafo de
2.
rbol
3.
generador
si:
G.
Obviamente si
V (T ) = V (G).
G
rbol generador.
Repaso de rboles
Propiedad:
rbol
Demostracin: si
Si
A(q)q
A(q)
m=n1
es rbol, entonces
denota el antecesor de
para algn
q 6=
raz.
Hay
n1
DF S(T ) = T .
6= raz)
Propiedad:
(1) T
Prueba:
rbol
(1) (2) y (3)
T
T
conexo y
no tiene
m=n1
ciclos y m = n 1
(2)
(3)
ya est.
(3) (1) : T
no tiene ciclos y
m=n1T
rbol
Como
Entonces
es acclico
C xy
entre
ciclos.
xy T
, es un camino en
entre
41
absurdo.
y.
Conclusin: si
x, y
Tb
T {x, y}
conexo y acclico.
T {xy} Tb
E Tb = V Tb 1
(15)
= |V (T )| 1
= |E(T )|
( 15)
Tb
(16)
es rbol
HIP.(3)
b E Tb |E(T )| + 1
Absurdo pues T {x, y} T
( 16) Por
(2) (1) : T
conexo y
m=n1T
rbol
siendo conexo.
Por lo tanto puedo ir borrando un lado de cada ciclo manteniendo continuidad.
Eventualmente llegamos a un grafo
es rbol.
E(H) = V (H) 1 = V (T ) 1 = E(T )
Absurdo porque por lo menos quit un lado.
Denicin: dado un grafo
Un MST
20
para
G es
X
W : E(G) R0 .
W (xy)
xyE(T 0 )
xyE(T )
Notacin
W (xy) = W (T ).
xyE(T )
Problema: dado
G.
3.1. Kruskal
Repetir hasta tener
n1
M akeset(x):
2.
F ind(x):
3.
U nion(A, B):
construye un conjunto
{x}.
42
x.
3.1.1.
1 Kruskal ( )
2 {
3
4
//
(x V ) {
Makeset ( x ) ;
//
8
9
m = 0;
11
12
13
14
Edges_Sort ( ) ;
10
//
= 1;
W(E[ i +1])
while (M
15
< N 1) {
16
A = find (x [ i ]) ;
17
B = find (y [ i ]) ;
if
18
19
(A
6=
B) {
t r e e . add ( e [ i ] ) ;
20
m++;
21
// agregar al arbol e [ i ]
Union (A, B) ;
22
23
i ++;
24
25
26
27 }
3.1.2.
j=0
el grafo en el paso
Hiptesis inductiva:
Supongamos que
Sea
Kj
Kj Tj
un MST
Tj : Kj Tj
MST.
e : Kj+1 = Kj {e}
Si
e Tj
e 6 Tj
En este caso, Tj {e} tiene: n 1 + 1 = n lados no
Como Tj es conexo Tj {e} es conexo.
Como no es rbol Tj {e} tiene un ciclo C .
Pero Tj no tiene ciclos e forma parte del ciclo C .
ya est pues
Kj+1 Tj
43
es rbol.
Sea
un lado de
que no est en
Kj+1 (f Kj+1
es acclico).
Tj+1
es un ciclo: y
f C (Tj {e}) f
es conexo y tiene:
n1+11=n1
lados
Tj+1
es un rbol.
rbol
Ya que
Kj Tj
f Tj
absurdo).
W (f ) W (e)
W (Tj+1 ) W (Tj )
Tj+1
Como Tj es MST
3.1.3.
tambin.
Complejidad de Kruskal
Volviendo al pseudocdigo:
= O(n)
= O(m log(m)) = O(m log(n2 )) = O(2m log(n)) = O(m log(n))
= n (2 Compl(F ind) + Compl(U nion))
Como en general
>
Compl(Kruskal) = O(m log(n))
3.1.4.
Kruskal Delete
(?)
3.1.5.
n1
lados.
44
3.1.6.
Ejemplo de Kruskal
A
B
C
D
E
F
(0)
3
1
3
B
5
1
4
C D E F
3 1 3
1 4
7
7 4 2
4 3
2 3
(3)
A
1
(1)
B
A
E
A
(4)
A
3
D
B
F
D
F
(5)
F
B
(2)
D
C
A
3
F
F
~ 00@@@
00 @@3
002 @@
~~
~
00 @
~
1
00 _ E
A PPPP
00
PPP
PPP
0 4
5
P
PPP 00
3
P
_D
B@
4
@@
~
~
@@
~~
@
~~ 7
1 @@
~
~
C
45
_E
_D
_E
C
B
~~
~
~
1
~~
~~
4
F
3 ~~~
_D
3.2. PRIM
Prim(x) = agregar el lado de menor peso que conecte
regin
con
V R.
3.2.1.
1 Prim ( v e r t i c e
x)
2 {
(p V ) {
3
4
Costo [ p ]
Vecino [ p ]
= NULL ;
7
8
Costo ( x ) = 0 ;
M = 0;
10
Aux = V ;
11
12
13
while (m
14
< n 1) {
15
q Aux
(q (p)) {
16
if
17
( c o s t o ( q )> V( pq ) ) {
18
c o s t o ( q ) = W( pq ) ;
19
vecino (q) = p ;
20
21
22
Add ( p , v e c i n o ( p ) ) ;
23
m++;
24
25
26
27 }
3.2.2.
Correctitud de Prim
Pj
es el rbol en el paso
j = 0:
j,
Pj+1 Tj
une
Como
Pj
Tj
con
ya est.
De lo contrario existe
M ST Tj : Pj Tj
Trivial.
probaremos que
induccin.
e Pj+1 : e 6 Tj
(y de hecho, ser
Pj+1 = Pj {e}
V Pj
es Spanning Tree
debe conectar
46
Pj
con
V Pj .
).
Sea
Como
un lado en
Tj
W (e) W (f )
Entonces:
Tj {e} f
3.2.3.
Pj+1 .
Ejemplo de Prim
A
B
C
D
E
F
P rim(A),
3
1
3
B
5
1
4
C D E F
3 1 3
1 4
7
7 4 2
4 3
2 3
Mtodo Grco:
(1)
_E
(2)
A
3
_E
(3)
A
3
D
(4)
A
3
_E
(5)
D
_F
A
3
_E
_F
_C
_E
_F
1)
B
(5, A)
C
(, )
D
(3, A)
(1,A)
F
(3, A)
2)
(5, A)
(, )
(3,A)
(3, A)
3) (4, D)
(7, D)
(2,D)
4)
(4,D)
(7, D)
5)
(1,B)
3.3.1.
O(1)
O(n)
Union es
Find es
2m
Finds() y
n1
Unions().
mn >
m log(n)
| {z }
no sirve.
3.3.2.
1
2 Makeset ( x ) {
3
R( x ) = x ;
//O(1)
4 }
5
6 Find ( x ) {
7
return
// O(1)
R( x ) ;
8 }
9
10 Union ( R1 , R2 ) {
11
(x V ) {
if
12
13
15
R2 ) {
R1 ;
(R( x ) ==
R( x )
14
// O(n)
}
}
16 }
Compl(Kruskal) =
O(m log(n))
|
{z
}
+ O(m) + O(n2 )
| {z }
| {z }
m Finds
n Unions
m n2
Es decir no funciona tan bien si es un grafo ralo, i.e. con pocos lados.
48
3.3.3.
1 Makeset ( v e r t i c e
2
(x)
Rk ( x ) = 0 ;
= x;
//Ranking
x ) { // recursivo a la cola
4 }
5 Find ( v e r t i c e
if
( (x) == x )
return x ;
e l s e return
7
8
x){
f i n d ( (x) )
9 }
10 F i n d ( v e r t i c e
11
// i t e r a t i v o
while ( y 6= (y) )
12
13
y =
return
14
(y) ;
y;
15 }
16
x){
y = x;
/ Pre :
F1
= Find ( x [ 1 ] ) ;
F2
17 Union ( F1 , F2 ) {
18
19
20
21
22
k1
k2
(F2 )
F1 ;
25
(F1 ) = F2 ;
i f ( k1 == k2 ) {
Rank ( F2 ) = k2
26
27
= Rank ( F2 ) ;
} else {
24
F1 6= F2
= Rank ( F1 ) ;
i f ( K1 > K2 ) {
23
= Find ( x [ 2 ] ) ;
+1;
28 }
makeset(x) = (1)
U nion = O(1);
F ind(x) = O(rank(representante(x)) O(log(n))
Teorema Find es
Corolario:
O(log(n))
Demostracin:
Compl(Kruskal) =
O(m log(n))
|
{z
}
n Unions
F2
49
F2
cuando
(F1 ) == F2
Lema interno 2:
Rank(x) = k 2k
x,
contando a
x.
induccin.
Prueba por
k=0
obvio
k+1
Rank(x) = k + 1 alguna
si unimos 2 de rango k .
Veamos para
vez fue
y union lo subi a
Finalmente
Pues sea
n
elementos de rango
2k
k,
k+1
x 2k + 2k = 2k+1
Rango(x) log(n) x
el mayor rango
k.
n
2k
2k n
k log2 (n).
21
en O(m)?
i f ( (x) 6= x ) {
(x)
3
4
5
= F i n d ( (x) ) ;
return (x) ;
6 }
F ind()
puede ser
O(log(n))
Se puede ver:
Teorema: la complejidad de
Esencialmente:
O log (n) = #
O(m) n
Finds
+n
O m log (n)
razonable.
log
{3,4}
{5,. . . ,16}
16
{17,. . . ,2 }
16
65536
{2 +1,. . . ,2
}
log(n)
21 Bucket sort.
50
4. Matchings
Dado un grafo
G, un matching en G es un subgrafo M
de todos
sus vrtices es 1.
Un matching es
perfecto si cubre a
G,
es decir:
V (M ) = V (G).
G es bipartito.
G = (X Y, E) asumimos que X
pq E p X, q Y
G,
S no
no
S
E = sx
xy : y Y
yt
xX
yY
xy E
Dado
1.
B
C
;;
;;
;;
;
;
A;
} AAA
AA
}}
}
AA
}}
A
}
}
B
A@
~C
@@
~~
@@
~
@@ ~~
@ ~~
1 @@
2
3
@@
~~
@@
~~
~
@@
~
@ ~~~
Teorema
Flujos maximales en N corresponden con matchings maximales en
V (fmax ) = #lados.
construimos un matching
fM .
Vf =
Dado
sea
Mf
Mf = (Vf , Ef ) :
{x, y}
xyEf
Ef = {xy E : f ( xy) = 1}
Hay que ver que
Mf
51
es matching.
y dado un Matching
Caso 1:
zX
Entonces
como
Mf
Vf
tenemos que
z : Mf (z) 2
no es matching
Mf (z) 2
Mf (z) 1 z Vf
entonces
y1 6= y2
zy1 , zy2 Ef
y1 , y2 :
f (zy1 ) = f (zy2 ) = 1
Outf (z) 2
Caso 2:
z Y x1 , x2 :
Entonces
Absurdo.
x1 6= x2
x1 z, x2 z Ef
f (x1 z) = f (x2 z) = 1
Inf (z) 2
Pero
Concluimos que
Mf
Outf (z) = f ( zt ) 1
Absurdo
es matching.
fM
denido as:
fM ( xy) =
fM ( sx) =
1
0
Si
Si no
1
0
Si
1
0
Si
fM ( yt ) =
xy E(M )
x V (M ) X
Si no
y V (M ) Y
Si no
Mf (z) = Outf (z) =
Adems est claro que:
MfM = M
y que:
52
1
0
Si
Si
f Mf = f
z V (M )
z 6 V (M )
V (f ) = |EMf |
=0
z }| {
V (f ) = Outf (s) Inf (s)
X
=
f ( sx)
xX
= #{x : f ( sx) = 1}
= #{x : Inf (x) = 1}
= #{x : Outf (x) = 1}
= |Vf X|
= |EMf |
por ser
un ujo
Denicin: Sea
Denicin: Si
si cubre a X, i.e.
S V (S) =
G = (X Y, E)
X V (M ).
(z) = {w V : z S : wz E}
zS
es bipartito, un matching se dice completo de
G = (X Y, E) bipartito entonces:
un matching completo de X
Y : (S) S S X
|X| = |Y | = n
Matching inical:
O(n2 ).
O(n)
O(n2 ).
veces.
Nos queda:
O(n3 )
53
sea
2. Mimizar la suma.
23
4.4.1.
Dado
Algoritmo de Gross
C:
1. Ordenar los
Ci,j
como :
2. Tomar, para r= 1, . . . , k:
(Ar )i,j =
3. Hallar
r : Ar
1
0
Si
Ci,j br
Si no
Ar1
(r es el umbral)
f)} M
f
max{Ci,j : {i, j} E(M )} max{Ci,j : {i, j} E(M
54
4.4.2.
II
III
IV
VI
VII
15
2
10
100
e2
10
10
10
10
10
10
10
10
Ordeno los
Intento extenderlo
II
III
IV
VI
VII
II
III
IV
VI
VII
?
?
II
III
IV
VI
VII
II
III
IV
VI
VII
II
III
?
E
Extiendo el Matching
I
II
III
IV
VI
VII
1
II
III
V
?
IV
?
55
?
?
?
II
III
V
?
?
II
III
IV
VI
VII
1
1
II
III
IV
VI
VII
1
E
E
E
E
I
II
III
V
?
IV
?
II
III
IV
VI
1
I
II
III
V
?
IV
?
II
III
IV
VI
VII
1
III
?
E
E
E
E
II
III
IV
VI
VII
1
G
C
E
E
E
E
I
II
III
V
?
IV
?
Obtengo:
Filas etiqueteadas.
Luego
Columnas etiqueteadas.
Matching Perfecto.
56
III
V
?
II
I
II
III
V
?
IV
?
?
S = {C, D, E, G} =
VII
I
II
III
V
?
IV
?
III
V
?
Umbral 9
II
III
IV
VI
VII
Busco Matching
I
II
III
IV
VI
VII
II
III
IV
VI
VII
II
III
IV
VI
VII
II
III
IV
VI
VII
II
III
?
Extiendo el Matching
I
II
III
IV
VI
VII
1
Existe Matching
I
II
III
V
?
Tengo un Matching de
8,
57
8,
?
I
II
III
V
?
Umbral de
e2
I
II
III
IV
VI
VII
Busco Matching
I
II
III
IV
VI
VII
II
III
IV
VI
VII
?
?
?
II
III
IV
VI
VII
II
III
IV
VI
VII
II
III
0
0
?
Extiendo el Matching
I
II
III
IV
VI
VII
1
II
III
58
V
?
?
?
?
II
III
V
?
?
I
1
II
1
III
0
IV
1
V
1
VI
1
VII
1
B
C
D
E
F
G
1
0
1
0
1
0
1
1
1
1
1
1
0
1
0
0
0
1
0
0
0
1
1
1
1
1
0
0
0
1
0
B
G
B
II
1
III
0
IV
1
E
E
B
I
1
E
E
1
0
0
0
1
0
B
V
1
VI
1
VII
1
B
C
D
E
F
G
1
0
1
0
1
0
1
1
1
0
1
1
1
1
1
1
1
0
1
0
0
0
1
0
0
0
1
1
1
1
1
0
0
0
1
0
B
D
G
C
E
E
B
E
E
B
1
0
0
0
1
0
B
1
1
0
1
1
I
II
III
V
?
III
V
?
?
I
1
II
1
III
0
IV
1
V
1
VI
1
VII
1
B
C
D
E
F
G
1
0
1
0
1
0
1
1
1
1
1
1
0
1
0
0
0
1
0
0
0
1
1
1
1
1
0
0
0
1
0
B
G
E
E
B
B
III
0
IV
1
E
E
1
0
0
0
1
0
B
V
1
VI
1
VII
1
1
0
0
0
1
0
B
A
1
1
0
1
1
D
I
1
II
III
III
V
V
?
?
B
C
D
E
F
G
1
0
1
0
1
0
1
1
1
0
1
1
1
1
1
1
1
0
1
0
0
0
1
0
0
0
1
1
1
1
1
0
0
0
1
0
B
D
G
C
E
E
B
E
E
B
II
?
C
II
1
Extiendo el Matching
I
II
III
IV
VI
VII
1
I
1
II
III
III
1
V
?
V
?
?
II
B
D
G
C
E
E
B
A
E
E
B
A
2
Hall un Matching que minimiza el mximo: (e )
n
o
M = A V II : 4; B V I : 2; C III : 1; D I : e2 ; E V : 1; F IV : 4; G II : 2
59
I
II
III
III
V
V
?
?
?
I
I
II
III
III
V
V
?
?
?
II
G = (X Y, E)
Y |S| |(S)| S X
Prueba:
: 11
de X en Y y la imagen de S
(S)
11
un matching completo
24
X,
Columnas
Y)
las las de
S0
Tj
Sea
Y as seguimos:
Nos queda denido:
Sj
Sj
con
Tk+1 =
Observacin:
1.
|Sj | = |Tj | j
2.
(S0 . . . Sj ) = T1 T2 . . . Tj+1
Sea
Pues
Sj
Tj
S = S0 S1 . . . Sk
|(S)| = |(S0 . . . Sk )|
= |T1 . . . Tk Tk+1 |
= |T1 . . . Tk |
= |T1 | + . . . + |Tk |
= |S1 | + . . . + |Sk |
= |S| |S0 |
< |S|
?.
60
(17)
(18)
(19)
(20)
Tk+1 =
G = (X Y, E) : E 6= ,
W V con W X W Y
Prueba: sea
Dado
regular.
Denimos:
W X
|Ew | = {xy E : x w}
X
=
{y : xy E}
xw
X
xw
d(x)
|{z}
(21)
= |w|
( 21) Por ser
regular.
W Y
Observemos que por ser G bipartito: Ex = E = Ey
|Ex | = |Ey | |X| = |Y |
Como 6= 0 |X| = |Y |
Similar prueba si:
G,
Y.
|S| |(S)| S X
SX
Sea
e Es
xS
: e = xy
yY
y (x) (S)
e = x y E(s)
Es E(S)
|Es | |E(S) |
|S| |(S)|
|S| |(S)|
61
i=1
j : ij M
La solucin a este problema usa una idea sencilla: sea
obtiene de
Entonces sea
Si a la
e
C
Mmn (C)
Ci,j
Si i 6=
i
Ci,j k Si i = i
Ci,j k , similar para
la
i le
k,
n
P
ei,j =
C
n
P
i=1
j : ij M
las columnas.
i=1
j : ij M
Mmn (C)
Segunda Observacin: si
i.e.
ei,j
C
tambin minimiza
e
C
Ci,j 0.
Entonces si M es un matching de
00 s
es decir
Ci,j = 0 i, j M
M minimiza la
suma.
Idea bsica: restar o sumar la inteligentemente hasta hallar un matching de ceros.
Ese matching automticamente minimiza la matriz original.
4.7.1.
II
III
IV
10
11
10
10
II
III
IV
II
III
IV
Matching de
00 s.
II
III
IV
-5
62
Sumamos 5 a la columna I
I
II
III
IV
14
Hall un Matching de
I
II
III
IV
14
II
III
IV
14
n
o
M = AIV : 7; BIII : 5; CI : 1; DII : 1
4.7.2.
Algoritmo Hngaro
00 s
Sino tomar
a las
y restarle un cierto
Cul m?
m = mn S (S)
Equivalentemente: restar
4.7.3.
S (S)
y sumar
I II III
A 2 5
7
B
9
10
5
C 1 6
7
D 10 1
4
IV
7
11
10
4
I II
A 0 3
B
4 5
C0 5
D 9 0
63
III
5
0
6
3
IV
5
6
9
3
S (S)
00 s.
I II
A 0 3
B
4 5
C0 5
D 9 0
III
5
0
6
3
3. Buscamos Matching de
I
II
III
IV
IV
2
3
6
0
00 s
II
III
IV
II
III
IV
6C
C
4. No hay matching perfecto
I
II
III
IV
6I
6
6C
Tengo
S = {A, C}
(S) = I
Con
5. Calculo:
m = mn S (S)
I
II
III
IV
I II
6I
A 0 1
m=2 B
6 5
C 0 3
6
D 11 0
6C
Referencias:
S (s)
S (s)
S (S)
S (S)
No lo modico
Sumo
No lo modico
Resto
64
III
3
0
4
3
IV
0
3
4
0
6. Busco Matching de
00 S
I
a)
b)
No encuentro:
A 0
B
6
C 0
D 11
II
1
5
3
III
3
IV
0
3
4
0
4
3
Intento extenderlo:
I
II
III
IV
11
*
II
III
IV
11
II
III
IV
0
11
*
II
III
IV
11
I
Extiendo el matching
*
I
A 0
B
6
C 0
D 11
II
1
5
3
7. Llegu al Matching de
00 s:
III
3
0
4
3
IV
0
4
0
A mano
1. Tachar las de
S,
y las columnas de
(S).
m.
m.
m.
4.7.4.
O(n2 ).
O(n2 ).
O(n2 )
Paso
65
hay
O(n)
pasos.
I
*
m,
restarlo y sumarlo:
O(n2 ).
O(n3 ).
en un paso? No sabemos
de matriz a buscar el
m,
sabemos)
restarlo y sumarlo.
AA
BC
S
S
(S) (S)
Referencias
A: puede haber ceros.
B: No hay ceros, no hay matching.
C: Ceros del Matching.
S (S)
S (S)
no cambian.
S (S)
S (S),
j,
la
i.
i 6 S, j (s).
Cuando revisamos la columna
agregado la la
a S,
hubieramos
absurdo.
si est libre
extendemos el matching.
Sino hay una la
con
{i, j} M :matching,
a S.
Entonces S crece.
Observacin:
no formaba parte de
S,
debe estar en
S.
veces.
66
Por lo tanto:
Compl(Hungaro) = n Compl(paso)
Compl(paso) =
Compl(cambiar
n
|{z}
la matriz)
+ O(n2 )
cambio de matriz
Compl(cambiar
la matriz)
= O(n2 )
Compl(paso) = O(n3 )
Compl(Hungaro) = O(n4 )
Sin embargo, siendo truquero se puede reducir la complejidad a
O(n3 ).
Compl(Hungaro) = O(n3 )
1. Cmo restar m de
y sumar
(S)
en tiempo
O(n)?
Agregamos etiquetas: cada la y cada columna tiene una etiqueta que indica cunto
se le suma o resta.
Cambiar las etiquetas es:
O(n).
if(Ci,j
== 0)
mn{S (S)}
en tiempo
O(1).
O(n)?
Vericar el mnimo de
Y los
4.7.5.
(y)
67
A = {0, 1}.
es un subconjunto de
An .
25
es:
El peso de Hamming de
Claramente:
Con
00=0
01=1
10=1
11=0
es decir la suma mdulo
Propiedad
2.
es una distancia.
Prueba:
1.
d(x, x) = 0
2.
d(x, y) = 0 x = y
3.
d(x, y) = d(y, x)
4. Desigualdad triangular:
A = {i : xi = yi }, B = {i : xi = zi }, C = {i : yi = zi }.
xi = zi
xi = y i
yi = zi
Sean:
Si
i B C i A,
Aplico De morgan:
es decir
(B C) A
(B C) A
y obtengo:
ABC
|A| |B C|
|B| + |C|
d(x, y) d(x, z) + d(z, y)
68
en
Zn2
Deniciones
detecta
Un cdigo
corrige
Notacin:
errores si:
errores si:
C Br (x) = {x} x C
Bt (x) Bt (y) = x, y C : x 6= y
= mn{d(x, y) : x 6= y : x, y C}
Ejemplos:
1.
2.
HH
000
011
110
101
000
011
110
(C2 ) = 2
101
3.
000000
000000
010101
101010
111111
010101
101010
111111
69
(C3 ) = 3
d1
errores y no detecta
2. C corrige
b d1
c
2
errores y no corrige
d = (C)
entonces:
errores.
b d1
c+1
2
errores.
Prueba:
1. Detecta
d 1:
Recordemos que
Sea
x C, y Bd1 (x) C x = y
d(x, y) 1 <
x=y
Pero x, y C
No detecta d:
Sean
x, y C : x 6= y
d(x, y) =
2. Corrige
Sea
c
b d1
2
y Bd (x) C
Como y 6= x
no
detecta d errores.
errores:
t = b d1
c.
2
Sean
x, y C, x 6= y .
Supongamos
d(x, y)
d(x, z) + d(z, y)
2t
1
0 1 absurdo
( 22) Ya que
x 6= y
(22)
(23)
C.
c+1
b d1
2
errores:
x, y C : d(x, y) = d,
Entonces
tiene
d1
0
Sea e con b
c
2
0
Sea z = x e
Quiero ver que
sea
e=xy
unos.
+1
unos de estos.
teseccin es no vaca.
Luego
d(x, z) = |x z| = |x x e0 | = |e0 | = b d1
c+1
2
70
d1
d(y, z) = |y z| = |y x e | = |e e | = d 1 + b
c
2
0
Demostracin de
d1
c+1
2
d = 2k + 1 (Impar)
d 1 + b d1
c = 2k + 1 1 k = k = b d1
c < b d1
c+1
2
2
2
Si
d = 2k (Par)
c = 2k 1 (k 1) = 2k 1 k + 1 = k = k 1 + 1 = b d1
c+1
d 1 + b d1
2
2
Si
26
2. Sean:
Ei+
el evento Bit
cambia de
0 1.
Ei
el evento Bit
cambia de
1 0.
Entonces:
P Ei+ = P Ei .
3.
Ei
4.
es independiente de
Denotamos
Ei
Ej i 6= j
1
2
cdigo de longitud
|C|
Como
que corrija
2n
1 + n + ... +
errores entonces:
n
t
Bt (x)
xC
corrije t errores, esa unin es disjunta.
Prueba: sea
A=
|A| =
|Bt (x)|
xC
Calculemos
|Bt (x)|:
71
i.
Bt (x) =
t
S
Sr (x)
unin disjunta.
r=0
t
X
|Bt (x)| =
|Sr (x)|
r=0
y Sr (x) d(x, y) = r
(x y) = r donde deno (x y) = e
e : |e| = r : y = e x
| {z }
Sr (0)
n ubicaciones
|Sr (0)| = nr
De las
e Sr (0)
|A| =
tiene
r 10 s
n r 00 s
10 s.
|Bt (x)|
xC
!
t
X X
n
=
r
r=0
xC
t
X
n
=
C
r
r=0
Como
A Zn2 |A| 2n
t
X
n
C 2n
r
r=0
2n
C
1 + n + . . . + nt
27
C 6=
2. Si
3.
x, y C x y C
x C, k Z2 k x C
27 SEV.
72
es un subespacio vectorial de
se da si:
Zn2
Pero
Z2 = {0, 1}
1*X = X
0*X = 0
Trivialmente est en
C,
0 C.
C 6= x C x
x} C
| {z
=0
C es lineal si y slo si
()
C 6=
x, y C x y C
Teorema: C lineal
n
o
= mn |x| : x C, x 6= 0
Prueba:
w = mn{|x| : x C, x 6= 0}
Sean x, y C : x 6= y con d(x, y) =
Sea
= |x y|
x y C y x y 6= 0, pues x 6= y w
x C, x 6= 0 con |x| = w
Entonces w = d(x, 0) , 0 C pues C es lineal.
Como es la menor distancia tenemos que w
Como C es lineal
Sea ahora
=w
Ejemplos:
C1 , C2 , C3
es lineal
denotar la dimensin de
Corolario: si
Prueba: obvio pues
C.
es lineal de dimensin
k |C| = 2k
73
G para C
de C.
Por lo tanto si C es de dimesin k y longitud n y G es generadora
Kx N .
G generadora G es Kx N, con k = Dim(x) y C = EF (G)
n
k
Recordemos que: EF (G) = {x Z2 : U Z2 : x = U G}
1 0 0 1 1 1
Sea G = 0 1 0 1 0 1
0 0 1 1 0 0
de C entonces
C = {x1 x2 x3 x4 x5 x6 : u1 , u2 , u3 : x = U G}
u1 u2 u3 G = u1 u2 u3 (u1 u2 u3 )u1 (u1 u2 )
0
C=
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
0
1
1
0
1
0
0
1
0
0
0
0
1
1
1
1
1
=2
1
En particular:
n1
z}|{
G=
.
.
.
Da el cdigo:
=2
cdigos de repeticin
z }| { z }| { z }| {
C = {x1 . . . xk x1 . . . xk . . . x1 . . . xk }
|
{z
}
r veces
n=rk
Una matriz generadora
G= I I I I I
|
{z
r I0s
74
I = peso
}
mnimo no nulo
=r
es
Propiedad
I
|{z}
kx k
}|
{
A
|{z}
es generadora de
nk
t
A
C n k |{z}
k
I
|{z}
es de chequeo.
nk
EF [I A] = N u [At I]
x EF [I|A] U : x = U [I|A] = U (U.A)
Sea
Ut
[A |I]x = [A |I]
(U.A)t
t
Entonces
Dim(N u([|{z}
At | |{z}
I
])) = k
k
nk
dep. indeptes
= At U t (U A)t
= At U t At U t
=0
x N u([At I])
son iguales
Ejemplo:
1 0 0 1 1 1
1 1 1 1 0 0
G = 0 1 0 1 0 1 H = 1 0 0 0 1 0
1 1 0 0 0 1
0 0 1 1 0 0
x1
x2
1 1 1 1 0 0
x
3
Entonces N u(H) = x1 . . . x6 : 1 0 0 0 1 0 = 0
x4
1 1 0 0 0 1
x6
x1 x2 x3 x4 = 0
= x1 . . . x6 : x1 x5 = 0
x1 x2 x6 = 0
x4 = x1 x2 x3
= x1 . . . x6 : x5 = x1
que es lo que
x6 = x1 x2
75
tena antes.
Ejercicio: calcular las palabras del cdigo que genera la siguiente matriz
1 1 0 1 1 1 0 0
0
0
1
1
0
0
0
1
0
Si G =
H=
0
0
1 0 1 1 0 0 0 1
0
0
1
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
1
1
1
0
1
1
0
1
1
0
0
1
0
1
0
G.
en este caso
=3
3.
=2
= 1.
Como C es lineal
Como
( < 3) = 1
x C : |x| = 1 i : x = ei 28
x C Hxt = 0
Columna
de H.
H (i) = 0
0
0
0
1
1
1
0
1
absurdo.
= 2.
x C : |x| = 2
i,j : i 6= j : x = ei ej
absurdo.
Ejemplo
Sea
1
0
H=
0
0
0
1
0
0
0
0
1
0
1
0
1
1
1
1
1
0
1
1
H
1
1
es
4x 9
C = N u(H)
?
|C| = 2k = 25 = 32
k =94=5
9 variables - 4 independientes
(C) =
Como H no tiene la columna
(1)
Como H
+ H (5) = H (9)
28 Recordemos
ni columnas repetidas
ei = 0 . . . 0 |{z}
1 0 . . . 0.
i
76
(C) 3
Entonces
x = 010001001 C (C) 3
(C) = 3
C=
r (2r 1)
r.
cdigo de Hamming
1 0 1
=H=
0 1 1
orden posible.
xz =0
= xyz :
yz =0
= {xyz : z = x = y}
= cdigo de repeticin
1 0 0
= H = 0 1 0
0 0 1
1010101
0110011
=H=
0001111
0000000
1 0 1 1
1 1 0 1 K = 4
0 1 1 1
0110101
0010011
K = 11
0001111
1111111
r = 2n
1+n
1 + n = 2r
2n
2n
r
= r = 2nr = 22 1r = 2k =
1+n
2
error
H (i) 6= 0, H (j)
xC
y el receptor recibe
e=0
Hy y = Hxt = 0 y C
Si e = e
Si
Hy t = Hxt Heti
= 0 H (i)
= H (i)
77
y =xe
con
|e| 1.
El algoritmo es:
1. Calcular
Hy t .
2. Si
Hy t = 0
3. Si
Hy t = H (i)
4. Si
Hy t 6= 0, H (i) i
Aceptar
y.
y ei .
Aceptar
hubo
Ejemplo:
1
0
H=
0
0
Se recibe:
0
1
0
0
0
0
1
0
0
0
0
1
0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
y = 01000100
Hy t = H (2) H (3) H (7)
0
0
1
1
1 0 1 0
(6)
=
0 1 0 = 1 = H
0
0
1
1
x = y e6 = 011001100
z = 000011000
Pero si recibo
Hz t = H (5) H (6)
0
1
1
1 0 1
(i)
=
1 1 = 0 6= 0, H i
1
1
0
Hubo
errores
H (i) =
(i)
Binario
Es ms fcil:
1. Calcular
2. Si
Hy t
Hy t = 0
3. Sino tomar
aceptar
i:
(representacin binaria
i)= Hy t
78
y acepto
y ei
Ejemplo
n = 25 1 = 31 k = 31 5 = 26
= 226
5
Supongamos que llega y = e1 e2 e11 e27 e31
5 tiene
Entonces:
Mandaron
x = y e1
y = e2 e7 e17
Si hubiese sido
0
1
1
0
1 1 0 0
(20)
Hy t =
0 1 0 = 1 = H
0 0 0 0
0
0
1
1
Mandaron
x = y e20
errores.
Prueba:
x : |x| = (C) = d
Entonces i1 , i2 , . . . , id : x = ei1 . . . eid
Sea
0 = Hxt
= Heti1 . . . Hetid
= H (i1 ) . . . H (id )
{H (i1 ) , . . . , H (id ) } es L.D.
mn{j : j columnas dependientes de H} = j
i1 , . . . , ij : {H i1 , . . . , H ij } es L.D.
Entonces c1 , . . . , cj no todos nulos tal que: c1 Hi1 , . . . , cj Hij = 0
79
29
columnas L.D. de H}
Prueba:
Sean
d = (C), H : C = N u(H)
d = mn{j : j columnas L.D. de H}
= 1 + max{j : Todo conjunto de j columnas es L.I.}
1 + max{j : Existe un conjunto de j columnas L.I.}
1 + Rango(H)
=1+nk
=nk+1
30
1. Es lineal.
2. Invariante por rotaciones: i.e.
W C Rot(W ) C
Ejemplos
Z2 +
Identicacin:
Denicin: si
tiene longitud
es el resto de dividir
P (x)
por
H(x).
entonces:
Rot(W ) = xW (x)
mod (1 + xn )
Prueba:
xW (x) = x w0 + w1 x + . . . wn1 xn1
= w0 x + w1 x2 + . . . + wn2 xn1 + wn1 xn
30 MDS: maximun distances separable.
80
Aplico mdulo
1 + xn
xW (x)
( 24)
(24)
(25)
Cociente
( 25) Conmuto.
Ejemplo
Tomo
Corolario:
Sea
cclico de longitud
n,
W (x) C
Q(x) Z2 (x)
W (x)Q(x) mod (1 + xn ) C
el menor grado.
81
Propiedad: sea
G(x)
Generador :
Prueba:
G(x)|P (x)
Como
clase pasada
mod (1 + xn ) C
La parte importante
Sea
Q(x), R(x) Z2 (x) : P (x) = Q(x)G(x) + R(x): con Gr(R(x)) < Gr(G(x))
Gr(R(x)) < n 1
Como Gr(R(x)) < Gr(G(x))
Gr(Q(x)G(x)) n 1
Gr(P (x)) n 1
Sean
Entonces por
Q(x)G(x) C
R(x) = P (x) + Q(x)G(x) C
| {z } | {z }
C
Gr(R(x)) < Gr(G(x)) n 1
R(x) = 0
R(x) C
P (x) = Q(x)G(x) G(x)|P (x)
82
Teorema
Sea C cclico de longitud
n,
G(x)
el polinomio generador de
C,
con
k = Dim(C)
Entonces:
1.
Gr(G(x)) = n k
2.
G(x)|1 + xn
3.
G(x) =
+g1 (x) + . . .
i.e.
g0 = 1
Prueba
1. Sea
Gr(Q(x)) n t 1
Viceversa si
Entonces:
P (x) = Q(x)G(x)
con
Gr(Q(x)) n t 1
Gr(P (x)) n t 1 + t = n 1
P (x) C
Esto da una
Gr(Q(x)) n t 1
C = P (x) : Q(x) :
P (x) = Q(x)G(x)
n
o
biyeccin entre C y
Q(x) : Gr(Q(x)) n t 1 = ?
|C| = | ? |
= |{Q(x) = q0 + q1 x + . . . + qnt1 xnt1 }|
= 2nt
Como
2. Supongamos
Por lo demostrado en
Multiplico por
1.
xk :
83
(26)
Como
G(x)
es cclico y
en
Z2 ,
0.
es generador
G(x)|1 + xn
1 + xn = V (x)G(x) 1 = v0 g0 g0 = 1
m(x)
de grado
k 1.
V (x) = m(x)G(x)
Ejemplo:
G(x) = 1 + x2 + x3 : n = 7
Cunto vale
k = Dim(C)
n k = Gr(G(x)) 7 k = 3 k = 4
Por lo tanto el cdigo tiene
24 = 16
palabras.
m = 1011 m(x) = 1 + x2 + x3
V (x) = m(x)G(x)
= (1 + x2 + x3 )2
= 1 + x4 + x6
1000101
Mando
1000101
84
Otra:
m = 0110 x + x2
V (x) = (x + x2 )(1 + x2 + x3 )
= x + x 3 + x4 + x2 + x4 + x5
= x + x 2 + x3 + x5
0111010
Esto corresponde a la siguiente matriz generadora:
1 g1 g2 . . . gnk1
0 1 g1 g2
...
0 0 1 g1
g2
.. .. .. . .
..
. . .
.
.
0 0 0 0
1
1
0
G=
0
0
0
1
gnk1
...
gnk1
..
..
g1
0
1
0
0
1
0
1
0
1
1
0
1
0
1
1
0
..
g2
0
0
1
1
0
0
1
...
...
...
.
..
. . . gnk1
0
0
0
1
Segundo Mtodo
Idea: sea
= 0 mod (G(x))
Si el
Gr(P (x)) n 1
ya est:
Truco
Dado
m(x)
con
En denitiva
Ejemplos:
m(x) = 1011 1 + x2 + x3 : n = 7 k = 4
x3 m(x) mod G(x) = 0
V (x) = 0 + x3 m(x) = x3 + x5 + x6 0001011
m(x) = 0110 x + x2
x3 m(x) = x4 + x5
?
85
0
0
.
.
.
1
x4 x + x3 (G(x))
x + 1 + x2 (G(x))
1 + x + x2 (G(x))
x5 x + x2 + x3 (G(x))
x + x2 + 1 + x2 (G(x))
1 + x (G(x))
x4 + x5 mod G(x) 1 +
x + x2 + 1 +
x x2
V (x) = (x4 + x5 ) mod G(x) + x4 + x5
= x2 + x4 + x5
001
0110
| {z }
m(x)
.
.
.
.
.
.
.
.
.
n1
mod G(x) + x
xnk
xnk1
G = ..
.
xn1
Tenamos:
x3
x4
x5
x6
mod
mod
mod
mod
G(x) 1 + x2
1
1
G(x) 1 + x + x2
G=
1
G(x) 1 + x
2
G(x) 1 + x
0
0
1
1
1
1
1
0
1
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
1 0 0 1 1 1 0
H = 0 1 0 0 1 1 1
0 0 1 1 1 0 1
En general esto vale claramente: es decir,
86
Ejemplo: sea
G(x) = 1 + x2 + x3 + x4 : n = 7
x4
mod G(x) 1 + x2 + x3
x5
mod G(x) x + x3 + x4
x6
x + x3 + 1 + x2 + x3
1 + x + x2
mod G(x) x + x2 + x3
1
0
H=
0
0
5.5.1.
0
0
1
0
0
0
0
1
1
0
1
1
1
1
1
0
0
1
1
1
|E| t,
Sean
Si
0
1
0
0
V (x)
y llega
generador.
error corregible.
Si
los sndromes:
i : |Si | t
E(x) = Rotni (Si )
= xni Si
mod (1 + xn )
xU (x)
mod G(x) = Q(x)(1 + xn mod G(x)) + Rot(U )
|
{z
}
mod (G(x))
0 pues G(x)|1+xn
G(x) = 1 + x4 + x6 + x7 + x8 : n = 15
con
t=2
x8
mod G(x) 1 + x4 + x6 + x7
x9
mod G(x) x + x5 + x7 + x8
(los encuadrados).
x10
x + x5 + x7 + 1 + x4 + x 6 + x7
1 + x + x4 + x5 + x6
mod G(x) x + x2 + x5 + x6 + x7
x11
mod G(x) x2 + x3 + x6 + x7 + x8
x12
x13
x2 + x3 + x6 + x 7 + 1 + x4 + x6 + x7
1 + x2 + x3 + x4
mod G(x) x + x3 + x4 + x5
mod G(x) x2 + x4 + x5 + x6
=1+x +x +x
|S0 | = 4 > 2
m
od G(x)
88
x9
m
od G(x)
x13
m
od G(x)
Contino calculando
|S1 |,
|Si | 2
S2 = 1 + x + x2 + x3 + x5 + x6 + x7
S3 = 1 + x + x2 + x3 + x4
.
.
.
S6 = x3 + x4 + x5 + x6 + x7
S 7 = x4 + x5 + x6 + x 7 + x8
= x4 + x5 + x6 + x7 + 1 + x4 + x6 + x7
= 1 + x5
|S7 | = 2 2 = 7
Entonces:
con
t=3
S16 = 1 + x8 + x9
Denicin:
Polinomio Chequeador
H(x) =
1 + xn
G(x)
Propiedad (ejercicio)
6. P-NP
Denicin: un problema de decisin es un problema cuyas nicas respuestas posibles
son S o No (alternativamente 1 0).
Ejemplos
nN
Primo: dado un
es
tiene
es
un matching perfecto?
primo?
(G) k ?
: I {S,No}
S = {x I : (x) = S}
Se puede pensar que un problema es un par (I,S) con
En general, dado
xI
S I.
|x|.
I = {G : G es un grafo nito},
N |n| = dlog(n)e = #bits de n
x,
x.
Lo denotaremos
Por ejemplo si
Si I =
podemos tomar
|G| = #de
vrtices.
6.1. La clase P
P = clase de problemas de decisin tal que existe un polinomio
tal que
xI
en tiempo
Resolver es que:
O bien en la forma {I,S}
A(x) =
A(x) =
P (x),
y un algoritmo
O(P (x))
Si (x) = S
N o Si (x) = No
S
xS
Ejemplos:
2 color P
Matching bipartito: como Matching con instancia reducida a grafos bipartitos.
31
6.2. La Clase NP
NP
P N P ).
P.
90
Formalmente
(I, S) N P j, t Ix J
1.
P (x)
tales que:
(Ix J, T ) P
2. Dado
xI
vale
x S y J :
(x, y) T
|y| P (|x|)
6.3. Co-NP
Denicin:
Ejemplo: Primo
Co-NP
Complemento de
Es
b
(x)
=
N o Si (x) = S
S
Si (x) = N o
k-color
P.
(I, S) (I, I S)
NP k
O(m) = O(n2 )
Observacin:
Claramente
P NP .
6.4. SAT
Un problema importate: SAT
Una variable booleana
1.
x1 , . . . , x n .
(x = 1 x).
x1 x2 . . .
32
, es una conjuncin ( ) de
clasulas.
Ejemplo:
33
dada
en CNF:
91
SAT N P
DN F SAT P : 0 s
Xor SAT P : 0 s
de
de
0 s
0 s
(Xors)
SAT P P = N P
6.4.1.
Reduccin Polinomial
Polinomio
1. Calcular
2.
= (I, S) , (J, T )
se reduce a ( P ) si:
P (x) y una funcin : I J :
(x)
es
O(P (x))
x S (x)
i.e.
(x)
(x) =
S
es polinomial en
x.
((x)) =S.
Ejemplos sencillos:
Tonto y no del todo correcto:
Dada
ax2 + bx + c = 0
Dado
tR
Entonces:
es
tiene solucin
R?
t 0?
tomando
t = b2 4 a c
Otro ejemplo
Dado
P
6.4.2.
Dada A
tQ
tomando
Qnn
es
A es invertible?
t 6= 0?
Det(A) = t.
k color P SAT
k color P SAT
Prueba: Dado un grafo G debemos construir polinomialmente una B CN F
(G) k B es satisfactible i.e. un asignamiento que la vuelve verdadera.
Sea n = # vrtices de G.
Tomemos variables xi,j : 1 i n, 1 j k
Para i = 1, . . . , n deno Ci = xi,1 xi,2 . . . xi,k
Teorema:
C = C1 C2 . . . Cn
92
tal que:
Di =
xi,j xi,r
j, r
j<r
j, r = 1, . . . , k
D = D1 . . . Dn
Fi,j,l = xi,j xl,j
k
n
V
V
Fi,j,l
F =
j=1
i, l = 1
il E
Sea
Tomo
Ahora hay que probar que
B =C DF
es
O(n + m)
(G) k B es satisfactible.
k colores de G.
xi,j x
bi,j {0, 1}
Denimos
x
bi,j =
1 Si C(i) = j
0 c.c.
b = B(b
B
xi,j ) = 1
Todo vrtice tiene un color
i j k : C(i) = j
i j k : x
bi,j = 1
i : x
b x
bi,2 . . . x
bi,k = 1
{z
}
| i,1
ci
C
b=C
b1 . . . C
bn = 1
C
Todo vrtice tiene un slo color
i ! j k : C(i) = j
i @j 6= r : C(i) = j, C(i) = r
i, j < r : C(i) 6= j C(i) 6= r
x
bi,j = 0 x
bi,r = 0
o lo mismo: x
bi,j = 1 x
bi,r = 1
b =1
x
bi,j x
bi,r = 1 i, j < r D
Finalmente como el coloreo es propio
93
il E, j : x
bi,j x
bi,r = 1 Fb = 1
b=C
bD
b Fb = 1
B
Fin
()
N P P SAT
SAT P P = N P
6.5. NP Completo
Denicin: un problema
1.
NP
2.
N P P
es
NP completo si:
2000.
La clave es lo siguiente: Si
Entonces es NP Completo.
NP
Completo y
P ,
P : N P
polinomios.
A continuacin daremos algunos ejemplos NP completos.
6.5.1.
3-SAT es NP completo
3-SAT es como SAT pero en cada clasula debe haber exactamente 3 literales distintos.
3-SAT es NP completo
Prueba: probaremos que SAT
Es decir dada
b
B
sea satisfactible
B = D1 . . . Dm : Di clasulas.
B = x1 , . . . , x n
clasula D arbitraria, deniremos una conjuncin
B0
lo es.
Supongamos que
Variables de
Dada una
de clasulas E (con ms
B 0 = E1 E2 . . . Em
donde
94
Ei
corresponde a
Di .
Cada
Ei
Construccin:
Si D tiene 3 literales: E =D
Si D tiene 2 literales :
D = L1 L2
y 6 {x1 , . . . , xn }.
E = (L1 L2 y) (L1 L2 y)
c1 L
c2 = 1 (L
c1 L
c2 yb) (L
c1 L
c2 yb) = 1
Claramente si L
Tomamos
Viceversa:
Si
{xb1 , . . . , x
cn , yb}
Tal que
c1 L
c2 yb) (L
c1 L
c2 yb) = 1
(L
Entonces si:
c1 L
c2 = 1
yb = 0 L
c1 L
c2 = 1
yb = 1 L
Observacin: probamos un poco ms de lo que dijimos
~
~
~
~
~
x
b:D x
b = 1 yb : E x
b, yb = 1
Esto valdr en todos los casos.
D tiene un literal: i.e.
D=L
Tomamos:
E = (L y1 y2 ) (L y1 y2 ) (L y1 y2 ) (L y1 y2 )
Y es casi obvio que:
b=1E
b=1
L
D
tiene
i.e. cualquier
yb1 , yb2
literales.
D = L1 L2 . . . Lk
Entonces tomamos
k3
variables nuevas:
y1 , y2 , . . . , yk3
polinomial en
K >> 4:
E = (L1 L2 y1 )(y1 y2 L3 )(y2 y3 L4 ). . .(y k4 yk3 Lk2 )(y k3 Lk1 Lk )
95
#clasulas = k 2 lineal en k .
b = 1 j : L
bj = 1
D
Supongamos ahora
Denamos:
b =(L
b1 L
b2 yb1 ) (b
b ) . . . (b
bj ) (b
bj+1 )
E
y 1 yb2 L
y j2 ybj1 L
y j1 b
yj L
|{z}
|{z} 3
|{z}
|{z}
=1
=1
HIP =1
=1
bk1 L
bk )
. . . (b
y k3 L
|{z}
=1
b=1
E
Veamos la vuelta: supongamos que
b=1
E
Como
b=1
E
Tenemos:
yb1 = 1
yb2 = 1
yb1 yb2 = 1
yb3 = 1
. . . ybk4 = 1
yb2 yb3 = 1
y
b
=
1
k3
Absurdo
yb3 yb4 = 1
. . . ybk4 ybk3 = 1
b
y k3 = 1
6.5.2.
3-color es NP completo
P 3-color.
B(3-CN F ) G
Sea B = D1 . . . Dm con variables {x1 , . . . , xn }.
Dj = L1,j L2,j L3,j
Vamos a construir G = (V, E) con |V |, |E| polinomiales
Prueba: veremos que 3-SAT
Es decir necesitamos :
en
96
n, m
6.5.3.
Ejemplo 3-SAT
P 3-color
B = (x1 x2 x3 ) (x2 x3 x4 )
efefefnt YXP?PXYXYXYY
efefefefefnefnefnnn ??PPPXPXYPXYXYXYXYXYXYXYYYYY
e
e
e
f
e
e
f
e
?? PP
XX YY
e ff
nnn
eefefefefefeffff
?? PPPP XXXXXYXYXYXYXYXYYYYYYY
e
n
e
e
n
e
e
e
f
n
e
XXXXX YYYYYYY
f
e
f
P
n
e
?
f
e
e
P
f
n
P
XXXX
YYYYY
fff
eeeee
nn _
_ x4
_ x3
x 4
x3
x1, eee _ x1 fff
x2
x2 A
3
u
,,
A
3
u
A
u
33
AA
,,
uu
33
AA
uu
u
,,
'
AA
33
uu
u
,,
A
33
bb
efhmbefhm s
AA uuu
bb
e
bb
f
e
bb
e
f
h
bb
e
f
bb
e
h
bb
f
e
,,
33 bbbbbbbbbbbb eeeeefefefhfhfhhhmmm ||
uAu A
,,
uu Abb
bbbbbbbbbbbb33 eeeeeefefefefefefhfhfhfhfhhhmmmmm |||
bb
A
u
bb
bb
u
A
bb
ff
hh
mm
u bb
bbbb
,,
eeee3
AA
||
eeeefefeffffffffhhhhhh mmmmm
bbbbbbbbbbbbuuu
e
bb
e
A
bb
||
e
bb
e
,,
bb
e
e
A
u
|
e1,2
e
f
e
m
e
f
2,2
e
m
f
u
e
AAfffff
||
,,
eeeeee
uu
mmm
||
ueueeeeeee ffffffffffAAA
mmm
e
|
m
e
u
,,
e
m
e
e
|
AA
ff
ee u
|
mmm
,
eeeeee uuuu fffffffff
A
||
mmm
eeeeee
f
m
e
|
f
e
f
e
m
e
f
e
f
e
e2,1
e1,1
e2,3
e1,3
i1,1
{{
{{
{
{
{{
i1,2
CC
CC
CC
CC
_ i1,3
{{
{{
{
{
{{
i2,1 _
i2,2
CC
CC
CC
CC
i2,3
En general:
Donde:
2 + 2n + 6m vrtices, polinomial.
1 + 3n + 3m + 9m = 1 + 3n + 12m
Hay que ver
(Como
tiene tringulos
lados, polinomial.
satisfactible
(G) 3
(G) 3 (G) = 3)
97
1jm
k = 1, 2, 3
Veamos
primero:
Sea C un coloreo de G de
variables tal que:
b) = 1
B(~x
Denamos:
xbk =
Si
C(xk ) = C(s)
Sino
b) = 1
B(~x
j
1
0
basta ver:
b1,j L
b2,j L
b3,j = 1
j L
Tj es un tringulo entonces
b) = 1
Dj (~x
Entonces:
k : C(ij,k ) = C(t)
ij,k ej,k E C(ej,k ) 6= C(ij,k ) = C(t)
sej,k E C(ej,k ) 6= C(s)
st E C(s) 6= C(t)
C(ej,k ) =
tercer color
Como
tLk,j
Por denicin
bk,j = 1
L
Fin
tercer color
C(Lk,j ) = C(s)
b : B(~x
b) = 1 debemos construir un coloreo
Dado un asignamiento de valores ~x
con 3 colores.
Denimos: C(s) = 1 , C(t) = 2 st No Crea Problemas (NCP).
Coloreamos:
C(xi ) = xbi
xi xi
(N CP )
C(xi ) = 1 xbi
Adems:
txi , txi
(N CP )
Como
Coloreamos:
Como
Y coloreo:
C(ej,k ) = 0
b
Lk,j = 1 C(Lk,j ) = 1
C(ej,r ) = 2 : r 6= k
C(Lr,j ) {0, 1}
Lk,j ej,k
(N CP )
ej,r Lr,j
98
(N CP )
propio
Como:
C(ej,k ) = 0
C(ej,r ) = 2 sej,k
C(s) = 1
y los
sej,r
(N CP )
Finalmente coloreamos:
C(ij,k ) = 2
C(ej,k ) = 0
Y coloreamos:
C(ij,r ) = 0
ej,k ij,k
(N CP )
1 : r 6= k
ij,r ej,r
|{z} |{z}
(N CP )
0 1 =2
Finalmente el tringulo:
ij,1
Tiene un vrtice de color
}}
}}
}
}
}}
ij,2
AA
AA
AA
AA
_ ij,3
99
(N CP )
6.5.4.
2-SAT
D[j] =
Registros
x[0]
Representan
x
b1
, ...
, x[n-1]
, ...
; x[n]
x
bn
b1
x
(L, R).
, ...,
x[2n-1]
, ...,
bn
x
2-SAT
1
2
3
4
5
6
7
8
9
V ariables : x0 , . . . , xn1
SET EAR(v) : //M ACRO
X[v] = 1;
X[2n 1 v] = 0;
Q = Q P [v] P [2n 1 v]
N = { 0 , . . . , n 1} ;
M = { 0 , . . . , m1} ;
Q = ;
// Variables no seteadas
// Disjunciones no s a t i s f e c h a s
for
( i
= 0
i < 2n ,
10
x[ i ]
11
P[ i ]
= {j
i ++){
M :
aparece
en D[ j ] } ;
12 }
13
14
for ( j
while (
15
16
17
= 0
j < m,
j ++){
Sat [ j ]
18
L ,R:
if
19
D( j ) = {L , R } ;
( ( x [ L ] == 1 )
20
Sat [ j ]
21
23
if (
| | ( x [ R ] == 1 ) ) {
= 1;
Remover
} else
22
de Q ;
x [ R] < 0 ) {
//Pero no de M
SETEAR(R) ;
24
} else
25
26
if (x [
i ] <0) {
SETEAR( L )
27
28
t N, s M {
29
30
SAT( s ) = 0 ;
31
x[ t ]
= x [ 2 n1 t ]
32
33
Q =
34
SETEAR[ 2 n1 i ]
1;
} else
35
return
36
37
38
i N, j : SAT [j] = 1 {
39
40
= 0;
6= )
Tomar i N, SETEAR( i ) ;
while (Q 6= ) {
E l e j i r j Q;
N
Borrar
"NO SAT"
de P [ i ]
y P [ 2 n1 i ]
41 }
42 R e t u r n
"SAT"
100
6.5.5.
HORN-SAT
HORN-SAT es como CNF-SAT pero donde cada clasula tiene a lo sumo un literal
xi positivo y xi
Teorema: HORN -SAT P
positivo :
negativo
Prueba:
x1 x2 . . . xk xk+1 x1 x2 . . . xk xk+1
x1 x2 . . . xk xk+1
El algoritmo empieza seteando todas las variables a
f alse.
x x2 . . . xk xk+1
{z
}
|1
|{z}
f alse
van a ser
true
f alse
Las de la forma:
x1 x2 . . . xk x1 x2 . . . xk f alse
x1 x2 . . . xk f alse
|
{z
}
f alse
true
El nico problema son las disjunciones de la forma:
x true |{z}
x
f alse
f alse
Si hay una de esas, listo.
Si hay, seteamos estas variables a
true todas
x1 . . . xk xk+1
donde x1 , . . . , xk sean true.
true
(permanentemente).
las variables
xk+1
x1 x2 . . . xk
xi true
Ejemplo:
(x y z w) (x y) (z w x) (x z) (y z) y
yb = 1 zb = 1 x
b = 0, zb = 0
Pero supongamos que siguiera con . . . (y z U ) (u x)
b=1x
yb = 1 zb = 1 U
b=0
No se puede resolver x y
101
34
Cada individuo, tiene cromosomas, que forman cadenas de ADN, se dividen en Genes
que codican un aspecto del individo, por ejemplo el color de los ojos.
El conjunto de cromosomas se llama genotipo y las caractersticas inducidas en el
individuo por la accin del genotipo y el medio ambiente es el fenotipo.
Tambin podemos distinguir entre especies haploides y diploides, (humanos).
Los cromosomas vienen en pares con genes dominantes y recesivos.
Haploide: los cromosomas no estn emparejados.
En la mayora de los algoritmos genticos los individuos son haploides y con un solo
cromosoma.
A veces a los individuos se les llama cromosomas. Pero a veces el individuo tiene algo
ms que cromosomas.
Una parte importante de un AG es codicar el problema con cromosomas.
Un cromosoma ser una cadena de smbolos, usualmente bits o un vector de nmeros.
Ejemplos:
1. Maximizar una funcin en
2. Hallar un recorrido de
35
(TSP)
R3
34 AG.
102
36
.
es la funcin.
2. Sera la
A veces como en TSP no toda cadena representa una solucin, por ejemplo puede ser
que no haya camino entre las dos ciudades.
Generalmente se soluciona creativamente, por ejemplo en TSP, podemos suponer que
hay ruta entre
Algo
Algo
7.4.1.
Seleccin
mutacin.
0, 001
7.5. Ejemplo de un AG
Problema: dado un nmero natural escribirlo como suma, resta, producto y divisin
de los dgitos:
0, 1, . . . , 9
36 Adaptabilidad.
103
7.5.1.
Smbolo
n
o
A = 0, . . . , 9, +, , / pero
N .
N )
y suponer
0000 0
0001 1
1011
1100
.
.
.
1101 /
1110 N ada
1111 N ada
1001 9
1010 +
El cromosoma ser el genotipo, el fenotipo en cambio tiene que ser una cadena:
0 Smbolo N Smbolo . . . que adems no tenga /0.
Genotipo F enotipo
Si es 14 15 lo descartamos.
Si es
Si es un Smbolo y se requiere un
lo descartamos.
Sino se usa.
Si tenemos un /0 cambiar 0 por 1.
7.5.2.
Fitness ser algo como :
7.5.3.
1
F enotipo31
si
si
P
F enotipo 6= 31
P
F enotipo = 31
b1 b2 b3 . . . b400
Progenitor 2: bb1 bb2 bb3 . . . b
b400
Elijo al azar un nmero x : 0 < x < 400
Por ejemplo x = 127
Ospring 1: b1 b2 b3 . . . b127b
b128 . . . . . . bb400
b
b
b
b
Ospring 2: b1 b2 b3 . . . b127 b128 . . . . . . b400
Progenitor 1:
7.5.4.
y obtengo:
p.
104
7.5.5.
7.5.6.
Operadores de Seleccin
Fi
calculada.
Fi .
Sea
F =
Fi
i=1
la tness promedio.
Fi
= # esperado de veces que el individuo
F
acuerdo a su tness.
Sea
Ei =
i,
Fi
Ei = n P
n
Fj
j=1
Queremos seleccionar
Ei
veces.
El primer mtodo propuesto por Holland, hace esto lo ms cercano posible se llama
Stochastic Sampling With Replaciment (SSWR), o
i:
Ruleta.
y se toma el primer
Ej > A
j=1
Ejemplo: 6 individuos.
Fitnesses
F1
F2
F3
F4
F5
F6
= 17,118
= 9,51
= 36,138
= 22,824
6
P
Fj = 190, 200
j=1
= 91,296
= 13,314
r (0, 1)
y calculo
r 190, 2.
Para entender mejor otros conceptos, haremos el clculo sin embargo con los
E1
E2
Fi
E3
6
Ei =
E4
190, 2
5
E6
105
= 0, 54
= 0, 30
= 1, 14
= 0, 72
= 2, 88
= 0, 42
Ei0 s
# al azar: entre 0 y 1:
0, 15 0, 7 0, 22 0, 91 0, 46 0, 63
Multiplico por n = 6
0, 90 4, 2 1, 32 5, 46 2,
76 3, 78
1 0, 54
2 0, 84
i
P
3 1, 98
1 3
Ahora calculo:
Ej
4 2, 70
j=1
5 5, 58 2 , 4 , 5 , 6
66
Ruleta:
Bias.
Spread.
Eciencia computacional.
Fi
y el valor esperado de veces que el individuo ser selecFi
cionado con el mtodo propuesto.
Bias: es la diferencia entre
Bias(Ruleta) = 0.
Spread: rango de ospring que un individuo puede tener:
randoms.
r, r + 1, r + 2, . . . , r + (n 1).
En nuestro caso:
r = 0, 15
0,15
1,15
2,15
3,15
4,15
5,15
I1
I3
I4
I5
I5
I5
11
20
31
41
53
60
(0, 54)
(0, 34)
(1, 14)
(0, 72)
(2, 88)
(0, 42)
Bias = 0.
Pero
Y un factor extra que tengo que elegir a quin reproduzco con quin.
106
7.5.7.
bEi c veces.
n
P
n bEi c posibles
Se toma
Los
i=1
Por ejemplo se usan los restos:
Ri = Ei bEi c
Ejemplo:
R1
R2
R3
R4
R5
R6
= 0, 54
= 0, 30
= 0, 14
= 0, 72
= 0, 88
= 0, 42
0, 45
I1
2, 1
I5
n=3
0, 6
I2
Orden
7.5.8.
0,54
0,54
0,30
0,84
0,14
0,98
0,72
1,7
0,88
2,58
0,42
11
21
31
40
53
60
BIAS = 0
cuando hay gran disparidad de Fitness, un grupo de individuos con alta tness relativa,
cope la poblacin.
Impidiendo una bsqueda exhaustiva, de todo el espacio de bsqueda, por otro lado,
luego de que se estabilice la poblacin es difcil para un individuo con mayor Fitness
inuenciar la bsqueda produciendo un estancamiento.
Por esto se han propuesto alternativas.
107
7.5.9.
Sigma Scaling
Se dene
ci =
E
Ventaja: al principio,
1+
Ei
en forma distinta
Fi F
2
=
6 0
:=
=0
ci
E
Fi .
0.
= 30, 627
ci
E
Primero:
ci
E
Ej
j=1
c1
E
c2
E
c3
E
c4
E
c5
E
c6
E
= 0, 762
= 0, 638
= 1, 072
= 0, 855
= 1, 973
= 0, 700
0, 762
1, 400
2, 472
3, 327
5, 3
6
Aplico Ruleta
Elijo n = 6, nmeros al azar
0, 15 0, 7 0, 22 0, 91 0, 46 0, 63
Multiplico por n = 6
0, 90 4, 2 1, 32 5, 46 2, 76 3, 78
I2
I5
I2
I6
I4
I5
Obtengo algo ms balanceado.
Si hubiera aplicado SUS: con
r = 0, 15
0, 15 1, 15 2, 15 3, 15 4, 15 5, 15
I1
I2
I3
I4
I5
I5
Roletamiento
Fi
eT
P rj : T temperatura, inicialmente alta y luego se enfra
eT
Otros mtodos no usan Fitness Proporcion
bi =
E
Fi
108
Mtodos de rango: se usa la Fitness para ordenar la poblacin y luego se usa una
medida que dependa slo de la posicin en esa poblacin.
Lineal Ranking:
LR(pos) = (2 SP ) + 2(SP 1)
SP parmetro en
pos1
n1
[1, 2]
Ei
Ei = LR(P os(i))
SP = 1 LR(pos)
= 1pos,
SP = 2 LR(pos) = 2 pos1
n1
SP (1; 2), por ejemplo SP = 1, 2
Por ejemplo
no hay presin.
LR(pos) = 0, 8 + 0, 4
pos 1
n1
Tomando
F2
< F6
< F1
< F4
< F3
< F5
SP = 1, 2:
E1
E2
E3
E4
E5
E6
= LR(3) = 0, 96 : 3 es la posicin
= LR(1) = 0, 8
= LR(5) = 1, 12
= LR(4) = 1, 04
= LR(6) = 1, 2
= LR(2) = 0, 8 + 0,4
= 0, 88
5
Seleccin Torneo
Seleccionar
terminsticamente o probabilsticamente.
7.5.10.
Crossover
SinglePoint
TwoPointCroospoint
Mscara Binaria
Supongo que tengo esta mscara, generada al azar: 1,0,0,1,1,0,1,0,1,1.
P1 =
P2 =
109
Obtengo:
O1 = 2, 2, 3, 2, 5, 3, 1, 8, 5, 8
O2 = 4, 9, 7, 6, 6, 3, 9, 6, 0, 9
Partial Map Crossover
Qu pasa con codicacin del problema que depende de la posicin?
P1 = BIGAECJF DH
P2 = DJBF ECIGAH
Corta en dos lugares:
O1 = BIG|JECA|F DH
O2 = DJB|F IEC|GAH
Tambin se puede hacer con mscaras. Order Based
Cyclic Crossover
VERRRRRRRRRRRRRRRR
7.5.11.
Mutacin
p 0.
Strings de enteros o reales: tambin se puede hacer algo as, pero en el caso en que
haya fronteras se hace otra cosa.
Por ejemplo supongamos que la frontera es:
|Ii | 10
es
I = 0, 5|7, 8|5
Ie = 0, 5|10|5
O bien:
7.5.12.
5
Ie = 0, 5 10+7,8
2
110
3. Steady State
4. Migraciones
Inundacin: mato a los peores cada tanto, se usa mucho con restricciones muy cortas,
cada tanto tengo una subpoblacin no vlida pero la penalizo, pero es muy buena, luego
la mato.
37
Referencias
[1] Biggs: Matematica Discreta
[2] Roberts, Applied Combinatorics
[3] Tarjan: Data Structures and Network Flows
[4] BFS:
http://en.wikipedia.org/wiki/Breadth-first_search
[5] DFS:
http://en.wikipedia.org/wiki/Depth-first_search
http://www.essex.ac.uk/linguistics/external/clmt/latex4ling/trees/
pstrees/
[7] Cdigo C en latex:
http://stackoverflow.com/questions/586572/make-code-in-latex-look-nice
111