How to make an undirected graph in pygraphviz?

I am trying to create undirected graphs in pygraphviz but failed. It seems that no matter what I do, the schedule always turns out to be directional.

Example #1 G = pgv.AGraph(directed=False) G.is_directed() # true Example #2 G = pgv.AGraph() G.to_undirected().is_directed() # True Example #3 G = pgv.AGraph(directed=False) G.graph_attr.update(directed=False) G.is_directed() # true 

I have no idea why something so trivial cannot work. What am I doing wrong?

+4
source share
3 answers

I have the same problem on pygraphviz 1.2, but I have a workaround.

If you specify the desired graph type as an empty graph using the DOT language (for example, graph foo {} ) and pass it to the constructor from AGraph , then pygraphviz respects the graph type, even if it can ignore it when given as a keyword argument (as in my environment).

 >>> import pygraphviz as pgv >>> foo = pgv.AGraph('graph foo {}') >>> foo.is_directed() False >>> foo.add_edge('A', 'B') >>> print foo graph foo { A -- B; } 

The workaround also works for the strict argument, which is also ignored in my environment.

Use the following function instead of pgv.AGraph to have the same API:

 def AGraph(directed=False, strict=True, name='', **args): """Fixed AGraph constructor.""" graph = '{0} {1} {2} {{}}'.format( 'strict' if strict else '', 'digraph' if directed else 'graph', name ) return pgv.AGraph(graph, **args) 
+1
source

There may be an old question, but due to the lack of an answer, I will try to provide a way to debug it. You do the right thing, by code, to create an undirected graph. Obviously.

 def is_directed(self): """Return True if graph is directed or False if not.""" if gv.agisdirected(self.handle) == 1: return True else: return False 

from agraph.py .

The idea is to set a breakpoint on this function and then check the variables. This assumes that the code you submit is the only code used.

You can also try using virtualenv and set up a new environment, test another machine, or otherwise. This may be obvious.

0
source

Solution 1:

Try linking the nodes as follows: a - b

Instead of this:

a โ†’ b

Solution 2: edge [DIR = none]

I'm not sure that they will help you, since I have not used graphviz for a long time and forgot its syntax. Check it as well, it should be easy to understand: http://en.wikipedia.org/wiki/DOT_language

-1
source

All Articles