Skip to content

Commit

Permalink
Merge pull request #81 from clue/fix-mbf
Browse files Browse the repository at this point in the history
Fix Moore-Bellman-Ford for unweighted edges
  • Loading branch information
clue committed Dec 29, 2013
2 parents fe95b81 + c235c39 commit aadcd1e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ you spot any mistakes.
* Fix: Stricter checks for invalid cycles, such as one with an invalid
predecessor-map or no edges at all ([#87](https://github.com/clue/graph/issues/87)

* Fix: The `Algorithm\ShortestPath\MooreBellmanFord` now also works for unweighted
edges. This also fixes an issue where `Algorithm\DetectNegativeCycle` didn't work
for unweighted edges. ([#81](https://github.com/clue/graph/issues/81)

* Fix: Throwing an `UnexpectedValueException` if writing GraphViz Dot script
to a temporary file fails and remove its debugging output
([#77](https://github.com/clue/graph/issues/77) and [#78](https://github.com/clue/graph/issues/78) @Metabor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function bigStep(Edges $edges, array &$totalCostOfCheapestPathTo, array
// New possible costs of this path
$newCost = $totalCostOfCheapestPathTo[$fromVertex->getId()] + $edge->getWeight();
if (is_infinite($newCost)) {
$newCost = $edge->getWeight();
$newCost = $edge->getWeight() + 0;
}

// No path has been found yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ public function testSeparateGraphsAreNotReachable()
$alg->getEdgesTo($vg2);
}

public function testGraphUnweighted()
{
// 1 -> 2
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$e1 = $v1->createEdgeTo($v2);

$alg = $this->createAlg($v1);

$expectedWeight = $this->getExpectedWeight(array($e1));
$this->assertEquals($expectedWeight, $alg->getDistance($v2));
$this->assertEquals(array(2 => $expectedWeight), $alg->getDistanceMap());
$this->assertEquals(array($e1), $alg->getEdges()->getVector());
$this->assertEquals(array($e1), $alg->getEdgesTo($v2)->getVector());
$this->assertEquals(array(2), $alg->getVertices()->getIds());
}

public function testGraphTwoComponents()
{
// 1 -[10]-> 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,28 @@ public function testLoopNegativeWeightIsCycle()

$cycle = $alg->getCycleNegative();
}

public function testNegativeComponentHasCycle()
{
// 1 -[1]-> 2 3 --[-1]--> 4
// ^ |
// \---[-2]----/
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v3 = $graph->createVertex(3);
$v4 = $graph->createVertex(4);
$e1 = $v1->createEdgeTo($v2)->setWeight(1);
$e2 = $v3->createEdgeTo($v4)->setWeight(-1);
$e3 = $v4->createEdgeTo($v3)->setWeight(-2);

// second component has a cycle
$alg = $this->createAlg($v3);
$cycle = $alg->getCycleNegative();

// first component does not have a cycle
$alg = $this->createAlg($v1);
$this->setExpectedException('UnderflowException');
$alg->getCycleNegative();
}
}

0 comments on commit aadcd1e

Please sign in to comment.