* Find all connected components among nodes via edges (undirected). * Returns an array of node-ID sets, one per component.
(nodes: FlowNode[], edges: FlowEdge[])
| 540 | * Returns an array of node-ID sets, one per component. |
| 541 | */ |
| 542 | function findConnectedComponents(nodes: FlowNode[], edges: FlowEdge[]): Set<string>[] { |
| 543 | const adj = new Map<string, Set<string>>(); |
| 544 | for (const n of nodes) adj.set(n.id, new Set()); |
| 545 | for (const e of edges) { |
| 546 | adj.get(e.source)?.add(e.target); |
| 547 | adj.get(e.target)?.add(e.source); |
| 548 | } |
| 549 | const visited = new Set<string>(); |
| 550 | const components: Set<string>[] = []; |
| 551 | for (const n of nodes) { |
| 552 | if (visited.has(n.id)) continue; |
| 553 | const comp = new Set<string>(); |
| 554 | const stack = [n.id]; |
| 555 | while (stack.length > 0) { |
| 556 | const cur = stack.pop()!; |
| 557 | if (visited.has(cur)) continue; |
| 558 | visited.add(cur); |
| 559 | comp.add(cur); |
| 560 | for (const nb of adj.get(cur) ?? []) { |
| 561 | if (!visited.has(nb)) stack.push(nb); |
| 562 | } |
| 563 | } |
| 564 | components.push(comp); |
| 565 | } |
| 566 | return components; |
| 567 | } |
| 568 | |
| 569 | export function getLayoutedElements( |
| 570 | nodes: FlowNode[], |
no test coverage detected