Skip to content

feat(export): export edge weights for weighted graph algorithms - #200

Merged
prrao87 merged 8 commits into
mainfrom
feat/graph-export-weights
Nov 25, 2026
Merged

prrao87 merged 8 commits into
mainfrom
feat/graph-export-weights

Conversation

@prrao87

@prrao87 prrao87 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Why

Graph libraries such as icebug (via Graph.fromCSR), igraph and scipy run weighted algorithms (Dijkstra, weighted PageRank, community detection) when each CSR entry comes with a weight. export_graph returned only the structure, so there was no way to get weights out. This PR adds them as an opt-in, so exports that don't ask for weights use no extra memory.

What changed

The API follows networkx's weight="..." keyword and igraph's combine_edges for repeated edges:

g = engine.export_graph(weight="cost", combine_edges="min")
graph = ib.Graph.fromCSR(g.num_nodes, g.directed, g.out_indices, g.out_indptr,
                         g.in_indices, g.in_indptr, g.out_weights, g.in_weights)
  • Weights only on request. weight names a numeric column that every included relationship type must have. The export then returns out_weights / in_weights as float64 arrays aligned with out_indices / in_indices. Without it, the export sorts plain u64 keys, reads no extra column, and both fields are None. A weighted export sorts (key, weight) pairs instead.

  • Several relationship types. weight also takes a mapping from relationship type to column, so types can store weights under different names, and default_weight gives the weight of every edge of a type that has no column: one the mapping leaves out, or one without the shared column. Without a default, such a type is an error, so a guessed weight never changes a result silently. A column the mapping names must exist even with a default, and a mapping entry for a type that isn't exported is an error. Columns other than the endpoints and the weight are never read.

    engine.export_graph(weight={"ROAD": "km", "FERRY": "minutes"})
    engine.export_graph(weight={"ROAD": "km"}, default_weight=1.0)
  • Repeated edges. An edge listed in several rows gets one weight: "sum" (default; counts, amounts), "min" (distances, costs) or "max". An undirected export treats a -> b and b -> a as one edge and combines them the same way. A self-loop's weight counts once. An unknown rule raises ValueError in Python and InvalidArgument in TypeScript.

  • Null weights. An edge with a null weight is left out and counted in dropped_edges, the same as an edge whose endpoint key is missing. Substituting 1.0 would make a costly edge look cheap to a shortest-path search.

  • In-edges by transpose, for every export. A directed export used to sort a second, reversed copy of the edges to get the in-edge CSR. Weighted and unweighted exports now sort only the out-edges and derive the in-edges with one counting-and-scatter pass, which keeps each in-edge row sorted and free of duplicates. Undirected exports push each reverse edge into the same vector instead of appending a second one. The two paths share this code; only the sort differs (keys alone, or keys with weights).

  • TypeScript gets the same options ({ weight, combineEdges, defaultWeight }, with weight a string or an object) and fields (outWeights, inWeights as Float64 vectors). Rust gets EdgeWeights (columns, default, combine rule) and a CombineEdges enum.

  • Docs: a table of every export_graph parameter where the graph algorithms page introduces it (and a CSS rule so code in a table's first column no longer breaks mid-word), a "Weighted graphs" section, the icebug call with weights, updated memory limits, the Python-to-TypeScript table and regenerated TypeDoc. outweights (a TypeDoc anchor) is added to the typos allowlist.

Commits are split by concern: weights on request, the combine rule, null weights, then the memory work (one vector for undirected edges, the docs numbers, the in-edge transpose), then per-type weight columns and the default weight.

Validation

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt: pass.

  • Rust, Python and Node tests cover mixed relationship types: a shared column with a default, a column per type, a mapping with a default, and each error case.

  • A randomized Rust test checks the transpose against sorting the reversed edges.

  • make test export tests and make lint (ruff, pyright): pass. The two icebug tests (unweighted PageRank and components, and a new weighted Dijkstra check that picks 0→1→2 at cost 2 over the direct edge at cost 5) pass with uv run --with icebug==13.6 --with networkx. They skip in the dev env.

  • npm test (88 pass), npm run lint, docs/check_snippets.py, zensical build --strict, typos: pass.

  • Peak memory on a synthetic graph (4M nodes, 40M edges, random endpoints and weights), release wheels, the committed branch before the transpose vs after, interleaved runs. Measured as peak physical footprint during export_graph above the footprint after opening the engine. Footprint is used because macOS keeps large freed blocks in a process's footprint, so a design that frees and reallocates looks smaller in a live-data count than it is. Another job was running, load 4 to 7: memory repeated within 30 MiB, and times are indicative only.

    Export main Before transpose This PR
    Directed, unweighted 784 MiB 784 MiB, 1.33 s 784 MiB, 1.40 s
    Undirected, unweighted 1090 MiB 1090 MiB 785 MiB
    Directed, weighted n/a 2185 MiB 1897 MiB
    Undirected, weighted n/a 2195 MiB 2190 MiB

    Unweighted exports use no more memory than on main, and undirected ones use 28% less. Directed unweighted is about 5% slower: the transpose's scatter is sequential, where the second sort it replaces ran in parallel. A weighted export peaks at 2.4x (directed) to 2.8x (undirected) an unweighted one. Its final arrays are already twice the size; the rest is the sort's pairs and blocks the allocator keeps after a free.

  • All four exports (directed and undirected, with and without weights) are byte-identical before and after the transpose (SHA-256 of every array). On the weighted 40M-edge export, icebug's weighted PageRank (0.77 s) and Dijkstra from one source (4.1 s, 4.0M nodes reached) run, and the weights change PageRank scores relative to the unweighted graph.

  • Not covered: icebug's weighted community detection or centrality beyond Dijkstra.

Follow-up: #202, exporting an edge list or Cypher result (a filtered subgraph) with the same rules. Other possible follow-ups: parallelize the transpose to recover the 5% on directed unweighted exports; measure on Linux, where glibc returns large freed blocks to the OS and the weighted peaks should be lower.

🤖 Generated with Claude Code

export_graph takes a `weight` column name (Python keyword, `options.weight`
in TypeScript) and returns `out_weights` / `in_weights` as float64 arrays
aligned with `out_indices` / `in_indices`, the form icebug's `fromCSR`,
scipy and igraph take.

Only a weighted export pays for weights: it reads the extra column and
sorts (key, weight) pairs, 16 bytes per edge instead of 8. Unweighted
exports keep the existing u64-only path and allocate nothing for weights.

Duplicate edges get the sum of their weights. In an undirected export,
a -> b and b -> a combine the same way, and a self-loop's weight counts
once. Null weights are an error for now.
A weighted export lists each edge once, so an edge in several rows, or in
an undirected export both a -> b and b -> a, needs one weight.
`combine_edges` picks the rule: "sum" (the default; counts, amounts),
"min" (distances, costs) or "max", after igraph's `combine_edges`.
It is ignored without `weight`. An unknown rule is a ValueError in Python
and an InvalidArgument error in TypeScript.

Rust: `CombineEdges` enum, exported from the crate, passed to
`CypherEngine::export_graph`.
A null weight is unknown, and no default stands in for it safely: 1.0
would make a costly edge look cheap to a shortest-path search. A weighted
export now leaves such an edge out and counts it in `dropped_edges`, as
it does an edge whose endpoint key is missing, instead of failing the
whole export. Unweighted exports ignore the column and drop nothing extra.
Pushing each reverse edge straight into the out vector, instead of a
second vector appended before the sort, avoids holding both at once:
about 0.6 GB less peak memory for a 40M-edge undirected weighted export.
Measured on a 40M-edge graph: an unweighted export peaks at the same
memory as before weights were added, and a weighted one at up to about
three times that.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

Latest commit: b5a2504
Status: ✅  Deploy successful!
Preview URL: https://e27f2cf2.lance-csr.pages.dev
Branch Preview URL: https://feat-graph-export-weights.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 25, 2026 00:14
Directed exports sorted a second copy of the edges, reversed, to get the
in-edge CSR. Both paths, weighted and unweighted, now sort only the
out-edges and derive the in-edges in one counting-and-scatter pass,
which keeps in-edge rows sorted and duplicate-free. Undirected unweighted
exports also push reverse edges into the out vector instead of appending
a second one, as the weighted path already did. The CSR offset count is
shared by the three builders.

Peak physical footprint at 40M edges (release, macOS, load 4 to 7),
before -> after: directed unweighted 784 -> 784 MiB (about 5% slower:
the scatter is sequential where the second sort was parallel); undirected
unweighted 1090 -> 785 MiB; directed weighted 2185 -> 1897 MiB; undirected
weighted unchanged. All four exports are byte-identical before and after.
An export over several relationship types needed one weight column name
that every type had. `weight` now also takes a mapping from relationship
type to column, so types can store weights under different names, and
`default_weight` (`defaultWeight` in TypeScript) gives the weight of
every edge of a type without a column: one the mapping leaves out, or
one without the shared column.

Without a default, a type with no weight column is still an error, so a
guessed weight never changes an algorithm's answer silently. A column
the mapping names must exist even with a default, a mapping entry for a
type that isn't exported is an error, and so is a shared column that no
type has. `default_weight` without `weight` is an error.

Rust: `EdgeWeights` (column, per-type columns, default, combine rule)
replaces the separate weight and combine arguments of
`CypherEngine::export_graph`.
The graph algorithms page explained each parameter in the section that
uses it, so a reader had to know where to look. A table under the
introduction now lists every parameter, its default and what it
controls, linking to the section that explains it.

Table cells broke inline code between any two letters, which split
parameter names such as combine_edges across lines. Code in a table's
first column now breaks only at spaces; no table on the site overflows
at desktop width with the change.
@prrao87
prrao87 merged commit cdc4e93 into main Nov 25, 2026
10 checks passed
@prrao87
prrao87 deleted the feat/graph-export-weights branch November 25, 2026 01:00
prrao87 added a commit that referenced this pull request Nov 26, 2026
Minor bump: #200 adds a weights argument to the Rust
CypherEngine::export_graph, and #198 changes the rows some
variable-length queries return.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant