-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3eaf977
Move Graph balanace helpers to new Algorithm\Balance
clue 8bb89bb
Add tests with 100% code coverage for new Algorithm\Balance
clue 91d526d
Fix inverted logic
clue 2036b83
Document new `Algorithm\Balance`
clue 578909f
Improve documentation
clue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 theAlgorithm\Degree
(see PR #29).