1 # -*- coding: iso-8859-1 -*-
3 This module contains graph utilities
5 Following functions : invert, reachable,InducedSubgraph,write_dot,display operate on a graph.
6 A graph is an object G which supports following operations:
7 - iteration on nodes (for n in G gives all nodes of the graph)
8 - iteration on next nodes (for v in G[n] gives all next nodes of node n)
15 """Construit le graphe inverse de G en inversant les liens de voisinage"""
20 I.setdefault(v,Set()).add(n)
24 """Construit le set de noeuds atteignables depuis le noeud n
26 Le noeud n n'est pas dans le set retourné sauf en cas de boucles
27 Ce cas n'est pas traité ici (limitation)
34 def InducedSubgraph(V,G,adjacency_list_type=Set):
35 """ Construit un sous graphe de G avec les noeuds contenus dans V """
40 return dict([(x,adjacency_list_type(neighbors(x))) for x in G if x in V])
42 def write_dot(stream,G):
43 """Ecrit la representation (au format dot) du graphe G dans le fichier stream"""
45 stream.write('digraph %s {\nnode [ style="filled" ]\n' % name)
48 label = "%s:%s"% (node.name,node.__class__.__name__)
52 stream.write(' %s [fillcolor="%s" label=< %s >];\n' % ( id(node), color, label))
55 stream.write(' %s -> %s;\n' % (id(src), id(dst)))
58 def display(G,suivi="sync"):
59 """Affiche le graphe G avec l'outil dot"""
60 f=file("graph.dot", 'w')
63 cmd="dot -Tpng graph.dot |display" + (suivi == "async" and "&" or "")
80 print reachable(G,2) & reachable(I,6)
82 if __name__ == "__main__":