|
| 1 | +import { tarjan } from "../tarjan"; |
| 2 | + |
| 3 | +describe("tarjan", () => { |
| 4 | + |
| 5 | + it("it should return no sccs for empty graph", () => { |
| 6 | + expect(tarjan([])).toStrictEqual([]); |
| 7 | + }); |
| 8 | + |
| 9 | + it("it should return one scc for graph with one element", () => { |
| 10 | + expect(tarjan([[]])).toStrictEqual([[0]]); |
| 11 | + }); |
| 12 | + |
| 13 | + it("it should return one scc for graph with element that points to itself", () => { |
| 14 | + expect(tarjan([[0]])).toStrictEqual([[0]]); |
| 15 | + }); |
| 16 | + |
| 17 | + it("it should return one scc for two element graph with cycle", () => { |
| 18 | + expect(tarjan([[1], [0]])).toStrictEqual([[1, 0]]); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should return one scc for each element for straight line", () => { |
| 22 | + expect(tarjan([[1], [2], [3], []])).toStrictEqual([[3], [2], [1], [0]]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should return sccs for straight line with backedge in middle", () => { |
| 26 | + expect(tarjan([[1], [2], [3, 0], []])).toStrictEqual([[3], [2, 1, 0]]); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should return sccs for straight line with backedge from end to middle", () => { |
| 30 | + expect(tarjan([[1], [2], [3], [1]])).toStrictEqual([[3, 2, 1], [0]]); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should return scc for each element for graph with no edges", () => { |
| 34 | + expect(tarjan([[], [], [], []])).toStrictEqual([[0], [1], [2], [3]]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should return sccs disconnected graph", () => { |
| 38 | + expect(tarjan([[1, 2], [0, 2], [0, 1], []])).toStrictEqual([[2, 1, 0], [3]]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should return sccs disconnected graph", () => { |
| 42 | + expect(tarjan([[1, 2], [0, 2], [0, 1], [4], [5], [3]])).toStrictEqual([[2, 1, 0], [5, 4, 3]]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should return single scc", () => { |
| 46 | + expect(tarjan([[1], [2], [3], [0, 4], [3]])).toStrictEqual([[4, 3, 2, 1, 0]]); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should return one scc for complete connected graph", () => { |
| 50 | + const input = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 1, 3, 4], [0, 1, 2, 4], [0, 1, 2, 3]]; |
| 51 | + expect(tarjan(input)).toStrictEqual([[4, 3, 2, 1, 0]]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should return sccs", () => { |
| 55 | + const input = [[1], [2], [0, 3], [4], []]; |
| 56 | + expect(tarjan(input)).toStrictEqual([[4], [3], [2, 1, 0]]); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should return sccs", () => { |
| 60 | + const input = [[1], [2], [0, 3, 4], [0], [5], [6, 7], [2, 4], [8], [5, 9], [5]]; |
| 61 | + const expected = [[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]]; |
| 62 | + expect(tarjan(input)).toStrictEqual(expected); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should return sccs", () => { |
| 66 | + const input = [[1], [0, 2], [0, 3], [4], [5, 7], [6], [4, 7], []]; |
| 67 | + const expected = [[7], [6, 5, 4], [3], [2, 1, 0]]; |
| 68 | + expect(tarjan(input)).toStrictEqual(expected); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should return sccs where first scc cannot reach second scc", () => { |
| 72 | + const input = [[1], [2], [0], [4], [5], [2, 3]]; |
| 73 | + const expected = [[2, 1, 0], [5, 4, 3]]; |
| 74 | + expect(tarjan(input)).toStrictEqual(expected); |
| 75 | + }); |
| 76 | + |
| 77 | +}) |
| 78 | + |
0 commit comments