Refactor BZgraflow: extract graph structure building into a separate method and enhance cross-layer link detection
This commit is contained in:
45
bzGraflow.js
45
bzGraflow.js
@@ -207,19 +207,7 @@ class BZgraflow extends Buildoz{
|
|||||||
linksWithoutBackEdges = this.flow.links
|
linksWithoutBackEdges = this.flow.links
|
||||||
}
|
}
|
||||||
|
|
||||||
const parents = {}
|
const { parents, adj } = this.buildGraphStructures(this.flow.nodes, linksWithoutBackEdges)
|
||||||
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 layers = this.computeLayers(this.flow.nodes, parents)
|
const layers = this.computeLayers(this.flow.nodes, parents)
|
||||||
let maxHeight = 0; let maxWidth = 0
|
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))))
|
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) {
|
computeLayers(nodes, parents) {
|
||||||
const layer = {}
|
const layer = {}
|
||||||
const dfs = (id) => {
|
const dfs = (id) => {
|
||||||
@@ -453,6 +459,19 @@ class BZgraflow extends Buildoz{
|
|||||||
return backEdges
|
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)
|
Buildoz.define('graflow', BZgraflow)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user