|
1 |
| -import { findAllPath, findShortestPath } from "../../src"; |
| 1 | +import { findAllPath, findShortestPath } from '../../src'; |
2 | 2 |
|
3 | 3 | const data = {
|
4 | 4 | nodes: [
|
5 | 5 | {
|
6 |
| - id: "A", |
7 |
| - label: "A", |
| 6 | + id: 'A', |
| 7 | + label: 'A', |
8 | 8 | },
|
9 | 9 | {
|
10 |
| - id: "B", |
11 |
| - label: "B", |
| 10 | + id: 'B', |
| 11 | + label: 'B', |
12 | 12 | },
|
13 | 13 | {
|
14 |
| - id: "C", |
15 |
| - label: "C", |
| 14 | + id: 'C', |
| 15 | + label: 'C', |
16 | 16 | },
|
17 | 17 | {
|
18 |
| - id: "D", |
19 |
| - label: "D", |
| 18 | + id: 'D', |
| 19 | + label: 'D', |
20 | 20 | },
|
21 | 21 | {
|
22 |
| - id: "E", |
23 |
| - label: "E", |
| 22 | + id: 'E', |
| 23 | + label: 'E', |
24 | 24 | },
|
25 | 25 | {
|
26 |
| - id: "F", |
27 |
| - label: "F", |
| 26 | + id: 'F', |
| 27 | + label: 'F', |
28 | 28 | },
|
29 | 29 | {
|
30 |
| - id: "G", |
31 |
| - label: "G", |
| 30 | + id: 'G', |
| 31 | + label: 'G', |
32 | 32 | },
|
33 | 33 | {
|
34 |
| - id: "H", |
35 |
| - label: "H", |
| 34 | + id: 'H', |
| 35 | + label: 'H', |
36 | 36 | },
|
37 | 37 | ],
|
38 | 38 | edges: [
|
39 | 39 | {
|
40 |
| - source: "A", |
41 |
| - target: "B", |
| 40 | + source: 'A', |
| 41 | + target: 'B', |
42 | 42 | },
|
43 | 43 | {
|
44 |
| - source: "B", |
45 |
| - target: "C", |
| 44 | + source: 'B', |
| 45 | + target: 'C', |
46 | 46 | },
|
47 | 47 | {
|
48 |
| - source: "C", |
49 |
| - target: "G", |
| 48 | + source: 'C', |
| 49 | + target: 'G', |
50 | 50 | },
|
51 | 51 | {
|
52 |
| - source: "A", |
53 |
| - target: "D", |
| 52 | + source: 'A', |
| 53 | + target: 'D', |
54 | 54 | },
|
55 | 55 | {
|
56 |
| - source: "A", |
57 |
| - target: "E", |
| 56 | + source: 'A', |
| 57 | + target: 'E', |
58 | 58 | },
|
59 | 59 | {
|
60 |
| - source: "E", |
61 |
| - target: "F", |
| 60 | + source: 'E', |
| 61 | + target: 'F', |
62 | 62 | },
|
63 | 63 | {
|
64 |
| - source: "F", |
65 |
| - target: "D", |
| 64 | + source: 'F', |
| 65 | + target: 'D', |
66 | 66 | },
|
67 | 67 | {
|
68 |
| - source: "D", |
69 |
| - target: "E", |
| 68 | + source: 'D', |
| 69 | + target: 'E', |
70 | 70 | },
|
71 | 71 | ],
|
72 | 72 | };
|
73 | 73 |
|
74 |
| -describe("Shortest Path from source to target on graph", () => { |
75 |
| - it("find the shortest path", () => { |
76 |
| - const { length, path } = findShortestPath(data, "A", "C"); |
| 74 | +describe('Shortest Path from source to target on graph', () => { |
| 75 | + it('find the shortest path', () => { |
| 76 | + const { length, path } = findShortestPath(data, 'A', 'C'); |
77 | 77 | expect(length).toBe(2);
|
78 |
| - expect(path).toStrictEqual(["A", "B", "C"]); |
| 78 | + expect(path).toStrictEqual(['A', 'B', 'C']); |
79 | 79 | });
|
80 | 80 |
|
81 |
| - it("find all shortest paths", () => { |
82 |
| - const { length, allPath } = findShortestPath(data, "A", "F"); |
| 81 | + it('find all shortest paths', () => { |
| 82 | + const { length, allPath } = findShortestPath(data, 'A', 'F'); |
83 | 83 | expect(length).toBe(2);
|
84 |
| - expect(allPath[0]).toStrictEqual(["A", "E", "F"]); |
85 |
| - expect(allPath[1]).toStrictEqual(["A", "D", "F"]); |
| 84 | + expect(allPath[0]).toStrictEqual(['A', 'E', 'F']); |
| 85 | + expect(allPath[1]).toStrictEqual(['A', 'D', 'F']); |
86 | 86 |
|
87 | 87 | const {
|
88 | 88 | length: directedLenght,
|
89 | 89 | path: directedPath,
|
90 | 90 | allPath: directedAllPath,
|
91 |
| - } = findShortestPath(data, "A", "F", true); |
| 91 | + } = findShortestPath(data, 'A', 'F', true); |
92 | 92 | expect(directedLenght).toBe(2);
|
93 |
| - expect(directedAllPath[0]).toStrictEqual(["A", "E", "F"]); |
94 |
| - expect(directedPath).toStrictEqual(["A", "E", "F"]); |
| 93 | + expect(directedAllPath[0]).toStrictEqual(['A', 'E', 'F']); |
| 94 | + expect(directedPath).toStrictEqual(['A', 'E', 'F']); |
95 | 95 | });
|
96 | 96 |
|
97 |
| - it("find all paths", () => { |
98 |
| - const allPaths = findAllPath(data, "A", "E"); |
| 97 | + it('find all paths', () => { |
| 98 | + const allPaths = findAllPath(data, 'A', 'E'); |
99 | 99 | expect(allPaths.length).toBe(3);
|
100 |
| - expect(allPaths[0]).toStrictEqual(["A", "D", "F", "E"]); |
101 |
| - expect(allPaths[1]).toStrictEqual(["A", "D", "E"]); |
102 |
| - expect(allPaths[2]).toStrictEqual(["A", "E"]); |
| 100 | + expect(allPaths[0]).toStrictEqual(['A', 'D', 'F', 'E']); |
| 101 | + expect(allPaths[1]).toStrictEqual(['A', 'D', 'E']); |
| 102 | + expect(allPaths[2]).toStrictEqual(['A', 'E']); |
103 | 103 | });
|
104 | 104 |
|
105 |
| - it("find all paths in directed graph", () => { |
106 |
| - const allPaths = findAllPath(data, "A", "E", true); |
| 105 | + it('find all paths in directed graph', () => { |
| 106 | + const allPaths = findAllPath(data, 'A', 'E', true); |
107 | 107 | expect(allPaths.length).toStrictEqual(2);
|
108 |
| - expect(allPaths[0]).toStrictEqual(["A", "D", "E"]); |
109 |
| - expect(allPaths[1]).toStrictEqual(["A", "E"]); |
| 108 | + expect(allPaths[0]).toStrictEqual(['A', 'D', 'E']); |
| 109 | + expect(allPaths[1]).toStrictEqual(['A', 'E']); |
110 | 110 | });
|
111 | 111 |
|
112 |
| - it("find all shortest paths in weighted graph", () => { |
| 112 | + it('find all shortest paths in weighted graph', () => { |
113 | 113 | data.edges.forEach((edge, i) => {
|
114 | 114 | edge.weight = ((i % 2) + 1) * 2;
|
115 |
| - if (edge.source === "F" && edge.target === "D") edge.weight = 10; |
| 115 | + if (edge.source === 'F' && edge.target === 'D') edge.weight = 10; |
116 | 116 | });
|
117 |
| - const { length, path, allPath } = findShortestPath( |
118 |
| - data, |
119 |
| - "A", |
120 |
| - "F", |
121 |
| - false, |
122 |
| - "weight" |
123 |
| - ); |
| 117 | + const { length, path, allPath } = findShortestPath(data, 'A', 'F', false, 'weight'); |
124 | 118 | expect(length).toBe(6);
|
125 |
| - expect(allPath[0]).toStrictEqual(["A", "E", "F"]); |
126 |
| - expect(path).toStrictEqual(["A", "E", "F"]); |
| 119 | + expect(allPath[0]).toStrictEqual(['A', 'E', 'F']); |
| 120 | + expect(path).toStrictEqual(['A', 'E', 'F']); |
127 | 121 | });
|
128 | 122 | });
|
0 commit comments