Avec une telle représentation d'un graphe,
la programmation du BFS se réalisera avec une fonction bfs(graphe, sommet de départ)
dont les variables
seront les suivantes :
Déroulement détaillé de la fonction bfs()
 |
P = {'b' : None} Q = ['b'] Découverts(gris ou noirs) = ['b'] Fermés(noirs) = [] |
 |
P = {'b': None , 'a': 'b' ,
'd': 'b' , 'e': 'b'} Q = ['b', 'a', 'd', 'e'] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e'] Fermés(noirs) = [] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b'} Q = ['a', 'd', 'e'] Découverts(gris ou noirs) = ['a', 'd', 'e'] Fermés(noirs) = ['b'] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b' , 'c':'a'} Q = ['d', 'e', 'c'] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e', 'c'] Fermés(noirs) = ['b', 'a'] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b' , 'c':'a'} Q = ['e', 'c'] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e', 'c'] Fermés(noirs) = ['b', 'a', 'd'] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b' , 'c':'a' , 'f':'e' , 'g':'e'} Q = ['c', 'f', 'g'] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e', 'c', 'f', 'g'] Fermés(noirs) = ['b', 'a', 'd', 'e'] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b' , 'c':'a' , 'f':'e' , 'g':'e'} Q = ['f', 'g'] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e', 'c', 'f', 'g'] Fermés(noirs) = ['b', 'a', 'd', 'e', 'c'] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b' , 'c':'a' , 'f':'e' , 'g':'e'} Q = ['g'] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e', 'c', 'f', 'g'] Fermés(noirs) = ['b', 'a', 'd', 'e', 'c', 'f'] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b' , 'c':'a , 'f':'e' , 'g':'e' , 'h':'g'} Q = ['h'] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e', 'c', 'f', 'g', 'h'] Fermés(noirs) = ['b', 'a', 'd', 'e', 'c', 'f', 'g'] |
 |
P = {'b':None , 'a':'b' , 'd':'b' , 'e':'b' , 'c':'a' , 'f':'e' , 'g':'e' , 'h':'g'} Q = [] Découverts(gris ou noirs) = ['b', 'a', 'd', 'e', 'c', 'f', 'g', 'h'] Fermés(noirs) = ['b', 'a', 'd', 'e', 'c', 'f', 'g', 'h'] |
L'arborescence associée au parcours peut donc être modélisée de la façon suivante :
L'ordre de parcours est ligne après ligne et de gauche à droite pour chacune des lignes.
class Maillon:
def __init__(self, valeur, precedent=None, suivant=None):
self.valeur = valeur
self.precedent = precedent
self.suivant = suivant
class File:
def __init__(self):
self.longueur = 0
self.debut = None
self.fin = None
def enfiler(self, valeur):
if self.longueur == 0:
self.debut = self.fin = Maillon(valeur)
else:
self.fin = Maillon(valeur, self.fin)
self.fin.precedent.suivant = self.fin
self.longueur += 1
def defiler(self):
if self.longueur > 0:
valeur = self.debut.valeur
if self.longueur > 1:
self.debut = self.debut.suivant
self.debut.precedent = None
else:
self.debut = self.fin = None
self.longueur -= 1
return valeur
def estVide(self):
return self.longueur == 0
def bfs(G,s) :
P = {s :None}
Q = File()
Q.enfiler(s)
while not(Q.estVide()) :
u = Q.defiler()
for v in G[u] :
if v in P : continue
P[v]=u
Q.enfiler(v)
return P
G = dict()
G['a'] = ['b','c']
G['b'] = ['a','d','e']
G['c'] = ['a','d']
G['d'] = ['b','c','e']
G['e'] = ['b','d','f','g']
G['f'] = ['e','g']
G['g'] = ['e','f','h']
G['h'] = ['g']
P = bfs(G,'b')
print(P)