Skip to content

Commit 2522757

Browse files
committed
Merge branch 'master' into fix-algo-minimumcostflow
Conflicts: lib/Fhaculty/Graph/Algorithm/MinimumCostFlow/SuccessiveShortestPath.php
2 parents 6c8f3b3 + f4b1e96 commit 2522757

27 files changed

+380
-59
lines changed

src/BaseSet.php renamed to src/BaseDual.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Fhaculty\Graph\Algorithm;
44

55
use Fhaculty\Graph\Algorithm\Base;
6-
use Fhaculty\Graph\Set;
6+
use Fhaculty\Graph\Set\DualAggregate;
77
use Fhaculty\Graph\Graph;
88
use Fhaculty\Graph\Walk;
99

@@ -12,12 +12,12 @@
1212
*
1313
* @see Set
1414
*/
15-
abstract class BaseSet extends Base
15+
abstract class BaseDual extends Base
1616
{
1717
/**
1818
* Set to operate on
1919
*
20-
* @var Set
20+
* @var DualAggregate
2121
*/
2222
protected $set;
2323

@@ -26,7 +26,7 @@ abstract class BaseSet extends Base
2626
*
2727
* @param Graph|Walk|Set $graphOrWalk either the Graph or Walk to operate on (or the common base class Set)
2828
*/
29-
public function __construct(Set $graphOrWalk)
29+
public function __construct(DualAggregate $graphOrWalk)
3030
{
3131
$this->set = $graphOrWalk;
3232
}

src/ConnectedComponents.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,20 @@ private function createSearch(Vertex $vertex)
6565
* connected here.
6666
*
6767
* @return boolean
68-
* @uses AlgorithmSearchBreadthFirst::getNumberOfVertices()
6968
* @see self::getNumberOfComponents()
7069
*/
7170
public function isSingle()
7271
{
7372
try {
74-
$vertex = $this->graph->getVertexFirst();
73+
$vertex = $this->graph->getVertices()->getVertexFirst();
7574
}
7675
catch (UnderflowException $e) {
7776
// no first vertex => empty graph => has zero components
7877
return false;
7978
}
8079
$alg = $this->createSearch($vertex);
8180

82-
return ($this->graph->getNumberOfVertices() === $alg->getNumberOfVertices());
81+
return (count($this->graph->getVertices()) === count($alg->getVertices()));
8382
}
8483

8584
/**

src/Degree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Degree extends BaseGraph
3131
public function getDegree()
3232
{
3333
// get initial degree of any start vertex to compare others to
34-
$degree = $this->getDegreeVertex($this->graph->getVertexFirst());
34+
$degree = $this->getDegreeVertex($this->graph->getVertices()->getVertexFirst());
3535

3636
foreach ($this->graph->getVertices() as $vertex) {
3737
/** @var $vertex Vertex */

src/Directed.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44

55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
7+
use Fhaculty\Graph\Edge\Undirected as EdgeUndirected;
78

89
/**
910
* Basic algorithms for working with the undirected or directed Graphs (digraphs) / Walks.
1011
*
1112
* @link http://en.wikipedia.org/wiki/Glossary_of_graph_theory#Direction
1213
* @link http://en.wikipedia.org/wiki/Digraph_%28mathematics%29
1314
*/
14-
class Directed extends BaseSet
15+
class Directed extends BaseDual
1516
{
1617
/**
17-
* checks whether the graph has any directed edges (aka digraph)
18+
* checks whether the graph has any directed edges
19+
*
20+
* This method is intentionally not named "isDirected()" (aka digraph),
21+
* because that might be misleading in regards to empty and/or mixed graphs.
1822
*
1923
* @return boolean
2024
*/
21-
public function isDirected()
25+
public function hasDirected()
2226
{
2327
foreach ($this->set->getEdges() as $edge) {
2428
if ($edge instanceof EdgeDirected) {
@@ -28,4 +32,35 @@ public function isDirected()
2832

2933
return false;
3034
}
35+
36+
/**
37+
* checks whether the graph has any undirected edges
38+
*
39+
* This method is intentionally not named "isUndirected()",
40+
* because that might be misleading in regards to empty and/or mixed graphs.
41+
*
42+
* @return boolean
43+
*/
44+
public function hasUndirected()
45+
{
46+
foreach ($this->set->getEdges() as $edge) {
47+
if ($edge instanceof EdgeUndirected) {
48+
return true;
49+
}
50+
}
51+
52+
return false;
53+
}
54+
55+
/**
56+
* checks whether this is a mixed graph (contains both directed and undirected edges)
57+
*
58+
* @return boolean
59+
* @uses self::hasDirected()
60+
* @uses self::hasUndirected()
61+
*/
62+
public function isMixed()
63+
{
64+
return ($this->hasDirected() && $this->hasUndirected());
65+
}
3166
}

src/Flow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @link http://en.wikipedia.org/wiki/Flow_network
1919
* @see Algorithm\Balance
2020
*/
21-
class Flow extends BaseSet
21+
class Flow extends BaseDual
2222
{
2323
/**
2424
* check if this graph has any flow set (any edge has a non-NULL flow)

src/Loop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Fhaculty\Graph\Algorithm;
44

5-
use Fhaculty\Graph\Algorithm\BaseSet;
5+
use Fhaculty\Graph\Algorithm\BaseDual;
66
use Fhaculty\Graph\Edge\Base as Edge;
77
use Fhaculty\Graph\Vertex;
88

@@ -14,7 +14,7 @@
1414
*
1515
* @link http://en.wikipedia.org/wiki/Loop_%28graph_theory%29
1616
*/
17-
class Loop extends BaseSet
17+
class Loop extends BaseDual
1818
{
1919
/**
2020
* checks whether this graph has any loops (edges from vertex to itself)

src/MaximumMatching/Flow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Flow extends Base
1818
public function getEdges()
1919
{
2020
$alg = new Directed($this->graph);
21-
if ($alg->isDirected()) {
21+
if ($alg->hasDirected()) {
2222
throw new UnexpectedValueException('Input graph contains directed edges');
2323
}
2424

src/MinimumSpanningTree/Kruskal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function getEdges()
125125

126126
// definition of spanning tree: number of edges = number of vertices - 1
127127
// above algorithm does not check isolated edges or may otherwise return multiple connected components => force check
128-
if (count($returnEdges) !== ($this->graph->getNumberOfVertices() - 1)) {
128+
if (count($returnEdges) !== (count($this->graph->getVertices()) - 1)) {
129129
throw new UnexpectedValueException('Graph is not connected');
130130
}
131131

src/MinimumSpanningTree/Prim.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getEdges()
3535
$returnEdges = array();
3636

3737
// iterate n-1 times (per definition, resulting MST MUST have n-1 edges)
38-
for ($i = 0, $n = $this->startVertex->getGraph()->getNumberOfVertices() - 1; $i < $n; ++$i) {
38+
for ($i = 0, $n = count($this->startVertex->getGraph()->getVertices()) - 1; $i < $n; ++$i) {
3939
$markInserted[$vertexCurrent->getId()] = true;
4040

4141
// get unvisited vertex of the edge and add edges from new vertex

src/Property/GraphProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function isNull()
4646
*/
4747
public function isTrivial()
4848
{
49-
return ($this->graph->getEdges()->isEmpty() && $this->graph->getNumberOfVertices() === 1);
49+
return ($this->graph->getEdges()->isEmpty() && count($this->graph->getVertices()) === 1);
5050
}
5151
}

0 commit comments

Comments
 (0)