ThePrimeagen / kata-machine

1.28k stars 1.27k forks source link

BFS Graph Matrix bug? #65

Open katayamahide opened 11 months ago

katayamahide commented 11 months ago

Hi @ThePrimeagen and folks,

I really appreciate your passionable cource. By the way, I may found a bug in BFS Graph Matrix. As for the BFSGraphMatrix function bfs, if arguments of source and needle are same number, it returns null. But I think it should return the exact number in array.

I mean for example,

// src/day1/DFSGraphList.ts
bfs(matrix, 1, 1)) // expected output  [1], but got null;

To solve this bug, I think the code should be modified like below.

- if (prev[needle] === -1) {
-     return null;
- }

+ if (prev[needle] === -1 && source !== needle) {
+    return null;
+}

Also the test like below should be added .

// src/__tests__/BFSGraphMatrix.ts
expect(bfs(matrix2, 0, 0)).toEqual([0]);