From 6da8c62e159f2b497ddb93f9ad1511d90df0949f Mon Sep 17 00:00:00 2001 From: Adrian Mejia Date: Mon, 30 Mar 2020 16:30:25 -0400 Subject: [PATCH] :wrench: chore (lab): solve the problem --- lab/exercises/10-mixed/critical-routers.js | 27 ++++++--- .../10-mixed/critical-routers.spec.js | 57 ++++++++++++++++++- 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/lab/exercises/10-mixed/critical-routers.js b/lab/exercises/10-mixed/critical-routers.js index 39458cf1..d112b1f9 100644 --- a/lab/exercises/10-mixed/critical-routers.js +++ b/lab/exercises/10-mixed/critical-routers.js @@ -3,6 +3,10 @@ * A router is considered critical if when they are removed, * other routers lose connection to other routers different from the one removed. * + * @runtime O(|E| * |V|^3) + * @space O(|E| + |V|) + * @pomodoro IIII (2h) + * * @param {number} numRouters - The number of routers connected in a data center >= 3. * @param {number} numLinks - The number of links. * @param {[[number, number]]} links - The pair of routers connected by the link. @@ -15,21 +19,26 @@ function criticalRouters(numRouters, numLinks, links) { // console.log({graph}); for (let curr = 1; curr <= numRouters; curr++) { - for (let i = 1; i <= numRouters; i++) { - for (let j = 1; j <= numRouters; j++) { - if (curr === i || curr === j || i === j) { continue; } - if (!isConnected(graph, i, j, curr)) { - critical.push(curr); - i++; - break; - } - } + if (isCritical(graph, curr)) { + critical.push(curr); } } return critical; } +function isCritical(graph, curr) { + for (let i = 1; i <= graph.size; i++) { + for (let j = 1; j <= graph.size; j++) { + if (curr === i || curr === j || i === j) { continue; } + if (!isConnected(graph, i, j, curr)) { + return true; + } + } + } + return false; +} + function addEdge(graph, from, to) { // console.log('addEdge', {graph, from, to}); const adjacents = graph.get(from); diff --git a/lab/exercises/10-mixed/critical-routers.spec.js b/lab/exercises/10-mixed/critical-routers.spec.js index c8023cfe..c4ec5597 100644 --- a/lab/exercises/10-mixed/critical-routers.spec.js +++ b/lab/exercises/10-mixed/critical-routers.spec.js @@ -2,10 +2,63 @@ const critialRouters = require('./critical-routers'); describe('Critical Routers', () => { - it('should work', () => { + it('should work with daisy chain nodes', () => { + const numRouters = 3; + const numLinks = 3; + const links = [ + [1, 2], + [2, 3], + [3, 1], + ]; + expect(critialRouters(numRouters, numLinks, links)).toEqual([]); + }); + + it('should work with 7 nodes', () => { const numRouters = 7; const numLinks = 7; - const links = [[1, 2], [1, 3], [2, 4], [3, 4], [3, 6], [6, 7], [4, 5]]; + const links = [ + [1, 2], + [1, 3], + [2, 4], + [3, 4], + [3, 6], + [6, 7], + [4, 5], + ]; expect(critialRouters(numRouters, numLinks, links)).toEqual([3, 4, 6]); }); + + it('should work with 6 nodes', () => { + const numRouters = 6; + const numLinks = 5; + const links = [ + [1, 2], + [2, 3], + [3, 4], + [4, 5], + [3, 6], + ]; + expect(critialRouters(numRouters, numLinks, links)).toEqual([2, 3, 4]); + }); + + it('should work with 10 nodes', () => { + const numRouters = 10; + const numLinks = 13; + const links = [ + [1, 2], + [1, 3], + [2, 3], + [3, 4], + [4, 5], + [4, 6], + [5, 6], + [5, 7], + [6, 7], + [7, 8], + [8, 9], + [8, 10], + [9, 10], + ]; + expect(critialRouters(numRouters, numLinks, links)).toEqual([3, 4, 7, 8]); + }); });