-
Notifications
You must be signed in to change notification settings - Fork 0
/
anti_embedding.js
83 lines (65 loc) · 1.73 KB
/
anti_embedding.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
// Copyright: https://mit-license.org/
#include "assert.js"
#include "util.js"
#include "undirected_graph.js"
#include "mersenne-twister.js"
function chi(G) {
var nfaces = 0;
planar_face_traversal(G, {begin_face: function () {
nfaces += 1;
}});
return n_vertices(G) - n_edges(G) + nfaces;
}
function swap_edge(G, v, e) {
var i = ind(G, v, e);
var f = next_incident_edge(G, v, e);
var j = ind(G, v, f);
var a = G.E[e][i][1];
var b = G.E[f][j][1];
G.V[v][b] = e;
G.E[e][i][1] = b;
G.V[v][a] = f;
G.E[f][j][1] = a;
}
function adj_list_str(G) {
var str = "[";
forall_vertices(G, function (v) {
str = str + "[";
forall_incident_edges(G, v, function (e) {
str = str + opposite(G, v, e);
if (next_incident_edge(G,v,e) !== first_incident_edge(G,v)) {
str = str + ",";
}
});
str = str + "]";
if (v !== n_vertices(G)-1) {
str = str + ",";
}
});
return str + "]";
}
assert.assert(process.argv.length > 3);
var N = parseInt(process.argv[3]);
var seed = (process.argv.length === 5) ? parseInt(process.argv[4]) : 1234;
console.log("N=", N, "seed=", seed);
var mt19937 = new MersenneTwister(seed);
function rand_uint32() {
return mt19937.genrand_int32();
}
function random_edge(G) {
return rand_uint32() % n_edges(G);
}
var adj = parse2file(process.argv[2]);
var G = from_adjacency_list(adj);
var m = chi(G);
console.log(m);
while (N > 0) {
var e = random_edge(G);
var v = (rand_uint32() >= 2*(1<<30)) ? source(G, e) : target(G, e);
swap_edge(G, v, e);
if (chi(G) < m) {
m = chi(G);
console.log(m, adj_list_str(G));
}
N = N - 1;
}