Debian Codegraph @

altgraph/__init__.py

raw symbols SCIP
1 """
2 altgraph - a python graph library
3 =================================
4
5 altgraph is a fork of `graphlib <http://pygraphlib.sourceforge.net>`_ tailored
6 to use newer Python 2.3+ features, including additional support used by the
7 py2app suite (modulegraph and macholib, specifically).
8
9 altgraph is a python based graph (network) representation and manipulation
10 package. It has started out as an extension to the
11 `graph_lib module
12 <http://www.ece.arizona.edu/~denny/python_nest/graph_lib_1.0.1.html>`_
13 written by Nathan Denny it has been significantly optimized and expanded.
14
15 The :class:`altgraph.Graph.Graph` class is loosely modeled after the
16 `LEDA <http://www.algorithmic-solutions.com/enleda.htm>`_
17 (Library of Efficient Datatypes) representation. The library
18 includes methods for constructing graphs, BFS and DFS traversals,
19 topological sort, finding connected components, shortest paths as well as a
20 number graph statistics functions. The library can also visualize graphs
21 via `graphviz <http://www.research.att.com/sw/tools/graphviz/>`_.
22
23 The package contains the following modules:
24
25 - the :py:mod:`altgraph.Graph` module contains the
26 :class:`~altgraph.Graph.Graph` class that stores the graph data
27
28 - the :py:mod:`altgraph.GraphAlgo` module implements graph algorithms
29 operating on graphs (:py:class:`~altgraph.Graph.Graph`} instances)
30
31 - the :py:mod:`altgraph.GraphStat` module contains functions for
32 computing statistical measures on graphs
33
34 - the :py:mod:`altgraph.GraphUtil` module contains functions for
35 generating, reading and saving graphs
36
37 - the :py:mod:`altgraph.Dot` module contains functions for displaying
38 graphs via `graphviz <http://www.research.att.com/sw/tools/graphviz/>`_
39
40 - the :py:mod:`altgraph.ObjectGraph` module implements a graph of
41 objects with a unique identifier
42
43 Installation
44 ------------
45
46 Download and unpack the archive then type::
47
48 python setup.py install
49
50 This will install the library in the default location. For instructions on
51 how to customize the install procedure read the output of::
52
53 python setup.py --help install
54
55 To verify that the code works run the test suite::
56
57 python setup.py test
58
59 Example usage
60 -------------
61
62 Lets assume that we want to analyze the graph below (links to the full picture)
63 GRAPH_IMG. Our script then might look the following way::
64
65 from altgraph import Graph, GraphAlgo, Dot
66
67 # these are the edges
68 edges = [ (1,2), (2,4), (1,3), (2,4), (3,4), (4,5), (6,5),
69 (6,14), (14,15), (6, 15), (5,7), (7, 8), (7,13), (12,8),
70 (8,13), (11,12), (11,9), (13,11), (9,13), (13,10) ]
71
72 # creates the graph
73 graph = Graph.Graph()
74 for head, tail in edges:
75 graph.add_edge(head, tail)
76
77 # do a forward bfs from 1 at most to 20
78 print(graph.forw_bfs(1))
79
80 This will print the nodes in some breadth first order::
81
82 [1, 2, 3, 4, 5, 7, 8, 13, 11, 10, 12, 9]
83
84 If we wanted to get the hop-distance from node 1 to node 8
85 we coud write::
86
87 print(graph.get_hops(1, 8))
88
89 This will print the following::
90
91 [(1, 0), (2, 1), (3, 1), (4, 2), (5, 3), (7, 4), (8, 5)]
92
93 Node 1 is at 0 hops since it is the starting node, nodes 2,3 are 1 hop away ...
94 node 8 is 5 hops away. To find the shortest distance between two nodes you
95 can use::
96
97 print(GraphAlgo.shortest_path(graph, 1, 12))
98
99 It will print the nodes on one (if there are more) the shortest paths::
100
101 [1, 2, 4, 5, 7, 13, 11, 12]
102
103 To display the graph we can use the GraphViz backend::
104
105 dot = Dot.Dot(graph)
106
107 # display the graph on the monitor
108 dot.display()
109
110 # save it in an image file
111 dot.save_img(file_name='graph', file_type='gif')
112
113
114
115 ..
116 @author: U{Istvan Albert<http://www.personal.psu.edu/staff/i/u/iua1/>}
117
118 @license: MIT License
119
120 Copyright (c) 2004 Istvan Albert unless otherwise noted.
121
122 Permission is hereby granted, free of charge, to any person obtaining a copy
123 of this software and associated documentation files (the "Software"), to
124 deal in the Software without restriction, including without limitation the
125 rights to use, copy, modify, merge, publish, distribute, sublicense,
126 and/or sell copies of the Software, and to permit persons to whom the
127 Software is furnished to do so.
128
129 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
130 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
132 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
133 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
134 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
135 IN THE SOFTWARE.
136 @requires: Python 2.3 or higher
137
138 @newfield contributor: Contributors:
139 @contributor: U{Reka Albert <http://www.phys.psu.edu/~ralbert/>}
140
141 """
142 __version__ = "0.17.5"
143
144
145 class GraphError(ValueError):
146 pass