Repository navigation
feat(export): export edge weights for weighted graph algorithms - #200
Merged
Merged
Conversation
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.
Deploying lance-csr with
|
| 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 |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_graphreturned 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'scombine_edgesfor repeated edges:Weights only on request.
weightnames a numeric column that every included relationship type must have. The export then returnsout_weights/in_weightsasfloat64arrays aligned without_indices/in_indices. Without it, the export sorts plainu64keys, reads no extra column, and both fields areNone. A weighted export sorts(key, weight)pairs instead.Several relationship types.
weightalso takes a mapping from relationship type to column, so types can store weights under different names, anddefault_weightgives 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.Repeated edges. An edge listed in several rows gets one weight:
"sum"(default; counts, amounts),"min"(distances, costs) or"max". An undirected export treatsa -> bandb -> aas one edge and combines them the same way. A self-loop's weight counts once. An unknown rule raisesValueErrorin Python andInvalidArgumentin 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 }, withweighta string or an object) and fields (outWeights,inWeightsasFloat64vectors). Rust getsEdgeWeights(columns, default, combine rule) and aCombineEdgesenum.Docs: a table of every
export_graphparameter 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 testexport tests andmake 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 withuv 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_graphabove 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.mainUnweighted 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