( mutable: MutableGraph<N, E, T>, source: NodeIndex, target: NodeIndex, data: E )
| 2279 | * @since 3.18.0 |
| 2280 | */ |
| 2281 | export const addEdge = <N, E, T extends Kind = "directed">( |
| 2282 | mutable: MutableGraph<N, E, T>, |
| 2283 | source: NodeIndex, |
| 2284 | target: NodeIndex, |
| 2285 | data: E |
| 2286 | ): EdgeIndex => { |
| 2287 | assertMutable(mutable) |
| 2288 | const impl = graphImpl(mutable) |
| 2289 | |
| 2290 | // Validate that both nodes exist |
| 2291 | if (!impl.nodes.has(source)) { |
| 2292 | throw missingNode(source) |
| 2293 | } |
| 2294 | if (!impl.nodes.has(target)) { |
| 2295 | throw missingNode(target) |
| 2296 | } |
| 2297 | |
| 2298 | const edgeIndex = impl.nextEdgeIndex |
| 2299 | |
| 2300 | // Create edge data |
| 2301 | const edgeData = new Edge({ source, target, data }) |
| 2302 | impl.edges.set(edgeIndex, edgeData) |
| 2303 | |
| 2304 | // Update adjacency lists |
| 2305 | const sourceAdjacency = impl.adjacency.get(source) |
| 2306 | if (sourceAdjacency !== undefined) { |
| 2307 | sourceAdjacency.push(edgeIndex) |
| 2308 | } |
| 2309 | |
| 2310 | const targetReverseAdjacency = impl.reverseAdjacency.get(target) |
| 2311 | if (targetReverseAdjacency !== undefined) { |
| 2312 | targetReverseAdjacency.push(edgeIndex) |
| 2313 | } |
| 2314 | |
| 2315 | // For undirected graphs, add reverse connections |
| 2316 | if (impl.type === "undirected") { |
| 2317 | const targetAdjacency = impl.adjacency.get(target) |
| 2318 | if (targetAdjacency !== undefined) { |
| 2319 | targetAdjacency.push(edgeIndex) |
| 2320 | } |
| 2321 | |
| 2322 | const sourceReverseAdjacency = impl.reverseAdjacency.get(source) |
| 2323 | if (sourceReverseAdjacency !== undefined) { |
| 2324 | sourceReverseAdjacency.push(edgeIndex) |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | // Update allocators |
| 2329 | impl.nextEdgeIndex = impl.nextEdgeIndex + 1 |
| 2330 | |
| 2331 | // Only invalidate cycle flag if the graph was acyclic |
| 2332 | // Adding edges cannot remove cycles from cyclic graphs |
| 2333 | invalidateCycleFlagOnAddition(impl) |
| 2334 | |
| 2335 | return edgeIndex |
| 2336 | } |
| 2337 | |
| 2338 | /** |
no test coverage detected