MCPcopy Create free account
hub / github.com/ScaleML/AgentSPEX / findConnectedComponents

Function findConnectedComponents

yaml-flow-editor/src/utils/layout.ts:542–567  ·  view source on GitHub ↗

* Find all connected components among nodes via edges (undirected). * Returns an array of node-ID sets, one per component.

(nodes: FlowNode[], edges: FlowEdge[])

Source from the content-addressed store, hash-verified

540 * Returns an array of node-ID sets, one per component.
541 */
542function 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
569export function getLayoutedElements(
570 nodes: FlowNode[],

Callers 1

getLayoutedElementsFunction · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected