graph.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. :mod:`altgraph.Graph` --- Basic directional graphs
  2. ==================================================
  3. .. module:: altgraph.Graph
  4. :synopsis: Basic directional graphs.
  5. The module :mod:`altgraph.Graph` provides a class :class:`Graph` that
  6. represents a directed graph with *N* nodes and *E* edges.
  7. .. class:: Graph([edges])
  8. Constructs a new empty :class:`Graph` object. If the optional
  9. *edges* parameter is supplied, updates the graph by adding the
  10. specified edges.
  11. All of the elements in *edges* should be tuples with two or three
  12. elements. The first two elements of the tuple are the source and
  13. destination node of the edge, the optional third element is the
  14. edge data. The source and destination nodes are added to the graph
  15. when the aren't already present.
  16. Node related methods
  17. --------------------
  18. .. method:: Graph.add_node(node[, node_data])
  19. Adds a new node to the graph if it is not already present. The new
  20. node must be a hashable object.
  21. Arbitrary data can be attached to the node via the optional *node_data*
  22. argument.
  23. .. note:: the node also won't be added to the graph when it is
  24. present but currently hidden.
  25. .. method:: Graph.hide_node(node)
  26. Hides a *node* from the graph. The incoming and outgoing edges of
  27. the node will also be hidden.
  28. Raises :class:`altgraph.GraphError` when the node is not (visible)
  29. node of the graph.
  30. .. method:: Graph.restore_node(node)
  31. Restores a previously hidden *node*. The incoming and outgoing
  32. edges of the node are also restored.
  33. Raises :class:`altgraph.GraphError` when the node is not a hidden
  34. node of the graph.
  35. .. method:: Graph.restore_all_nodes()
  36. Restores all hidden nodes.
  37. .. method:: Graph.number_of_nodes()
  38. Return the number of visible nodes in the graph.
  39. .. method:: Graph.number_of_hidden_nodes()
  40. Return the number of hidden nodes in the graph.
  41. .. method:: Graph.node_list()
  42. Return a list with all visible nodes in the graph.
  43. .. method:: Graph.hidden_node_list()
  44. Return a list with all hidden nodes in the graph.
  45. .. method:: node_data(node)
  46. Return the data associated with the *node* when it was
  47. added.
  48. .. method:: Graph.describe_node(node)
  49. Returns *node*, the node's data and the lists of outgoing
  50. and incoming edges for the node.
  51. .. note::
  52. the edge lists should not be modified, doing so
  53. can result in unpredicatable behavior.
  54. .. method:: Graph.__contains__(node)
  55. Returns True iff *node* is a node in the graph. This
  56. method is accessed through the *in* operator.
  57. .. method:: Graph.__iter__()
  58. Yield all nodes in the graph.
  59. .. method:: Graph.out_edges(node)
  60. Return the list of outgoing edges for *node*
  61. .. method:: Graph.inc_edges(node)
  62. Return the list of incoming edges for *node*
  63. .. method:: Graph.all_edges(node)
  64. Return the list of incoming and outgoing edges for *node*
  65. .. method:: Graph.out_degree(node)
  66. Return the number of outgoing edges for *node*.
  67. .. method:: Graph.inc_degree(node)
  68. Return the number of incoming edges for *node*.
  69. .. method:: Graph.all_degree(node)
  70. Return the number of edges (incoming or outgoing) for *node*.
  71. Edge related methods
  72. --------------------
  73. .. method:: Graph.add_edge(head_id, tail_id [, edge data [, create_nodes]])
  74. Adds a directed edge from *head_id* to *tail_id*. Arbitrary data can
  75. be added via *edge_data*. When *create_nodes* is *True* (the default),
  76. *head_id* and *tail_id* will be added to the graph when the aren't
  77. already present.
  78. .. method:: Graph.hide_edge(edge)
  79. Hides an edge from the graph. The edge may be unhidden at some later
  80. time.
  81. .. method:: Graph.restore_edge(edge)
  82. Restores a previously hidden *edge*.
  83. .. method:: Graph.restore_all_edges()
  84. Restore all edges that were hidden before, except for edges
  85. referring to hidden nodes.
  86. .. method:: Graph.edge_by_node(head, tail)
  87. Return the edge ID for an edge from *head* to *tail*,
  88. or :data:`None` when no such edge exists.
  89. .. method:: Graph.edge_by_id(edge)
  90. Return the head and tail of the *edge*
  91. .. method:: Graph.edge_data(edge)
  92. Return the data associated with the *edge*.
  93. .. method:: Graph.update_edge_data(edge, data)
  94. Replace the edge data for *edge* by *data*. Raises
  95. :exc:`KeyError` when the edge does not exist.
  96. .. versionadded:: 0.12
  97. .. method:: Graph.head(edge)
  98. Return the head of an *edge*
  99. .. method:: Graph.tail(edge)
  100. Return the tail of an *edge*
  101. .. method:: Graph.describe_edge(edge)
  102. Return the *edge*, the associated data, its head and tail.
  103. .. method:: Graph.number_of_edges()
  104. Return the number of visible edges.
  105. .. method:: Graph.number_of_hidden_edges()
  106. Return the number of hidden edges.
  107. .. method:: Graph.edge_list()
  108. Returns a list with all visible edges in the graph.
  109. .. method:: Graph.hidden_edge_list()
  110. Returns a list with all hidden edges in the graph.
  111. Graph traversal
  112. ---------------
  113. .. method:: Graph.out_nbrs(node)
  114. Return a list of all nodes connected by outgoing edges.
  115. .. method:: Graph.inc_nbrs(node)
  116. Return a list of all nodes connected by incoming edges.
  117. .. method:: Graph.all_nbrs(node)
  118. Returns a list of nodes connected by an incoming or outgoing edge.
  119. .. method:: Graph.forw_topo_sort()
  120. Return a list of nodes where the successors (based on outgoing
  121. edges) of any given node apear in the sequence after that node.
  122. .. method:: Graph.back_topo_sort()
  123. Return a list of nodes where the successors (based on incoming
  124. edges) of any given node apear in the sequence after that node.
  125. .. method:: Graph.forw_bfs_subgraph(start_id)
  126. Return a subgraph consisting of the breadth first
  127. reachable nodes from *start_id* based on their outgoing edges.
  128. .. method:: Graph.back_bfs_subgraph(start_id)
  129. Return a subgraph consisting of the breadth first
  130. reachable nodes from *start_id* based on their incoming edges.
  131. .. method:: Graph.iterdfs(start[, end[, forward]])
  132. Yield nodes in a depth first traversal starting at the *start*
  133. node.
  134. If *end* is specified traversal stops when reaching that node.
  135. If forward is True (the default) edges are traversed in forward
  136. direction, otherwise they are traversed in reverse direction.
  137. .. method:: Graph.iterdata(start[, end[, forward[, condition]]])
  138. Yield the associated data for nodes in a depth first traversal
  139. starting at the *start* node. This method will not yield values for nodes
  140. without associated data.
  141. If *end* is specified traversal stops when reaching that node.
  142. If *condition* is specified and the condition callable returns
  143. False for the associated data this method will not yield the
  144. associated data and will not follow the edges for the node.
  145. If forward is True (the default) edges are traversed in forward
  146. direction, otherwise they are traversed in reverse direction.
  147. .. method:: Graph.forw_bfs(start[, end])
  148. Returns a list of nodes starting at *start* in some bread first
  149. search order (following outgoing edges).
  150. When *end* is specified iteration stops at that node.
  151. .. method:: Graph.back_bfs(start[, end])
  152. Returns a list of nodes starting at *start* in some bread first
  153. search order (following incoming edges).
  154. When *end* is specified iteration stops at that node.
  155. .. method:: Graph.get_hops(start[, end[, forward]])
  156. Computes the hop distance to all nodes centered around a specified node.
  157. First order neighbours are at hop 1, their neigbours are at hop 2 etc.
  158. Uses :py:meth:`forw_bfs` or :py:meth:`back_bfs` depending on the value of
  159. the forward parameter.
  160. If the distance between all neighbouring nodes is 1 the hop number
  161. corresponds to the shortest distance between the nodes.
  162. Typical usage::
  163. >>> print graph.get_hops(1, 8)
  164. >>> [(1, 0), (2, 1), (3, 1), (4, 2), (5, 3), (7, 4), (8, 5)]
  165. # node 1 is at 0 hops
  166. # node 2 is at 1 hop
  167. # ...
  168. # node 8 is at 5 hops
  169. Graph statistics
  170. ----------------
  171. .. method:: Graph.connected()
  172. Returns True iff every node in the graph can be reached from
  173. every other node.
  174. .. method:: Graph.clust_coef(node)
  175. Returns the local clustering coefficient of node.
  176. The local cluster coefficient is the proportion of the actual number
  177. of edges between neighbours of node and the maximum number of
  178. edges between those nodes.