( graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">, config: MaximumFlowConfig<E> )
| 5385 | |
| 5386 | /** @internal */ |
| 5387 | const solveMaximumFlow = <N, E>( |
| 5388 | graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">, |
| 5389 | config: MaximumFlowConfig<E> |
| 5390 | ): FlowSolution => { |
| 5391 | if ((graph as Graph<N, E, Kind> | MutableGraph<N, E, Kind>).type === "undirected") { |
| 5392 | throw new GraphError({ message: "Cannot compute flow of undirected graph" }) |
| 5393 | } |
| 5394 | const cache = csr.get(graph) |
| 5395 | const source = csr.getNodeIndex(cache, config.source) |
| 5396 | if (source === undefined) { |
| 5397 | throw missingNode(config.source) |
| 5398 | } |
| 5399 | const target = csr.getNodeIndex(cache, config.target) |
| 5400 | if (target === undefined) { |
| 5401 | throw missingNode(config.target) |
| 5402 | } |
| 5403 | if (source === target) { |
| 5404 | throw new GraphError({ message: "Flow source and target must be different nodes" }) |
| 5405 | } |
| 5406 | |
| 5407 | const edges = csr.getEdges(cache) as Array<Edge<E>> |
| 5408 | const edgeIds = csr.getEdgeIds(cache) |
| 5409 | const endpoints = csr.getEdgeEndpoints(cache) |
| 5410 | const capacities = new Float64Array(edges.length) |
| 5411 | const arcs: Array<ResidualArc> = [] |
| 5412 | const adjacency: Array<Array<number>> = Array.from({ length: cache.nodeIds.length }, () => []) |
| 5413 | const forwardArc = new Int32Array(edges.length) |
| 5414 | forwardArc.fill(-1) |
| 5415 | |
| 5416 | withMutationGuard(graph, () => { |
| 5417 | for (let edge = 0; edge < edges.length; edge++) { |
| 5418 | const capacity = config.capacity(edges[edge].data) |
| 5419 | if (!Number.isFinite(capacity) || capacity < 0) { |
| 5420 | throw new GraphError({ message: `Edge ${edgeIds[edge]} capacity must be a finite non-negative number` }) |
| 5421 | } |
| 5422 | capacities[edge] = capacity |
| 5423 | const from = endpoints.sources[edge] |
| 5424 | const to = endpoints.targets[edge] |
| 5425 | if (from === to) { |
| 5426 | continue |
| 5427 | } |
| 5428 | const index = arcs.length |
| 5429 | forwardArc[edge] = index |
| 5430 | adjacency[from].push(index) |
| 5431 | arcs.push({ from, to, capacity, edge, flow: 0 }) |
| 5432 | adjacency[to].push(index + 1) |
| 5433 | arcs.push({ from: to, to: from, capacity: 0, edge: -1, flow: 0 }) |
| 5434 | } |
| 5435 | }) |
| 5436 | |
| 5437 | const parentArc = new Int32Array(cache.nodeIds.length) |
| 5438 | const visited = new Uint8Array(cache.nodeIds.length) |
| 5439 | const queue = new Uint32Array(cache.nodeIds.length) |
| 5440 | let value = 0 |
| 5441 | while (true) { |
| 5442 | parentArc.fill(-1) |
| 5443 | visited.fill(0) |
| 5444 | let head = 0 |
no test coverage detected