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;
|
const nodeNum = nodes.length;
|
||||||
for (let i = 0; i < nodeNum; i++) {
|
for (let i = 0; i < nodeNum; i++) {
|
||||||
//Process the vertices
|
// Process the vertices
|
||||||
let minNode = minVertex(D, nodes, marks);
|
const minNode = minVertex(D, nodes, marks);
|
||||||
let minNodId = minNode.get('id');
|
const minNodId = minNode.get('id');
|
||||||
marks[minNodId] = true;
|
marks[minNodId] = true;
|
||||||
|
|
||||||
if (D[minNodId] === Infinity) continue; // Unreachable vertices cannot be the intermediate point
|
if (D[minNodId] === Infinity) continue; // Unreachable vertices cannot be the intermediate point
|
||||||
@ -45,7 +45,7 @@ const dijkstra = (
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
const path = {};
|
const path = {};
|
||||||
for (let target in D) {
|
for (const target in D) {
|
||||||
path[target] = [target];
|
path[target] = [target];
|
||||||
let prev = prevs[target];
|
let prev = prevs[target];
|
||||||
while (prev !== undefined) {
|
while (prev !== undefined) {
|
||||||
@ -62,11 +62,11 @@ function minVertex(
|
|||||||
nodes: INode[],
|
nodes: INode[],
|
||||||
marks: { [key: string]: boolean },
|
marks: { [key: string]: boolean },
|
||||||
) {
|
) {
|
||||||
//找出最小的点
|
// 找出最小的点
|
||||||
let minDis = Infinity,
|
let minDis = Infinity;
|
||||||
minNode;
|
let minNode;
|
||||||
for (let i = 0; i < nodes.length; i++) {
|
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) {
|
if (!marks[nodeId] && D[nodeId] <= minDis) {
|
||||||
minDis = D[nodeId];
|
minDis = D[nodeId];
|
||||||
minNode = nodes[i];
|
minNode = nodes[i];
|
||||||
|
@ -1,56 +1,67 @@
|
|||||||
import { IGraph } from '../interface/graph';
|
import { IGraph } from '../interface/graph';
|
||||||
import { INode } from '../interface/item';
|
import { INode } from '../interface/item';
|
||||||
import { isString } from '@antv/util'
|
import { isString } from '@antv/util';
|
||||||
import dijkstra from './dijkstra'
|
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(start)) start = graph.findById(start) as INode;
|
||||||
if (isString(end)) end = graph.findById(end) 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')] };
|
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(start)) start = graph.findById(start) as INode;
|
||||||
if (isString(end)) end = graph.findById(end) 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 visited = [start];
|
||||||
const isVisited = { [start.get('id')]: true }
|
const isVisited = { [start.get('id')]: true };
|
||||||
const stack = [] // 辅助栈,用于存储访问过的节点的邻居节点
|
const stack = []; // 辅助栈,用于存储访问过的节点的邻居节点
|
||||||
const allPaths = []
|
const allPaths = [];
|
||||||
let neighbors = directed ? start.getNeighbors('target') : start.getNeighbors()
|
let neighbors = directed ? start.getNeighbors('target') : start.getNeighbors();
|
||||||
stack.push(neighbors)
|
stack.push(neighbors);
|
||||||
|
|
||||||
while (visited.length > 0 && stack.length > 0) {
|
while (visited.length > 0 && stack.length > 0) {
|
||||||
let children = stack[stack.length - 1]
|
const children = stack[stack.length - 1];
|
||||||
if (children.length) {
|
if (children.length) {
|
||||||
const child = children.shift()
|
const child = children.shift();
|
||||||
if (child) {
|
if (child) {
|
||||||
visited.push(child)
|
visited.push(child);
|
||||||
isVisited[child.get('id')] = true
|
isVisited[child.get('id')] = true;
|
||||||
neighbors = directed ? child.getNeighbors('target') : child.getNeighbors()
|
neighbors = directed ? child.getNeighbors('target') : child.getNeighbors();
|
||||||
stack.push(neighbors.filter(neighbor => !isVisited[neighbor.get('id')]))
|
stack.push(neighbors.filter((neighbor) => !isVisited[neighbor.get('id')]));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const node = visited.pop()
|
const node = visited.pop();
|
||||||
isVisited[node.get('id')] = false
|
isVisited[node.get('id')] = false;
|
||||||
stack.pop()
|
stack.pop();
|
||||||
continue
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (visited[visited.length - 1] === end) {
|
if (visited[visited.length - 1] === end) {
|
||||||
const path = visited.map(node => node.get('id'))
|
const path = visited.map((node) => node.get('id'));
|
||||||
allPaths.push(path)
|
allPaths.push(path);
|
||||||
|
|
||||||
const node = visited.pop()
|
const node = visited.pop();
|
||||||
isVisited[node.get('id')] = false
|
isVisited[node.get('id')] = false;
|
||||||
stack.pop()
|
stack.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return allPaths
|
return allPaths;
|
||||||
}
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user