mirror of
https://github.com/Doodle3D/Doodle3D-Slicer.git
synced 2024-11-22 05:37:55 +01:00
use update item
Instead of pushing new item every time
This commit is contained in:
parent
13ffea4115
commit
9a2bd4a178
@ -154,12 +154,13 @@ function shortestPath(graph, start, end) {
|
||||
const distances = graph.map(() => Infinity);
|
||||
distances[start] = 0;
|
||||
|
||||
const heap = new Heap((a, b) => a.distance - b.distance);
|
||||
heap.push({ nodeIndex: start, distance: 0 });
|
||||
const heap = new Heap((a, b) => distances[a] - distances[b]);
|
||||
for (let i = 0; i < graph.length; i ++) {
|
||||
heap.push(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < graph.length; i ++) {
|
||||
let nodeIndex;
|
||||
do { nodeIndex = heap.pop().nodeIndex } while (visited[nodeIndex]);
|
||||
const nodeIndex = heap.pop();
|
||||
|
||||
if (nodeIndex === end) break;
|
||||
|
||||
@ -168,11 +169,14 @@ function shortestPath(graph, start, end) {
|
||||
|
||||
for (let i = 0; i < node.length; i ++) {
|
||||
const child = node[i];
|
||||
|
||||
if (visited[child.to]) continue;
|
||||
|
||||
const distance = distances[nodeIndex] + child.distance;
|
||||
if (distance < distances[child.to]) {
|
||||
heap.push({ nodeIndex: child.to, distance });
|
||||
distances[child.to] = distance;
|
||||
traverse[child.to] = nodeIndex;
|
||||
heap.updateItem(child.to);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user