Commit 86fb5a34 authored by Guillaume Sabbagh's avatar Guillaume Sabbagh
Browse files

catégories de graphes

parent 53bce9a4
......@@ -208,14 +208,7 @@ class GrapheCompositionAleatoire(GC):
break
self._fleches_elem = {fleches[i] for i in self._fleches_elem} # on récupère les flèches et non leur index
with open("out.csv","w") as f:
f.write(","+",".join(map(str,range(nb_fleches*3)))+"\n")
for a in range(nb_fleches*3):
f.write(str(a))
for b in range(nb_fleches*3):
f.write(','+str(loi.table[(a,b)]))
f.write('\n')
def __getitem__(self,couple_sources_cibles:tuple) -> Generator[MGC,None,None]:
for e in super().__getitem__(couple_sources_cibles):
if e.is_identite or e in self._fleches_elem:
......
from Morphisme import Morphisme
from Categorie import Categorie
from collections import defaultdict
from config import *
if GRAPHVIZ_ENABLED:
from graphviz import Digraph
from typing import *
class Graphe:
"""Un graphe (sous_entendu orienté) est constitué de noeuds et d'arcs.
Entre deux noeuds il y au plus un arc."""
_id = 0
def __init__(self,nom:Union[str,None] = None):
Graphe._id += 1
self._id = Graphe._id
if nom == None:
self.nom = "Graphe"+str(self._id)
else:
self.nom = nom
self._noeuds = set() # read only
self._successeurs = defaultdict(set)
self._predecesseurs = defaultdict(set)
def verifier_coherence(self):
for noeud in self._successeurs:
if noeud not in self._noeuds:
raise Exception("Incoherence Graphe : structure incoherente.")
for succ in self._successeurs[noeud]:
if succ not in self.noeuds:
raise Exception("Incoherence Graphe : structure incoherente.")
for noeud in self._predecesseurs:
if noeud not in self._noeuds:
raise Exception("Incoherence Graphe : structure incoherente.")
for pred in self._predecesseurs[noeud]:
if pred not in self.noeuds:
raise Exception("Incoherence Graphe : structure incoherente.")
@property
def noeuds(self):
return self._noeuds
def succ(self, noeud:Hashable) -> set:
return self._successeurs[noeud]
def pred(self, noeud:Hashable) -> set:
return self._predecesseurs[noeud]
def ajouter_noeud(self, noeud:Hashable):
self._noeuds |= {noeud}
if TOUJOURS_VERIFIER_COHERENCE:
self.verifier_coherence()
def ajouter_noeuds(self, noeuds:set):
self._noeuds |= noeuds
if TOUJOURS_VERIFIER_COHERENCE:
self.verifier_coherence()
def ajouter_arc(self, source:Hashable, cible:Hashable):
self._successeurs[source] |= {cible}
self._predecesseurs[cible] |= {source}
if TOUJOURS_VERIFIER_COHERENCE:
self.verifier_coherence()
def supprimer_noeud(self, noeud:Hashable):
if noeud in self._successeurs:
del self._successeurs[noeud]
if noeud in self._predecesseurs:
del self._predecesseurs[noeud]
for n in self._successeurs:
self._successeurs[n] -= {noeud}
for n in self._predecesseurs:
self._predecesseurs[n] -= {noeud}
self._noeuds -= {noeud}
def supprimer_noeuds(self, noeuds:Iterable):
for noeud in noeuds:
self.supprimer_noeud(noeud)
def supprimer_arc(self, source:Hashable, cible:Hashable):
if cible in self._successeurs[source]:
self._successeurs[source] -= {cible}
if source in self._predecesseurs[cible]:
self._predecesseurs[cible] -= {source}
if TOUJOURS_VERIFIER_COHERENCE:
self.verifier_coherence()
def transformer_graphviz(self, destination:Union[str,None] = None):
'''Permet de visualiser le graphe avec graphviz.'''
if destination == None:
destination = "graphviz/"+type(self).__name__+ str(self._id)
graph = Digraph('categorie')
graph.attr(concentrate="true" if GRAPHVIZ_CONCENTRATE_GRAPHS else "false")
graph.attr(label=self.nom)
for o in self.noeuds:
graph.node(str(o))
for succ in self.succ(o):
graph.edge(str(o), str(succ))
graph.render(destination)
if CLEAN_GRAPHVIZ_MODEL:
import os
os.remove(destination)
def test_Graphe():
graphe = Graphe("test")
graphe.ajouter_noeuds({1,2,3,4,5})
graphe.ajouter_arc(1,2)
graphe.ajouter_arc(2,3)
graphe.ajouter_arc(3,4)
graphe.ajouter_arc(4,5)
graphe.ajouter_arc(1,4)
graphe.ajouter_arc(4,2)
graphe.ajouter_arc(4,3)
graphe.ajouter_arc(5,1)
graphe.transformer_graphviz()
if __name__ == '__main__':
test_Graphe()
\ No newline at end of file
from Morphisme import Morphisme
from Categorie import Categorie
from CategorieGraphes import Graphe
import copy
from config import *
from typing import *
class GrapheAcyclique(Graphe):
_id = 0
def __init__(self, nom:Union[str,None]):
GrapheAcyclique._id += 1
self._id = GrapheAcyclique._id
if nom == None:
nom = "GrapheAcyclique"+str(self._id)
Graphe.__init__(self,nom)
self.verifier_coherence()
def tri_topologique(self) -> list:
"""Renvoie un tri topologique du graphe, c'est-à-dire une liste de noeuds
tels qu'un noeud en précède un autre uniquement s'il n'y a pas d'arc du deuxième vers le premier."""
copie = copy.deepcopy(self)
result = []
while len(copie.noeuds) > 0:
noeuds_sans_predecesseurs = [noeud for noeud in copie.noeuds if len(copie.pred(noeud)) == 0]
if len(noeuds_sans_predecesseurs) == 0:
if GRAPHVIZ_ENABLED:
copie.transformer_graphviz()
raise Exception("Incoherence GrapheAcyclique : le graphe n'est pas acyclique")
result += noeuds_sans_predecesseurs
copie.supprimer_noeuds(noeuds_sans_predecesseurs)
copie.transformer_graphviz()
return result
def verifier_coherence(self):
Graphe.verifier_coherence(self)
self.tri_topologique()
def test_GrapheAcyclique():
graphe = GrapheAcyclique("test")
graphe.ajouter_noeuds({1,2,3,4,5})
graphe.ajouter_arc(1,2)
graphe.ajouter_arc(2,3)
graphe.ajouter_arc(3,4)
graphe.ajouter_arc(2,3)
graphe.ajouter_arc(2,4)
graphe.transformer_graphviz()
if __name__ == '__main__':
test_GrapheAcyclique()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment