diff --git a/src/GraphStructure.ts b/src/GraphStructure.ts index 131c6a2..8e55e4f 100644 --- a/src/GraphStructure.ts +++ b/src/GraphStructure.ts @@ -26,6 +26,18 @@ export function hasMultipleEdges(g: Graph) { return g.edges().some(({ source: s, target: t }) => edgeSet[s].size === edgeSet[s].add(t).size); } +export function isSimple(g: Graph, directed: boolean = false): boolean { + let edgeSet = Array.from({ length: g.nodes().length }, () => new Set()); + return g + .edges() + .some( + e => + e.source === e.target || + edgeSet[e.source].size === edgeSet[e.source].add(e.target).size || + (!directed && edgeSet[e.target].size === edgeSet[e.target].add(e.source).size) + ); +} + const emptyObject = () => ({}); /** @@ -68,8 +80,7 @@ export function fromRandom( } export class NodeEdgeList implements Graph { - constructor(protected _nodes: Node[], protected _edges: Edge[]) { - } + constructor(protected _nodes: Node[], protected _edges: Edge[]) {} static from(g: Graph) { return new NodeEdgeList(g.nodes(), g.edges()); @@ -351,4 +362,4 @@ export class GridGraph implements Graph { nodes(): Node[] { throw new Error(".graph.grid.unsupported"); } -} \ No newline at end of file +} diff --git a/src/algorithms/matching/Matching.tsx b/src/algorithms/matching/Matching.tsx index 81f735c..dcf2b49 100644 --- a/src/algorithms/matching/Matching.tsx +++ b/src/algorithms/matching/Matching.tsx @@ -1,5 +1,5 @@ import { NewGraphAlgorithm, ParameterDescriptor, Step } from "@/GraphAlgorithm"; -import { AdjacencyList, hasMultipleEdges, hasSelfLoop, Edge, Graph, Node, NodeEdgeList } from "@/GraphStructure"; +import { AdjacencyList, hasSelfLoop, Edge, Graph, Node, NodeEdgeList, isSimple } from "@/GraphStructure"; import { Queue } from "@/utils/DataStructure"; import NetworkGraphRenderer from "@/ui/render/NetworkGraphRenderer"; import GraphMatrixInput from "@/ui/input/GraphMatrixInput"; @@ -15,7 +15,11 @@ export class EdmondsGabow_alpha implements NewGraphAlgorithm { // TODO: no self loop, no multiple edges graphInputComponent = ( g} + checker={g => { + if (hasSelfLoop(g)) throw new Error(".input.error.self_loop"); + if (!isSimple(g)) throw new Error(".input.error.multiple_edges"); + return g; + }} formatters={[new EdgeListFormatter(false, false)]} description={"Please input a graph without self loop and multiple edges"} /> @@ -211,9 +215,7 @@ export class EdmondsGabow_alpha implements NewGraphAlgorithm { } *run(graph: Graph): Generator { - if (hasSelfLoop(graph)) throw new Error("algo Gabow : self loop"); this.adjlist = AdjacencyList.from(graph, false); - if (hasMultipleEdges(this.adjlist)) throw new Error("algo Gabow : mutiple edges"); this.edges = graph.edges(); this.nodes = graph.nodes(); this.n = this.nodes.length; diff --git a/src/locales/en-US.js b/src/locales/en-US.js index 8fb16d4..99fc45e 100644 --- a/src/locales/en-US.js +++ b/src/locales/en-US.js @@ -83,11 +83,13 @@ module.exports = { not_square: "Not a matrix", not_matrix: "Not a square matrix", asymmetric: "Not a symmetric matrix", - edge_count_incorrect: "incorrect edge count", - edge_format_incorrect: "incorrect edge format", + edge_count_incorrect: "Incorrect edge count", + edge_format_incorrect: "Incorrect edge format", no_weight: "No weight", non_increment: "Non increment", invalid_format: "Invalid format", + self_loop: "Self-loop found", + multiple_edges: "Multiple-edges found", network: { no_flow: "No flow", no_cost: "No cost" diff --git a/src/locales/zh-CN.js b/src/locales/zh-CN.js index b44a099..1cf9294 100644 --- a/src/locales/zh-CN.js +++ b/src/locales/zh-CN.js @@ -88,6 +88,8 @@ module.exports = { no_weight: "输入没有权值", non_increment: "输入不递增", invalid_format: "输入格式非法", + self_loop: "输入存在自环", + multiple_edges: "输入存在重边", network: { no_flow: "输入没有流量", no_cost: "输入没有权值"