UNPKG

@graphty/layout

Version:

graph layout algorithms based on networkx

37 lines 960 B
/** * Grid graph generation function */ /** * Create a grid graph with rows x cols nodes * @param rows - Number of rows * @param cols - Number of columns * @returns Graph object with grid topology */ export function gridGraph(rows, cols) { const nodes = []; const edges = []; // Create nodes for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { nodes.push(`${i},${j}`); } } // Create edges for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { // Connect to right neighbor if (j < cols - 1) { edges.push([`${i},${j}`, `${i},${j + 1}`]); } // Connect to bottom neighbor if (i < rows - 1) { edges.push([`${i},${j}`, `${i + 1},${j}`]); } } } return { nodes: () => nodes, edges: () => edges }; } //# sourceMappingURL=grid.js.map