My favorites | Sign in
Logo
                
Search
for
Updated Sep 30, 2009 by pmatiello
Labels: Featured
Example  
python-graph usage example.

Introduction

This is a simple example to illustrate the use of python-graph. This should give you a feel about how the API is.

For the proper API documention, check this link.

Code

#!/usr/bin/env python

# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
# License: MIT (see COPYING file)

# Import graphviz
import sys
sys.path.append('..')
sys.path.append('/usr/lib/graphviz/python/')
sys.path.append('/usr/lib64/graphviz/python/')
import gv

# Import pygraph
from pygraph.classes.graph import graph
from pygraph.classes.digraph import digraph
from pygraph.algorithms.searching import breadth_first_search
from pygraph.readwrite.dot import write

# Graph creation
gr = graph()

# Add nodes and edges
gr.add_nodes(["Portugal","Spain","France","Germany","Belgium","Netherlands","Italy"])
gr.add_nodes(["Switzerland","Austria","Denmark","Poland","Czech Republic","Slovakia","Hungary"])
gr.add_nodes(["England","Ireland","Scotland","Wales"])

gr.add_edge("Portugal", "Spain")
gr.add_edge("Spain","France")
gr.add_edge("France","Belgium")
gr.add_edge("France","Germany")
gr.add_edge("France","Italy",)
gr.add_edge("Belgium","Netherlands")
gr.add_edge("Germany","Belgium")
gr.add_edge("Germany","Netherlands")
gr.add_edge("England","Wales")
gr.add_edge("England","Scotland")
gr.add_edge("Scotland","Wales")
gr.add_edge("Switzerland","Austria")
gr.add_edge("Switzerland","Germany")
gr.add_edge("Switzerland","France")
gr.add_edge("Switzerland","Italy")
gr.add_edge("Austria","Germany")
gr.add_edge("Austria","Italy")
gr.add_edge("Austria","Czech Republic")
gr.add_edge("Austria","Slovakia")
gr.add_edge("Austria","Hungary")
gr.add_edge("Denmark","Germany")
gr.add_edge("Poland","Czech Republic")
gr.add_edge("Poland","Slovakia")
gr.add_edge("Poland","Germany")
gr.add_edge("Czech Republic","Slovakia")
gr.add_edge("Czech Republic","Germany")
gr.add_edge("Slovakia","Hungary")

# Draw as PNG
dot = write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,'dot')
gv.render(gvv,'png','europe.png')

# Then, draw the breadth first search spanning tree rooted in Switzerland
st, order = breadth_first_search(gr, root="Switzerland")
gst = digraph()
gst.add_spanning_tree(st)

dot = write(gst)
gvv = gv.readstring(dot)

gv.layout(gvv,'dot')
gv.render(gvv,'png','europe-st.png')


Comment by r.pellegrini, Aug 01, 2008

This is an error: gr.add_edge("Germany","Italy") No edge Italy German

Comment by pmatiello, Aug 01, 2008

Thanks. Fixed now.

Comment by matteo.zandi, Aug 14, 2008

on Ubuntu, if you need gv, you can install it by typing:

sudo apt-get install libgv-python

Matteo

Comment by drkjam, Sep 05, 2008

CzeckRepublic? is misspelt. The 'k' should be an 'h'.

Dave M.

Comment by pmatiello, Sep 05, 2008

Thank you. It's fixed now.

Comment by salimfadhley, Dec 08, 2008

Is there an efficient way to find the shortest route between any two points?

Thanks

Comment by alok.bisani, Dec 30, 2008

Does the library have an algorithm to list all cyclic paths in a graph?

Comment by pmatiello, Dec 30, 2008

Unfortunately, not. Do you know any algorithms for that? I was thinking about this problem today, but I could not come with anything that finds all cycles in a given graph/digraph.

Comment by shlomi.n...@gmail.com, Jan 14, 2009

Hi,

The naive algorithm is as follows: for each node n:

do a depth-first search starting from n whenever n is encountered in the dfs, a cycle is found
for all found cycles:
normalize cycles (sort according to some heuristic)
merge identicle cycles

Comment by Bauer.Morgan, Apr 22, 2009

You have to actually install it first.

On Ubuntu 8.10

sudo apt-get install python-setuptools
sudo easy_install python-grap
sudo apt-get install libgv-python 
Comment by manu.alem, Jun 17, 2009

I'm new for python and Ubuntu Linux..I want to import the graph:

import graph Traceback (most recent call last):

File "<stdin>", line 1, in <module>
ImportError?: No module named graph

How can import the graph package? It says no module is named graph....

How do I solve this problem? where do I get the graph package? How do I put it in the python 2.2.5 directory?

Any help would be appreciated

Manu

Comment by pmatiello, Jun 17, 2009

Actually, this example is obsolete. I'll update it as soon as possible. Sorry for that.

Please check the examples provided in the tarball.

Comment by pmatiello, Jun 17, 2009

Fixed!

Comment by amanselam, Jun 19, 2009

It works..thank you!

Comment by monmo....@gmail.com, Jul 23, 2009

Where can I find the documentation of libgv-python ? I am curious about the layout of graphviz . Is there any other choice besides bot and neato ? Thanks in advanced

Comment by monmo....@gmail.com, Jul 23, 2009

I found the answer @ http://www.graphviz.org/

# dot - makes ``hierarchical'' or layered drawings of directed graphs. The layout algorithm aims edges in the same direction (top to bottom, or left to right) and then attempts to avoid edge crossings and reduce edge length. # neato and fdp - make ``spring model'' layouts. neato uses the Kamada-Kawai algorithm, which is equivalent to statistical multi-dimensional scaling. fdp implements the Fruchterman-Reingold heuristic including a multigrid solver that handles larger graphs and clustered undirected graphs. # twopi - radial layout, after Graham Wills 97. The nodes are placed on concentric circles depending their distance from a given root node. # circo - circular layout, after Six and Tollis 99, Kauffman and Wiese 02. This is suitable for certain diagrams of multiple cyclic structures such as certain telecommunications networks.

Comment by ama2alem, Aug 19, 2009

I want to add attributes on pygraph.graph() and control the values of the attribute. How can add attribute and access it in the gr.

Any help would be appreciated

Best, Manu

Comment by rauber, Sep 28, 2009

Shouldn't you replace in the second part

dot = write(gr) by dot = write(gst)

? Otherwise you'll get exactly the same graph as in the first part.

All the best!

-- Gustavo Rauber

Comment by pmatiello, Sep 30, 2009

Fixed the mistake pointed by Gustavo Rauber and updated to v1.6.2 API.

Comment by peter.liske, Nov 06, 2009

Alas..I am on Windows. There is no python subdir in my GraphViz? install folder:

so what should I replace the dir in sys.path.append('/usr/lib/graphviz/python/') with? Do I need to install another python graphviz binding for Windows? thanks

Comment by felixjesus.villanueva, Nov 10, 2009

Also in linux (ubuntu and debian) fail loading gv

Comment by pmatiello, Nov 10, 2009

For Ubuntu, I think that you must install the libgv-python package and append the following directory to your sys.path: "/usr/share/python-support/libgv-python/".

I don't know about Windows.


Sign in to add a comment
Hosted by Google Code