@algorithm.ts/mcmf
Version:
The MCMF algorithm implemented in typescript
95 lines (91 loc) • 2.56 kB
TypeScript
import { ICircularQueue } from '@algorithm.ts/queue';
/**
* An arc (or edge) in the residual network.
*/
interface IMcmfEdge {
/**
* The starting node of the arc.
*/
readonly from: number;
/**
* The ending node of the arc.
*/
readonly to: number;
/**
* Capacity of the arc.
*/
readonly cap: number;
/**
* Flow on the arc.
*/
flow: number;
/**
* Price of every unit flow.
*/
cost: number;
}
interface IMcmf {
/**
* Initialize the mcmf algorithm.
* @param source the source node
* @param target the target node
* @param n the number of nodes
*/
init(source: number, target: number, n: number): void;
/**
* Add an edge into the residual network.
* @param from the starting node
* @param to the ending node
* @param cap capacity
* @param cost unit price of flow
*/
addEdge(from: number, to: number, cap: number, cost: number): void;
/**
* Calculate the min cost and max flow of the residual network.
*/
minCostMaxFlow(): {
mincost: number;
maxflow: number;
};
/**
* Calculate the minium cut of the residual network.
*/
mincut(): Array<Readonly<IMcmfEdge>>;
}
interface IMcmfOptions {
/**
* A big number, representing the unreachable cost.
*/
INF?: number;
}
declare class Mcmf implements IMcmf {
protected readonly _INF: number;
protected readonly _inq: boolean[];
protected readonly _dist: number[];
protected readonly _path: number[];
protected readonly _edges: IMcmfEdge[];
protected readonly _G: number[][];
protected readonly _Q: ICircularQueue<number>;
protected _N: number;
protected _source: number;
protected _sink: number;
protected _maxflow: number;
protected _mincost: number;
protected _edgesTot: number;
protected _modifiedTimestamp: number;
protected _resolvedTimestamp: number;
constructor(options?: IMcmfOptions);
init(source: number, sink: number, n: number): void;
addEdge(from: number, to: number, cap: number, cost: number): void;
minCostMaxFlow(): {
mincost: number;
maxflow: number;
};
mincut(): Array<Readonly<IMcmfEdge>>;
/**
* Negative loops should not appear in the residual network,
* so there is no need to check if there are negative loops.
*/
protected _bellmanFord(): boolean;
}
export { type IMcmf, type IMcmfEdge, type IMcmfOptions, Mcmf };