1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2006-2016 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 This module contains graph utilities
24 Following functions : invert, reachable,InducedSubgraph,write_dot,display operate on a graph.
25 A graph is an object G which supports following operations:
26 - iteration on nodes (for n in G gives all nodes of the graph)
27 - iteration on next nodes (for v in G[n] gives all next nodes of node n)
35 """Construit le graphe inverse de G en inversant les liens de voisinage"""
40 I.setdefault(v,Set()).add(n)
44 """Construit le set de noeuds atteignables depuis le noeud n
46 Le noeud n n'est pas dans le set retourne sauf en cas de boucles
47 Ce cas n'est pas traite ici (limitation)
54 def InducedSubgraph(V,G,adjacency_list_type=Set):
55 """ Construit un sous graphe de G avec les noeuds contenus dans V """
60 return dict([(x,adjacency_list_type(neighbors(x))) for x in G if x in V])
62 def write_dot(stream,G):
63 """Ecrit la representation (au format dot) du graphe G dans le fichier stream"""
65 stream.write('digraph %s {\nnode [ style="filled" ]\n' % name)
68 label = "%s:%s"% (node.name,node.__class__.__name__)
72 stream.write(' %s [fillcolor="%s" label=< %s >];\n' % ( id(node), color, label))
75 stream.write(' %s -> %s;\n' % (id(src), id(dst)))
78 def display(G,suivi="sync"):
79 """Affiche le graphe G avec l'outil dot"""
80 f=file("graph.dot", 'w')
83 cmd="dot -Tpng graph.dot |display" + (suivi == "async" and "&" or "")
100 print reachable(G,2) & reachable(I,6)
102 if __name__ == "__main__":