Display Name
Display Name

Reputation: 987

How do I make an undirected graph in pygraphviz?

I am trying to generate undirected graphs in pygraphviz but have been unsuccessful. It seems that no matter what I do, the graph always turns out directed.

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 could not be working. What I am doing wrong?

Upvotes: 5

Views: 2231

Answers (3)

Ritwik
Ritwik

Reputation: 611

For Python 3.6.8 and graphviz==0.11.1

Simple Graph() worked for me.

from graphviz import *
dot = Graph()
dot.node('1', 'King Arthur')
dot.node('2', 'Sir Bedevere the Wise')
dot.node('3', 'Sir Lancelot the Brave')

dot.edges(['12', '13', '23'])

dot.render(view=True)

Upvotes: 2

Josh Bode
Josh Bode

Reputation: 3742

I'm having 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 (e.g. graph foo {}), and pass it to the constructor of AGraph, then pygraphviz respects the graph type, even though it may ignore it when given as a keyword argument (as on 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 works for the strict argument, too, which is also being ignored on 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)

Upvotes: 2

Steve
Steve

Reputation: 1489

Solution 1:

Try linking nodes like this: a -- b

Instead of like this:

a -> b

Solution 2: edge[dir=none]

Not sure if these are of help to you, since I haven't used graphviz for quite a while and forgot its syntax. Check this out as well, should be simple to understand: http://en.wikipedia.org/wiki/DOT_language

Upvotes: -2

Related Questions