Skip to content

Commit d823ece

Browse files
authored
Merge pull request #44 from fy-hb/main
fix #43
2 parents e972614 + 1f5819b commit d823ece

3 files changed

Lines changed: 89 additions & 8 deletions

File tree

src/components/animateGraph.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { buildTreeLayers } from "./graphTreeLayers";
2121
import { buildBipartite } from "./graphBipartite";
2222
import { buildGraphGrid } from "./graphGrid";
2323

24-
import { buildBridges } from "./graphBridges";
24+
import { buildBridges, buildCuts } from "./graphBridges";
2525

2626
import { buildEBCC } from "./graphEBCC";
2727
import { buildVBCC } from "./graphVBCC";
@@ -569,7 +569,8 @@ function buildSettings(): void {
569569
);
570570
}
571571
if (settings.showBridges) {
572-
[cutMap, bridgeMap] = buildBridges(nodes, adj, rev);
572+
bridgeMap = buildBridges(nodes, edges);
573+
cutMap = buildCuts(nodes, adj, rev);
573574
}
574575
if (settings.showMSTs) {
575576
mstMap = buildMSTs(nodes, edges, edgeLabels);

src/components/graphBridges.ts

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { CutMap, BridgeMap } from "../types";
22

3-
export function buildBridges(
3+
export function buildCuts(
44
nodes: string[],
55
adj: Map<string, string[]>,
66
rev: Map<string, string[]>,
7-
): [CutMap, BridgeMap] {
7+
): CutMap {
88
let depth = new Map<string, number>();
99
let memo = new Map<string, number>();
1010

@@ -130,5 +130,88 @@ export function buildBridges(
130130
}
131131
}
132132

133-
return [cutMap, bridgeMap];
133+
return cutMap;
134+
}
135+
136+
export function buildBridges(
137+
nodes: string[],
138+
edges: string[],
139+
): BridgeMap {
140+
141+
const adjFull = new Map<string, string[]>();
142+
143+
for (const u of nodes) {
144+
adjFull.set(u, []);
145+
}
146+
147+
for (const e of edges) {
148+
let u = e.split(" ")[0];
149+
let v = e.split(" ")[1];
150+
adjFull.get(u)!.push(v);
151+
adjFull.get(v)!.push(u);
152+
}
153+
154+
let colorMap = new Map<string, number>();
155+
156+
let color = 0;
157+
let time = 0;
158+
159+
let dfn = new Map<string, number>();
160+
let low = new Map<string, number>();
161+
let stack: string[] = [];
162+
163+
let bcc = new Map<number, string[]>();
164+
165+
const dfs = (u: string, parent: string | null): void => {
166+
time++;
167+
dfn.set(u, time);
168+
low.set(u, time);
169+
stack.push(u);
170+
171+
for (const v of adjFull.get(u)!) {
172+
if (v === parent) {
173+
parent = null;
174+
continue;
175+
} else if (!dfn.has(v)) {
176+
dfs(v, u);
177+
low.set(u, Math.min(low.get(u)!, low.get(v)!));
178+
} else {
179+
low.set(u, Math.min(low.get(u)!, dfn.get(v)!));
180+
}
181+
}
182+
183+
if (low.get(u) === dfn.get(u)) {
184+
color ++;
185+
const component = [];
186+
let w: string;
187+
do {
188+
w = stack.pop()!;
189+
component.push(w);
190+
colorMap.set(w, color);
191+
} while (w !== u);
192+
bcc.set(color, component);
193+
}
194+
};
195+
196+
for (const u of nodes) {
197+
if (!dfn.has(u)) {
198+
dfs(u, null);
199+
}
200+
}
201+
202+
let bridgeMap: BridgeMap = new Map<string, boolean>();
203+
204+
for (const e of edges) {
205+
let u = e.split(" ")[0];
206+
let v = e.split(" ")[1];
207+
if (colorMap.get(u) !== colorMap.get(v)) {
208+
bridgeMap.set([u, v].join(" "), true);
209+
bridgeMap.set([v, u].join(" "), true);
210+
} else {
211+
bridgeMap.set([u, v].join(" "), false);
212+
bridgeMap.set([v, u].join(" "), false);
213+
}
214+
}
215+
216+
return bridgeMap;
134217
}

src/components/graphVBCC.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ export function buildVBCC(
104104
colorMap.get(v)!.push({edge: e, group: belong});
105105
}
106106

107-
console.log(colorMap);
108-
console.log(edgeMap);
109-
110107
for (const [idx, nodes] of bcc) {
111108
if (nodes.length <= 1) {
112109
colorMap.set(nodes[0], [{edge: null, group: idx}]);

0 commit comments

Comments
 (0)