Skip to content

Commit 5bca150

Browse files
authored
Merge pull request #9 from antvis/gaddi
Gaddi
2 parents 75a4f7f + 9259f82 commit 5bca150

File tree

16 files changed

+42600
-23
lines changed

16 files changed

+42600
-23
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"bootstrap": "lerna bootstrap",
1414
"ls": "lerna list"
1515
},
16+
"dependencies": {
17+
"@antv/util": "^2.0.9"
18+
},
1619
"devDependencies": {
1720
"lerna": "^3.20.2"
1821
}

packages/graph/src/adjacent-matrix.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { GraphData, Matrix } from './types'
1+
import { GraphData, Matrix } from "./types";
22

33
const adjMatrix = (graphData: GraphData, directed?: boolean) => {
4-
const { nodes, edges } = graphData
4+
const { nodes, edges } = graphData;
55
const matrix: Matrix[] = [];
66
// map node with index in data.nodes
77
const nodeMap: {
88
[key: string]: number;
99
} = {};
1010

1111
if (!nodes) {
12-
throw new Error('invalid nodes data!');
12+
throw new Error("invalid nodes data!");
1313
}
14-
14+
1515
if (nodes) {
1616
nodes.forEach((node, i) => {
1717
nodeMap[node.id] = i;
@@ -25,6 +25,7 @@ const adjMatrix = (graphData: GraphData, directed?: boolean) => {
2525
const { source, target } = edge;
2626
const sIndex = nodeMap[source as string];
2727
const tIndex = nodeMap[target as string];
28+
if ((!sIndex && sIndex !== 0) || (!tIndex && tIndex !== 0)) return;
2829
matrix[sIndex][tIndex] = 1;
2930
if (!directed) {
3031
matrix[tIndex][sIndex] = 1;

packages/graph/src/dijkstra.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { GraphData, NodeConfig, EdgeConfig } from './types';
2-
import { getOutEdgesNodeId, getEdgesByNodeId } from './util';
1+
import { GraphData, NodeConfig, EdgeConfig } from "./types";
2+
import { getOutEdgesNodeId, getEdgesByNodeId } from "./util";
33

44
const minVertex = (
55
D: { [key: string]: number },
@@ -50,7 +50,7 @@ const dijkstra = (
5050
if (directed) relatedEdges = getOutEdgesNodeId(minNodeId, edges);
5151
else relatedEdges = getEdgesByNodeId(minNodeId, edges);
5252

53-
relatedEdges.forEach(edge => {
53+
relatedEdges.forEach((edge) => {
5454
const edgeTarget = edge.target;
5555
const edgeSource = edge.source;
5656
const w = edgeTarget === minNodeId ? edgeSource : edgeTarget;
@@ -86,20 +86,21 @@ const dijkstra = (
8686

8787
export default dijkstra;
8888

89-
function findAllPaths(source, target, prevs, findedPaths) {
89+
function findAllPaths(source, target, prevs, foundPaths) {
9090
if (source === target) {
9191
return [source];
9292
}
93-
if (findedPaths[target]) {
94-
return findedPaths[target];
93+
if (foundPaths[target]) {
94+
return foundPaths[target];
9595
}
9696
const paths = [];
9797
for (let prev of prevs[target]) {
98-
const prevPaths = findAllPaths(source, prev, prevs, findedPaths);
98+
const prevPaths = findAllPaths(source, prev, prevs, foundPaths);
99+
if (!prevPaths) return;
99100
for (let prePath of prevPaths) {
100101
paths.push([...prePath, target]);
101102
}
102103
}
103-
findedPaths[target] = paths;
104+
foundPaths[target] = paths;
104105
return;
105106
}

packages/graph/src/floydWarshall.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import getAdjMatrix from './adjacent-matrix';
2-
import { GraphData, Matrix } from './types';
1+
import getAdjMatrix from "./adjacent-matrix";
2+
import { GraphData, Matrix } from "./types";
33

44
const floydWarshall = (graphData: GraphData, directed?: boolean) => {
5-
const adjacentMatrix = getAdjMatrix(graphData, directed);;
5+
const adjacentMatrix = getAdjMatrix(graphData, directed);
66

77
const dist: Matrix[] = [];
88
const size = adjacentMatrix.length;

0 commit comments

Comments
 (0)