forked from TamimEhsan/AlgorithmVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph-helpers.js
More file actions
34 lines (33 loc) · 1.4 KB
/
Copy pathgraph-helpers.js
File metadata and controls
34 lines (33 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// graph-helpers.js — Shared utilities for graph-based algorithm visualizations
//
// Extracted from duplicated code across graph.js and shortestPath.js.
// Both files had identical pathActions() implementations.
// Dijkstra.js and Astar.js shared getAllNodes, sortNodesByDistance,
// updateUnvisitedNeighbors, getUnvisitedNeighbors.
/**
* Reconstruct a start→end path from parent maps and emit markNode/markEdge actions.
* Shared by BFS, DFS, Dijkstra, and Bellman-Ford action planners.
*
* @param {Object} parent - Map of nodeId → parent nodeId
* @param {Object} parentEdge - Map of nodeId → edge id used to reach this node
* @param {string} startId - Source node ID
* @param {string} endId - Destination node ID
* @returns {Array} Array of markNode/markEdge action objects
*/
export function buildPathActions(parent, parentEdge, startId, endId) {
const nodes = [];
const edgeSteps = [];
let cur = endId;
while (cur !== undefined && cur !== null) {
nodes.push(cur);
if (parentEdge[cur] != null) edgeSteps.push({ id: parentEdge[cur], from: parent[cur], to: cur });
if (cur === startId) break;
cur = parent[cur];
}
nodes.reverse();
edgeSteps.reverse();
return [
...nodes.map((id) => ({ type: 'markNode', id, state: 'path' })),
...edgeSteps.map((e) => ({ type: 'markEdge', id: e.id, state: 'path', from: e.from, to: e.to })),
];
}