import { Graph } from "../graph"; import { Vertex, VertexIndex } from "../vertex"; /** * Returns the adjacencies list. (Can be seen as an abstract graph) * @param g * @param subset the subset of vertices to induce the graph (keep every arc xy when x and y are in subset) * @returns outNeighbors: Map> which tells for every vertex id the out-neighbors id of this vertex * */ export function getInducedSubgraph(g: Graph, subset: Set) { const subOutNeighbors = new Map>(); for (const v of subset){ const vSubOutNeighbors = new Set(); for (const neigh of v.outNeighbors.values()){ if (subset.has(neigh)){ vSubOutNeighbors.add(neigh.index); } } subOutNeighbors.set(v.index, vSubOutNeighbors); } const subInNeighbors = new Map>(); for (const v of subset){ const vSubInNeighbors = new Set(); for (const neigh of v.inNeighbors.values()){ if (subset.has(neigh)){ vSubInNeighbors.add(neigh.index); } } subInNeighbors.set(v.index, vSubInNeighbors); } return { outNeighbors: subOutNeighbors, inNeighbors: subInNeighbors } }