Skip to content

Commit 794219e

Browse files
author
shiwu
committed
fix: shortestPath with wrong result;
1 parent df020f8 commit 794219e

File tree

2 files changed

+68
-74
lines changed

2 files changed

+68
-74
lines changed

packages/graph/src/dijkstra.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { GraphData, NodeConfig, EdgeConfig } from "./types";
2-
import { getOutEdgesNodeId, getEdgesByNodeId } from "./util";
1+
import { isArray } from '@antv/util';
2+
import { GraphData, NodeConfig, EdgeConfig } from './types';
3+
import { getOutEdgesNodeId, getEdgesByNodeId } from './util';
34

45
const minVertex = (
56
D: { [key: string]: number },
67
nodes: NodeConfig[],
7-
marks: { [key: string]: boolean }
8+
marks: { [key: string]: boolean },
89
): NodeConfig => {
910
// 找出最小的点
1011
let minDis = Infinity;
@@ -23,7 +24,7 @@ const dijkstra = (
2324
graphData: GraphData,
2425
source: string,
2526
directed?: boolean,
26-
weightPropertyName?: string
27+
weightPropertyName?: string,
2728
) => {
2829
const { nodes = [], edges = [] } = graphData;
2930
const nodeIds = [];
@@ -50,14 +51,11 @@ const dijkstra = (
5051
if (directed) relatedEdges = getOutEdgesNodeId(minNodeId, edges);
5152
else relatedEdges = getEdgesByNodeId(minNodeId, edges);
5253

53-
relatedEdges.forEach((edge) => {
54+
relatedEdges.forEach(edge => {
5455
const edgeTarget = edge.target;
5556
const edgeSource = edge.source;
5657
const w = edgeTarget === minNodeId ? edgeSource : edgeTarget;
57-
const weight =
58-
weightPropertyName && edge[weightPropertyName]
59-
? edge[weightPropertyName]
60-
: 1;
58+
const weight = weightPropertyName && edge[weightPropertyName] ? edge[weightPropertyName] : 1;
6159
if (D[w] > D[minNode.id] + weight) {
6260
D[w] = D[minNode.id] + weight;
6361
prevs[w] = [minNode.id];
@@ -87,6 +85,7 @@ const dijkstra = (
8785
export default dijkstra;
8886

8987
function findAllPaths(source, target, prevs, foundPaths) {
88+
debugger;
9089
if (source === target) {
9190
return [source];
9291
}
@@ -98,7 +97,8 @@ function findAllPaths(source, target, prevs, foundPaths) {
9897
const prevPaths = findAllPaths(source, prev, prevs, foundPaths);
9998
if (!prevPaths) return;
10099
for (let prePath of prevPaths) {
101-
paths.push([...prePath, target]);
100+
if (isArray(prePath)) paths.push([...prePath, target]);
101+
else paths.push([prePath, target]);
102102
}
103103
}
104104
foundPaths[target] = paths;
Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,122 @@
1-
import { findAllPath, findShortestPath } from "../../src";
1+
import { findAllPath, findShortestPath } from '../../src';
22

33
const data = {
44
nodes: [
55
{
6-
id: "A",
7-
label: "A",
6+
id: 'A',
7+
label: 'A',
88
},
99
{
10-
id: "B",
11-
label: "B",
10+
id: 'B',
11+
label: 'B',
1212
},
1313
{
14-
id: "C",
15-
label: "C",
14+
id: 'C',
15+
label: 'C',
1616
},
1717
{
18-
id: "D",
19-
label: "D",
18+
id: 'D',
19+
label: 'D',
2020
},
2121
{
22-
id: "E",
23-
label: "E",
22+
id: 'E',
23+
label: 'E',
2424
},
2525
{
26-
id: "F",
27-
label: "F",
26+
id: 'F',
27+
label: 'F',
2828
},
2929
{
30-
id: "G",
31-
label: "G",
30+
id: 'G',
31+
label: 'G',
3232
},
3333
{
34-
id: "H",
35-
label: "H",
34+
id: 'H',
35+
label: 'H',
3636
},
3737
],
3838
edges: [
3939
{
40-
source: "A",
41-
target: "B",
40+
source: 'A',
41+
target: 'B',
4242
},
4343
{
44-
source: "B",
45-
target: "C",
44+
source: 'B',
45+
target: 'C',
4646
},
4747
{
48-
source: "C",
49-
target: "G",
48+
source: 'C',
49+
target: 'G',
5050
},
5151
{
52-
source: "A",
53-
target: "D",
52+
source: 'A',
53+
target: 'D',
5454
},
5555
{
56-
source: "A",
57-
target: "E",
56+
source: 'A',
57+
target: 'E',
5858
},
5959
{
60-
source: "E",
61-
target: "F",
60+
source: 'E',
61+
target: 'F',
6262
},
6363
{
64-
source: "F",
65-
target: "D",
64+
source: 'F',
65+
target: 'D',
6666
},
6767
{
68-
source: "D",
69-
target: "E",
68+
source: 'D',
69+
target: 'E',
7070
},
7171
],
7272
};
7373

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');
7777
expect(length).toBe(2);
78-
expect(path).toStrictEqual(["A", "B", "C"]);
78+
expect(path).toStrictEqual(['A', 'B', 'C']);
7979
});
8080

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');
8383
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']);
8686

8787
const {
8888
length: directedLenght,
8989
path: directedPath,
9090
allPath: directedAllPath,
91-
} = findShortestPath(data, "A", "F", true);
91+
} = findShortestPath(data, 'A', 'F', true);
9292
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']);
9595
});
9696

97-
it("find all paths", () => {
98-
const allPaths = findAllPath(data, "A", "E");
97+
it('find all paths', () => {
98+
const allPaths = findAllPath(data, 'A', 'E');
9999
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']);
103103
});
104104

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);
107107
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']);
110110
});
111111

112-
it("find all shortest paths in weighted graph", () => {
112+
it('find all shortest paths in weighted graph', () => {
113113
data.edges.forEach((edge, i) => {
114114
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;
116116
});
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');
124118
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']);
127121
});
128122
});

0 commit comments

Comments
 (0)