effect 3.21.0 → 3.22.4

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 (190) hide show
  1. package/dist/cjs/BigDecimal.js +4 -3
  2. package/dist/cjs/BigDecimal.js.map +1 -1
  3. package/dist/cjs/Context.js +1 -1
  4. package/dist/cjs/Cron.js +96 -52
  5. package/dist/cjs/Cron.js.map +1 -1
  6. package/dist/cjs/Data.js.map +1 -1
  7. package/dist/cjs/Duration.js +10 -3
  8. package/dist/cjs/Duration.js.map +1 -1
  9. package/dist/cjs/Effect.js +49 -35
  10. package/dist/cjs/Effect.js.map +1 -1
  11. package/dist/cjs/Graph.js +184 -78
  12. package/dist/cjs/Graph.js.map +1 -1
  13. package/dist/cjs/JSONSchema.js +2 -1
  14. package/dist/cjs/JSONSchema.js.map +1 -1
  15. package/dist/cjs/LayerMap.js +7 -5
  16. package/dist/cjs/LayerMap.js.map +1 -1
  17. package/dist/cjs/Mailbox.js +3 -2
  18. package/dist/cjs/Mailbox.js.map +1 -1
  19. package/dist/cjs/Micro.js +4 -3
  20. package/dist/cjs/Micro.js.map +1 -1
  21. package/dist/cjs/ParseResult.js +18 -0
  22. package/dist/cjs/ParseResult.js.map +1 -1
  23. package/dist/cjs/Schema.js +1 -1
  24. package/dist/cjs/Schema.js.map +1 -1
  25. package/dist/cjs/Stream.js +1 -1
  26. package/dist/cjs/TestClock.js +1 -1
  27. package/dist/cjs/TestClock.js.map +1 -1
  28. package/dist/cjs/internal/cause.js +6 -5
  29. package/dist/cjs/internal/cause.js.map +1 -1
  30. package/dist/cjs/internal/config.js +5 -1
  31. package/dist/cjs/internal/config.js.map +1 -1
  32. package/dist/cjs/internal/configProvider.js +14 -1
  33. package/dist/cjs/internal/configProvider.js.map +1 -1
  34. package/dist/cjs/internal/context.js +10 -9
  35. package/dist/cjs/internal/context.js.map +1 -1
  36. package/dist/cjs/internal/core-effect.js +4 -3
  37. package/dist/cjs/internal/core-effect.js.map +1 -1
  38. package/dist/cjs/internal/effect/circular.js +7 -6
  39. package/dist/cjs/internal/effect/circular.js.map +1 -1
  40. package/dist/cjs/internal/fiberRuntime.js +4 -2
  41. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  42. package/dist/cjs/internal/layer.js +4 -3
  43. package/dist/cjs/internal/layer.js.map +1 -1
  44. package/dist/cjs/internal/logger.js +11 -2
  45. package/dist/cjs/internal/logger.js.map +1 -1
  46. package/dist/cjs/internal/mailbox.js +11 -19
  47. package/dist/cjs/internal/mailbox.js.map +1 -1
  48. package/dist/cjs/internal/opCodes/config.js +3 -1
  49. package/dist/cjs/internal/opCodes/config.js.map +1 -1
  50. package/dist/cjs/internal/reloadable.js +1 -1
  51. package/dist/cjs/internal/reloadable.js.map +1 -1
  52. package/dist/cjs/internal/runtime.js +14 -8
  53. package/dist/cjs/internal/runtime.js.map +1 -1
  54. package/dist/cjs/internal/schedule.js +3 -0
  55. package/dist/cjs/internal/schedule.js.map +1 -1
  56. package/dist/cjs/internal/stackTraceLimit.js +49 -0
  57. package/dist/cjs/internal/stackTraceLimit.js.map +1 -0
  58. package/dist/cjs/internal/stm/tMap.js +2 -2
  59. package/dist/cjs/internal/stm/tMap.js.map +1 -1
  60. package/dist/cjs/internal/stream/emit.js +6 -34
  61. package/dist/cjs/internal/stream/emit.js.map +1 -1
  62. package/dist/cjs/internal/stream.js +26 -16
  63. package/dist/cjs/internal/stream.js.map +1 -1
  64. package/dist/cjs/internal/tracer.js +4 -3
  65. package/dist/cjs/internal/tracer.js.map +1 -1
  66. package/dist/cjs/internal/version.js +1 -1
  67. package/dist/dts/Context.d.ts +1 -1
  68. package/dist/dts/Cron.d.ts +1 -0
  69. package/dist/dts/Cron.d.ts.map +1 -1
  70. package/dist/dts/Data.d.ts +7 -7
  71. package/dist/dts/Data.d.ts.map +1 -1
  72. package/dist/dts/Duration.d.ts.map +1 -1
  73. package/dist/dts/Effect.d.ts +48 -43
  74. package/dist/dts/Effect.d.ts.map +1 -1
  75. package/dist/dts/Graph.d.ts +22 -3
  76. package/dist/dts/Graph.d.ts.map +1 -1
  77. package/dist/dts/LayerMap.d.ts +21 -1
  78. package/dist/dts/LayerMap.d.ts.map +1 -1
  79. package/dist/dts/Mailbox.d.ts.map +1 -1
  80. package/dist/dts/Micro.d.ts.map +1 -1
  81. package/dist/dts/ParseResult.d.ts.map +1 -1
  82. package/dist/dts/Stream.d.ts +1 -1
  83. package/dist/dts/internal/context.d.ts.map +1 -1
  84. package/dist/dts/internal/core-effect.d.ts.map +1 -1
  85. package/dist/dts/internal/layer.d.ts.map +1 -1
  86. package/dist/dts/internal/stackTraceLimit.d.ts +2 -0
  87. package/dist/dts/internal/stackTraceLimit.d.ts.map +1 -0
  88. package/dist/dts/internal/stream.d.ts.map +1 -1
  89. package/dist/esm/BigDecimal.js +4 -3
  90. package/dist/esm/BigDecimal.js.map +1 -1
  91. package/dist/esm/Context.js +1 -1
  92. package/dist/esm/Cron.js +96 -52
  93. package/dist/esm/Cron.js.map +1 -1
  94. package/dist/esm/Data.js.map +1 -1
  95. package/dist/esm/Duration.js +10 -3
  96. package/dist/esm/Duration.js.map +1 -1
  97. package/dist/esm/Effect.js +49 -35
  98. package/dist/esm/Effect.js.map +1 -1
  99. package/dist/esm/Graph.js +177 -73
  100. package/dist/esm/Graph.js.map +1 -1
  101. package/dist/esm/JSONSchema.js +2 -1
  102. package/dist/esm/JSONSchema.js.map +1 -1
  103. package/dist/esm/LayerMap.js +7 -5
  104. package/dist/esm/LayerMap.js.map +1 -1
  105. package/dist/esm/Mailbox.js +3 -2
  106. package/dist/esm/Mailbox.js.map +1 -1
  107. package/dist/esm/Micro.js +4 -3
  108. package/dist/esm/Micro.js.map +1 -1
  109. package/dist/esm/ParseResult.js +18 -0
  110. package/dist/esm/ParseResult.js.map +1 -1
  111. package/dist/esm/Schema.js +1 -1
  112. package/dist/esm/Schema.js.map +1 -1
  113. package/dist/esm/Stream.js +1 -1
  114. package/dist/esm/TestClock.js +1 -1
  115. package/dist/esm/TestClock.js.map +1 -1
  116. package/dist/esm/internal/cause.js +6 -5
  117. package/dist/esm/internal/cause.js.map +1 -1
  118. package/dist/esm/internal/config.js +5 -1
  119. package/dist/esm/internal/config.js.map +1 -1
  120. package/dist/esm/internal/configProvider.js +14 -1
  121. package/dist/esm/internal/configProvider.js.map +1 -1
  122. package/dist/esm/internal/context.js +10 -9
  123. package/dist/esm/internal/context.js.map +1 -1
  124. package/dist/esm/internal/core-effect.js +4 -3
  125. package/dist/esm/internal/core-effect.js.map +1 -1
  126. package/dist/esm/internal/effect/circular.js +7 -6
  127. package/dist/esm/internal/effect/circular.js.map +1 -1
  128. package/dist/esm/internal/fiberRuntime.js +4 -2
  129. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  130. package/dist/esm/internal/layer.js +4 -3
  131. package/dist/esm/internal/layer.js.map +1 -1
  132. package/dist/esm/internal/logger.js +10 -1
  133. package/dist/esm/internal/logger.js.map +1 -1
  134. package/dist/esm/internal/mailbox.js +10 -17
  135. package/dist/esm/internal/mailbox.js.map +1 -1
  136. package/dist/esm/internal/opCodes/config.js +2 -0
  137. package/dist/esm/internal/opCodes/config.js.map +1 -1
  138. package/dist/esm/internal/reloadable.js +1 -1
  139. package/dist/esm/internal/reloadable.js.map +1 -1
  140. package/dist/esm/internal/runtime.js +14 -8
  141. package/dist/esm/internal/runtime.js.map +1 -1
  142. package/dist/esm/internal/schedule.js +3 -0
  143. package/dist/esm/internal/schedule.js.map +1 -1
  144. package/dist/esm/internal/stackTraceLimit.js +40 -0
  145. package/dist/esm/internal/stackTraceLimit.js.map +1 -0
  146. package/dist/esm/internal/stm/tMap.js +2 -2
  147. package/dist/esm/internal/stm/tMap.js.map +1 -1
  148. package/dist/esm/internal/stream/emit.js +6 -34
  149. package/dist/esm/internal/stream/emit.js.map +1 -1
  150. package/dist/esm/internal/stream.js +22 -13
  151. package/dist/esm/internal/stream.js.map +1 -1
  152. package/dist/esm/internal/tracer.js +4 -3
  153. package/dist/esm/internal/tracer.js.map +1 -1
  154. package/dist/esm/internal/version.js +1 -1
  155. package/package.json +1 -1
  156. package/src/BigDecimal.ts +4 -4
  157. package/src/Context.ts +1 -1
  158. package/src/Cron.ts +107 -58
  159. package/src/Data.ts +8 -11
  160. package/src/Duration.ts +12 -3
  161. package/src/Effect.ts +78 -59
  162. package/src/Graph.ts +222 -89
  163. package/src/JSONSchema.ts +4 -1
  164. package/src/LayerMap.ts +31 -6
  165. package/src/Mailbox.ts +3 -2
  166. package/src/Micro.ts +4 -3
  167. package/src/ParseResult.ts +18 -0
  168. package/src/Schema.ts +1 -1
  169. package/src/Stream.ts +1 -1
  170. package/src/TestClock.ts +1 -1
  171. package/src/internal/cause.ts +6 -5
  172. package/src/internal/config.ts +14 -1
  173. package/src/internal/configProvider.ts +24 -1
  174. package/src/internal/context.ts +10 -9
  175. package/src/internal/core-effect.ts +4 -3
  176. package/src/internal/effect/circular.ts +27 -28
  177. package/src/internal/fiberRuntime.ts +14 -13
  178. package/src/internal/layer.ts +4 -3
  179. package/src/internal/logger.ts +9 -1
  180. package/src/internal/mailbox.ts +13 -52
  181. package/src/internal/opCodes/config.ts +6 -0
  182. package/src/internal/reloadable.ts +1 -0
  183. package/src/internal/runtime.ts +14 -8
  184. package/src/internal/schedule.ts +8 -0
  185. package/src/internal/stackTraceLimit.ts +65 -0
  186. package/src/internal/stm/tMap.ts +2 -2
  187. package/src/internal/stream/emit.ts +7 -39
  188. package/src/internal/stream.ts +62 -24
  189. package/src/internal/tracer.ts +4 -3
  190. package/src/internal/version.ts +1 -1
