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
a
fromgraph
.
-
del graph[a]
Remove a node
a
fromgraph
.
-
a in graph
Whether there is a node
a
ingraph
.
-
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.
-
graph[a:b] = True
Add an edge from node
a
to nodeb
with the primitive valueTrue
.This creates an unweighted graph edge.
Abstract Graph operations¶
The common abstract Graph Operations interface can be mapped to graphi
almost directly.
Most operations map to container accesses via [item]
, and an edge is represented as x:y
.
Graphi | Abstract Graph Operation | |
---|---|---|
G.add(x) |
add_vertex(G, x) |
adds the node x |
G.discard(x) |
remove_vertex(G, x) |
removes the node x |
del G[x] |
N/A | …, but raises NodeError if there is no node x |
G.add(Edge[x:y]) |
add_edge(G, x, y) |
adds an edge from node x to node y |
G.discard(Edge[x:y]) |
remove_edge(G, x, y) |
removes the edge from node x to node y |
del G[x:y] |
N/A | …, but raises EdgeError if there is no edge from node x to node y |
Edge[x:y] in G |
adjacent(G, x, y) |
tests whether there is an edge from node x to node y |
G[x:y] |
N/A | raises EdgeError if there is no edge from node x to node y |
list(G[x]) |
neighbors(G, x) |
lists all nodes y with an edge from node x to node y |
N/A | get_vertex_value(G, x) |
returns the value associated with node x |
N/A | set_vertex_value(G, x, v) |
sets the value associated with node x to v |
G[x:y] |
get_edge_value(G, x, y) |
returns the value associated with the edge x:y |
G[x:y] = w |
set_edge_value(G, x, y, v) |
sets the value associated with the edge x:y to v |
Note that there is no concept for associating a value to a node in a graph -
for a node x
, G[x]
is the adjacency list.
Instead, use a separate dict
to assign external values to nodes,
or set values directly on nodes.