Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
Deal with cross-edges, fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
michielbdejong committed Nov 11, 2016
1 parent 3832439 commit 93db307
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
10 changes: 9 additions & 1 deletion node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function Node() {
};
this._routes = {};
this._lastTimestampGenerated = 0;
this._cycleFound = false;
}

Node.prototype._getTimestamp = function() {
Expand Down Expand Up @@ -85,14 +86,20 @@ Node.prototype._probeIsKnown = function(treeToken) {
Node.prototype.handleProbeMessage = function(neighborId, direction, msgObj) {
if (direction === 'in') {
if (this._probeIsKnown(msgObj.treeToken)) {
this._cycleFound = true;
if (this._routes[msgObj.treeToken].wasBacktracked()) {
//cross-edge, backtrack again
this._neighbors['in'][neighborId].sendProbeMessage(msgObj);
} else {
this._cycleFound = true;
}
return;
}
this._routes[msgObj.treeToken] = new Route(neighborId, msgObj.treeToken, msgObj.pathToken);
var firstOutNeighborId = this._routes[msgObj.treeToken].getNextSiblingToTry(this.getActiveNeighbors().out);
if (firstOutNeighborId) {
this._neighbors.out[firstOutNeighborId].sendProbeMessage(msgObj);
} else { // backtrack
this._routes[msgObj.treeToken].markBacktracked();
this._neighbors['in'][neighborId].sendProbeMessage(msgObj);
}
} else {
Expand All @@ -101,6 +108,7 @@ Node.prototype.handleProbeMessage = function(neighborId, direction, msgObj) {
msgObj.pathToken = this._routes[msgObj.treeToken].getPathToken();
this._neighbors.out[nextOutNeighborId].sendProbeMessage(msgObj);
} else { // backtrack
this._routes[msgObj.treeToken].markBacktracked();
this._neighbors['in'][neighborId].sendProbeMessage(msgObj);
}
}
Expand Down
9 changes: 9 additions & 0 deletions route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function Route(inNeighbor, treeToken) {
this._treeToken = treeToken;
this._outNeighbors = {
};
this._backtracked = false;
}

Route.prototype.getNextSiblingToTry = function(outNeighborIds) {
Expand All @@ -28,4 +29,12 @@ Route.prototype.getOutNeighborNick = function(pathToken) {
}
};

Route.prototype.markBacktracked = function() {
this._backtracked = true;
};

Route.prototype.wasBacktracked = function() {
return this._backtracked;
};

module.exports = Route;
58 changes: 58 additions & 0 deletions test/cross-edge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var Node = require('../index.js');
var Graph = require('./helpers/graph.js');
var assert = require('assert');

describe('Cross-edge', function() {
var graph;
beforeEach(function() {
graph = new Graph({
a: new Node(),
b: new Node(),
c: new Node(),
});

// --------
// v \
// a -> b -> c
// \ ^
// --------

graph.connect('a', 'b');
graph.connect('b', 'c');
graph.connect('a', 'c');
graph.connect('c', 'a');
return graph.propagate();
});

it('should find both cycles', function() {
assert.deepEqual(graph.nodes.a.getActiveNeighbors(), {
'in': ['c'],
out: ['b', 'c'],
});
assert.deepEqual(graph.nodes.b.getActiveNeighbors(), {
'in': ['a'],
out: ['c'],
});
assert.deepEqual(graph.nodes.c.getActiveNeighbors(), {
'in': ['b', 'a'],
out: ['a'],
});
});
describe('incoming probe message for a', function() {
beforeEach(function() {
graph.nodes.a.handleProbeMessage('c', 'in', {
treeToken: 'asdf',
});
return graph.propagate();
});
it('a should not find a route', function() {
assert.equal(graph.nodes.a._cycleFound, true);
});
it('b should not find a route', function() {
assert.equal(graph.nodes.b._cycleFound, false);
});
it('c should not find a route', function() {
assert.equal(graph.nodes.c._cycleFound, false);
});
});
});
11 changes: 10 additions & 1 deletion test/square.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ describe('Four nodes', function() {
});
return graph.propagate();
});
it('should find a route', function() {
it('a should find a route', function() {
assert.equal(graph.nodes.a._cycleFound, true);
});
it('b should not find a route', function() {
assert.equal(graph.nodes.b._cycleFound, false);
});
it('c should not find a route', function() {
assert.equal(graph.nodes.c._cycleFound, false);
});
it('d should not find a route', function() {
assert.equal(graph.nodes.d._cycleFound, false);
});
});
});
8 changes: 7 additions & 1 deletion test/triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ describe('Three nodes', function() {
});
return graph.propagate();
});
it('should find a route', function() {
it('a should find a route', function() {
assert.equal(graph.nodes.a._cycleFound, true);
});
it('b should not find a route', function() {
assert.equal(graph.nodes.b._cycleFound, false);
});
it('c should not find a route', function() {
assert.equal(graph.nodes.c._cycleFound, false);
});
});
});

0 comments on commit 93db307

Please sign in to comment.