Documente Academic
Documente Profesional
Documente Cultură
Algoritmos de retroceso
Retroceso o Backtracking
Retroceso o Backtracking
Backtracking:
Caractersticas
Caractersticas
Mtodo general.
Inicio
x1=0
x1=0
x2=0
x1=0
x2=0
x3=0
x1=1
x1=0
x2=1
x1=0
x2=0
x3=1
x1=0
x2=1
x3=0
x1=1
x2=0
x1=0
x2=1
x3=1
x1=1
x2=0
x3=0
x1=1
x2=1
x1=1
x2=0
x3=1
x1=1
x2=1
x3=0
x1=1
x2=1
x3=1
Caractersticas
Inicio
x1=0
x1=0
x2=0
x1=0
x2=0
x3=0
x1=1
x1=0
x2=1
x1=0
x2=0
x3=1
x1=0
x2=1
x3=0
x1=1
x2=0
x1=0
x2=1
x3=1
x1=1
x2=0
x3=0
x1=1
x2=1
x1=1
x2=0
x3=1
x1=1
x2=1
x3=0
x1=1
x2=1
x3=1
Caractersticas
Representacin simplificada del rbol.
x1
x2
2
0
x3
9
1
10
1
13
11
12
14
15
Ejemplo
Ejemplo Posibilidad 1
En cada nivel i decidir si el elemento i est o no en la
solucin. Representacin de la solucin: (x1, x2, x3),
donde xi = (0, 1).
rbol de
soluciones 1
(13)
k=2
(11)
k=3
(7)
10
13
11
12
14
15
11
k=1
18
13
20
24
31
Sumas
totales
Ejemplo Posibilidad 1
Ejemplo Posibilidad 2
En cada nivel i decidir qu elemento se aade (1, 2 3) en
la solucin. Representacin de la solucin: (s1, , s3),
donde m n y si {1, 2, 3}.
0
rbol de
soluciones 2
13
24
3
31
11
6
3
20
18
k=1
k=2
7
k=3
Ejemplo Posibilidad 2
binarios.
rboles
n-arios.
rboles
permutacionales.
rboles
combinatorios.
rboles binarios
s= (x1, x2, ..., xn), con xi {0, 1}
x1
x2
5
1
rboles k-arios
s= (x1, x2, ..., xn), con xi {1,..,k}
1
x1
x2
2
6
10
3
2
8
11
12
13
rboles permutacionales
s = (x1, x2, ..., xn), con xi {1,..,n} y xi xj
1
1
2
2
6
3
3
3
5
2
x1
7
3
11
9
1
12
14
2
10
x3
1
13
x2
15
rboles combinatorios
s= (x1, x2, ..., xm), con mn, xi {1,..,n} y xi < xi+1
1
1
2
2
3
6
3
5
x1
8
x2
7
3
4
x3
Mtodo general.
Mtodo general
Mtodo Clase.Backtracking (var s: TuplaSolucin)
nivel 1
s sINICIAL
fin falso
Hacer
Generar (nivel, s)
Si Solucin (nivel, s) entonces
fin verdadero
Sino Si Criterio (nivel, s) entonces
nivel nivel + 1
Sino Mientras NO MasHermanos (nivel, s) hacer
Retroceder (nivel, s)
fMientras
fSi
fSi
Mientras fin = falso
fMetodo
Mtodo general
Variables:
Funciones:
Mtodo general
Mtodo general
m1 m2 mn
nodos en el nivel 1
nodos en el nivel 2
nodos en el nivel 3
Conclusiones
Variables:
Representacin de la solucin con un rbol binario.
s: array [1..n] de {-1, 0, 1}
s[i] = 0 el nmero i-simo no se utiliza
s[i] = 1 el nmero i-simo s se utiliza
s[i] = -1 valor de inicializacin (nmero i-simo
no estudiado)
sINICIAL: (-1, -1, ..., -1)
fin: Valdr verdadero cuando se haya encontrado
solucin.
tact: Suma acumulada hasta ahora (inicialmente 0).
Generar (nivel, s)
s[nivel] s[nivel] + 1
si s[nivel] =1 entonces tact:= tact + tnivel
Solucin (nivel, s)
devolver (nivel = n) Y (tact = P)
Criterio (nivel, s)
devolver (nivel<n) Y (tactP)
MasHermanos (nivel, s)
devolver s[nivel] < 1
Retroceder (nivel, s)
tact:= tact tnivel*s[nivel]
s[nivel]:= -1
nivel:= nivel 1
Formulacin matemtica:
i=1..n
Ejemplo: n = 4; M = 7
b = (2, 3, 4, 5)
p = (1, 2, 3, 4)
4 kg
3 kg
2 kg
7 Kg.
PVP 5
PVP 4
PVP 3
1 kg PVP 2
Ejemplo: n = 2; M = 100
b = (2, 190)
p = (1, 100)
100 kg
100 Kg.
1 kg
PVP 2
PVP 190
xi = 0 No se coge el objeto i
xi = 1 S se coge el objeto i
xi = -1 Objeto i no estudiado
En cada nivel i se prueba
0
la posibilidad de incluir
2
o no el objeto i
0
1
Las soluciones
estn en nivel n
3
6
0
4
1
5
0
7
9
0
0
11
x2
10
1
x1
13
1
12
0
14
x3
15
2
2
3
6
3
5
x1
8
x2
3
4
x3
Solucin (nivel, s)
Criterio (nivel, s)
MasHermanos (nivel, s)
devolver s[nivel] < 1
Retroceder (nivel, s)
pact pact p[nivel]*s[nivel]
bact bact b[nivel]*s[nivel]
s[nivel] -1
nivel nivel 1
si s[nivel] = 1 entonces
pact pact + p[nivel]
bact bact + b[nivel]
finsi
Ejemplo: n = 4; M = 7;
b = (2, 3, 4, 5)
2
p = (1, 2, 3, 4)
0
3
0
4
0 1
5
pact: 0 4
bact: 0 5
7
9
12
15
x1
1
17
10
18
19
1 0
16
9
12
20
x2
1
25
1
14
1 0
13
11
1 0
0
8
0
22
1 0
21
23
26
24
8
11
29
1 0
27
x3
0
28
7
10
1
30
x4
31
10
14