Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1be17c

Browse files
🔧 chore (lab): solve the problem
1 parent 94c8900 commit c1be17c

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

‎lab/exercises/10-mixed/critical-routers.js‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ function criticalRouters(numRouters, numLinks, links) {
1515
// console.log({graph});
1616

1717
for (let curr = 1; curr <= numRouters; curr++) {
18-
for (let i = 1; i <= numRouters; i++) {
19-
for (let j = 1; j <= numRouters; j++) {
20-
if (curr === i || curr === j || i === j) { continue; }
21-
if (!isConnected(graph, i, j, curr)) {
22-
critical.push(curr);
23-
i++;
24-
break;
25-
}
26-
}
18+
if (isCritical(graph, curr)) {
19+
critical.push(curr);
2720
}
2821
}
2922

3023
return critical;
3124
}
3225

26+
function isCritical(graph, curr) {
27+
for (let i = 1; i <= graph.size; i++) {
28+
for (let j = 1; j <= graph.size; j++) {
29+
if (curr === i || curr === j || i === j) { continue; }
30+
if (!isConnected(graph, i, j, curr)) {
31+
return true;
32+
}
33+
}
34+
}
35+
return false;
36+
}
37+
3338
function addEdge(graph, from, to) {
3439
// console.log('addEdge', {graph, from, to});
3540
const adjacents = graph.get(from);

‎lab/exercises/10-mixed/critical-routers.spec.js‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,63 @@
22
const critialRouters = require('./critical-routers');
33

44
describe('Critical Routers', () => {
5-
it('should work', () => {
5+
it('should work with daisy chain nodes', () => {
6+
const numRouters = 3;
7+
const numLinks = 3;
8+
const links = [
9+
[1, 2],
10+
[2, 3],
11+
[3, 1],
12+
];
13+
expect(critialRouters(numRouters, numLinks, links)).toEqual([]);
14+
});
15+
16+
it('should work with 7 nodes', () => {
617
const numRouters = 7;
718
const numLinks = 7;
8-
const links = [[1, 2], [1, 3], [2, 4], [3, 4], [3, 6], [6, 7], [4, 5]];
19+
const links = [
20+
[1, 2],
21+
[1, 3],
22+
[2, 4],
23+
[3, 4],
24+
[3, 6],
25+
[6, 7],
26+
[4, 5],
27+
];
928
expect(critialRouters(numRouters, numLinks, links)).toEqual([3, 4, 6]);
1029
});
30+
31+
it('should work with 6 nodes', () => {
32+
const numRouters = 6;
33+
const numLinks = 5;
34+
const links = [
35+
[1, 2],
36+
[2, 3],
37+
[3, 4],
38+
[4, 5],
39+
[3, 6],
40+
];
41+
expect(critialRouters(numRouters, numLinks, links)).toEqual([2, 3, 4]);
42+
});
43+
44+
it('should work with 10 nodes', () => {
45+
const numRouters = 10;
46+
const numLinks = 13;
47+
const links = [
48+
[1, 2],
49+
[1, 3],
50+
[2, 3],
51+
[3, 4],
52+
[4, 5],
53+
[4, 6],
54+
[5, 6],
55+
[5, 7],
56+
[6, 7],
57+
[7, 8],
58+
[8, 9],
59+
[8, 10],
60+
[9, 10],
61+
];
62+
expect(critialRouters(numRouters, numLinks, links)).toEqual([3, 4, 7, 8]);
63+
});
1164
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /