__init__.py 4.9 KB

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