This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
nuutila.js
127 lines (111 loc) · 3.5 KB
/
nuutila.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
125
126
127
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
if(typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require, exports) {
var Graph = require('./graph').Graph,
numset = require('./numset');
Graph.prototype.reachability = function(nodePred) {
var self = this;
var next_dfsnum = 1, next_component = 1;
var ccr = [],
component = [],
dfsnum = [],
vstack = [],
cstack = [],
reach = [],
members = [];
function visit(v) {
var cstack_height = cstack.length,
vstack_height = vstack.length,
self_loop = false;
vstack[vstack.length] = v;
ccr[v] = v;
component[v] = 0;
dfsnum[v] = next_dfsnum++;
if(!nodePred || nodePred(self.id2node[v]))
numset.iter(self.succ[v], function(w) {
if(w === v) {
self_loop = true;
} else {
var w_visited = !!dfsnum[w];
if(!w_visited)
visit(w);
if(!component[w]) {
if(dfsnum[ccr[w]] < dfsnum[ccr[v]])
ccr[v] = ccr[w];
} else if(!w_visited || dfsnum[w] < dfsnum[v]) {
cstack[cstack.length] = component[w];
}
}
});
if(ccr[v] === v) {
var comp = component[v] = next_component++;
if(self_loop || vstack[vstack.length-1] !== v)
reach[comp] = comp;
else
reach[comp] = void(0);
for(var i=cstack.length-1;i>=cstack_height;--i) {
var x = cstack[i];
if(!numset.contains(reach[comp], x))
reach[comp] = numset.add(numset.addAll(reach[comp], reach[x]), x);
}
cstack.length = cstack_height;
do {
w = vstack[vstack.length-1];
--vstack.length;
component[w] = comp;
members[comp] = numset.add(members[comp], w);
} while(w !== v);
}
}
return {
getReachable: function(src) {
var src_id = self.nodeId(src);
if(!dfsnum[src_id])
visit(src_id);
var res = [];
numset.iter(reach[component[src_id]], function(r) {
numset.iter(members[r], function(id) {
res[res.length] = self.id2node[id];
});
});
return res;
},
iterReachable: function(src, cb) {
debugger;
var src_id = self.nodeId(src);
if(!dfsnum[src_id])
visit(src_id);
numset.iter(reach[component[src_id]], function(r) {
numset.iter(members[r], function(id) {
cb(self.id2node[id]);
});
});
},
reaches: function(src, dest) {
var src_id = self.nodeId(src), dest_id = self.nodeId(dest);
if(!dfsnum[src_id])
visit(src_id);
var found = false;
numset.iter(reach[component[src_id]], function(r) {
if(numset.contains(members[r], dest_id)) {
found = true;
return true;
}
});
return found;
}
};
};
return exports;
});