Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Graph balance helpers to new Algorithm\Balance #30

Merged
merged 5 commits into from
Jun 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ you spot any mistakes.
* BC break: `Graph::createVertices()` now returns an array of vertices instead of the chainable `Graph` (#19)
* BC break: Move `Set::getWeight()`, `Set::getWeightFlow()`, `Set::getWeightMin()` and `Set::isWeighted()` to new `Algorithm\Weight` (#33)
* BC break: Move `Graph::getDegree()`, `Graph::getDegreeMin()`, `Graph::getDegreeMax()`, `Graph::isRegular()` and `Graph::isBalanced()` to new `Algorithm\Degree` (#29)
* BC break: Move `Graph::getBalance()` and `Graph::isBalancedFlow()` to new `Algorithm\Balance` (#30)
* Feature: `Graph::createVertices()` now also accepts an array of vertex IDs (#19)
* Fix: Various issues with `Vertex`/`Edge` layout attributes (#32)

Expand Down
76 changes: 76 additions & 0 deletions lib/Fhaculty/Graph/Algorithm/Balance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Fhaculty\Graph\Algorithm;

use Fhaculty\Graph\Algorithm\Base;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Vertex;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some class documentation would help but checking out http://en.wikipedia.org/wiki/Directed_graph#Indegree_and_outdegree which has the wording 'balanced digraph' is not helping that much.

Is there a way to help future coders to fix bugs by some form of documentation? Otherwise this is as good to go for me :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a hard time finding some links on balanced flow networks...

The balanced digraph you're talking about refers to its indegee/outdegree and should be located in the Algorithm\Degree (see PR #29).

/**
* Basic algorithms for working with the balance of flow graphs
*
* A flow network (also known as a transportation network) is a directed graph
* where each edge has a capacity and each edge receives a flow.
*
* @link http://en.wikipedia.org/wiki/Flow_network
* @see Algorithm\Degree if you're looking for balanced degrees instead of balanced flows
*/
class Balance extends Base
{
/**
* Graph to operate on
*
* @var Graph
*/
private $graph;

/**
* instanciate new Balance algorithm
*
* @param Graph $graph graph to operate on
*/
public function __construct(Graph $graph)
{
$this->graph = $graph;
}

public function getBalance()
{
$balance = 0;
// Sum for all vertices of value
foreach ($this->graph->getVertices() as $vertex) {
$balance += $vertex->getBalance();
}

return $balance;
}

/**
* check if the current flow is balanced (aka "balanced flow" or "b-flow")
*
* a flow is considered balanced if each edge's current flow does not exceed its
* maximum capacity (which is always guaranteed due to the implementation
* of Edge::setFlow()) and each vertices' flow (i.e. outflow-inflow) equals
* its balance.
*
* checking whether the FLOW is balanced is not to be confused with checking
* whether the GRAPH is balanced (see Graph::isBalanced() instead)
*
* @return boolean
* @see Algorithm\Degree::isBalanced() if you merely want to check indegree=outdegree
* @uses Vertex::getFlow()
* @uses Vertex::getBalance()
*/
public function isBalancedFlow()
{
// no need to check for each edge: flow <= capacity (setters already check that)
// check for each vertex: outflow-inflow = balance
foreach ($this->graph->getVertices() as $vertex) {
if ($vertex->getFlow() !== $vertex->getBalance()) {
return false;
}
}

return true;
}
}
40 changes: 0 additions & 40 deletions lib/Fhaculty/Graph/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,46 +441,6 @@ public function isComplete()
return true;
}

public function getBalance()
{
$balance = 0;
// Sum for all vertices of value
foreach ($this->getVertices() as $vertex) {
$balance += $vertex->getBalance();
}

return $balance;
}

/**
* check if the current flow is balanced (aka "balanced flow" or "b-flow")
*
* a flow is considered balanced if each edge's current flow does not exceed its
* maximum capacity (which is always guaranteed due to the implementation
* of Edge::setFlow()) and each vertices' flow (i.e. outflow-inflow) equals
* its balance.
*
* checking whether the FLOW is balanced is not to be confused with checking
* whether the GRAPH is balanced (see Graph::isBalanced() instead)
*
* @return boolean
* @see Graph::isBalanced() if you merely want to check indegree=outdegree
* @uses Vertex::getFlow()
* @uses Vertex::getBalance()
*/
public function isBalancedFlow()
{
// no need to check for each edge: flow <= capacity (setters already check that)
// check for each vertex: outflow-inflow = balance
foreach ($this->vertices as $vertex) {
if ($vertex->getFlow() === $vertex->getBalance()) {
return false;
}
}

return true;
}

/**
* adds a new Edge to the Graph (MUST NOT be called manually!)
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Fhaculty/Graph/Algorithm/BalanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Fhaculty\Graph\Algorithm\Balance as BalanceAlgorithm;
use Fhaculty\Graph\Graph;

class BalanceTest extends TestCase
{
public function testGraphEmpty()
{
$graph = new Graph();

$alg = new BalanceAlgorithm($graph);

$this->assertEquals(0, $alg->getBalance());
$this->assertTrue($alg->isBalancedFlow());
}

public function testGraphSimple()
{
// source(+100) -> sink(-10)
$graph = new Graph();
$graph->createVertex('source')->setBalance(100);
$graph->createVertex('sink')->setBalance(-10);

$alg = new BalanceAlgorithm($graph);

$this->assertEquals(90, $alg->getBalance());
$this->assertFalse($alg->isBalancedFlow());
}
}
5 changes: 0 additions & 5 deletions tests/Fhaculty/Graph/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ public function testGraphCloneEdgeless()
$this->assertGraphEquals($graphExpected, $graphEdgeless);
}

public function testBalance()
{
$this->assertEquals(0, $this->graph->getBalance());
}

/**
* check to make sure we can actually create vertices with automatic IDs
*/
Expand Down