“Backtracking” et divers
heuristiques
Michel Van Caneghem
Janvier 2003
UE3 : Algorithme et Complexité #7 – c
2003 – Michel Van Caneghem
Le “backtracking”
C’est la méthode d’énumération standard quand toutes les
autres méthodes échouent.
Programmation non-déterministe : “Nondeterministic Al-
gorithms”, R. Flyod, JACM Oct 1967. Ainsi que “Backtra-
cking programming”, Golomb and Baumert, JACM 1965.
Pour réaliser la programmation non-déterministe, il faut
se souvenir de tous les choix, qui ont été faits pour reve-
nir en cas d’échec sur des choix précédents.
UE3 : Algorithme et Complexité #7 – c
2003 – Michel Van Caneghem 1
Le problème des nreines
Il s’agit de mettre nreines sur un échiquier de taille n×n
sans qu’aucune reine ne soit en prise (4n1000000).
UE3 : Algorithme et Complexité #7 – c
2003 – Michel Van Caneghem 2
Un programme non déterministe
ligne = diagM = diagD = false;
for (int col = 1; col <= nReines; col++) {
CHOISIR(lig);
if (lig[lig] || diagM[lig+col] ||
diagD[lig-col]) ECHEC;
ligne[lig] = true;
diagM[lig+col] = diagD[lig-col] = true;
if (col == nReines) SUCCES;
}
UE3 : Algorithme et Complexité #7 – c
2003 – Michel Van Caneghem 3
Le programme déterministe récursif
ligne = diagM = diagD = false; avancer(1);
void avancer(int col) {
if (col == nReines+1) {ecrire(); return;}
for (int lig = 1; lig <= nReines; lig++) {
if (lig[lig] || diagM[lig+col] ||
diagD[lig-col]) continue;
ligne[lig] = true;
diagM[lig+col] = diagD[lig-col] = true;
avancer(col + 1) ;
ligne[lig] = false;
diagM[lig+col] = diagD[lig-col] = false;
}
}
UE3 : Algorithme et Complexité #7 – c
2003 – Michel Van Caneghem 4
1 / 21 100%