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
17 changes: 14 additions & 3 deletions src/GraphStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>());
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 = () => ({});

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -351,4 +362,4 @@ export class GridGraph implements Graph {
nodes(): Node[] {
throw new Error(".graph.grid.unsupported");
}
}
}
10 changes: 6 additions & 4 deletions src/algorithms/matching/Matching.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -15,7 +15,11 @@ export class EdmondsGabow_alpha implements NewGraphAlgorithm {
// TODO: no self loop, no multiple edges
graphInputComponent = (
<GraphMatrixInput
checker={g => 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"}
/>
Expand Down Expand Up @@ -211,9 +215,7 @@ export class EdmondsGabow_alpha implements NewGraphAlgorithm {
}

*run(graph: Graph): Generator<Step> {
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;
Expand Down
6 changes: 4 additions & 2 deletions src/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ module.exports = {
no_weight: "输入没有权值",
non_increment: "输入不递增",
invalid_format: "输入格式非法",
self_loop: "输入存在自环",
multiple_edges: "输入存在重边",
network: {
no_flow: "输入没有流量",
no_cost: "输入没有权值"
Expand Down
Loading