mirror of
https://gitee.com/antv/g6.git
synced 2024-11-30 10:48:24 +08:00
fix eslint problems
This commit is contained in:
parent
e0cbaf870d
commit
ddc764f1ee
@ -21,9 +21,9 @@ const dijkstra = (
|
||||
|
||||
const nodeNum = nodes.length;
|
||||
for (let i = 0; i < nodeNum; i++) {
|
||||
//Process the vertices
|
||||
let minNode = minVertex(D, nodes, marks);
|
||||
let minNodId = minNode.get('id');
|
||||
// Process the vertices
|
||||
const minNode = minVertex(D, nodes, marks);
|
||||
const minNodId = minNode.get('id');
|
||||
marks[minNodId] = true;
|
||||
|
||||
if (D[minNodId] === Infinity) continue; // Unreachable vertices cannot be the intermediate point
|
||||
@ -45,7 +45,7 @@ const dijkstra = (
|
||||
});
|
||||
}
|
||||
const path = {};
|
||||
for (let target in D) {
|
||||
for (const target in D) {
|
||||
path[target] = [target];
|
||||
let prev = prevs[target];
|
||||
while (prev !== undefined) {
|
||||
@ -62,11 +62,11 @@ function minVertex(
|
||||
nodes: INode[],
|
||||
marks: { [key: string]: boolean },
|
||||
) {
|
||||
//找出最小的点
|
||||
let minDis = Infinity,
|
||||
minNode;
|
||||
// 找出最小的点
|
||||
let minDis = Infinity;
|
||||
let minNode;
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
let nodeId = nodes[i].get('id');
|
||||
const nodeId = nodes[i].get('id');
|
||||
if (!marks[nodeId] && D[nodeId] <= minDis) {
|
||||
minDis = D[nodeId];
|
||||
minNode = nodes[i];
|
||||
|
@ -1,56 +1,67 @@
|
||||
import { IGraph } from '../interface/graph';
|
||||
import { INode } from '../interface/item';
|
||||
import { isString } from '@antv/util'
|
||||
import dijkstra from './dijkstra'
|
||||
import { isString } from '@antv/util';
|
||||
import dijkstra from './dijkstra';
|
||||
|
||||
export const findShortestPath = (graph: IGraph, start: INode | string, end: INode | string, directed?: boolean, weightPropertyName?: string) => {
|
||||
export const findShortestPath = (
|
||||
graph: IGraph,
|
||||
start: INode | string,
|
||||
end: INode | string,
|
||||
directed?: boolean,
|
||||
weightPropertyName?: string,
|
||||
) => {
|
||||
if (isString(start)) start = graph.findById(start) as INode;
|
||||
if (isString(end)) end = graph.findById(end) as INode;
|
||||
|
||||
const { length, path } = dijkstra(graph, start.get('id'), directed, weightPropertyName)
|
||||
const { length, path } = dijkstra(graph, start.get('id'), directed, weightPropertyName);
|
||||
return { length: length[end.get('id')], path: path[end.get('id')] };
|
||||
}
|
||||
};
|
||||
|
||||
export const findAllPath = (graph: IGraph, start: INode | string, end: INode | string, directed?: boolean) => {
|
||||
export const findAllPath = (
|
||||
graph: IGraph,
|
||||
start: INode | string,
|
||||
end: INode | string,
|
||||
directed?: boolean,
|
||||
) => {
|
||||
if (isString(start)) start = graph.findById(start) as INode;
|
||||
if (isString(end)) end = graph.findById(end) as INode;
|
||||
if (directed === undefined) directed = graph.get('directed')
|
||||
if (directed === undefined) directed = graph.get('directed');
|
||||
|
||||
if (start === end) return [[start.get('id')]]
|
||||
if (start === end) return [[start.get('id')]];
|
||||
|
||||
const visited = [start]
|
||||
const isVisited = { [start.get('id')]: true }
|
||||
const stack = [] // 辅助栈,用于存储访问过的节点的邻居节点
|
||||
const allPaths = []
|
||||
let neighbors = directed ? start.getNeighbors('target') : start.getNeighbors()
|
||||
stack.push(neighbors)
|
||||
const visited = [start];
|
||||
const isVisited = { [start.get('id')]: true };
|
||||
const stack = []; // 辅助栈,用于存储访问过的节点的邻居节点
|
||||
const allPaths = [];
|
||||
let neighbors = directed ? start.getNeighbors('target') : start.getNeighbors();
|
||||
stack.push(neighbors);
|
||||
|
||||
while (visited.length > 0 && stack.length > 0) {
|
||||
let children = stack[stack.length - 1]
|
||||
const children = stack[stack.length - 1];
|
||||
if (children.length) {
|
||||
const child = children.shift()
|
||||
const child = children.shift();
|
||||
if (child) {
|
||||
visited.push(child)
|
||||
isVisited[child.get('id')] = true
|
||||
neighbors = directed ? child.getNeighbors('target') : child.getNeighbors()
|
||||
stack.push(neighbors.filter(neighbor => !isVisited[neighbor.get('id')]))
|
||||
visited.push(child);
|
||||
isVisited[child.get('id')] = true;
|
||||
neighbors = directed ? child.getNeighbors('target') : child.getNeighbors();
|
||||
stack.push(neighbors.filter((neighbor) => !isVisited[neighbor.get('id')]));
|
||||
}
|
||||
} else {
|
||||
const node = visited.pop()
|
||||
isVisited[node.get('id')] = false
|
||||
stack.pop()
|
||||
continue
|
||||
const node = visited.pop();
|
||||
isVisited[node.get('id')] = false;
|
||||
stack.pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (visited[visited.length - 1] === end) {
|
||||
const path = visited.map(node => node.get('id'))
|
||||
allPaths.push(path)
|
||||
const path = visited.map((node) => node.get('id'));
|
||||
allPaths.push(path);
|
||||
|
||||
const node = visited.pop()
|
||||
isVisited[node.get('id')] = false
|
||||
stack.pop()
|
||||
const node = visited.pop();
|
||||
isVisited[node.get('id')] = false;
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
return allPaths
|
||||
}
|
||||
return allPaths;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user