/** * check if balance is okay and throw exception otherwise * * @throws UnexpectedValueException * @return AlgorithmMCF $this (chainable) */ protected function checkBalance() { $alg = new AlgorithmFlow($this->graph); $balance = $alg->getBalance(); $tolerance = 1.0E-6; if ($balance >= $tolerance || $balance <= -$tolerance) { throw new UnexpectedValueException('The given graph is not balanced value is: ' . $balance); } return $this; }
public function testGraphBalance() { // source(+100) -> sink(-10) $graph = new Graph(); $graph->createVertex('source')->setBalance(100); $graph->createVertex('sink')->setBalance(-10); $alg = new AlgorithmFlow($graph); $this->assertEquals(90, $alg->getBalance()); $this->assertFalse($alg->isBalancedFlow()); }