Skip to content

Algorithms

Each function in this page takes an EdgeFrame as its first argument. Any other value raises TypeError where you call the function. See Errors. The node-valued functions return an expression that is also a lazy NodeFrame of (id, value), with every frame method. Inside with_columns(...) it is a column. See Expressions.

Over a filtered edge frame, a kernel runs on the subgraph view: the parent's nodes and the kept edges. Over a reshaped frame (distinct, group_by().agg(), a join, ...) it runs on the graph of the frame's rows. See Work on a subgraph.

Every kernel is deterministic. The same input and the same parameters give the same output, on any number of threads. The kernels that use randomness take a seed. seed=None uses a fixed default seed, so it also gives the same output on every call.

Node-valued kernels

ur.degree

ur.degree(edges, direction: str = "out")

The number of edges at each node. direction is "out", "in", or "both". Output type: uint32.

ur.pagerank

ur.pagerank(edges, damping: float = 0.85, max_iter: int = 30, tol: float = 1e-6,
            weight=None, dtype: str = "f64", direction: str = "out")

PageRank over the directed rows. The scores sum to 1. A node with no out-edges spreads its score to all nodes. With weight=, each out-edge takes a share of the score in proportion to its weight. direction="both" runs on the undirected view, which is nx.pagerank(G.to_undirected()). direction="in" is not supported. damping is between 0 and 1, max_iter at least 1 and tol at least 0 (tol=0 runs every iteration); a value outside its range raises ComputeError. Output type: float64, or float32 with dtype="f32".

ur.connected_components

ur.connected_components(edges, mode: str = "weak")

One integer label per node. mode="weak" ignores direction: two nodes share a label if a path connects them. mode="strong" follows direction: two nodes share a label only if each can reach the other. A component's label is the first-seen number of its first node: the position, among the node ids in the order they first appear in the edges (or in the vertex table), of the first of its nodes to appear. So a label depends only on the components and that order, never on how the index numbers its nodes internally or on the order the kernel works in. Group by the label to get the components.

ur.triangle_count

ur.triangle_count(edges)

The number of triangles each node is part of, on the undirected view of the rows.

ur.clustering_coefficient

ur.clustering_coefficient(edges, dtype: str = "f64")

The local clustering coefficient, on the undirected view. Output type: float64, or float32 with dtype="f32".

ur.closeness

ur.closeness(edges, weight=None, dtype: str = "f64", direction: str = "out")

Closeness centrality, following out-edges. The value is the number of reachable nodes divided by the sum of the distances to them. A node that reaches nothing scores 0.0. With weight=, the distances are weighted path costs. direction="in" measures the distance to the node instead (NetworkX's directed convention), and direction="both" ignores direction.

ur.betweenness

ur.betweenness(edges, sample: float | None = None, weight=None, seed: int | None = None,
               dtype: str = "f64", direction: str = "out")

Betweenness centrality (Brandes), not normalized. The default is directed: each ordered pair counts once. direction="both" runs on the undirected view and counts each unordered pair once, which is nx.betweenness_centrality(G.to_undirected(), normalized=False). direction="in" is not supported. The exact computation is O(n·m). sample= (a fraction in (0, 1]; any other number raises ComputeError) estimates it from a random subset of source nodes, scaled to the full count. seed makes the estimate reproducible. With weight=, the shortest paths are weighted.

ur.label_propagation

ur.label_propagation(edges, max_iter: int = 20, seed: int | None = None, parallel: bool = False)

Community detection by label propagation, on the undirected view. One integer label per node. max_iter is at least 1. The result is a heuristic: compare the modularity, not the labels.

parallel=True lets nodes vote in parallel sub-rounds, which is faster on a machine with many cores. A given seed still gives the same labels on any number of threads, but not the labels the serial default finds, and NetworkX has no equivalent of it. On the planted-partition graphs it is tested on, its modularity and community count are within 1% of the serial run's.

ur.louvain

ur.louvain(edges, weight=None, resolution: float = 1.0, seed: int | None = None, parallel: bool = False)

Community detection by Louvain modularity optimization, on the undirected view. One integer label per node. A larger resolution gives more, smaller communities; it must be above 0. With weight=, the modularity is weighted.

parallel=True moves nodes in parallel sub-rounds, which is faster on a machine with many cores. A given seed still gives the same communities on any number of threads, but not the communities the serial default finds, and NetworkX has no equivalent of it. Its modularity is within about 2% of the serial run's on the graphs it is tested on (karate, planted partitions, R-MAT).

Neighbour aggregation

ur.neighbors

ur.neighbors(edges, direction: str = "out", from_=None).agg(expr, dtype: str = "f64")

For each node, an aggregation of an attribute over its neighbours. expr is an aggregation expression, for example ur.col("age").mean(). The topology comes from edges. The attribute comes from the NodeFrame the expression runs in, which must be an attribute table, not edges.nodes(). from_= is not supported yet; see Limits.

mean, sum, min, and max need a numeric attribute. count and n_unique accept a string attribute too.

Traversals

Traversals return an EdgeFrame or a NodeFrame with rows of their own. The shared methods (filter, sort, head, ...) apply to the result, and a node-valued kernel can run over it.

ur.hop

ur.hop(edges, n: int = 1, direction: str = "out").from_(seeds) -> EdgeFrame

The nodes within n hops of each seed. seeds is an iterable of ids, or a NodeFrame whose id column gives them. An id is an integer or a string. On a graph of integer ids, a string that spells an integer, such as '7', is that integer, and any other string is an error. On a graph of string ids, an integer is its string. A float, a bool or any other value raises TypeError. A seed that is not in the graph reaches nothing. The result has one row per (seed, reached) pair, in the columns src and dst, in no particular order (it can differ from one run to another; sort it when the order matters). direction is "out", "in", or "both".

ur.shortest_path

ur.shortest_path(edges, source, target, weight=None, direction: str = "out") -> EdgeFrame

One shortest path from source to target. Each is one node id, given as a seed of hop is; a sequence holding exactly one id is that id, and a sequence of several raises ValueError. The result has one row per edge on the path, in order, with the columns src, dst, hop, and cost. hop is the 0-based position. cost is the total cost from source to that edge's dst. Without weight=, the path has the fewest edges and cost is hop + 1. With weight=, the path has the least total weight (Dijkstra), and weights must not be negative. If there is no path, the result is empty.

ur.random_walk

ur.random_walk(edges, start, steps: int, walks_per_node: int = 1, seed: int | None = None) -> NodeFrame

Random walks along out-edges. start is an iterable of ids or a NodeFrame, as the seeds of hop. The result has the columns walk_id, step, and node, one row per position on each walk. A walk stops early at a node with no out-edges. seed makes the walks reproducible.

The direction parameter

direction is "out" (the default), "in", or "both". "out" follows each row from src to dst, and "in" follows it backward.

"both" reads the rows as an undirected graph. For degree, neighbors, hop, shortest_path, and closeness, a walk follows each row in either direction. For pagerank and betweenness, the kernel runs on the simple undirected view: one edge for each distinct pair of nodes joined by a row in either direction, with self-loops dropped. With weight=, such a pair takes the smallest of its rows' weights. Use "both" when your data is undirected and stores each edge once. See Semantics vs NetworkX.

The weight parameter

weight is an expression over the columns of edges, for example ur.col("amount") or ur.col("amount") * ur.col("fx"). It gives one float64 per edge. A null weight is an error. See Weight the edges.

The dtype parameter

dtype="f32" emits a float-valued column as float32. The kernel still computes in float64 and casts on output. The default is "f64".