From 9fda2c094c7fd26f4ca73fff981fc4934c1e5f18 Mon Sep 17 00:00:00 2001 From: Hussein El Motayam Date: Wed, 11 Mar 2020 19:31:53 +0200 Subject: [PATCH] Support unmapped adjacency list indices #14 --- johnson.js | 2 +- test/test.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/johnson.js b/johnson.js index f897a51..9dcaf27 100644 --- a/johnson.js +++ b/johnson.js @@ -70,7 +70,7 @@ module.exports = function findCircuits(edges, cb) { function subgraph(minId) { // Remove edges with indice smaller than minId for(var i = 0; i < edges.length; i++) { - if(i < minId) edges[i] = []; + if(i < minId || !edges[i]) edges[i] = []; edges[i] = edges[i].filter(function(i) { return i >= minId; }); diff --git a/test/test.js b/test/test.js index 1623fa6..c2cde8a 100644 --- a/test/test.js +++ b/test/test.js @@ -43,6 +43,25 @@ test('find elementarty circuits in', function(t) { t.end(); }); + t.test('another simple directed graph with orphaned nodes', function(t) { + // V4 V2 + // +-<---o---<---o + // | | + // o V0 ^ o V3 + // | V1| + // +------>------o + var g = []; + g[0] = [1]; + g[1] = [2]; + // g[3] is unmapped + g[2] = [4]; + g[4] = [0]; + + var circuits = findCircuits(g); + t.deepEqual(circuits, [[0, 1, 2, 4, 0]]); + t.end(); + }); + t.test('another simple directed graph with callback', function(t) { var g = [ [1],