Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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];
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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]];
};
Original file line number Diff line number Diff line change
@@ -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<string>();
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");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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];
Expand All @@ -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);
Expand Down
Loading