MCPcopy Create free account
hub / github.com/codemix/graph / reconstructPath

Method reconstructPath

packages/graph/src/Steps.ts:5266–5297  ·  view source on GitHub ↗

* Reconstruct the path from the previous map.

(
    target: Vertex<any, any>,
    previous: Map<ElementId, { vertex: Vertex<any, any>; edge: Edge<any, any> } | null>,
    totalWeight?: number,
  )

Source from the content-addressed store, hash-verified

5264 * Reconstruct the path from the previous map.
5265 */
5266 private reconstructPath(
5267 target: Vertex<any, any>,
5268 previous: Map<ElementId, { vertex: Vertex<any, any>; edge: Edge<any, any> } | null>,
5269 totalWeight?: number,
5270 ): ShortestPathResult {
5271 const vertices: Vertex<any, any>[] = [];
5272 const edges: Edge<any, any>[] = [];
5273
5274 let current: Vertex<any, any> | null = target;
5275
5276 while (current !== null) {
5277 vertices.unshift(current);
5278 const prev = previous.get(current.id);
5279 if (prev === null || prev === undefined) {
5280 break;
5281 }
5282 edges.unshift(prev.edge);
5283 current = prev.vertex;
5284 }
5285
5286 const result: ShortestPathResult = {
5287 vertices,
5288 edges,
5289 length: edges.length,
5290 };
5291
5292 if (totalWeight !== undefined) {
5293 result.weight = totalWeight;
5294 }
5295
5296 return result;
5297 }
5298
5299 /**
5300 * Get neighboring vertices based on direction.

Callers 2

bfsMethod · 0.95
dijkstraMethod · 0.95

Calls 1

getMethod · 0.65

Tested by

no test coverage detected