Skip to content

Instantly share code, notes, and snippets.

@alekseyl1992
Created March 22, 2015 22:49
Show Gist options
  • Save alekseyl1992/63c29f4c223e36a45d96 to your computer and use it in GitHub Desktop.
Save alekseyl1992/63c29f4c223e36a45d96 to your computer and use it in GitHub Desktop.
temp
function calcNearest() {
// build queue
var nodeList = _toNodeList(distTable);
var queue = _.sortBy(nodeList, 'value');
// build path
var path = [];
var l = 0;
var idToFind = null;
var nextNode = queue[0];
path.push(nextNode.i);
idToFind = nextNode.j;
l += nextNode.value;
while (queue.length != 0) {
nextNode = _.find(queue, {i: idToFind});
if (nextNode && !_.contains(path, nextNode.i)) {
path.push(nextNode.i);
idToFind = nextNode.j;
l += nextNode.value;
queue.remove(nextNode);
continue;
}
nextNode = _.find(queue, {j: idToFind});
if (nextNode && idToFind == nextNode.j && !_.contains(path, nextNode.j)) {
path.push(nextNode.j);
idToFind = nextNode.i;
queue.remove(nextNode);
l += nextNode.value;
continue;
}
}
path.push(idToFind);
// close list
var i = _.min([path[0], path[path.length - 1]]);
var j = _.max([path[0], path[path.length - 1]]);
var closeNode = _.find(nodeList, {
i: i,
j: j
});
path.push(closeNode.i);
l += closeNode.value;
// prepare path for output
path = _.map(path, function (p) { return p + 1; });
return {
path: path.join('-'),
l: l
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment