Skip to content

Commit 97b96ae

Browse files
added support for edge references
1 parent 8bec166 commit 97b96ae

File tree

17 files changed

+102
-96
lines changed

17 files changed

+102
-96
lines changed

js/src/001 undirected/offline/algo/eulerian/blossom.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

js/src/001 undirected/offline/algo/eulerian/eventour.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ var eventour_t = function(){
3232

3333
while(true){
3434
var end = true;
35-
it[i] = g.eitr(u, function(e){
36-
if(free[i][e[0][0]] > 0){
35+
it[i] = g.eitr(u, function(_, v){
36+
if(free[i][v[0]] > 0){
3737
tour.splice(j, 0, i);
38-
u = e[0];
38+
u = v;
3939

4040
++j;
4141
if (!done[u[0]]) r.push([u[0], j]);

js/src/001 undirected/offline/algo/eulerian/oddgraph.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var oddgraph_t = function(){
88

99
g.vitr(function(v){
1010
var i = 0;
11-
g.eitr(v, function(e){
12-
i += (e[0] !== v);
11+
g.eitr(v, function(_, u){
12+
i += (u !== v);
1313
});
1414

1515
if(i % 2 === 1){

js/src/001 undirected/offline/algo/eulerian/simplegraph.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,23 @@ var simplegraph_t = function(){
1212
i = v[0]; // indice of v in dist
1313
V[i] = h.vadd(v);
1414

15-
g.eitr(v, function(e){
15+
g.eitr(v, function(_, u, w){
1616

17-
var u = e[0]; // dest
18-
var w = e[1]; // weigth
1917
j = u[0];
2018

2119
if(i >= j) return;
2220

23-
if (w < dist[i][j][1] ){
24-
dist[i][j] = e;
21+
if (w < dist[i][j] ){
22+
dist[i][j] = w;
2523
}
2624
});
2725
});
2826

2927

3028
for (i = 0; i < order; ++i){
3129
for (j = i + 1; j < order; ++j){
32-
if ( dist[i][j][1] < Infinity ){
33-
h.eadd( V[i], V[j], dist[i][j][1] );
30+
if ( dist[i][j] < Infinity ){
31+
h.eadd( V[i], V[j], dist[i][j] );
3432
}
3533
}
3634
}

js/src/001 undirected/offline/algo/sp/dijkstra.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ var dijkstra_t = function(priority_queue_t){
1111
used[s[0]] = true;
1212
busy[s[0]] = true;
1313

14-
g.eitr(s, function(e){
15-
dist[e[0][0]] = e[1];
16-
busy[e[0][0]] = true;
17-
left.push(e[0]);
14+
g.eitr(s, function(_, u, w){
15+
dist[u[0]] = w;
16+
busy[u[0]] = true;
17+
left.push(u);
1818
});
1919

2020
while(left.length){
2121

2222
var m = left.pop();
2323
used[m[0]] = true;
2424

25-
g.eitr(m, function(e){
26-
var y = e[0];
25+
g.eitr(m, function(_, u, w){
26+
var y = u;
2727

2828
if(!used[y[0]]){
2929

30-
var v = dist[m[0]] + e[1];
30+
var v = dist[m[0]] + w;
3131

3232
if(v < dist[y[0]]){
3333
dist[y[0]] = v;

js/src/001 undirected/offline/algo/sp/sptreedfs.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ var sptreedfs_t = function(){
44

55
var dfs = function(g, next, dist, s, t){
66

7-
g.eitr([s], function(e){
8-
var u = e[0][0];
9-
var w = e[1];
10-
7+
g.eitr([s], function(_, u, w){
8+
u = u[0];
9+
1110
if(dist[u][t] === w + dist[s][t] && next[u][t] === -1){
1211
next[u][t] = s;
1312
dfs(g, next, dist, u, t);

js/src/001 undirected/offline/algo/util/amat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var amat_t = function(){
55
var amat = function(g, order, dist){
66

77
g.vitr(function(v){
8-
g.eitr(v, function(e){
9-
dist[v[0]][e[0][0]] = e[1];
8+
g.eitr(v, function(_, u, w){
9+
dist[v[0]][u[0]] = w;
1010
});
1111
});
1212

js/src/001 undirected/offline/algo/util/d2s.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ var d2s = function(g, h, V){
77
});
88

99
g.vitr(function(u){
10-
g.eitr(u, function(e){
11-
var v = e[0], w = e[1];
10+
g.eitr(u, function(_, v, w){
1211
if(u[0] <= v[0]) h.eadd(V[u[0]], V[v[0]], w);
1312
});
1413
});

js/src/001 undirected/online/data/dense.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ var dense_graph_t = function(){
1616
this.ad.push(ref);
1717

1818
var j = len;
19-
while(j--) this.pt[j].push([null, -1]);
19+
while(j--) this.pt[j].push([null, null, -1]);
2020

2121
this.pt.push(new Array(len + 1));
2222

23-
j = len + 1;
24-
while(j--) this.pt[len][j] = [null, -1];
23+
j = len;
24+
while(j--) this.pt[len][j] = this.pt[j][len];
25+
26+
this.pt[len][len] = [null, null, -1];
2527

2628
return ref;
2729
};
@@ -44,21 +46,21 @@ var dense_graph_t = function(){
4446
graph.prototype.eadd = function(u, v, w){
4547
var i = u[0], j = v[0];
4648

47-
this.pt[i][j][0] = v;
48-
this.pt[i][j][1] = w;
49-
this.pt[j][i][0] = u;
50-
this.pt[j][i][1] = w;
51-
52-
return [this.pt[i][j], this.pt[j][i]];
49+
this.pt[i][j][0] = u;
50+
this.pt[i][j][1] = v;
51+
this.pt[i][j][2] = w;
52+
53+
return this.pt[i][j];
5354

5455
};
5556

5657
graph.prototype.edel = function(e){
5758

58-
var i = e[0][0][0], j = e[1][0][0];
59+
var i = e[0][0], j = e[1][0];
5960

6061
this.pt[i][j][0] = null;
61-
this.pt[j][i][0] = null;
62+
this.pt[i][j][1] = null;
63+
this.pt[i][j][2] = -1;
6264

6365
};
6466

@@ -79,7 +81,7 @@ var dense_graph_t = function(){
7981

8082
if(this.pt[i][j][0] === null) continue;
8183

82-
if(fn.call(this, this.pt[i][j])) break;
84+
if(fn.call(this, this.pt[i][j], this.ad[j], this.pt[i][j][2])) break;
8385

8486
}
8587

js/src/001 undirected/online/data/sparse.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ var sparse_graph_t = function(){
5151
this.end = [null, this.beg, null];
5252
this.beg[2] = this.end;
5353

54+
this.ebeg = [null, null, -1, null, null];
55+
this.eend = [null, null, -1, this.ebeg, null];
56+
this.ebeg[4] = this.eend;
57+
5458
};
5559

5660
/**
@@ -69,7 +73,7 @@ var sparse_graph_t = function(){
6973
// > [end][1][1] is the previous [end][1]
7074
// > [end] and [end][1] are sane
7175
// > [end][1][1][2] is still pointing to [end]
72-
this.end[1] = [h, this.end[1], this.end, [-1, -1, null, null]];
76+
this.end[1] = [h, this.end[1], this.end, [null, -1, null, null]];
7377

7478
// update [end][1][1][2] to fix the invariant break
7579
this.end[1][1][2] = this.end[1];
@@ -80,14 +84,7 @@ var sparse_graph_t = function(){
8084

8185
graph.prototype.vdel = function(i){
8286

83-
this.eitr(i, function(e) {
84-
e[2][3] = e[3];
85-
if(e[3] !== null) e[3][2] = e[2];
86-
e = e[4];
87-
if(e === null) return;
88-
e[2][3] = e[3];
89-
if(e[3] !== null) e[3][2] = e[2];
90-
});
87+
this.eitr(i, function(e) { this.edel(e); });
9188

9289

9390
i[1][2] = i[2]; // next of pref becomes next
@@ -101,27 +98,35 @@ var sparse_graph_t = function(){
10198
if(i[3][3][3] !== null) i[3][3][3][2] = i[3][3];
10299

103100
if(j !== i){
104-
j[3][3] = [i, w, j[3], j[3][3], i[3][3]];
101+
j[3][3] = [i, w, j[3], j[3][3], null];
105102
if(j[3][3][3] !== null) j[3][3][3][2] = j[3][3];
106-
107-
i[3][3][4] = j[3][3];
108103
}
109104

110-
return [i[3][3], j[3][3]];
105+
this.eend[3] = [i, j, w, this.eend[3], this.eend, i[3][3], j[3][3]];
106+
107+
this.eend[3][3][4] = this.eend[3];
108+
109+
i[3][3][4] = j[3][3][4] = this.eend[3];
110+
111+
return this.eend[3];
111112

112113
};
113114

114115
graph.prototype.edel = function(e){
115116

116-
e[0][2][3] = e[0][3];
117-
if(e[0][3] !== null) e[0][3][2] = e[0][2];
117+
e[5][2][3] = e[5][3];
118+
if(e[5][3] !== null) e[5][3][2] = e[5][2];
118119

119120

120-
if(e[1] !== e[0]){
121-
e[1][2][3] = e[1][3];
122-
if(e[1][3] !== null) e[1][3][2] = e[1][2];
121+
if(e[6] !== e[5]){
122+
e[6][2][3] = e[6][3];
123+
if(e[6][3] !== null) e[6][3][2] = e[6][2];
123124
}
124125

126+
127+
e[3][4] = e[4]; // next of pref becomes next
128+
e[4][3] = e[3]; // prev of next becomes prev
129+
125130
};
126131

127132

@@ -144,7 +149,7 @@ var sparse_graph_t = function(){
144149

145150
while(e !== null){
146151

147-
if(fn.call(this, e)) return e[3];
152+
if(fn.call(this, e[4], e[0], e[1])) return e[3];
148153

149154
e = e[3];
150155
}

0 commit comments

Comments
 (0)