@graphty/layout 1.2.8 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/README.md +394 -404
  2. package/dist/layout.js +881 -821
  3. package/dist/layout.js.map +1 -1
  4. package/dist/src/algorithms/index.d.ts +2 -2
  5. package/dist/src/algorithms/index.js +2 -2
  6. package/dist/src/algorithms/index.js.map +1 -1
  7. package/dist/src/algorithms/optimization/index.d.ts +4 -4
  8. package/dist/src/algorithms/optimization/index.js +3 -3
  9. package/dist/src/algorithms/optimization/index.js.map +1 -1
  10. package/dist/src/algorithms/optimization/kamada-kawai-solver.d.ts +2 -5
  11. package/dist/src/algorithms/optimization/kamada-kawai-solver.js +6 -9
  12. package/dist/src/algorithms/optimization/kamada-kawai-solver.js.map +1 -1
  13. package/dist/src/algorithms/optimization/lbfgs.d.ts +0 -1
  14. package/dist/src/algorithms/optimization/lbfgs.js +3 -5
  15. package/dist/src/algorithms/optimization/lbfgs.js.map +1 -1
  16. package/dist/src/algorithms/optimization/line-search.d.ts +0 -1
  17. package/dist/src/algorithms/optimization/line-search.js +0 -1
  18. package/dist/src/algorithms/optimization/line-search.js.map +1 -1
  19. package/dist/src/algorithms/optimization/types.d.ts +1 -1
  20. package/dist/src/algorithms/planarity/check.d.ts +3 -3
  21. package/dist/src/algorithms/planarity/check.js +7 -7
  22. package/dist/src/algorithms/planarity/check.js.map +1 -1
  23. package/dist/src/algorithms/planarity/embedding.d.ts +3 -5
  24. package/dist/src/algorithms/planarity/embedding.js +20 -16
  25. package/dist/src/algorithms/planarity/embedding.js.map +1 -1
  26. package/dist/src/algorithms/planarity/index.d.ts +4 -4
  27. package/dist/src/algorithms/planarity/index.js +4 -4
  28. package/dist/src/algorithms/planarity/index.js.map +1 -1
  29. package/dist/src/algorithms/planarity/lr-test.d.ts +3 -3
  30. package/dist/src/algorithms/planarity/lr-test.js +5 -5
  31. package/dist/src/algorithms/planarity/lr-test.js.map +1 -1
  32. package/dist/src/algorithms/planarity/special-graphs.d.ts +1 -4
  33. package/dist/src/algorithms/planarity/special-graphs.js +18 -15
  34. package/dist/src/algorithms/planarity/special-graphs.js.map +1 -1
  35. package/dist/src/generators/basic.d.ts +1 -1
  36. package/dist/src/generators/basic.js +4 -4
  37. package/dist/src/generators/basic.js.map +1 -1
  38. package/dist/src/generators/bipartite.d.ts +1 -1
  39. package/dist/src/generators/bipartite.js +2 -2
  40. package/dist/src/generators/bipartite.js.map +1 -1
  41. package/dist/src/generators/grid.d.ts +1 -1
  42. package/dist/src/generators/grid.js +1 -1
  43. package/dist/src/generators/grid.js.map +1 -1
  44. package/dist/src/generators/index.d.ts +5 -5
  45. package/dist/src/generators/index.js +5 -5
  46. package/dist/src/generators/index.js.map +1 -1
  47. package/dist/src/generators/random.d.ts +1 -1
  48. package/dist/src/generators/random.js +2 -2
  49. package/dist/src/generators/random.js.map +1 -1
  50. package/dist/src/generators/scale-free.d.ts +1 -1
  51. package/dist/src/generators/scale-free.js +4 -4
  52. package/dist/src/generators/scale-free.js.map +1 -1
  53. package/dist/src/index.d.ts +4 -4
  54. package/dist/src/index.js +4 -4
  55. package/dist/src/layouts/basic/index.d.ts +1 -1
  56. package/dist/src/layouts/basic/index.js +1 -1
  57. package/dist/src/layouts/basic/random.d.ts +1 -2
  58. package/dist/src/layouts/basic/random.js +3 -4
  59. package/dist/src/layouts/basic/random.js.map +1 -1
  60. package/dist/src/layouts/force-directed/arf.d.ts +1 -2
  61. package/dist/src/layouts/force-directed/arf.js +13 -8
  62. package/dist/src/layouts/force-directed/arf.js.map +1 -1
  63. package/dist/src/layouts/force-directed/forceatlas2.d.ts +1 -2
  64. package/dist/src/layouts/force-directed/forceatlas2.js +58 -35
  65. package/dist/src/layouts/force-directed/forceatlas2.js.map +1 -1
  66. package/dist/src/layouts/force-directed/fruchterman-reingold.d.ts +11 -12
  67. package/dist/src/layouts/force-directed/fruchterman-reingold.js +17 -17
  68. package/dist/src/layouts/force-directed/fruchterman-reingold.js.map +1 -1
  69. package/dist/src/layouts/force-directed/index.d.ts +5 -5
  70. package/dist/src/layouts/force-directed/index.js +5 -5
  71. package/dist/src/layouts/force-directed/index.js.map +1 -1
  72. package/dist/src/layouts/force-directed/kamada-kawai.d.ts +2 -3
  73. package/dist/src/layouts/force-directed/kamada-kawai.js +12 -10
  74. package/dist/src/layouts/force-directed/kamada-kawai.js.map +1 -1
  75. package/dist/src/layouts/force-directed/spring.d.ts +11 -12
  76. package/dist/src/layouts/force-directed/spring.js +11 -12
  77. package/dist/src/layouts/force-directed/spring.js.map +1 -1
  78. package/dist/src/layouts/geometric/circular.d.ts +1 -2
  79. package/dist/src/layouts/geometric/circular.js +10 -9
  80. package/dist/src/layouts/geometric/circular.js.map +1 -1
  81. package/dist/src/layouts/geometric/index.d.ts +3 -3
  82. package/dist/src/layouts/geometric/index.js +3 -3
  83. package/dist/src/layouts/geometric/shell.d.ts +1 -2
  84. package/dist/src/layouts/geometric/shell.js +5 -5
  85. package/dist/src/layouts/geometric/shell.js.map +1 -1
  86. package/dist/src/layouts/geometric/spiral.d.ts +1 -2
  87. package/dist/src/layouts/geometric/spiral.js +5 -9
  88. package/dist/src/layouts/geometric/spiral.js.map +1 -1
  89. package/dist/src/layouts/hierarchical/bfs.d.ts +2 -3
  90. package/dist/src/layouts/hierarchical/bfs.js +5 -6
  91. package/dist/src/layouts/hierarchical/bfs.js.map +1 -1
  92. package/dist/src/layouts/hierarchical/bipartite.d.ts +2 -3
  93. package/dist/src/layouts/hierarchical/bipartite.js +8 -9
  94. package/dist/src/layouts/hierarchical/bipartite.js.map +1 -1
  95. package/dist/src/layouts/hierarchical/index.d.ts +3 -3
  96. package/dist/src/layouts/hierarchical/index.js +3 -3
  97. package/dist/src/layouts/hierarchical/index.js.map +1 -1
  98. package/dist/src/layouts/hierarchical/multipartite.d.ts +2 -3
  99. package/dist/src/layouts/hierarchical/multipartite.js +7 -8
  100. package/dist/src/layouts/hierarchical/multipartite.js.map +1 -1
  101. package/dist/src/layouts/index.d.ts +5 -5
  102. package/dist/src/layouts/index.js +5 -5
  103. package/dist/src/layouts/index.js.map +1 -1
  104. package/dist/src/layouts/specialized/index.d.ts +2 -2
  105. package/dist/src/layouts/specialized/index.js +2 -2
  106. package/dist/src/layouts/specialized/index.js.map +1 -1
  107. package/dist/src/layouts/specialized/planar.d.ts +3 -3
  108. package/dist/src/layouts/specialized/planar.js +8 -8
  109. package/dist/src/layouts/specialized/planar.js.map +1 -1
  110. package/dist/src/layouts/specialized/spectral.d.ts +1 -2
  111. package/dist/src/layouts/specialized/spectral.js +30 -20
  112. package/dist/src/layouts/specialized/spectral.js.map +1 -1
  113. package/dist/src/types/embedding.d.ts +1 -1
  114. package/dist/src/types/index.d.ts +3 -3
  115. package/dist/src/types/index.js +3 -3
  116. package/dist/src/types/index.js.map +1 -1
  117. package/dist/src/types/layout.d.ts +1 -1
  118. package/dist/src/utils/graph.d.ts +1 -5
  119. package/dist/src/utils/graph.js +4 -6
  120. package/dist/src/utils/graph.js.map +1 -1
  121. package/dist/src/utils/index.d.ts +5 -5
  122. package/dist/src/utils/index.js +5 -5
  123. package/dist/src/utils/index.js.map +1 -1
  124. package/dist/src/utils/numpy.js +22 -16
  125. package/dist/src/utils/numpy.js.map +1 -1
  126. package/dist/src/utils/params.d.ts +1 -2
  127. package/dist/src/utils/params.js +0 -1
  128. package/dist/src/utils/params.js.map +1 -1
  129. package/dist/src/utils/random.d.ts +17 -0
  130. package/dist/src/utils/random.js +18 -1
  131. package/dist/src/utils/random.js.map +1 -1
  132. package/dist/src/utils/rescale.d.ts +1 -3
  133. package/dist/src/utils/rescale.js +12 -10
  134. package/dist/src/utils/rescale.js.map +1 -1
  135. package/dist/vitest.config.js +14 -14
  136. package/dist/vitest.config.js.map +1 -1
  137. package/package.json +29 -24
  138. package/src/algorithms/index.ts +2 -2
  139. package/src/algorithms/optimization/index.ts +4 -8
  140. package/src/algorithms/optimization/kamada-kawai-solver.ts +170 -179
  141. package/src/algorithms/optimization/lbfgs.ts +41 -48
  142. package/src/algorithms/optimization/line-search.ts +27 -28
  143. package/src/algorithms/optimization/types.ts +2 -2
  144. package/src/algorithms/planarity/check.ts +22 -21
  145. package/src/algorithms/planarity/embedding.ts +159 -165
  146. package/src/algorithms/planarity/index.ts +4 -8
  147. package/src/algorithms/planarity/lr-test.ts +46 -45
  148. package/src/algorithms/planarity/special-graphs.ts +73 -83
  149. package/src/generators/basic.ts +57 -57
  150. package/src/generators/bipartite.ts +37 -31
  151. package/src/generators/grid.ts +28 -28
  152. package/src/generators/index.ts +5 -11
  153. package/src/generators/random.ts +26 -25
  154. package/src/generators/scale-free.ts +56 -55
  155. package/src/index.ts +5 -5
  156. package/src/layouts/basic/index.ts +1 -1
  157. package/src/layouts/basic/random.ts +20 -16
  158. package/src/layouts/force-directed/arf.ts +107 -105
  159. package/src/layouts/force-directed/forceatlas2.ts +365 -351
  160. package/src/layouts/force-directed/fruchterman-reingold.ts +133 -134
  161. package/src/layouts/force-directed/index.ts +5 -5
  162. package/src/layouts/force-directed/kamada-kawai.ts +78 -80
  163. package/src/layouts/force-directed/spring.ts +24 -25
  164. package/src/layouts/geometric/circular.ts +63 -57
  165. package/src/layouts/geometric/index.ts +3 -3
  166. package/src/layouts/geometric/shell.ts +58 -53
  167. package/src/layouts/geometric/spiral.ts +60 -64
  168. package/src/layouts/hierarchical/bfs.ts +58 -59
  169. package/src/layouts/hierarchical/bipartite.ts +62 -63
  170. package/src/layouts/hierarchical/index.ts +3 -3
  171. package/src/layouts/hierarchical/multipartite.ts +62 -63
  172. package/src/layouts/index.ts +5 -5
  173. package/src/layouts/specialized/index.ts +2 -2
  174. package/src/layouts/specialized/planar.ts +42 -41
  175. package/src/layouts/specialized/spectral.ts +110 -99
  176. package/src/types/embedding.ts +5 -5
  177. package/src/types/graph.ts +4 -4
  178. package/src/types/index.ts +3 -3
  179. package/src/types/layout.ts +2 -2
  180. package/src/utils/graph.ts +31 -35
  181. package/src/utils/index.ts +5 -5
  182. package/src/utils/numpy.ts +96 -92
  183. package/src/utils/params.ts +14 -11
  184. package/src/utils/random.ts +57 -40
  185. package/src/utils/rescale.ts +169 -174
