-
Notifications
You must be signed in to change notification settings - Fork 2
/
subgraph.js
124 lines (116 loc) · 4.25 KB
/
subgraph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var _ = require('lodash'),
extend = require('extend'),
graphlib = require('graphlib')
// Implementation of Ullmann (1976)
// via http://stackoverflow.com/questions/13537716/how-to-partially-compare-two-graphs/13537776#13537776
function SubgraphSearch (graph, subgraph, opts) {
opts = opts || {}
extend (this, { graph, subgraph })
this.mapping = { assign: {}, label: {}, match: {} }
this.subnodes = subgraph.nodes()
this.subedges = subgraph.edges()
this.labelMatch = opts.labelMatch || function(gLabel,sLabel) { return gLabel === sLabel }
this.nodeLabelMatch = opts.nodeLabelMatch || this.labelMatch
this.edgeLabelMatch = opts.edgeLabelMatch || this.labelMatch
var possibleAssignments = {}
this.subnodes.forEach (function (sid) {
var pa = {}
graph.nodes().forEach (function (gid) {
pa[gid] = true
})
possibleAssignments[sid] = pa
})
this.isomorphisms = this.search (possibleAssignments)
}
SubgraphSearch.prototype.testEdgeMatch = function (v, w, label) {
return this.graph.hasEdge(v,w) && this.edgeLabelMatch (this.graph.edge(v,w), label)
}
SubgraphSearch.prototype.updatePossibleAssignments = function (possibleAssignments) {
var search = this, subgraph = this.subgraph
var changed
do {
changed = false
this.subnodes.forEach (function (i) {
Object.keys(possibleAssignments[i]).forEach (function (j) {
var pred = subgraph.predecessors(j), succ = subgraph.successors(j)
if (succ)
succ.forEach (function (x) {
var foundMatch = false, label = subgraph.edge(i,x)
Object.keys(possibleAssignments[x]).forEach (function (y) {
foundMatch = foundMatch || search.testEdgeMatch (j, y, label)
})
if (!foundMatch) {
delete possibleAssignments[i][j]
changed = true
}
})
if (pred)
pred.forEach (function (x) {
var foundMatch = false, label = subgraph.edge(x,i)
Object.keys(possibleAssignments[x]).forEach (function (y) {
foundMatch = foundMatch || search.testEdgeMatch (y, j, label)
})
if (!foundMatch) {
delete possibleAssignments[i][j]
changed = true
}
})
})
})
} while (changed)
}
SubgraphSearch.prototype.search = function (possibleAssignments) {
var ss = this, mapping = this.mapping, graph = this.graph, subgraph = this.subgraph, subnodes = this.subnodes, subedges = this.subedges
this.updatePossibleAssignments (possibleAssignments)
var nAssigned = Object.keys(mapping.assign).length
var edgeMatch
if (nAssigned) {
var edgeNotFound = false
edgeMatch = subedges.map (function (edge) {
var match
if (!edgeNotFound && mapping.assign[edge.v] && mapping.assign[edge.w]) {
match = ss.testEdgeMatch (mapping.assign[edge.v], mapping.assign[edge.w], subgraph.edge(edge))
if (!match)
edgeNotFound = edge || true
}
return match
})
if (edgeNotFound)
return []
}
if (nAssigned == subnodes.length) {
var result = _.cloneDeep(mapping)
result.edgeMatch = edgeMatch
return [result]
}
var nextToAssign = subnodes[nAssigned]
var sLabel = subgraph.node(nextToAssign)
var results = []
Object.keys(possibleAssignments[nextToAssign]).forEach (function (j) {
var jUsed = false
Object.keys(mapping.assign).forEach (function (i) {
if (mapping.assign[i] === j)
jUsed = true
})
if (!jUsed) {
var gLabel = graph.node(j)
var match = ss.nodeLabelMatch (gLabel, sLabel)
if (match) {
mapping.label[nextToAssign] = gLabel
mapping.match[nextToAssign] = match
mapping.assign[nextToAssign] = j
var newPossibleAssignments = _.cloneDeep (possibleAssignments)
newPossibleAssignments[nextToAssign] = {}
newPossibleAssignments[nextToAssign][j] = true
results = results.concat (ss.search (newPossibleAssignments))
delete mapping.assign[nextToAssign]
delete mapping.match[nextToAssign]
delete mapping.label[nextToAssign]
delete possibleAssignments[nextToAssign][j]
ss.updatePossibleAssignments (possibleAssignments)
}
}
})
return results
}
module.exports = { SubgraphSearch }