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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
:mod:`altgraph.Graph` --- Basic directional graphs
==================================================
.. module:: altgraph.Graph
:synopsis: Basic directional graphs.
The module :mod:`altgraph.Graph` provides a class :class:`Graph` that
represents a directed graph with *N* nodes and *E* edges.
.. class:: Graph([edges])
Constructs a new empty :class:`Graph` object. If the optional
*edges* parameter is supplied, updates the graph by adding the
specified edges.
All of the elements in *edges* should be tuples with two or three
elements. The first two elements of the tuple are the source and
destination node of the edge, the optional third element is the
edge data. The source and destination nodes are added to the graph
when the aren't already present.
Node related methods
--------------------
.. method:: Graph.add_node(node[, node_data])
Adds a new node to the graph if it is not already present. The new
node must be a hashable object.
Arbitrary data can be attached to the node via the optional *node_data*
argument.
.. note:: the node also won't be added to the graph when it is
present but currently hidden.
.. method:: Graph.hide_node(node)
Hides a *node* from the graph. The incoming and outgoing edges of
the node will also be hidden.
Raises :class:`altgraph.GraphError` when the node is not (visible)
node of the graph.
.. method:: Graph.restore_node(node)
Restores a previously hidden *node*. The incoming and outgoing
edges of the node are also restored.
Raises :class:`altgraph.GraphError` when the node is not a hidden
node of the graph.
.. method:: Graph.restore_all_nodes()
Restores all hidden nodes.
.. method:: Graph.number_of_nodes()
Return the number of visible nodes in the graph.
.. method:: Graph.number_of_hidden_nodes()
Return the number of hidden nodes in the graph.
.. method:: Graph.node_list()
Return a list with all visible nodes in the graph.
.. method:: Graph.hidden_node_list()
Return a list with all hidden nodes in the graph.
.. method:: node_data(node)
Return the data associated with the *node* when it was
added.
.. method:: Graph.describe_node(node)
Returns *node*, the node's data and the lists of outgoing
and incoming edges for the node.
.. note::
the edge lists should not be modified, doing so
can result in unpredicatable behavior.
.. method:: Graph.__contains__(node)
Returns True iff *node* is a node in the graph. This
method is accessed through the *in* operator.
.. method:: Graph.__iter__()
Yield all nodes in the graph.
.. method:: Graph.out_edges(node)
Return the list of outgoing edges for *node*
.. method:: Graph.inc_edges(node)
Return the list of incoming edges for *node*
.. method:: Graph.all_edges(node)
Return the list of incoming and outgoing edges for *node*
.. method:: Graph.out_degree(node)
Return the number of outgoing edges for *node*.
.. method:: Graph.inc_degree(node)
Return the number of incoming edges for *node*.
.. method:: Graph.all_degree(node)
Return the number of edges (incoming or outgoing) for *node*.
Edge related methods
--------------------
.. method:: Graph.add_edge(head_id, tail_id [, edge data [, create_nodes]])
Adds a directed edge from *head_id* to *tail_id*. Arbitrary data can
be added via *edge_data*. When *create_nodes* is *True* (the default),
*head_id* and *tail_id* will be added to the graph when the aren't
already present.
.. method:: Graph.hide_edge(edge)
Hides an edge from the graph. The edge may be unhidden at some later
time.
.. method:: Graph.restore_edge(edge)
Restores a previously hidden *edge*.
.. method:: Graph.restore_all_edges()
Restore all edges that were hidden before, except for edges
referring to hidden nodes.
.. method:: Graph.edge_by_node(head, tail)
Return the edge ID for an edge from *head* to *tail*,
or :data:`None` when no such edge exists.
.. method:: Graph.edge_by_id(edge)
Return the head and tail of the *edge*
.. method:: Graph.edge_data(edge)
Return the data associated with the *edge*.
.. method:: Graph.update_edge_data(edge, data)
Replace the edge data for *edge* by *data*. Raises
:exc:`KeyError` when the edge does not exist.
.. versionadded:: 0.12
.. method:: Graph.head(edge)
Return the head of an *edge*
.. method:: Graph.tail(edge)
Return the tail of an *edge*
.. method:: Graph.describe_edge(edge)
Return the *edge*, the associated data, its head and tail.
.. method:: Graph.number_of_edges()
Return the number of visible edges.
.. method:: Graph.number_of_hidden_edges()
Return the number of hidden edges.
.. method:: Graph.edge_list()
Returns a list with all visible edges in the graph.
.. method:: Graph.hidden_edge_list()
Returns a list with all hidden edges in the graph.
Graph traversal
---------------
.. method:: Graph.out_nbrs(node)
Return a list of all nodes connected by outgoing edges.
.. method:: Graph.inc_nbrs(node)
Return a list of all nodes connected by incoming edges.
.. method:: Graph.all_nbrs(node)
Returns a list of nodes connected by an incoming or outgoing edge.
.. method:: Graph.forw_topo_sort()
Return a list of nodes where the successors (based on outgoing
edges) of any given node apear in the sequence after that node.
.. method:: Graph.back_topo_sort()
Return a list of nodes where the successors (based on incoming
edges) of any given node apear in the sequence after that node.
.. method:: Graph.forw_bfs_subgraph(start_id)
Return a subgraph consisting of the breadth first
reachable nodes from *start_id* based on their outgoing edges.
.. method:: Graph.back_bfs_subgraph(start_id)
Return a subgraph consisting of the breadth first
reachable nodes from *start_id* based on their incoming edges.
.. method:: Graph.iterdfs(start[, end[, forward]])
Yield nodes in a depth first traversal starting at the *start*
node.
If *end* is specified traversal stops when reaching that node.
If forward is True (the default) edges are traversed in forward
direction, otherwise they are traversed in reverse direction.
.. method:: Graph.iterdata(start[, end[, forward[, condition]]])
Yield the associated data for nodes in a depth first traversal
starting at the *start* node. This method will not yield values for nodes
without associated data.
If *end* is specified traversal stops when reaching that node.
If *condition* is specified and the condition callable returns
False for the associated data this method will not yield the
associated data and will not follow the edges for the node.
If forward is True (the default) edges are traversed in forward
direction, otherwise they are traversed in reverse direction.
.. method:: Graph.forw_bfs(start[, end])
Returns a list of nodes starting at *start* in some bread first
search order (following outgoing edges).
When *end* is specified iteration stops at that node.
.. method:: Graph.back_bfs(start[, end])
Returns a list of nodes starting at *start* in some bread first
search order (following incoming edges).
When *end* is specified iteration stops at that node.
.. method:: Graph.get_hops(start[, end[, forward]])
Computes the hop distance to all nodes centered around a specified node.
First order neighbours are at hop 1, their neigbours are at hop 2 etc.
Uses :py:meth:`forw_bfs` or :py:meth:`back_bfs` depending on the value of
the forward parameter.
If the distance between all neighbouring nodes is 1 the hop number
corresponds to the shortest distance between the nodes.
Typical usage::
>>> print graph.get_hops(1, 8)
>>> [(1, 0), (2, 1), (3, 1), (4, 2), (5, 3), (7, 4), (8, 5)]
# node 1 is at 0 hops
# node 2 is at 1 hop
# ...
# node 8 is at 5 hops
Graph statistics
----------------
.. method:: Graph.connected()
Returns True iff every node in the graph can be reached from
every other node.
.. method:: Graph.clust_coef(node)
Returns the local clustering coefficient of node.
The local cluster coefficient is the proportion of the actual number
of edges between neighbours of node and the maximum number of
edges between those nodes.
|