|
6 | 6 | // { type: 'status', text } -> overlay status text |
7 | 7 | // plus edge states 'relax' | 'tree' | 'path' | 'negcycle'. |
8 | 8 |
|
9 | | -import { toFlow, weightedAdjacency, edgeList } from './graph'; |
| 9 | +import { toFlow, weightedAdjacency, edgeList } from './graph.js'; |
| 10 | +import { buildPathActions } from './helpers/graph-helpers.js'; |
10 | 11 |
|
11 | 12 | // re-exported for back-compat with existing imports from this module |
12 | 13 | export { toFlow, weightedAdjacency, edgeList }; |
@@ -51,23 +52,8 @@ export const SP_PRESETS = [ |
51 | 52 | }, |
52 | 53 | ]; |
53 | 54 |
|
54 | | -function pathActions(parent, parentEdge, startId, endId) { |
55 | | - const nodes = []; |
56 | | - const edgeSteps = []; |
57 | | - let cur = endId; |
58 | | - while (cur !== undefined && cur !== null) { |
59 | | - nodes.push(cur); |
60 | | - if (parentEdge[cur] != null) edgeSteps.push({ id: parentEdge[cur], from: parent[cur], to: cur }); |
61 | | - if (cur === startId) break; |
62 | | - cur = parent[cur]; |
63 | | - } |
64 | | - nodes.reverse(); |
65 | | - edgeSteps.reverse(); |
66 | | - return [ |
67 | | - ...nodes.map((id) => ({ type: 'markNode', id, state: 'path' })), |
68 | | - ...edgeSteps.map((e) => ({ type: 'markEdge', id: e.id, state: 'path', from: e.from, to: e.to })), |
69 | | - ]; |
70 | | -} |
| 55 | +// pathActions is now imported as buildPathActions from helpers/graph-helpers.js |
| 56 | +export { buildPathActions as pathActions }; |
71 | 57 |
|
72 | 58 | export function dijkstraActions(adj, startId, finishId, nodeIds) { |
73 | 59 | const actions = []; |
@@ -119,7 +105,7 @@ export function dijkstraActions(adj, startId, finishId, nodeIds) { |
119 | 105 | } |
120 | 106 |
|
121 | 107 | if (finishId != null && dist[finishId] < Infinity) { |
122 | | - actions.push(...pathActions(parent, parentEdge, startId, finishId)); |
| 108 | + actions.push(...buildPathActions(parent, parentEdge, startId, finishId)); |
123 | 109 | } |
124 | 110 | return actions; |
125 | 111 | } |
@@ -197,7 +183,7 @@ export function bellmanFordActions(edges, startId, finishId, nodeIds) { |
197 | 183 | } else { |
198 | 184 | actions.push({ type: 'status', text: 'Done' }); |
199 | 185 | if (finishId != null && dist[finishId] < Infinity) { |
200 | | - actions.push(...pathActions(parent, parentEdge, startId, finishId)); |
| 186 | + actions.push(...buildPathActions(parent, parentEdge, startId, finishId)); |
201 | 187 | } |
202 | 188 | } |
203 | 189 | return actions; |
|
0 commit comments