Skip to content
Closed
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
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

172 changes: 171 additions & 1 deletion src/core/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type {
GraphBuilderAnalysis,
GraphBuilderConfidence,
GraphBuilderEdge,
GraphBuilderGraph,
GraphBuilderNode
} from "./types.js";
import { buildAdjacency } from "./query.js";

const BETWEENNESS_NODE_LIMIT = 500;

export function analyzeGraph(graph: GraphBuilderGraph, topN = 10): GraphBuilderAnalysis {
const adjacency = buildAdjacency(graph);
const communities = assignCommunities(graph, adjacency);
Expand Down Expand Up @@ -51,13 +54,180 @@ export function analyzeGraph(graph: GraphBuilderGraph, topN = 10): GraphBuilderA
reason: edge.confidence === "INFERRED" ? "Cross-source inferred reference" : "Cross-source structural connection"
}));

const pageRank = computePageRank(graph);
const betweenness = graph.nodes.length <= BETWEENNESS_NODE_LIMIT ? computeBetweenness(graph, adjacency) : undefined;
const bridgeNodeIds = detectBridgeNodes(graph, adjacency);
const bridgeNodes = bridgeNodeIds
.map((id) => {
const node = nodeMap.get(id);
return node ? { id, label: node.label, degree: degrees.get(id) ?? 0 } : null;
})
.filter((entry): entry is { id: string; label: string; degree: number } => entry !== null)
.sort((left, right) => right.degree - left.degree);

return {
godNodes,
confidenceBreakdown,
isolatedNodes,
surprisingConnections,
communities
communities,
pageRank,
betweenness,
bridgeNodes
};
}

function computePageRank(graph: GraphBuilderGraph, iterations = 30, damping = 0.85): Record<string, number> {
const nodeIds = graph.nodes.map((node) => node.id);
const N = nodeIds.length;
if (N === 0) {
return {};
}

const pr: Record<string, number> = {};
const outDegree: Record<string, number> = {};
const inLinks: Record<string, string[]> = {};

for (const id of nodeIds) {
pr[id] = 1 / N;
outDegree[id] = 0;
inLinks[id] = [];
}

for (const edge of graph.edges) {
if (pr[edge.source] !== undefined && pr[edge.target] !== undefined) {
outDegree[edge.source] = (outDegree[edge.source] ?? 0) + 1;
inLinks[edge.target] = [...(inLinks[edge.target] ?? []), edge.source];
}
}

for (let i = 0; i < iterations; i++) {
const next: Record<string, number> = {};
for (const id of nodeIds) {
let rank = (1 - damping) / N;
for (const inId of (inLinks[id] ?? [])) {
const deg = outDegree[inId] ?? 1;
rank += damping * (pr[inId] ?? 0) / Math.max(deg, 1);
}
next[id] = rank;
}
for (const id of nodeIds) {
pr[id] = next[id] ?? (1 / N);
}
}

return pr;
}

function computeBetweenness(graph: GraphBuilderGraph, adjacency: Map<string, GraphBuilderEdge[]>): Record<string, number> {
const nodeIds = graph.nodes.map((node) => node.id);
const bc: Record<string, number> = {};
for (const id of nodeIds) {
bc[id] = 0;
}

for (const s of nodeIds) {
const stack: string[] = [];
const pred: Record<string, string[]> = {};
const sigma: Record<string, number> = {};
const dist: Record<string, number> = {};

for (const id of nodeIds) {
pred[id] = [];
sigma[id] = 0;
dist[id] = -1;
}
sigma[s] = 1;
dist[s] = 0;

const queue: string[] = [s];
while (queue.length > 0) {
const v = queue.shift()!;
stack.push(v);
for (const edge of (adjacency.get(v) ?? [])) {
const w = edge.source === v ? edge.target : edge.source;
if (dist[w] < 0) {
queue.push(w);
dist[w] = dist[v]! + 1;
}
if (dist[w] === dist[v]! + 1) {
sigma[w] += sigma[v]!;
pred[w]!.push(v);
}
}
}

const delta: Record<string, number> = {};
for (const id of nodeIds) {
delta[id] = 0;
}
while (stack.length > 0) {
const w = stack.pop()!;
for (const v of (pred[w] ?? [])) {
delta[v] += (sigma[v]! / Math.max(sigma[w]!, 1)) * (1 + delta[w]!);
}
if (w !== s) {
bc[w] += delta[w]!;
}
}
}

const n = nodeIds.length;
if (n > 2) {
const factor = 2 / ((n - 1) * (n - 2));
for (const id of nodeIds) {
bc[id] *= factor;
}
}

return bc;
}

function detectBridgeNodes(graph: GraphBuilderGraph, adjacency: Map<string, GraphBuilderEdge[]>): string[] {
const nodeIds = graph.nodes.map((node) => node.id);
const nodeSet = new Set(nodeIds);
const visited = new Set<string>();
const disc: Record<string, number> = {};
const low: Record<string, number> = {};
const parent: Record<string, string | null> = {};
const ap = new Set<string>();
let timer = 0;

const dfs = (u: string): void => {
visited.add(u);
disc[u] = low[u] = timer++;
let childCount = 0;

for (const edge of (adjacency.get(u) ?? [])) {
const v = edge.source === u ? edge.target : edge.source;
if (!nodeSet.has(v)) {
continue;
}
if (!visited.has(v)) {
childCount++;
parent[v] = u;
dfs(v);
low[u] = Math.min(low[u]!, low[v]!);
if (parent[u] === null && childCount > 1) {
ap.add(u);
}
if (parent[u] !== null && low[v]! >= disc[u]!) {
ap.add(u);
}
} else if (v !== parent[u]) {
low[u] = Math.min(low[u]!, disc[v]!);
}
}
};

for (const id of nodeIds) {
if (!visited.has(id)) {
parent[id] = null;
dfs(id);
}
}

return [...ap];
}

function assignCommunities(graph: GraphBuilderGraph, adjacency: Map<string, unknown[]>): GraphBuilderAnalysis["communities"] {
Expand Down
Loading
Loading