-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnkd.js
70 lines (39 loc) · 1.48 KB
/
lnkd.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
var dict1 = require('./dict.js');
var utility = require('./utility.js');
var Node = function (name ) {
this.id = name;
this.distance_array = [];
}
var graph = [ {node1:"a", node2:"c"} , {node1:"e", node2:"b"} ,{node1:"c", node2:"b"}, {node1:"c",node2:"d"}, {node1:"d",node2:"e"}, {node1:"c", node2:"e"} ] ;
var a = new Node("a");
var allNodes = [];
console.log("a =" + JSON.stringify(a));
console.log("graph =" + JSON.stringify(graph));
console.log("allNodes =" + allNodes);
//console.log("dict = " + JSON.stringify(dict1) );
// first pass to populate all the nodes
var i =0;
for ( i = 0; i < graph.length; i++ ) {
var currentLink = graph[i];
console.log("currentLink =" + JSON.stringify(currentLink));
console.log("currentLink[node1] =" + currentLink['node1']);
var idx1 = utility.aIndexOf(allNodes, function(obj) {
return obj.id == currentLink['node1'];
} );
if (-1 != idx1) {
console.log('Found it! (Psst: take the blue pill!)');
}else {
console.log('not found so inserting!');
allNodes.push(new Node(currentLink['node1']));
}
var idx2 = utility.aIndexOf(allNodes, function(obj) {
return obj.id == currentLink['node2'];
} );
if (-1 != idx2) {
console.log('2 Found it! (Psst: take the blue pill!)');
}else {
console.log('2 not found so inserting!');
allNodes.push(new Node(currentLink['node2']));
}
}
console.log("allNodes =" + JSON.stringify(allNodes));