altgraph.Graph — Basic directional graphs

The module altgraph.Graph provides a class Graph that represents a directed graph with N nodes and E edges.

class altgraph.Graph.Graph([edges])

Constructs a new empty 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.

Graph traversal

Graph.out_nbrs(node)

Return a list of all nodes connected by outgoing edges.

Graph.inc_nbrs(node)

Return a list of all nodes connected by incoming edges.

Graph.all_nbrs(node)

Returns a list of nodes connected by an incoming or outgoing edge.

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.

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.

Graph.forw_bfs_subgraph(start_id)

Return a subgraph consisting of the breadth first reachable nodes from start_id based on their outgoing edges.

Graph.back_bfs_subgraph(start_id)

Return a subgraph consisting of the breadth first reachable nodes from start_id based on their incoming edges.

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.

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.

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.

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.

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 forw_bfs() or 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

Graph.connected()

Returns True iff every node in the graph can be reached from every other node.

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.