From 8ce4464b0913628c3d92109742ebe73782a10547 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Mon, 2 Nov 2026 08:50:36 +0000 Subject: [PATCH 1/2] test: add seeded seed pair selection helper selectSeedPair draws two distinct nodes from a caller-supplied random source. It picks the second index from the remaining positions instead of retrying on a collision, so the number of draws per pair is fixed. --- .../validation/common/seed-pair-selection.ts | 27 +++++++++++ .../common/seed-pair-selection.unit.test.ts | 48 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.ts create mode 100644 src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.unit.test.ts diff --git a/src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.ts b/src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.ts new file mode 100644 index 0000000..d9f1cc4 --- /dev/null +++ b/src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.ts @@ -0,0 +1,27 @@ +/** + * Seeded selection of seed pairs for multi-pair comparison tests. + * + * The pair is drawn from a caller-supplied generator so the pairs a test compares are identical on every run. + */ + +import type { RandomSource } from "./graph-perturbation"; + +/** + * Selects two distinct nodes uniformly at random. + * @param nodeIds - Candidate node ids, at least two + * @param random - Source of randomness, such as `createSeededRng(seed)` + * @returns A pair of distinct ids drawn from `nodeIds` + * @throws If `nodeIds` holds fewer than two entries + */ +export const selectSeedPair = (nodeIds: readonly string[], random: RandomSource): [string, string] => { + if (nodeIds.length < 2) { + throw new Error(`A seed pair needs at least two nodes, received ${nodeIds.length}`); + } + + const index1 = Math.floor(random() * nodeIds.length); + // Drawing from the n - 1 remaining positions and skipping index1 is uniform and needs no retry loop. + const offset = Math.floor(random() * (nodeIds.length - 1)); + const index2 = offset >= index1 ? offset + 1 : offset; + + return [nodeIds[index1], nodeIds[index2]]; +}; diff --git a/src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.unit.test.ts b/src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.unit.test.ts new file mode 100644 index 0000000..f1086d6 --- /dev/null +++ b/src/experiments/evaluation/__tests__/validation/common/seed-pair-selection.unit.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "vitest"; + +import { createSeededRng } from "../../../../proofs/test-utils"; +import { selectSeedPair } from "./seed-pair-selection"; + +const NODE_IDS = ["a", "b", "c", "d", "e", "f"]; + +describe("selectSeedPair", () => { + it("returns the same pairs for the same seed", () => { + const draw = (): Array<[string, string]> => { + const random = createSeededRng(7); + return Array.from({ length: 20 }, () => selectSeedPair(NODE_IDS, random)); + }; + + expect(draw()).toEqual(draw()); + }); + + it("always returns two distinct members of the input", () => { + const random = createSeededRng(3); + for (let trial = 0; trial < 500; trial++) { + const [first, second] = selectSeedPair(NODE_IDS, random); + expect(first).not.toBe(second); + expect(NODE_IDS).toContain(first); + expect(NODE_IDS).toContain(second); + } + }); + + it("returns both nodes when only two exist", () => { + const random = createSeededRng(1); + for (let trial = 0; trial < 50; trial++) { + expect([...selectSeedPair(["x", "y"], random)].sort()).toEqual(["x", "y"]); + } + }); + + it("covers every ordered pair over enough draws", () => { + const random = createSeededRng(11); + const seen = new Set(); + for (let trial = 0; trial < 2000; trial++) { + seen.add(selectSeedPair(NODE_IDS, random).join(">")); + } + + expect(seen.size).toBe(NODE_IDS.length * (NODE_IDS.length - 1)); + }); + + it("rejects fewer than two nodes", () => { + expect(() => selectSeedPair(["only"], createSeededRng(1))).toThrow("at least two nodes"); + }); +}); From 9a0ea9dfab646b80f35baedff028a6ad94896bbf Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Mon, 2 Nov 2026 08:50:52 +0000 Subject: [PATCH 2/2] fix(test): seed the seed pair selection in the variability tests The multiple seed pair comparison chose its pairs with Math.random, so the pairs, and the path diversity statistics computed from them, differed on every run. Both copies of the test now draw their pairs from a fixed seeded generator, as the perturbation tests do. --- .../thesis-validation.exp.integration.test.ts | 13 ++++++------- .../variability-injection.exp.integration.test.ts | 14 +++++++------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts b/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts index 64aee88..ea9eaa7 100644 --- a/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts +++ b/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts @@ -26,6 +26,7 @@ import { loadBenchmarkFromContent, } from "../fixtures/benchmark-datasets"; import { addRandomEdges, perturbEdges } from "./validation/common/graph-perturbation"; +import { selectSeedPair } from "./validation/common/seed-pair-selection"; // ============================================================================ // Benchmark Graph Adapter @@ -753,6 +754,8 @@ describe("Thesis Validation: Application Metrics", () => { describe("Thesis Validation: Variability Injection", () => { describe("Multiple Seed Pair Analysis", () => { + const SEED_PAIR_SEED = 1; + /** * Test across multiple random seed pairs to assess consistency of performance. * This provides better statistical power than single-seed tests. @@ -763,6 +766,8 @@ describe("Thesis Validation: Variability Injection", () => { const allNodes = expander.getAllNodeIds(); const numberSeedPairs = 10; + /** Fixed so the compared seed pairs are identical on every run. */ + const random = createSeededRng(SEED_PAIR_SEED); const results: Array<{ seeds: [string, string]; @@ -774,13 +779,7 @@ describe("Thesis Validation: Variability Injection", () => { for (let index = 0; index < numberSeedPairs; index++) { // Select random seed pair - const index1 = Math.floor(Math.random() * allNodes.length); - let index2 = Math.floor(Math.random() * allNodes.length); - while (index2 === index1) { - index2 = Math.floor(Math.random() * allNodes.length); - } - - const seeds: [string, string] = [allNodes[index1], allNodes[index2]]; + const seeds = selectSeedPair(allNodes, random); const dp = new DegreePrioritisedExpansion(expander, seeds); const bfs = new StandardBfsExpansion(expander, seeds); diff --git a/src/experiments/evaluation/__tests__/validation/comparative/variability-injection.exp.integration.test.ts b/src/experiments/evaluation/__tests__/validation/comparative/variability-injection.exp.integration.test.ts index 074010d..9b69534 100644 --- a/src/experiments/evaluation/__tests__/validation/comparative/variability-injection.exp.integration.test.ts +++ b/src/experiments/evaluation/__tests__/validation/comparative/variability-injection.exp.integration.test.ts @@ -3,12 +3,16 @@ import { describe, expect, it } from "vitest"; import { DegreePrioritisedExpansion } from "../../../../../algorithms/traversal/degree-prioritised-expansion"; import { RandomPriorityExpansion } from "../../../../../experiments/baselines/random-priority" import { StandardBfsExpansion } from "../../../../../experiments/baselines/standard-bfs" +import { createSeededRng } from "../../../../proofs/test-utils"; import { loadBenchmarkByIdFromUrl } from "../../../fixtures/benchmark-datasets"; import { BenchmarkGraphExpander } from "../common/benchmark-graph-expander"; +import { selectSeedPair } from "../common/seed-pair-selection"; import { cohensD, confidenceInterval, mannWhitneyUTest, pathDiversity } from "../common/statistical-functions"; describe("Thesis Validation: Variability Injection", () => { describe("Multiple Seed Pair Analysis", () => { + const SEED_PAIR_SEED = 1; + /** * Test across multiple random seed pairs to assess consistency of performance. * This provides better statistical power than single-seed tests. @@ -19,6 +23,8 @@ describe("Thesis Validation: Variability Injection", () => { const allNodes = expander.getAllNodeIds(); const numberSeedPairs = 10; + /** Fixed so the compared seed pairs are identical on every run. */ + const random = createSeededRng(SEED_PAIR_SEED); const results: Array<{ seeds: [string, string]; @@ -30,13 +36,7 @@ describe("Thesis Validation: Variability Injection", () => { for (let index = 0; index < numberSeedPairs; index++) { // Select random seed pair - const index1 = Math.floor(Math.random() * allNodes.length); - let index2 = Math.floor(Math.random() * allNodes.length); - while (index2 === index1) { - index2 = Math.floor(Math.random() * allNodes.length); - } - - const seeds: [string, string] = [allNodes[index1], allNodes[index2]]; + const seeds = selectSeedPair(allNodes, random); const dp = new DegreePrioritisedExpansion(expander, seeds); const bfs = new StandardBfsExpansion(expander, seeds);