Algorithms#
Every algorithm is also available as ag.alg.<name>.
Traversal#
Nodes reachable from source in breadth-first order (source first). |
|
Edges of the breadth-first search tree rooted at source, in discovery order. |
|
Nodes grouped by hop distance from sources. |
|
Breadth-first search tree rooted at source, as a |
|
Nodes in depth-first preorder (a node before its descendants). |
|
Nodes in depth-first postorder (a node after all its descendants). |
|
Tree edges |
|
Depth-first search tree (forest when |
|
Every node reachable from node, excluding node itself. |
|
Every node from which node is reachable, excluding node itself. |
Shortest paths#
A shortest path from source to target, or to every reachable node. |
|
Length of a shortest path (hops, or total weight); same arguments as |
|
Dijkstra's algorithm: distances and shortest-path predecessors from source. |
|
Bellman–Ford shortest paths from source; negative weights allowed. |
|
A* search for a shortest path from source to target. |
|
Every shortest path from source to target (a lazy iterator). |
|
Distances between every pair of mutually reachable nodes, as |
|
All-pairs distances by Floyd–Warshall, vectorised over numpy. |
|
True when target is reachable from source (every node reaches itself). |
|
Every simple path (no repeated node) from source to target, lazily. |
|
The k shortest loopless paths from source to target (Yen's algorithm). |
Connectivity#
Connected components of an undirected graph, largest first. |
|
How many connected components an undirected graph has (0 for the empty graph). |
|
True when an undirected graph has exactly one connected component. |
|
The set of nodes in node's connected component (including node). |
|
Strongly connected components of a directed graph, largest first. |
|
Components of a directed graph when arc directions are ignored, largest first. |
|
How many strongly connected components a directed graph has. |
|
How many weakly connected components a directed graph has. |
|
True when every node reaches every other node (False for the empty graph). |
|
True when the graph is connected once arc directions are ignored (False if empty). |
|
The DAG obtained by contracting each strongly connected component to one node. |
|
Cut vertices of an undirected graph (removal disconnects their component), in graph order. |
|
Bridges of an undirected graph (edges whose removal disconnects), in edge order. |
|
Maximal biconnected subgraphs of an undirected graph, as node sets, largest first. |
|
True when the graph is connected and has no articulation point. |
DAGs and cycles#
True for a directed graph without directed cycles (undirected graphs: False). |
|
Nodes ordered so that every arc points forward. |
|
Every topological order of g, generated lazily by backtracking. |
|
Some cycle of g as a closed node list |
|
Every simple cycle of g, each once, as a closed node list |
|
A heaviest path of a DAG, as a node list. |
|
Length of |
|
Longest-path level of every node: sources are 0 and every arc climbs at least one level. |
|
Result of |
|
Critical-path method (CPM) with activities on the nodes. |
|
Graph with an arc |
|
The smallest DAG with the same reachability as the acyclic graph g. |
|
All lowest common ancestors of a and b in a DAG. |
|
Size of a largest antichain (set of pairwise unreachable nodes) of a DAG. |
|
A largest set of pairwise incomparable nodes of a DAG (its size is |
|
Fewest chains covering every node of a DAG exactly once (Dilworth decomposition). |
Centrality#
Degree divided by |
|
In-degree divided by |
|
Out-degree divided by |
|
Closeness |
|
Harmonic centrality |
|
Shortest-path betweenness (Brandes' algorithm, |
|
Betweenness of every edge: the share of shortest paths running through it. |
|
Eigenvector centrality (left Perron vector, unit Euclidean norm). |
|
Katz centrality |
|
PageRank by vectorised power iteration ( |
|
HITS hub and authority scores |
|
Several centralities in one call: |
Structure#
Edges present over edges possible: |
|
|
|
|
|
Mean of |
|
Share of arcs whose reverse arc also exists (directed graphs; self-loops never count). |
|
Number of triangles through each node (undirected graphs; self-loops ignored). |
|
Local clustering coefficient of every node. |
|
Mean of |
|
Global clustering |
|
Squares clustering |
|
Degree assortativity: Pearson correlation of the degrees at either end of each edge. |
|
Newman's assortativity for a categorical node attribute (missing values form a category). |
|
Pearson correlation of a numeric node attribute across edges (NaN when undefined). |
|
Greatest distance from each node (outgoing distances for directed graphs). |
|
Largest eccentricity ( |
|
Smallest eccentricity ( |
|
Nodes whose eccentricity equals the radius, in graph order. |
|
Nodes whose eccentricity equals the diameter, in graph order. |
|
Mean distance over all ordered pairs of distinct nodes (0.0 for a single node). |
|
Sum of distances over all (unordered, or ordered if directed) node pairs. |
|
Average inverse distance |
|
Mean over nodes of the global efficiency of the subgraph induced by their neighbors. |
|
k-core number of every node (Batagelj–Zaversnik bucket algorithm, |
|
Subgraph induced by the nodes of core number ≥ k ( |
|
Onion decomposition layer of every node (Hébert-Dufresne et al.; undirected). |
|
Rich-club coefficient |
|
True for a connected forest. |
|
True when the (underlying undirected) graph has no cycle: every component has |
|
True when all degrees agree (in- and out-degrees separately for directed graphs). |
|
|
|
Cheap headline statistics; never raises. |
Communities#
Modularity of a partition (networkx's definition, directed graphs included). |
|
|
|
|
|
Louvain community detection (Blondel et al. 2008) with node aggregation between levels. |
|
The Louvain dendrogram: the partition reached after every level, finest first. |
|
Greedy agglomerative modularity maximisation (Clauset–Newman–Moore). |
|
Asynchronous label propagation (Raghavan, Albert & Kumara 2007). |
|
Fluid communities (Parés et al. 2017): k communities compete by density. |
|
Divisive communities (Girvan & Newman 2002): yield a finer partition at each split. |
Flow#
A maximum flow: its |
|
A minimum |
|
Maximum |
|
Minimum |
Spanning trees#
Minimum spanning tree of an undirected graph, as a new |
|
Maximum spanning tree (forest) of an undirected graph; see |
|
Edges |
|
Edges |
Matching#
Maximum-cardinality matching of a bipartite graph (Hopcroft–Karp, O(m·√n)). |
|
Maximum-cardinality matching of a bipartite graph (Hopcroft–Karp, O(m·√n)). |
|
A maximal matching (no edge can be added), greedily in edge order. |
|
True when matching (a dict |
|
True when matching is a matching that no edge of g can extend. |
|
True when matching is a matching covering every node of g. |
|
Maximum-weight matching of a general graph (Edmonds' blossom algorithm, exact). |
|
Lightest matching among those of maximum cardinality. |
Coloring#
A proper vertex coloring by the greedy method: each node takes the smallest free color. |
|
True when the nodes split into two sides with every edge crossing (directions ignored). |
|
The two sides |
Link prediction#
Nodes adjacent to both u and v (other than u and v themselves), in u's neighbour order. |
|
|
|
|
|
|
|
|
|
The k most likely missing links as |
Matrices and spectra#
Dense adjacency matrix |
|
Row sums of the (symmetrised, for directed graphs) adjacency matrix. |
|
Graph Laplacian |
|
|
|
Eigenvalues of the adjacency matrix (real & sorted for undirected graphs, complex otherwise). |
|
Ascending eigenvalues of the (symmetrised) Laplacian. |
|
Second-smallest Laplacian eigenvalue (Fiedler value); 0 iff the graph is disconnected. |