From c2eefd10cd754ef6efb6b87296638e5b6fc509f7 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 15 Mar 2016 16:39:42 +0100 Subject: [PATCH] Comments and draft for graph --- Math/Optimization/Graph/EdgeInterface.php | 81 +++++++ Math/Optimization/Graph/Graph.php | 231 +++++++++++++++++++ Math/Optimization/Graph/GraphAbstract.php | 69 ------ Math/Optimization/Graph/VerticeInterface.php | 62 +++++ 4 files changed, 374 insertions(+), 69 deletions(-) create mode 100644 Math/Optimization/Graph/Graph.php delete mode 100644 Math/Optimization/Graph/GraphAbstract.php diff --git a/Math/Optimization/Graph/EdgeInterface.php b/Math/Optimization/Graph/EdgeInterface.php index e69de29bb..ce29f9c7b 100644 --- a/Math/Optimization/Graph/EdgeInterface.php +++ b/Math/Optimization/Graph/EdgeInterface.php @@ -0,0 +1,81 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Optimization\Graph; + +/** + * Graph class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface EdgeInterface +{ + /** + * Get edge id. + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getId(); + + /** + * Get edge weight. + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getWeight(); + + /** + * Set weight. + * + * @param mixed $weight Weight of edge + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setWeight($weight); + + /** + * Get vertices. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getVertices() : array; + + /** + * Set vertices. + * + * @param VerticeInterface $a Vertice a + * @param VerticeInterface $b Vertice b + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setVertices(VerticeInterface $a, VerticeInterface $b); +} \ No newline at end of file diff --git a/Math/Optimization/Graph/Graph.php b/Math/Optimization/Graph/Graph.php new file mode 100644 index 000000000..2a7412bc0 --- /dev/null +++ b/Math/Optimization/Graph/Graph.php @@ -0,0 +1,231 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Optimization\Graph; + +/** + * Graph class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Graph +{ + /** + * Vertices. + * + * @var VerticeInterface[] + * @since 1.0.0 + */ + private $vertices = []; + + /** + * Edge. + * + * @var EdgeInterface[] + * @since 1.0.0 + */ + private $edges = null; + + /** + * Constructor + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + $this->edges = new MultiMap(KeyType::STRICT, OrderType::LOOSE); + } + + /** + * Add vertice to graph. + * + * @param VerticeInterface $vertice Vertice + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function addVertice(VerticeInterface $vertice) : bool + { + if(!isset($this->vertices[$vertice->getId()])) { + $this->vertices[$vertice->getId()] = $vertice; + + return true; + } + + return false; + } + + /** + * Add edge to graph. + * + * @param EdgeInterface $edge Edge + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function addEdge(EdgeInterface $edge) : bool + { + if(!isset($this->edges[$edge->getId()])) { + $this->edges[$edge->getId()] = $edge; + + return true; + } + + return false; + } + + /** + * Remove vertice from graph. + * + * @param mixed $id Id of vertice to remove + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function removeVertice($id) : bool + { + if(isset($this->vertices[$id])) { + unset($this->vertices[$id]); + + return true; + } + + return false; + } + + /** + * Remove edge by nodes from graph. + * + * @param mixed $a First node of edge + * @param mixed $b Second node of edge + * + * @return EdgeInterface + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function removeEdge($a, $b) : bool + { + return $this->edges->remove([$a, $b]); + } + + /** + * Remove edge from graph. + * + * @param mixed $id Id of edge to remove + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function removeEdgeById($id) : bool + { + if(isset($this->edge[$id])) { + unset($this->edge[$id]); + + return true; + } + + return false; + } + + /** + * Get vertice. + * + * @param mixed $id Id of vertice + * + * @return VerticeInterface + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getVertice($id) : VerticeInterface + { + return $this->vertices[$id] ?? new NullVertice(); + } + + /** + * Get edge by nodes. + * + * Order of nodes is irrelevant + * + * @param mixed $a First node of edge + * @param mixed $b Second node of edge + * + * @return EdgeInterface + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getEdge($a, $b) : EdgeInterface + { + return $this->edges->get([$a, $b]) ?? new NullEdge(); + } + + /** + * Get edge by id. + * + * @param itn $id Edge id + * + * @return EdgeInterface + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getEdgeById(int $id) : EdgeInterface + { + return $this->edges->get([$a, $b]) ?? new NullEdge(); + } + + /** + * Count vertices. + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function countVertices() : int + { + return count($this->vertices); + } + + /** + * Count edges. + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function countEdges() : int + { + return count($this->edges); + } +} \ No newline at end of file diff --git a/Math/Optimization/Graph/GraphAbstract.php b/Math/Optimization/Graph/GraphAbstract.php deleted file mode 100644 index c63f3c78b..000000000 --- a/Math/Optimization/Graph/GraphAbstract.php +++ /dev/null @@ -1,69 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ -namespace phpOMS\Math\Optimization; - -class GraphAbstract { - private $vertices = []; - - private $edges = null; - - public function __construct() - { - $this->edges = new MultiMap(KeyType::STRICT, OrderType::LOOSE); - } - - public function addVertice(VerticeInterface $Vertice) : bool - { - if(!isset($this->vertices[$Vertice->getId()])) { - $this->vertices[$Vertice->getId()] = $Vertice; - - return true; - } - - return false; - } - - public function removeVertice($id) : bool - { - if(isset($this->vertices[$id])) { - unset($this->vertices[$id]); - - return true; - } - - return false; - } - - public function getVertice($id) : VerticeInterface - { - return $this->vertices[$id] ?? new NullVertice(); - } - - public function getEdge($a, $b) : EdgeInterface - { - return $this->edges->get($a, $b) ?? new NullEdge(); - } - - public function countVertices() : int - { - return count($this->vertices); - } - - public function countEdges() : int - { - return count($this->edges); - } -} \ No newline at end of file diff --git a/Math/Optimization/Graph/VerticeInterface.php b/Math/Optimization/Graph/VerticeInterface.php index e69de29bb..4f73a55dd 100644 --- a/Math/Optimization/Graph/VerticeInterface.php +++ b/Math/Optimization/Graph/VerticeInterface.php @@ -0,0 +1,62 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Optimization\Graph; + +/** + * Graph class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface VerticeInterface +{ + /** + * Get vertice id. + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getId(); + + /** + * Get edges. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getEdges() array; + + /** + * Add edge. + * + * @param EdgeInterface $edge Edge to add to vertice + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function addEdge(EdgeInterface $edge) : bool; +}