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 @@ -19,11 +19,13 @@ import type { GraphExpander, Neighbor } from "../../../interfaces/graph-expander
import { FrontierBalancedExpansion } from "../../baselines/frontier-balanced";
import { RandomPriorityExpansion } from "../../baselines/random-priority";
import { StandardBfsExpansion } from "../../baselines/standard-bfs";
import { createSeededRng } from "../../proofs/test-utils";
import {
createBenchmarkMeta,
loadBenchmarkByIdFromUrl,
loadBenchmarkFromContent,
} from "../fixtures/benchmark-datasets";
import { addRandomEdges, perturbEdges } from "./validation/common/graph-perturbation";

// ============================================================================
// Benchmark Graph Adapter
Expand Down Expand Up @@ -1203,61 +1205,15 @@ describe("Thesis Validation: Summary", () => {
// =========================================================================

describe("Thesis Validation: Perturbed Graph Robustness", () => {
/**
* Create a perturbed version of a graph by removing random edges.
* @param edges
* @param removeFraction
*/
const perturbEdges = (
edges: Array<{ source: string; target: string }>,
removeFraction: number
): Array<{ source: string; target: string }> => {
const shuffled = [...edges].sort(() => Math.random() - 0.5);
const keepCount = Math.floor(edges.length * (1 - removeFraction));
return shuffled.slice(0, keepCount);
};

/**
* Create a perturbed version by adding random edges.
* @param edges
* @param nodeIds
* @param addFraction
*/
const addRandomEdges = (
edges: Array<{ source: string; target: string }>,
nodeIds: string[],
addFraction: number
): Array<{ source: string; target: string }> => {
const addCount = Math.floor(edges.length * addFraction);
const newEdges: Array<{ source: string; target: string }> = [...edges];
const existing = new Set(edges.map((e) => `${e.source}-${e.target}`));

let added = 0;
let attempts = 0;
const maxAttempts = addCount * 10;

while (added < addCount && attempts < maxAttempts) {
const source = nodeIds[Math.floor(Math.random() * nodeIds.length)];
const target = nodeIds[Math.floor(Math.random() * nodeIds.length)];
const key = `${source}-${target}`;

if (source !== target && !existing.has(key)) {
newEdges.push({ source, target });
existing.add(key);
added++;
}
attempts++;
}

return newEdges;
};
/** Fixed so the perturbed graphs, and therefore the method ranking, are identical on every run. */
const PERTURBATION_SEED = 1;

it("should maintain performance under edge removal", async () => {
const benchmark = await loadBenchmarkByIdFromUrl("karate");
const originalEdges = benchmark.graph.getAllEdges();

// Remove 10% of edges
const perturbedEdges = perturbEdges(originalEdges, 0.1);
const perturbedEdges = perturbEdges(originalEdges, 0.1, createSeededRng(PERTURBATION_SEED));

const perturbedMeta = createBenchmarkMeta({
id: "karate-perturbed-removed",
Expand Down Expand Up @@ -1294,7 +1250,7 @@ describe("Thesis Validation: Summary", () => {
const allNodes = benchmark.graph.getAllNodes().map((n) => n.id);

// Add 20% more random edges
const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2);
const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2, createSeededRng(PERTURBATION_SEED));

const perturbedMeta = createBenchmarkMeta({
id: "karate-perturbed-added",
Expand Down Expand Up @@ -1327,13 +1283,14 @@ describe("Thesis Validation: Summary", () => {
const benchmark = await loadBenchmarkByIdFromUrl("lesmis");
const originalEdges = benchmark.graph.getAllEdges();

const random = createSeededRng(PERTURBATION_SEED);
const results: Array<{ perturbation: string; dpDiversity: number; bfsDiversity: number }> = [];

// Test original and two perturbed versions
const perturbations = [
{ name: "original", edges: originalEdges },
{ name: "5% removed", edges: perturbEdges(originalEdges, 0.05) },
{ name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1) },
{ name: "5% removed", edges: perturbEdges(originalEdges, 0.05, random) },
{ name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1, random) },
];

for (const { name, edges } of perturbations) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Seeded graph perturbation helpers for robustness tests.
*
* Every random choice is drawn from a caller-supplied generator so a test's
* perturbed graphs are identical on every run.
*/

export interface PerturbableEdge {
source: string;
target: string;
}

/**
* Pseudo-random source returning values in [0, 1), such as `createSeededRng(seed)`.
*/
export type RandomSource = () => number;

/**
* Creates a perturbed copy of an edge list by removing a random subset of edges.
* The subset is chosen by a Fisher-Yates shuffle, so every subset of the kept size is equally likely.
* @param edges - Original edges, left unmodified
* @param removeFraction - Fraction of edges to remove, in [0, 1]
* @param random - Source of randomness
* @returns The `floor(edges.length * (1 - removeFraction))` kept edges, in shuffled order
*/
export const perturbEdges = <E extends PerturbableEdge>(
edges: readonly E[],
removeFraction: number,
random: RandomSource,
): E[] => {
const shuffled = [...edges];
for (let index = shuffled.length - 1; index > 0; index--) {
const swapIndex = Math.floor(random() * (index + 1));
[shuffled[index], shuffled[swapIndex]] = [shuffled[swapIndex], shuffled[index]];
}

return shuffled.slice(0, Math.floor(edges.length * (1 - removeFraction)));
};

/**
* Creates a perturbed copy of an edge list by adding random non-loop edges that are not already present.
* Gives up after ten attempts per requested edge, so a near-complete graph may gain fewer edges than requested.
* @param edges - Original edges, left unmodified
* @param nodeIds - Nodes new edges may connect
* @param addFraction - Number of edges to add as a fraction of `edges.length`
* @param random - Source of randomness
* @returns The original edges followed by the added edges
*/
export const addRandomEdges = <E extends PerturbableEdge>(
edges: readonly E[],
nodeIds: readonly string[],
addFraction: number,
random: RandomSource,
): Array<E | PerturbableEdge> => {
const addCount = Math.floor(edges.length * addFraction);
const newEdges: Array<E | PerturbableEdge> = [...edges];
const existing = new Set(edges.map((e) => `${e.source}-${e.target}`));

let added = 0;
let attempts = 0;
const maxAttempts = addCount * 10;

while (added < addCount && attempts < maxAttempts) {
const source = nodeIds[Math.floor(random() * nodeIds.length)];
const target = nodeIds[Math.floor(random() * nodeIds.length)];
const key = `${source}-${target}`;

if (source !== target && !existing.has(key)) {
newEdges.push({ source, target });
existing.add(key);
added++;
}
attempts++;
}

return newEdges;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,26 @@ import { describe, expect, it } from "vitest";

import { DegreePrioritisedExpansion } from "../../../../../algorithms/traversal/degree-prioritised-expansion";
import { StandardBfsExpansion } from "../../../../../experiments/baselines/standard-bfs"
import { createSeededRng } from "../../../../proofs/test-utils";
import {
createBenchmarkMeta,
loadBenchmarkByIdFromUrl,
loadBenchmarkFromContent,
} from "../../../fixtures/benchmark-datasets";
import { BenchmarkGraphExpander } from "../common/benchmark-graph-expander";
import { addRandomEdges, perturbEdges } from "../common/graph-perturbation";
import { pathDiversity } from "../common/statistical-functions";

describe("Thesis Validation: Perturbed Graph Robustness", () => {
/**
* Create a perturbed version of a graph by removing random edges.
* @param edges
* @param removeFraction
*/
const perturbEdges = (
edges: Array<{ source: string; target: string }>,
removeFraction: number
): Array<{ source: string; target: string }> => {
const shuffled = [...edges].sort(() => Math.random() - 0.5);
const keepCount = Math.floor(edges.length * (1 - removeFraction));
return shuffled.slice(0, keepCount);
};

/**
* Create a perturbed version by adding random edges.
* @param edges
* @param nodeIds
* @param addFraction
*/
const addRandomEdges = (
edges: Array<{ source: string; target: string }>,
nodeIds: string[],
addFraction: number
): Array<{ source: string; target: string }> => {
const addCount = Math.floor(edges.length * addFraction);
const newEdges: Array<{ source: string; target: string }> = [...edges];
const existing = new Set(edges.map((e) => `${e.source}-${e.target}`));

let added = 0;
let attempts = 0;
const maxAttempts = addCount * 10;

while (added < addCount && attempts < maxAttempts) {
const source = nodeIds[Math.floor(Math.random() * nodeIds.length)];
const target = nodeIds[Math.floor(Math.random() * nodeIds.length)];
const key = `${source}-${target}`;

if (source !== target && !existing.has(key)) {
newEdges.push({ source, target });
existing.add(key);
added++;
}
attempts++;
}

return newEdges;
};
/** Fixed so the perturbed graphs, and therefore the method ranking, are identical on every run. */
const PERTURBATION_SEED = 1;

describe("Thesis Validation: Perturbed Graph Robustness", () => {
it("should maintain performance under edge removal", async () => {
const benchmark = await loadBenchmarkByIdFromUrl("karate");
const originalEdges = benchmark.graph.getAllEdges();

// Remove 10% of edges
const perturbedEdges = perturbEdges(originalEdges, 0.1);
const perturbedEdges = perturbEdges(originalEdges, 0.1, createSeededRng(PERTURBATION_SEED));

const perturbedMeta = createBenchmarkMeta({
id: "karate-perturbed-removed",
Expand Down Expand Up @@ -110,7 +66,7 @@ describe("Thesis Validation: Perturbed Graph Robustness", () => {
const allNodes = benchmark.graph.getAllNodes().map((n) => n.id);

// Add 20% more random edges
const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2);
const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2, createSeededRng(PERTURBATION_SEED));

const perturbedMeta = createBenchmarkMeta({
id: "karate-perturbed-added",
Expand Down Expand Up @@ -144,13 +100,14 @@ describe("Thesis Validation: Perturbed Graph Robustness", () => {
const benchmark = await loadBenchmarkByIdFromUrl("lesmis");
const originalEdges = benchmark.graph.getAllEdges();

const random = createSeededRng(PERTURBATION_SEED);
const results: Array<{ perturbation: string; dpDiversity: number; bfsDiversity: number }> = [];

// Test original and two perturbed versions
const perturbations = [
{ name: "original", edges: originalEdges },
{ name: "5% removed", edges: perturbEdges(originalEdges, 0.05) },
{ name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1) },
{ name: "5% removed", edges: perturbEdges(originalEdges, 0.05, random) },
{ name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1, random) },
];

for (const { name, edges } of perturbations) {
Expand Down
Loading