Skip to content

SetGraphOptions looks up each element's options in a table - #11

Merged
rocky merged 1 commit into
masterfrom
set-graph-options-index
Nov 4, 2026
Merged

rocky merged 1 commit into
masterfrom
set-graph-options-index

Conversation

@tombouctou

@tombouctou tombouctou commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

SetGraphOptions for vertices, and for edges with the tag All, found each element's options with a Position over all the groups of options. That ran once per vertex or edge, so the cost was the square of their number. SetEdgeWeights goes through it, and on a 30×30 grid graph (1740 edges) it took most of the time.

Change

  • The group of each key is entered once in a table: the DownValues of a local symbol, cleared afterwards.
  • Groups are entered last to first, so a key in several groups gets the first one, as Position[...][[1, 1]] gave.
  • The One variant (multigraphs, where each match is used once) is unchanged.

Check: results are identical to the old version for 10 cases: edge and vertex options, groups with several keys, a key in several groups, undirected edges given in reverse order, and keys without options.

Timings (on kimmy, Mathics3 with the MATH-425 changes, mathics-core #2099–#2103):

before after
SetEdgeWeights[GridGraph[30, 30]] 59 s 42 s
MinimumSpanningTree[SetEdgeWeights[GridGraph[30, 30]]] 132 s 118 s

The lookups are linear only once mathics-core finds stored values through its index again; mathics-core #2104 fixes that.

Tests: the package tests pass with COMBINATORICA_BOOK_SECS=5: 804 passed, 0 failed. They also give 21 XPASS, because Mathics3 there already has fixes that book/xfail.json on master still marks as pending; #10 updates that file.

🤖 Generated with Claude Code

SetGraphOptions for vertices, and for edges with the tag All, found the
options of each vertex or edge with a Position over all the groups of
options: once per element, so the square of their number. SetEdgeWeights
goes through it, and on a 30x30 grid graph (1740 edges) that was most of
its time.

Now the group of each key is entered once in a table, the DownValues of
a local symbol, cleared afterwards. Groups are entered last to first, so
that a key in several groups gets the first one, as Position gave.

SetEdgeWeights[GridGraph[15, 15]]: 10.9 s -> 3.3 s (Mathics3 with the
MATH-425 changes). The results are the same for edge and vertex options,
groups with several keys, a key in several groups and keys without
options.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rocky

rocky commented Nov 4, 2026

Copy link
Copy Markdown
Member

This changes the source code (for the better, probably even on WMA). I heard from Steve Skiena a while back that it was okay to change source code. Sriram V. Pemmaraju I've never heard from even though I have tried to contact him a couple of times.

@rocky

rocky commented Nov 4, 2026

Copy link
Copy Markdown
Member

LGTM and thanks!

@rocky
rocky merged commit 26cbc73 into master Nov 4, 2026
1 check passed
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.

2 participants