From 0563bdd9ee28fbc45d39e59680286ffeb34ad2d8 Mon Sep 17 00:00:00 2001 From: STEINNI Date: Sun, 18 Jan 2026 18:06:40 +0000 Subject: [PATCH] Refactor BZgraflow: extract graph structure building into a separate method and enhance cross-layer link detection --- bzGraflow.js | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/bzGraflow.js b/bzGraflow.js index a94375f..ca7ffbc 100644 --- a/bzGraflow.js +++ b/bzGraflow.js @@ -207,19 +207,7 @@ class BZgraflow extends Buildoz{ linksWithoutBackEdges = this.flow.links } - const parents = {} - const adj = {} - this.flow.nodes.forEach(n => { - parents[n.id] = [] - adj[n.id] = [] - }) - - linksWithoutBackEdges.forEach(link => { - const from = link.from[0] - const to = link.to[0] - parents[to].push(from) - adj[from].push(to) - }) + const { parents, adj } = this.buildGraphStructures(this.flow.nodes, linksWithoutBackEdges) const layers = this.computeLayers(this.flow.nodes, parents) let maxHeight = 0; let maxWidth = 0 @@ -366,6 +354,24 @@ class BZgraflow extends Buildoz{ return(this.flow.links.find(item => ((item.from[0]==nid1) && (item.to[0]==nid2)))) } + buildGraphStructures(nodes, links) { + const parents = {} + const adj = {} + nodes.forEach(n => { + parents[n.id] = [] + adj[n.id] = [] + }) + + links.forEach(link => { + const from = link.from[0] + const to = link.to[0] + parents[to].push(from) + adj[from].push(to) + }) + + return { parents, adj } + } + computeLayers(nodes, parents) { const layer = {} const dfs = (id) => { @@ -453,6 +459,19 @@ class BZgraflow extends Buildoz{ return backEdges } + findCrossLayerLinks(links) { + const { parents } = this.buildGraphStructures(this.flow.nodes, links) + const layers = this.computeLayers(this.flow.nodes, parents) + const crossLayerLinks = [] + for(const link of links){ + const from = link.from[0] + const to = link.to[0] + if(layers[from] !== layers[to]) { + crossLayerLinks.push(link) + } + } + return crossLayerLinks + } } Buildoz.define('graflow', BZgraflow)