@@ -2,125 +2,115 @@
2
2
  * Special graph detection functions for planarity testing
3
3
  */
4
4
 
5
- import { Node, Edge } from '../../types';
5
+ import { Edge,Node } from "../../types";
6
6
 
7
7
  /**
8
8
  * Check if graph is K5 (complete graph with 5 nodes)
9
- *
10
9
  * @param nodes - List of nodes
11
10
  * @param edges - List of edges
12
11
  * @returns True if graph is K5
13
12
  */
14
13
  export function isK5(nodes: Node[], edges: Edge[]): boolean {
15
- if (nodes.length !== 5) return false;
16
-
17
- // K5 has exactly 10 edges
18
- if (edges.length !== 10) return false;
19
-
20
- // Check if every pair of distinct nodes is connected
21
- for (let i = 0; i < nodes.length; i++) {
22
- for (let j = i + 1; j < nodes.length; j++) {
23
- const hasEdge = edges.some(
24
- e => (e[0] === nodes[i] && e[1] === nodes[j]) ||
25
- (e[0] === nodes[j] && e[1] === nodes[i])
26
- );
27
- if (!hasEdge) return false;
14
+ if (nodes.length !== 5) {return false;}
15
+
16
+ // K5 has exactly 10 edges
17
+ if (edges.length !== 10) {return false;}
18
+
19
+ // Check if every pair of distinct nodes is connected
20
+ for (let i = 0; i < nodes.length; i++) {
21
+ for (let j = i + 1; j < nodes.length; j++) {
22
+ const hasEdge = edges.some(
23
+ (e) => (e[0] === nodes[i] && e[1] === nodes[j]) || (e[0] === nodes[j] && e[1] === nodes[i]),
24
+ );
25
+ if (!hasEdge) {return false;}
26
+ }
28
27
  }
29
- }
30
28
 
31
- return true;
29
+ return true;
32
30
  }
33
31
 
34
32
  /**
35
33
  * Check if graph is K3,3 (complete bipartite with 3,3 nodes)
36
- *
37
34
  * @param nodes - List of nodes
38
35
  * @param edges - List of edges
39
36
  * @returns True if graph is K3,3
40
37
  */
41
38
  export function isK33(nodes: Node[], edges: Edge[]): boolean {
42
- if (nodes.length !== 6) return false;
39
+ if (nodes.length !== 6) {return false;}
43
40
 
44
- // K3,3 has exactly 9 edges
45
- if (edges.length !== 9) return false;
41
+ // K3,3 has exactly 9 edges
42
+ if (edges.length !== 9) {return false;}
46
43
 
47
- // Try to find a bipartite partition
48
- const nodePartitions = tryFindBipartitePartition(nodes, edges);
49
- if (!nodePartitions) return false;
44
+ // Try to find a bipartite partition
45
+ const nodePartitions = tryFindBipartitePartition(nodes, edges);
46
+ if (!nodePartitions) {return false;}
50
47
 
51
- const [part1, part2] = nodePartitions;
48
+ const [part1, part2] = nodePartitions;
52
49
 
53
- // Check if both partitions have size 3
54
- if (part1.length !== 3 || part2.length !== 3) return false;
50
+ // Check if both partitions have size 3
51
+ if (part1.length !== 3 || part2.length !== 3) {return false;}
55
52
 
56
- // Check if every node in part1 is connected to every node in part2
57
- for (const n1 of part1) {
58
- for (const n2 of part2) {
59
- const hasEdge = edges.some(
60
- e => (e[0] === n1 && e[1] === n2) ||
61
- (e[0] === n2 && e[1] === n1)
62
- );
63
- if (!hasEdge) return false;
53
+ // Check if every node in part1 is connected to every node in part2
54
+ for (const n1 of part1) {
55
+ for (const n2 of part2) {
56
+ const hasEdge = edges.some((e) => (e[0] === n1 && e[1] === n2) || (e[0] === n2 && e[1] === n1));
57
+ if (!hasEdge) {return false;}
58
+ }
64
59
  }
65
- }
66
60
 
67
- return true;
61
+ return true;
68
62
  }
69
63
 
70
64
  /**
71
65
  * Try to find a bipartite partition of the nodes
72
- *
73
66
  * @param nodes - List of nodes
74
67
  * @param edges - List of edges
75
68
  * @returns Array of two partitions, or null if not bipartite
76
69
  */
77
- export function tryFindBipartitePartition(
78
- nodes: Node[],
79
- edges: Edge[]
80
- ): [Node[], Node[]] | null {
81
- const colorMap: Record<Node, number> = {};
82
- const adjList: Record<Node, Node[]> = {};
83
-
84
- // Create adjacency list
85
- for (const node of nodes) {
86
- adjList[node] = [];
87
- }
88
-
89
- for (const [u, v] of edges) {
90
- adjList[u].push(v);
91
- adjList[v].push(u);
92
- }
93
-
94
- // BFS to color nodes
95
- const queue: Node[] = [nodes[0]];
96
- colorMap[nodes[0]] = 0;
97
-
98
- while (queue.length > 0) {
99
- const node = queue.shift()!;
100
- const nodeColor = colorMap[node];
101
-
102
- for (const neighbor of adjList[node]) {
103
- if (colorMap[neighbor] === undefined) {
104
- colorMap[neighbor] = 1 - nodeColor; // Toggle color (0/1)
105
- queue.push(neighbor);
106
- } else if (colorMap[neighbor] === nodeColor) {
107
- // Conflict: not bipartite
108
- return null;
109
- }
70
+ export function tryFindBipartitePartition(nodes: Node[], edges: Edge[]): [Node[], Node[]] | null {
71
+ const colorMap: Record<Node, number> = {};
72
+ const adjList: Record<Node, Node[]> = {};
73
+
74
+ // Create adjacency list
75
+ for (const node of nodes) {
76
+ adjList[node] = [];
77
+ }
78
+
79
+ for (const [u, v] of edges) {
80
+ adjList[u].push(v);
81
+ adjList[v].push(u);
110
82
  }
111
- }
112
83
 
113
- // Create partitions
114
- const part0: Node[] = [];
115
- const part1: Node[] = [];
84
+ // BFS to color nodes
85
+ const queue: Node[] = [nodes[0]];
86
+ colorMap[nodes[0]] = 0;
87
+
88
+ while (queue.length > 0) {
89
+ const node = queue.shift()!;
90
+ const nodeColor = colorMap[node];
91
+
92
+ for (const neighbor of adjList[node]) {
93
+ if (colorMap[neighbor] === undefined) {
94
+ colorMap[neighbor] = 1 - nodeColor; // Toggle color (0/1)
95
+ queue.push(neighbor);
96
+ } else if (colorMap[neighbor] === nodeColor) {
97
+ // Conflict: not bipartite
98
+ return null;
99
+ }
100
+ }
101
+ }
102
+
103
+ // Create partitions
104
+ const part0: Node[] = [];
105
+ const part1: Node[] = [];
116
106
 
117
- for (const node of nodes) {
118
- if (colorMap[node] === 0) {
119
- part0.push(node);
120
- } else {
121
- part1.push(node);
107
+ for (const node of nodes) {
108
+ if (colorMap[node] === 0) {
109
+ part0.push(node);
110
+ } else {
111
+ part1.push(node);
112
+ }
122
113
  }
123
- }
124
114
 
125
- return [part0, part1];
126
- }
115
+ return [part0, part1];
116
+ }
@@ -2,7 +2,7 @@
2
2
  * Basic graph generation functions
3
3
  */
4
4
 
5
- import { Graph, Node, Edge } from '../types';
5
+ import { Edge,Graph, Node } from "../types";
6
6
 
7
7
  /**
8
8
  * Create a complete graph with n nodes
@@ -10,19 +10,19 @@ import { Graph, Node, Edge } from '../types';
10
10
  * @returns Graph object with all nodes connected to all other nodes
11
11
  */
12
12
  export function completeGraph(n: number): Graph {
13
- const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
14
- const edges: Edge[] = [];
15
-
16
- for (let i = 0; i < n; i++) {
17
- for (let j = i + 1; j < n; j++) {
18
- edges.push([i, j]);
13
+ const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
14
+ const edges: Edge[] = [];
15
+
16
+ for (let i = 0; i < n; i++) {
17
+ for (let j = i + 1; j < n; j++) {
18
+ edges.push([i, j]);
19
+ }
19
20
  }
20
- }
21
-
22
- return {
23
- nodes: () => nodes,
24
- edges: () => edges
25
- };
21
+
22
+ return {
23
+ nodes: () => nodes,
24
+ edges: () => edges,
25
+ };
26
26
  }
27
27
 
28
28
  /**
@@ -31,17 +31,17 @@ export function completeGraph(n: number): Graph {
31
31
  * @returns Graph object with nodes connected in a cycle
32
32
  */
33
33
  export function cycleGraph(n: number): Graph {
34
- const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
35
- const edges: Edge[] = [];
36
-
37
- for (let i = 0; i < n; i++) {
38
- edges.push([i, (i + 1) % n]);
39
- }
40
-
41
- return {
42
- nodes: () => nodes,
43
- edges: () => edges
44
- };
34
+ const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
35
+ const edges: Edge[] = [];
36
+
37
+ for (let i = 0; i < n; i++) {
38
+ edges.push([i, (i + 1) % n]);
39
+ }
40
+
41
+ return {
42
+ nodes: () => nodes,
43
+ edges: () => edges,
44
+ };
45
45
  }
46
46
 
47
47
  /**
@@ -50,18 +50,18 @@ export function cycleGraph(n: number): Graph {
50
50
  * @returns Graph object with star topology
51
51
  */
52
52
  export function starGraph(n: number): Graph {
53
- const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
54
- const edges: Edge[] = [];
55
-
56
- // Connect all nodes to node 0 (center)
57
- for (let i = 1; i < n; i++) {
58
- edges.push([0, i]);
59
- }
60
-
61
- return {
62
- nodes: () => nodes,
63
- edges: () => edges
64
- };
53
+ const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
54
+ const edges: Edge[] = [];
55
+
56
+ // Connect all nodes to node 0 (center)
57
+ for (let i = 1; i < n; i++) {
58
+ edges.push([0, i]);
59
+ }
60
+
61
+ return {
62
+ nodes: () => nodes,
63
+ edges: () => edges,
64
+ };
65
65
  }
66
66
 
67
67
  /**
@@ -70,24 +70,24 @@ export function starGraph(n: number): Graph {
70
70
  * @returns Graph object with wheel topology
71
71
  */
72
72
  export function wheelGraph(n: number): Graph {
73
- const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
74
- const edges: Edge[] = [];
75
-
76
- // Connect all rim nodes to center (node 0)
77
- for (let i = 1; i < n; i++) {
78
- edges.push([0, i]);
79
- }
80
-
81
- // Connect rim nodes in a cycle
82
- for (let i = 1; i < n - 1; i++) {
83
- edges.push([i, i + 1]);
84
- }
85
- if (n > 2) {
86
- edges.push([n - 1, 1]);
87
- }
88
-
89
- return {
90
- nodes: () => nodes,
91
- edges: () => edges
92
- };
93
- }
73
+ const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
74
+ const edges: Edge[] = [];
75
+
76
+ // Connect all rim nodes to center (node 0)
77
+ for (let i = 1; i < n; i++) {
78
+ edges.push([0, i]);
79
+ }
80
+
81
+ // Connect rim nodes in a cycle
82
+ for (let i = 1; i < n - 1; i++) {
83
+ edges.push([i, i + 1]);
84
+ }
85
+ if (n > 2) {
86
+ edges.push([n - 1, 1]);
87
+ }
88
+
89
+ return {
90
+ nodes: () => nodes,
91
+ edges: () => edges,
92
+ };
93
+ }
@@ -2,7 +2,7 @@
2
2
  * Bipartite graph generation function
3
3
  */
4
4
 
5
- import { Graph, Node, Edge } from '../types';
5
+ import { Edge,Graph, Node } from "../types";
6
6
 
7
7
  /**
8
8
  * Create a bipartite graph with two sets of nodes
@@ -12,34 +12,40 @@ import { Graph, Node, Edge } from '../types';
12
12
  * @param seed - Random seed for reproducibility
13
13
  * @returns Graph object with bipartite structure and setA/setB properties
14
14
  */
15
- export function bipartiteGraph(n1: number, n2: number, p: number, seed?: number): Graph & { setA: Node[], setB: Node[] } {
16
- const setA: Node[] = Array.from({ length: n1 }, (_, i) => `A${i}`);
17
- const setB: Node[] = Array.from({ length: n2 }, (_, i) => `B${i}`);
18
- const nodes = [...setA, ...setB];
19
- const edges: Edge[] = [];
20
-
21
- // Simple deterministic pseudo-random if seed provided
22
- let currentSeed = seed;
23
- let random = seed !== undefined
24
- ? () => {
25
- currentSeed = (currentSeed! * 9301 + 49297) % 233280;
26
- return currentSeed / 233280;
27
- }
28
- : Math.random;
29
-
30
- // Only connect nodes between sets
31
- for (const a of setA) {
32
- for (const b of setB) {
33
- if (random() < p) {
34
- edges.push([a, b]);
35
- }
15
+ export function bipartiteGraph(
16
+ n1: number,
17
+ n2: number,
18
+ p: number,
19
+ seed?: number,
20
+ ): Graph & { setA: Node[]; setB: Node[] } {
21
+ const setA: Node[] = Array.from({ length: n1 }, (_, i) => `A${i}`);
22
+ const setB: Node[] = Array.from({ length: n2 }, (_, i) => `B${i}`);
23
+ const nodes = [...setA, ...setB];
24
+ const edges: Edge[] = [];
25
+
26
+ // Simple deterministic pseudo-random if seed provided
27
+ let currentSeed = seed;
28
+ const random =
29
+ seed !== undefined
30
+ ? () => {
31
+ currentSeed = (currentSeed! * 9301 + 49297) % 233280;
32
+ return currentSeed / 233280;
33
+ }
34
+ : Math.random;
35
+
36
+ // Only connect nodes between sets
37
+ for (const a of setA) {
38
+ for (const b of setB) {
39
+ if (random() < p) {
40
+ edges.push([a, b]);
41
+ }
42
+ }
36
43
  }
37
- }
38
-
39
- return {
40
- nodes: () => nodes,
41
- edges: () => edges,
42
- setA,
43
- setB
44
- };
45
- }
44
+
45
+ return {
46
+ nodes: () => nodes,
47
+ edges: () => edges,
48
+ setA,
49
+ setB,
50
+ };
51
+ }
@@ -2,7 +2,7 @@
2
2
  * Grid graph generation function
3
3
  */
4
4
 
5
- import { Graph, Node, Edge } from '../types';
5
+ import { Edge,Graph, Node } from "../types";
6
6
 
7
7
  /**
8
8
  * Create a grid graph with rows x cols nodes
@@ -11,32 +11,32 @@ import { Graph, Node, Edge } from '../types';
11
11
  * @returns Graph object with grid topology
12
12
  */
13
13
  export function gridGraph(rows: number, cols: number): Graph {
14
- const nodes: Node[] = [];
15
- const edges: Edge[] = [];
16
-
17
- // Create nodes
18
- for (let i = 0; i < rows; i++) {
19
- for (let j = 0; j < cols; j++) {
20
- nodes.push(`${i},${j}`);
14
+ const nodes: Node[] = [];
15
+ const edges: Edge[] = [];
16
+
17
+ // Create nodes
18
+ for (let i = 0; i < rows; i++) {
19
+ for (let j = 0; j < cols; j++) {
20
+ nodes.push(`${i},${j}`);
21
+ }
21
22
  }
22
- }
23
-
24
- // Create edges
25
- for (let i = 0; i < rows; i++) {
26
- for (let j = 0; j < cols; j++) {
27
- // Connect to right neighbor
28
- if (j < cols - 1) {
29
- edges.push([`${i},${j}`, `${i},${j + 1}`]);
30
- }
31
- // Connect to bottom neighbor
32
- if (i < rows - 1) {
33
- edges.push([`${i},${j}`, `${i + 1},${j}`]);
34
- }
23
+
24
+ // Create edges
25
+ for (let i = 0; i < rows; i++) {
26
+ for (let j = 0; j < cols; j++) {
27
+ // Connect to right neighbor
28
+ if (j < cols - 1) {
29
+ edges.push([`${i},${j}`, `${i},${j + 1}`]);
30
+ }
31
+ // Connect to bottom neighbor
32
+ if (i < rows - 1) {
33
+ edges.push([`${i},${j}`, `${i + 1},${j}`]);
34
+ }
35
+ }
35
36
  }
36
- }
37
-
38
- return {
39
- nodes: () => nodes,
40
- edges: () => edges
41
- };
42
- }
37
+
38
+ return {
39
+ nodes: () => nodes,
40
+ edges: () => edges,
41
+ };
42
+ }
@@ -2,14 +2,8 @@
2
2
  * Re-export all graph generation functions
3
3
  */
4
4
 
5
- export {
6
- completeGraph,
7
- cycleGraph,
8
- starGraph,
9
- wheelGraph
10
- } from './basic';
11
-
12
- export { gridGraph } from './grid';
13
- export { randomGraph } from './random';
14
- export { bipartiteGraph } from './bipartite';
15
- export { scaleFreeGraph } from './scale-free';
5
+ export { completeGraph, cycleGraph, starGraph, wheelGraph } from "./basic";
6
+ export { bipartiteGraph } from "./bipartite";
7
+ export { gridGraph } from "./grid";
8
+ export { randomGraph } from "./random";
9
+ export { scaleFreeGraph } from "./scale-free";
@@ -2,7 +2,7 @@
2
2
  * Random graph generation function
3
3
  */
4
4
 
5
- import { Graph, Node, Edge } from '../types';
5
+ import { Edge,Graph, Node } from "../types";
6
6
 
7
7
  /**
8
8
  * Create a random graph with n nodes and given edge probability
@@ -12,28 +12,29 @@ import { Graph, Node, Edge } from '../types';
12
12
  * @returns Graph object with random edges
13
13
  */
14
14
  export function randomGraph(n: number, p: number, seed?: number): Graph {
15
- const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
16
- const edges: Edge[] = [];
17
-
18
- // Simple deterministic pseudo-random if seed provided
19
- let currentSeed = seed;
20
- let random = seed !== undefined
21
- ? () => {
22
- currentSeed = (currentSeed! * 9301 + 49297) % 233280;
23
- return currentSeed / 233280;
24
- }
25
- : Math.random;
26
-
27
- for (let i = 0; i < n; i++) {
28
- for (let j = i + 1; j < n; j++) {
29
- if (random() < p) {
30
- edges.push([i, j]);
31
- }
15
+ const nodes: Node[] = Array.from({ length: n }, (_, i) => i);
16
+ const edges: Edge[] = [];
17
+
18
+ // Simple deterministic pseudo-random if seed provided
19
+ let currentSeed = seed;
20
+ const random =
21
+ seed !== undefined
22
+ ? () => {
23
+ currentSeed = (currentSeed! * 9301 + 49297) % 233280;
24
+ return currentSeed / 233280;
25
+ }
26
+ : Math.random;
27
+
28
+ for (let i = 0; i < n; i++) {
29
+ for (let j = i + 1; j < n; j++) {
30
+ if (random() < p) {
31
+ edges.push([i, j]);
32
+ }
33
+ }
32
34
  }
33
- }
34
-
35
- return {
36
- nodes: () => nodes,
37
- edges: () => edges
38
- };
39
- }
35
+
36
+ return {
37
+ nodes: () => nodes,
38
+ edges: () => edges,
39
+ };
40
+ }