package/dist/esm/Graph.js CHANGED
@@ -632,6 +632,23 @@ export const mapEdges = (mutable, f) => {
632
632
  });
633
633
  }
634
634
  };
635
+ /** @internal */
636
+ const rebuildAdjacency = mutable => {
637
+ mutable.adjacency.clear();
638
+ mutable.reverseAdjacency.clear();
639
+ for (const nodeIndex of mutable.nodes.keys()) {
640
+ mutable.adjacency.set(nodeIndex, []);
641
+ mutable.reverseAdjacency.set(nodeIndex, []);
642
+ }
643
+ for (const [edgeIndex, edgeData] of mutable.edges) {
644
+ mutable.adjacency.get(edgeData.source).push(edgeIndex);
645
+ mutable.reverseAdjacency.get(edgeData.target).push(edgeIndex);
646
+ if (mutable.type === "undirected") {
647
+ mutable.adjacency.get(edgeData.target).push(edgeIndex);
648
+ mutable.reverseAdjacency.get(edgeData.source).push(edgeIndex);
649
+ }
650
+ }
651
+ };
635
652
  /**
636
653
  * Reverses all edge directions in a mutable graph by swapping source and target nodes.
637
654
  *
@@ -656,6 +673,9 @@ export const mapEdges = (mutable, f) => {
656
673
  * @category transformations
657
674
  */
