def setUp(self): # c # / # a # \ # d - b self.g = Graphe(['a', 'b', 'c', 'd'], [('a', 'c', 1), ('a', 'd', 1), ('b', 'd', 1)])
class TestGraphe(TestCase): def setUp(self): # c # / # a # \ # d - b self.g = Graphe(['a', 'b', 'c', 'd'], [('a', 'c', 1), ('a', 'd', 1), ('b', 'd', 1)]) def test_voisins(self): self.assertEqual(sorted(self.g.voisins('a')), sorted([('c', 1), ('d', 1)])) self.assertEqual(sorted(self.g.voisins('d')), sorted([('a', 1), ('b', 1)]))
def __init__(self, N, n, e, Pm, Pc): self.listGraph = [] print("construction") for i in range(N): print i #Graphe(self,ID, typeG, nodes, edges) G = Graphe(i, n, e) self.listGraph.append(G) self.pm = Pm self.pc = Pc
def cycle(n): """Retourne un cycle à n sommet. ------------------- --- OBLIGATOIRE --- ------------------- :param n: Nombre de sommets, entier naturel >= 3 :Examples: >>> cycle(4) {4: 0--1 0--3 1--2 2--3} >>> cycle(5) {5: 0--1 0--4 1--2 2--3 3--4} """ g=Graphe(n, [(0,1),(0,n-1)]) for i in range(1,n-1): g.ajouter_arete(i,i+1) return g
def setUp(self): self.n = { 'a': Noeud(0, 0, 'a'), 'b': Noeud(0, 1, 'b'), 'c': Noeud(1, 0, 'c'), 'd': Noeud(-1, 0, 'd'), 'e': Noeud(0, -1, 'e'), } self.g1 = Graphe( [self.n['a'], self.n['b'], self.n['c'], self.n['d'], self.n['e']], [(self.n['a'], self.n['b'], 1), (self.n['a'], self.n['c'], 1), (self.n['a'], self.n['d'], 1)])
def graphe_complet(n): """Retourne un graphe complet à n sommet. ------------------- --- OBLIGATOIRE --- ------------------- :param n: Nombre de sommets, entier naturel :Examples: >>> graphe_complet(3) {3: 0--1 0--2 1--2} >>> graphe_complet(4) {4: 0--1 0--2 0--3 1--2 1--3 2--3} """ g=Graphe(n) for i in range(n): for j in range(n): if (i!=j) and (i<j): g.ajouter_arete(i,j) return g
def graphe_3(): """Retourne le graphe G3. 1 7 4 / \ / \ / \ 0 2 8 5 \ / \ / \ / 3 9 6 :Examples: >>> graphe_3() {10: 0--1 0--3 1--2 2--3 2--7 2--9 4--5 4--8 5--6 6--8 7--8 8--9} """ return Graphe(10, [ (0, 1), (1, 2), (2, 3), (3, 0), (2, 7), (7, 8), (8, 9), (9, 2), (8, 4), (4, 5), (5, 6), (6, 8)])
def setUp(self): self.n = { 'a': Noeud(0, 0, 'a'), 'b': Noeud(0, 2, 'b'), 'c': Noeud(2, 2, 'c'), 'd': Noeud(3, 4, 'd'), 'e': Noeud(4, 2, 'e'), 'f': Noeud(4, 4, 'f'), } self.g1 = Graphe([ self.n['a'], self.n['b'], self.n['c'], self.n['d'], self.n['e'], self.n['f'] ], [(self.n['a'], self.n['b'], self.n['a'].distance(self.n['b'])), (self.n['a'], self.n['c'], self.n['a'].distance(self.n['c'])), (self.n['b'], self.n['d'], self.n['b'].distance(self.n['d'])), (self.n['d'], self.n['f'], self.n['d'].distance(self.n['f'])), (self.n['c'], self.n['d'], self.n['c'].distance(self.n['d'])), (self.n['c'], self.n['e'], self.n['c'].distance(self.n['e'])), (self.n['e'], self.n['f'], self.n['e'].distance(self.n['f']))])
def graphe_1(): """Retourne le graphe G1. ------------------- --- OBLIGATOIRE --- ------------------- 4 / \ 0 --- 1 G1 | | 3 --- 2 :Examples: >>> graphe_1() {5: 0--1 0--3 0--4 1--2 1--4 2--3} """ g=Graphe(5,[ (0,1), (0,3), (0,4), (1,2), (1,4), (2,3) ]) return g
def graphe_2(): """Retourne le graphe G2. ------------------- --- OBLIGATOIRE --- ------------------- 2 3---/-\---1 \ 4 0 / G2 \ \ / / \ 6 / \ / 5 :Examples: >>> graphe_2() {7: 0--2 0--6 1--3 1--5 2--4 3--5 4--6} """ g=Graphe(7,[ (0,2), (0,6), (1,3), (1,5), (2,4), (3,5), (4,6) ]) return g
def creerGraphe(fichier): return Graphe(fichier)
def __init__(self, IdentifiantGraphe): Graphe.__init__(self, IdentifiantGraphe)
(villes['Fribourg'], villes['Bern'], villes['Fribourg'].distance(villes['Bern'])), (villes['Sion'], villes['Thun'], villes['Sion'].distance(villes['Thun'])), (villes['Neuchatel'], villes['Bern'], villes['Neuchatel'].distance(villes['Bern'])), (villes['Basel'], villes['Bern'], villes['Basel'].distance(villes['Bern'])), (villes['Zurich'], villes['Aarau'], villes['Zurich'].distance(villes['Aarau'])), (villes['Zurich'], villes['Luzern'], villes['Zurich'].distance(villes['Luzern'])), (villes['Bern'], villes['Aarau'], villes['Bern'].distance(villes['Aarau'])), (villes['Bern'], villes['Luzern'], villes['Bern'].distance(villes['Luzern'])), (villes['Luzern'], villes['Aarau'], villes['Luzern'].distance(villes['Aarau'])), (villes['St-Gallen'], villes['Zurich'], villes['St-Gallen'].distance(villes['Zurich'])), (villes['Thun'], villes['Bern'], villes['Thun'].distance(villes['Bern'])), (villes['Basel'], villes['Zurich'], villes['Basel'].distance(villes['Zurich'])), ] if __name__ == '__main__': graphe = Graphe(villes.values(), routes) print(graphe) print('Recherche DFS:') s = DFSSolver(graphe) iterations = s.solve(villes['Lausanne'], villes['Zurich']) print('Nombre d\'iterations: {}'.format(iterations)) ################################################# for (k, n) in villes.items(): n.etat = Noeud.NOT_VISITED print('Recherche BFS:')
from graphe import Graphe def parcours_en_profondeur(graphe, sommet): g.sommets["a"].visiter() g.arcs["ab"].choisir() g.sommets["b"].visiter() g.plot() g.arcs["bd"].choisir() g.sommets["d"].visiter() g.plot() def parcours_en_largeur(graphe, sommet): g.sommets["a"].visiter() g.arcs["ab"].choisir() g.sommets["b"].visiter() g.plot() g.arcs["ae"].choisir() g.sommets["e"].visiter() g.plot() if __name__ == "__main__": g = Graphe() g.graph_from_file("../maps/graphe4.txt") g.plot() #parcours_en_largeur(g,"a") #g.reset() #parcours_en_profondeur(g,"a")
(noeuds['G'], noeuds['L'], noeuds['G'].distance(noeuds['L'])), (noeuds['G'], noeuds['M'], noeuds['G'].distance(noeuds['M'])), (noeuds['I'], noeuds['J'], noeuds['I'].distance(noeuds['J'])), (noeuds['J'], noeuds['K'], noeuds['J'].distance(noeuds['K'])), (noeuds['J'], noeuds['N'], noeuds['J'].distance(noeuds['N'])), (noeuds['K'], noeuds['O'], noeuds['K'].distance(noeuds['O'])), (noeuds['K'], noeuds['P'], noeuds['K'].distance(noeuds['P'])), (noeuds['M'], noeuds['N'], noeuds['M'].distance(noeuds['N'])), (noeuds['N'], noeuds['O'], noeuds['N'].distance(noeuds['O'])), (noeuds['B'], noeuds['I'], noeuds['B'].distance(noeuds['I'])), (noeuds['B'], noeuds['F'], noeuds['B'].distance(noeuds['F'])), (noeuds['P'], noeuds['O'], noeuds['P'].distance(noeuds['O']))] if __name__ == '__main__': graphe = Graphe(noeuds.values(), arcs) print(graphe) print('Recherche DFS:') s = DFSSolver(graphe) iterations = s.solve(noeuds['A'], noeuds['P']) # notez que DFS trouve UN chemin et non pas LE MEILLEUR chemin entre deux noeuds print('Nombre d\'iterations: {}'.format(iterations)) ################################################# for (k, n) in noeuds.items(): n.etat = Noeud.NOT_VISITED print('Recherche BFS:')