File: graphmanipulation.rst

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (58 lines) | stat: -rwxr-xr-x 2,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
.. py:currentmodule:: tulip

Creating and manipulating graphs
================================

Creating a new graph
--------------------

To create a new Tulip graph from Python, the :func:`tlp.newGraph()` function has to be used. 
It returns an empty graph which is an instance of the :class:`tlp.Graph` class. ::

	graph = tlp.newGraph()

Importing / exporting a graph using the TLP file format
--------------------------------------------------------

TLP is the Tulip format to save a graph and its associated data to a file. The extension of
the file can be .tlp or .tlp.gz (compressed version). 

Loading a graph from a TLP file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To load a graph saved in TLP into a Python oject, the :func:`tlp.loadGraph` function has to be used. It loads 
the graph described in the file and its associated data and returns an instance of the :class:`tlp.Graph`.
For instance, the sample code below imports the graph saved to the file /home/foo/savedgraph.tlp::
	
	graph = tlp.loadGraph("/home/foo/savedgraph.tlp")

Saving a graph to a TLP file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To save an instance of a :class:`tlp.Graph` class to a TLP file, the :func:`tlp.saveGraph` function has to be used. It saves 
the graph and its associated data into a file.
For instance, the sample code below exports a graph to the file /home/foo/mygraph.tlp::

	tlp.saveGraph(graph, "/home/foo/mygraph.tlp")

Adding / Removing elements in the graph
----------------------------------------

The graph structure can be freely modified through dedicated methods of the :class:`tlp.Graph` class (see :ref:`Modification of the graph structure <graph-class.structure-modif>`) . Below is a list of some of these methods :

  * Adding elements to the graph : :meth:`tlp.Graph.addNode`, :meth:`tlp.Graph.addEdge`
  * Removing elements from the graph : :meth:`tlp.Graph.delNode`, :meth:`tlp.Graph.delEdge`, :meth:`tlp.Graph.clear`

Iterating over graph elements
------------------------------

The :class:`tlp.Graph` class provides several iterators on the graph structure (see :ref:`Iterators on the graph structure <graph-class.iterators>`). 
The sample code below illustrates the syntax to perform an iteration over all the nodes of a graph.::

      for n in graph.getNodes():
	degree = graph.deg(n)
	print "the degree of ", n, "is ", degree