658
675
  export const reverse = mutable => {
676
+ if (mutable.type === "undirected") {
677
+ return;
678
+ }
659
679
  // Reverse all edges by swapping source and target
660
680
  for (const [index, edgeData] of mutable.edges) {
661
681
  mutable.edges.set(index, {
@@ -664,20 +684,7 @@ export const reverse = mutable => {
664
684
  data: edgeData.data
665
685
  });
666
686
  }
667
- // Clear and rebuild adjacency lists with reversed directions
668
- mutable.adjacency.clear();
669
- mutable.reverseAdjacency.clear();
670
- // Rebuild adjacency lists with reversed directions
671
- for (const [edgeIndex, edgeData] of mutable.edges) {
672
- // Add to forward adjacency (source -> target)
673
- const sourceEdges = mutable.adjacency.get(edgeData.source) || [];
674
- sourceEdges.push(edgeIndex);
675
- mutable.adjacency.set(edgeData.source, sourceEdges);
676
- // Add to reverse adjacency (target <- source)
677
- const targetEdges = mutable.reverseAdjacency.get(edgeData.target) || [];
678
- targetEdges.push(edgeIndex);
679
- mutable.reverseAdjacency.set(edgeData.target, targetEdges);
680
- }
687
+ rebuildAdjacency(mutable);
681
688
  // Invalidate cycle flag since edge directions changed
682
689
  mutable.isAcyclic = Option.none();
683
690
  };
@@ -1129,8 +1136,11 @@ export const hasEdge = (graph, source, target) => {
1129
1136
  // Check if any edge in the adjacency list connects to the target
1130
1137
  for (const edgeIndex of adjacencyList) {
1131
1138
  const edge = graph.edges.get(edgeIndex);
1132
- if (edge !== undefined && edge.target === target) {
1133
- return true;
1139
+ if (edge !== undefined) {
1140
+ const neighbor = graph.type === "undirected" && edge.target === source ? edge.source : edge.target;
1141
+ if (neighbor === target) {
1142
+ return true;
1143
+ }
1134
1144
  }
1135
1145
  }
1136
1146
  return false;
@@ -1161,6 +1171,21 @@ export const hasEdge = (graph, source, target) => {
1161
1171
  * @category getters
1162
1172
  */
1163
1173
  export const edgeCount = graph => graph.edges.size;
1174
+ const getDirectedNeighbors = (graph, nodeIndex, direction) => {
1175
+ const adjacencyMap = direction === "incoming" ? graph.reverseAdjacency : graph.adjacency;
1176
+ const adjacencyList = adjacencyMap.get(nodeIndex);
1177
+ if (adjacencyList === undefined) {
1178
+ return [];
1179
+ }
1180
+ const result = [];
1181
+ for (const edgeIndex of adjacencyList) {
1182
+ const edge = graph.edges.get(edgeIndex);
1183
+ if (edge !== undefined) {
1184
+ result.push(direction === "incoming" ? edge.source : edge.target);
1185
+ }
1186
+ }
1187
+ return result;
1188
+ };
1164
1189
  /**
1165
1190
  * Returns the neighboring nodes (targets of outgoing edges) for a given node.
1166
1191
  *
@@ -1195,21 +1220,42 @@ export const neighbors = (graph, nodeIndex) => {
1195
1220
  if (graph.type === "undirected") {
1196
1221
  return getUndirectedNeighbors(graph, nodeIndex);
1197
1222
  }
1198
- const adjacencyList = graph.adjacency.get(nodeIndex);
1199
- if (adjacencyList === undefined) {
1200
- return [];
1223
+ return getDirectedNeighbors(graph, nodeIndex, "outgoing");
1224
+ };
1225
+ /**
1226
+ * Returns the outgoing neighbor node indices for a node in a directed graph.
1227
+ *
1228
+ * Throws a `GraphError` when used with an undirected graph.
1229
+ *
1230
+ * @since 3.22.0
1231
+ * @category queries
1232
+ */
1233
+ export const successors = (graph, nodeIndex) => {
1234
+ if (graph.type === "undirected") {
1235
+ throw new GraphError({
1236
+ message: "Cannot get successors of undirected graph"
1237
+ });
1201
1238
  }
1202
- const result = [];
1203
- for (const edgeIndex of adjacencyList) {
1204
- const edge = graph.edges.get(edgeIndex);
1205
- if (edge !== undefined) {
1206
- result.push(edge.target);
1207
- }
1239
+ return getDirectedNeighbors(graph, nodeIndex, "outgoing");
1240
+ };
1241
+ /**
1242
+ * Returns the incoming neighbor node indices for a node in a directed graph.
1243
+ *
1244
+ * Throws a `GraphError` when used with an undirected graph.
1245
+ *
1246
+ * @since 3.22.0
1247
+ * @category queries
1248
+ */
1249
+ export const predecessors = (graph, nodeIndex) => {
1250
+ if (graph.type === "undirected") {
1251
+ throw new GraphError({
1252
+ message: "Cannot get predecessors of undirected graph"
1253
+ });
1208
1254
  }
1209
- return result;
1255
+ return getDirectedNeighbors(graph, nodeIndex, "incoming");
1210
1256
  };
1211
1257
  /**
1212
- * Get neighbors of a node in a specific direction for bidirectional traversal.
1258
+ * Get directed neighbors of a node in a specific direction.
1213
1259
  *
1214
1260
  * @example
1215
1261
  * ```ts
@@ -1231,25 +1277,17 @@ export const neighbors = (graph, nodeIndex) => {
1231
1277
  * const incoming = Graph.neighborsDirected(graph, nodeB, "incoming")
1232
1278
  * ```
1233
1279
  *
1280
+ * @deprecated Use {@link successors} for outgoing neighbors or {@link predecessors} for incoming neighbors.
1234
1281
  * @since 3.18.0
1235
1282
  * @category queries
1236
1283
  */
1237
1284
  export const neighborsDirected = (graph, nodeIndex, direction) => {
1238
- const adjacencyMap = direction === "incoming" ? graph.reverseAdjacency : graph.adjacency;
1239
- const adjacencyList = adjacencyMap.get(nodeIndex);
1240
- if (adjacencyList === undefined) {
1241
- return [];
1242
- }
1243
- const result = [];
1244
- for (const edgeIndex of adjacencyList) {
1245
- const edge = graph.edges.get(edgeIndex);
1246
- if (edge !== undefined) {
1247
- // For incoming direction, we want the source node instead of target
1248
- const neighborNode = direction === "incoming" ? edge.source : edge.target;
1249
- result.push(neighborNode);
1250
- }
1285
+ if (graph.type === "undirected") {
1286
+ throw new GraphError({
1287
+ message: "Cannot get directed neighbors of undirected graph"
1288
+ });
1251
1289
  }
1252
- return result;
1290
+ return getDirectedNeighbors(graph, nodeIndex, direction);
1253
1291
  };
1254
1292
  /**
1255
1293
  * Exports a graph to GraphViz DOT format for visualization.
@@ -1440,6 +1478,40 @@ export const isAcyclic = graph => {
1440
1478
  if (Option.isSome(graph.isAcyclic)) {
1441
1479
  return graph.isAcyclic.value;
1442
1480
  }
1481
+ if (graph.type === "undirected") {
1482
+ const visited = new Set();
1483
+ for (const startNode of graph.nodes.keys()) {
1484
+ if (visited.has(startNode)) {
1485
+ continue;
1486
+ }
1487
+ visited.add(startNode);
1488
+ const stack = [{
1489
+ node: startNode,
1490
+ parent: null
1491
+ }];
1492
+ while (stack.length > 0) {
1493
+ const {
1494
+ node,
1495
+ parent
1496
+ } = stack.pop();
1497
+ const nodeNeighbors = getUndirectedNeighbors(graph, node);
1498
+ for (const neighbor of nodeNeighbors) {
1499
+ if (!visited.has(neighbor)) {
1500
+ visited.add(neighbor);
1501
+ stack.push({
1502
+ node: neighbor,
1503
+ parent: node
1504
+ });
1505
+ } else if (neighbor !== parent) {
1506
+ graph.isAcyclic = Option.some(false);
1507
+ return false;
1508
+ }
1509
+ }
1510
+ }
1511
+ }
1512
+ graph.isAcyclic = Option.some(true);
1513
+ return true;
1514
+ }
1443
1515
  // Stack-safe DFS cycle detection using iterative approach
1444
1516
  const visited = new Set();
1445
1517
  const recursionStack = new Set();
@@ -1466,7 +1538,7 @@ export const isAcyclic = graph => {
1466
1538
  visited.add(node);
1467
1539
  recursionStack.add(node);
1468
1540
  // Get neighbors for this node
1469
- const nodeNeighbors = Array.from(neighborsDirected(graph, node, "outgoing"));
1541
+ const nodeNeighbors = getDirectedNeighbors(graph, node, "outgoing");
1470
1542
  stack[stack.length - 1] = [node, nodeNeighbors, 0, false];
1471
1543
  continue;
1472
1544
  }
@@ -1591,6 +1663,8 @@ const getUndirectedNeighbors = (graph, nodeIndex) => {
1591
1663
  }
1592
1664
  return Array.from(neighbors);
1593
1665
  };
1666
+ const getTraversalNeighbors = (graph, nodeIndex, direction) => graph.type === "undirected" ? getUndirectedNeighbors(graph, nodeIndex) : getDirectedNeighbors(graph, nodeIndex, direction);
1667
+ const getTraversableNeighbor = (graph, current, edge) => graph.type === "undirected" && edge.target === current ? edge.source : edge.target;
1594
1668
  /**
1595
1669
  * Find connected components in an undirected graph.
1596
1670
  * Each component is represented as an array of node indices.
@@ -1667,6 +1741,11 @@ export const connectedComponents = graph => {
1667
1741
  * @category algorithms
1668
1742
  */
1669
1743
  export const stronglyConnectedComponents = graph => {
1744
+ if (graph.type === "undirected") {
1745
+ throw new GraphError({
1746
+ message: "Cannot find strongly connected components of undirected graph"
1747
+ });
1748
+ }
1670
1749
  const visited = new Set();
1671
1750
  const finishOrder = [];
1672
1751
  for (const startNode of graph.nodes.keys()) {
@@ -1682,7 +1761,7 @@ export const stronglyConnectedComponents = graph => {
1682
1761
  continue;
1683
1762
  }
1684
1763
  visited.add(node);
1685
- const nodeNeighborsList = neighbors(graph, node);
1764
+ const nodeNeighborsList = getDirectedNeighbors(graph, node, "outgoing");
1686
1765
  stack[stack.length - 1] = [node, nodeNeighborsList, 0, false];
1687
1766
  continue;
1688
1767
  }
@@ -1735,6 +1814,19 @@ export const stronglyConnectedComponents = graph => {
1735
1814
  }
1736
1815
  return sccs;
1737
1816
  };
1817
+ const validateNonNegativeEdgeWeights = (graph, cost, algorithm) => {
1818
+ const edgeWeights = new Map();
1819
+ for (const [edgeIndex, edgeData] of graph.edges) {
1820
+ const weight = cost(edgeData.data);
1821
+ if (weight < 0 || Number.isNaN(weight)) {
1822
+ throw new GraphError({
1823
+ message: `${algorithm} requires non-negative edge weights`
1824
+ });
1825
+ }
1826
+ edgeWeights.set(edgeIndex, weight);
1827
+ }
1828
+ return edgeWeights;
1829
+ };
1738
1830
  /**
1739
1831
  * Find the shortest path between two nodes using Dijkstra's algorithm.
1740
1832
  *
@@ -1777,6 +1869,7 @@ export const dijkstra = (graph, config) => {
1777
1869
  if (!graph.nodes.has(target)) {
1778
1870
  throw missingNode(target);
1779
1871
  }
1872
+ const edgeWeights = validateNonNegativeEdgeWeights(graph, cost, "Dijkstra's algorithm");
1780
1873
  // Early return if source equals target
1781
1874
  if (source === target) {
1782
1875
  return Option.some({
@@ -1827,12 +1920,8 @@ export const dijkstra = (graph, config) => {
1827
1920
  for (const edgeIndex of adjacencyList) {
1828
1921
  const edge = graph.edges.get(edgeIndex);
1829
1922
  if (edge !== undefined) {
1830
- const neighbor = edge.target;
1831
- const weight = cost(edge.data);
1832
- // Validate non-negative weights
1833
- if (weight < 0) {
1834
- throw new Error(`Dijkstra's algorithm requires non-negative edge weights, found ${weight}`);
1835
- }
1923
+ const neighbor = getTraversableNeighbor(graph, currentNode, edge);
1924
+ const weight = edgeWeights.get(edgeIndex);
1836
1925
  const newDistance = currentDistance + weight;
1837
1926
  const neighborDistance = distances.get(neighbor);
1838
1927
  // Relaxation step
@@ -1936,6 +2025,14 @@ export const floydWarshall = (graph, cost) => {
1936
2025
  next.get(i).set(j, j);
1937
2026
  edgeMatrix.get(i).set(j, edgeData.data);
1938
2027
  }
2028
+ if (graph.type === "undirected") {
2029
+ const reverseWeight = dist.get(j).get(i);
2030
+ if (weight < reverseWeight) {
2031
+ dist.get(j).set(i, weight);
2032
+ next.get(j).set(i, i);
2033
+ edgeMatrix.get(j).set(i, edgeData.data);
2034
+ }
2035
+ }
1939
2036
  }
1940
2037
  // Floyd-Warshall main loop
1941
2038
  for (const k of allNodes) {
@@ -2044,6 +2141,7 @@ export const astar = (graph, config) => {
2044
2141
  if (!graph.nodes.has(target)) {
2045
2142
  throw missingNode(target);
2046
2143
  }
2144
+ const edgeWeights = validateNonNegativeEdgeWeights(graph, cost, "A* algorithm");
2047
2145
  // Early return if source equals target
2048
2146
  if (source === target) {
2049
2147
  return Option.some({
@@ -2107,12 +2205,8 @@ export const astar = (graph, config) => {
2107
2205
  for (const edgeIndex of adjacencyList) {
2108
2206
  const edge = graph.edges.get(edgeIndex);
2109
2207
  if (edge !== undefined) {
2110
- const neighbor = edge.target;
2111
- const weight = cost(edge.data);
2112
- // Validate non-negative weights
2113
- if (weight < 0) {
2114
- throw new Error(`A* algorithm requires non-negative edge weights, found ${weight}`);
2115
- }
2208
+ const neighbor = getTraversableNeighbor(graph, currentNode, edge);
2209
+ const weight = edgeWeights.get(edgeIndex);
2116
2210
  const tentativeGScore = currentGScore + weight;
2117
2211
  const neighborGScore = gScore.get(neighbor);
2118
2212
  // If this path to neighbor is better than any previous one
@@ -2236,6 +2330,14 @@ export const bellmanFord = (graph, config) => {
2236
2330
  weight,
2237
2331
  edgeData: edgeData.data
2238
2332
  });
2333
+ if (graph.type === "undirected" && edgeData.source !== edgeData.target) {
2334
+ edges.push({
2335
+ source: edgeData.target,
2336
+ target: edgeData.source,
2337
+ weight,
2338
+ edgeData: edgeData.data
2339
+ });
2340
+ }
2239
2341
  }
2240
2342
  // Relax edges up to V-1 times
2241
2343
  const nodeCount = graph.nodes.size;
@@ -2272,14 +2374,8 @@ export const bellmanFord = (graph, config) => {
2272
2374
  if (affectedNodes.has(node)) continue;
2273
2375
  affectedNodes.add(node);
2274
2376
  // Add all nodes reachable from this node
2275
- const adjacencyList = graph.adjacency.get(node);
2276
- if (adjacencyList !== undefined) {
2277
- for (const edgeIndex of adjacencyList) {
2278
- const edge = graph.edges.get(edgeIndex);
2279
- if (edge !== undefined) {
2280
- queue.push(edge.target);
2281
- }
2282
- }
2377
+ for (const neighbor of getTraversalNeighbors(graph, node, "outgoing")) {
2378
+ queue.push(neighbor);
2283
2379
  }
2284
2380
  }
2285
2381
  // If target is affected by negative cycle, return null
@@ -2544,7 +2640,7 @@ export const dfs = (graph, config = {}) => {
2544
2640
  if (nodeDataOption === undefined) {
2545
2641
  continue;
2546
2642
  }
2547
- const neighbors = neighborsDirected(graph, current, direction);
2643
+ const neighbors = getTraversalNeighbors(graph, current, direction);
2548
2644
  for (let i = neighbors.length - 1; i >= 0; i--) {
2549
2645
  const neighbor = neighbors[i];
2550
2646
  if (!discovered.has(neighbor)) {
@@ -2617,7 +2713,7 @@ export const bfs = (graph, config = {}) => {
2617
2713
  const current = queue.shift();
2618
2714
  if (!discovered.has(current)) {
2619
2715
  discovered.add(current);
2620
- const neighbors = neighborsDirected(graph, current, direction);
2716
+ const neighbors = getTraversalNeighbors(graph, current, direction);
2621
2717
  for (const neighbor of neighbors) {
2622
2718
  if (!discovered.has(neighbor)) {
2623
2719
  queue.push(neighbor);
@@ -2690,6 +2786,9 @@ export const bfs = (graph, config = {}) => {
2690
2786
  * @category iterators
2691
2787
  */
2692
2788
  export const topo = (graph, config = {}) => {
2789
+ if (graph.type === "undirected") {
2790
+ throw new Error("Cannot perform topological sort on undirected graph");
2791
+ }
2693
2792
  // Check if graph is acyclic first
2694
2793
  if (!isAcyclic(graph)) {
2695
2794
  throw new Error("Cannot perform topological sort on cyclic graph");
@@ -2705,6 +2804,7 @@ export const topo = (graph, config = {}) => {
2705
2804
  [Symbol.iterator]: () => {
2706
2805
  const inDegree = new Map();
2707
2806
  const remaining = new Set();
2807
+ const initialSet = new Set(initials);
2708
2808
  const queue = [...initials];
2709
2809
  // Initialize in-degree counts
2710
2810
  for (const [nodeIndex] of graph.nodes) {
@@ -2716,12 +2816,16 @@ export const topo = (graph, config = {}) => {
2716
2816
  const currentInDegree = inDegree.get(edgeData.target) || 0;
2717
2817
  inDegree.set(edgeData.target, currentInDegree + 1);
2718
2818
  }
2719
- // Add nodes with zero in-degree to queue if no initials provided
2720
- if (initials.length === 0) {
2721
- for (const [nodeIndex, degree] of inDegree) {
2722
- if (degree === 0) {
2723
- queue.push(nodeIndex);
2724
- }
2819
+ for (const nodeIndex of initials) {
2820
+ if (inDegree.get(nodeIndex) !== 0) {
2821
+ throw new GraphError({
2822
+ message: `Initial node ${nodeIndex} has incoming edges`
2823
+ });
2824
+ }
2825
+ }
2826
+ for (const [nodeIndex, degree] of inDegree) {
2827
+ if (degree === 0 && !initialSet.has(nodeIndex)) {
2828
+ queue.push(nodeIndex);
2725
2829
  }
2726
2830
  }
2727
2831
  const nextMapped = () => {
@@ -2730,7 +2834,7 @@ export const topo = (graph, config = {}) => {
2730
2834
  if (remaining.has(current)) {
2731
2835
  remaining.delete(current);
2732
2836
  // Process outgoing edges, reducing in-degree of targets
2733
- const neighbors = neighborsDirected(graph, current, "outgoing");
2837
+ const neighbors = getDirectedNeighbors(graph, current, "outgoing");
2734
2838
  for (const neighbor of neighbors) {
2735
2839
  if (remaining.has(neighbor)) {
2736
2840
  const currentInDegree = inDegree.get(neighbor) || 0;
@@ -2822,7 +2926,7 @@ export const dfsPostOrder = (graph, config = {}) => {
2822
2926
  }
2823
2927
  if (!current.visitedChildren) {
2824
2928
  current.visitedChildren = true;
2825
- const neighbors = neighborsDirected(graph, current.node, direction);
2929
+ const neighbors = getTraversalNeighbors(graph, current.node, direction);
2826
2930
  for (let i = neighbors.length - 1; i >= 0; i--) {
2827
2931
  const neighbor = neighbors[i];
2828
2932
  if (!discovered.has(neighbor) && !finished.has(neighbor)) {