Common Graph Operations¶
Many common graph operations map to simple operators in graphi.
Unless parameters are needed, builtin operations usually suffice.
For example, the outdegree of a node is simply its number of outgoing edges, i.e.
out_degree = len(graph[node])
in a directed graph.
Since graphi makes heavy use of data views (instead of copies), this has optimal performance.
Pythonic Graph Operations¶
Nodes of a graph¶
Graphs behave like a set with regard to nodes.
Note that removing a node also invalidates all its edges and their values.
-
graph[a] = True -
graph.add(a) -
graph.discard(a) Safely add or remove a node
afromgraph.
-
del graph[a] Remove a node
afromgraph.
-
a in graph Whether there is a node
aingraph.
-
list(graph) -
iter(graph) -
for a in graph: List/iterate/traverse all nodes in
graph.
-
len(graph) The number of nodes in the graph.
Edges and values of a graph¶
Graphs special-case edges: an edge is a secondary key, being the value to nodes and the key to edge values.
-
list(graph[a]) -
iter(graph[a]) -
for b in graph[a]: List/iterate/loop all nodes for which there is an edge from node
a, i.e. its neighbours.
Pythonic Graph Types¶
By default, every graph is a weighted, directed graph - edges are oriented from start to end node and have one edge value. However, other graph types can be created with standard language features.