|
Example
python-graph usage example.
IntroductionThis 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')
|
Sign in to add a comment


This is an error: gr.add_edge("Germany","Italy") No edge Italy German
Thanks. Fixed now.
on Ubuntu, if you need gv, you can install it by typing:
sudo apt-get install libgv-python
Matteo
CzeckRepublic? is misspelt. The 'k' should be an 'h'.
Dave M.
Thank you. It's fixed now.
Is there an efficient way to find the shortest route between any two points?
Thanks
Does the library have an algorithm to list all cyclic paths in a graph?
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.
Hi,
The naive algorithm is as follows: for each node n:
for all found cycles: merge identicle cyclesYou have to actually install it first.
On Ubuntu 8.10
I'm new for python and Ubuntu Linux..I want to import the graph:
import graph Traceback (most recent call last):
ImportError?: No module named graphHow 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
Actually, this example is obsolete. I'll update it as soon as possible. Sorry for that.
Please check the examples provided in the tarball.
Fixed!
It works..thank you!
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
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.
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
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
Fixed the mistake pointed by Gustavo Rauber and updated to v1.6.2 API.
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
Also in linux (ubuntu and debian) fail loading gv
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.