|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Fhaculty\Graph\Algorithm\Tree; |
| 4 | + |
| 5 | +use Fhaculty\Graph\Algorithm\Tree\Base as Tree; |
| 6 | +use Fhaculty\Graph\Exception\UnderflowException; |
| 7 | +use Fhaculty\Graph\Exception\UnexpectedValueException; |
| 8 | +use Fhaculty\Graph\Algorithm\Search\Base as Search; |
| 9 | +use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst; |
| 10 | +use Fhaculty\Graph\Vertex; |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @link http://en.wikipedia.org/wiki/Spaghetti_stack |
| 15 | + * @see OutTree |
| 16 | + */ |
| 17 | +abstract class BaseDirected extends Tree |
| 18 | +{ |
| 19 | + const DIRECTION_CHILDREN = -1; |
| 20 | + |
| 21 | + /** |
| 22 | + * get root vertex for this in-tree |
| 23 | + * |
| 24 | + * @return Vertex |
| 25 | + * @throws UnderflowException if given graph is empty |
| 26 | + * @throws UnexpectedValueException if given graph is not a tree |
| 27 | + */ |
| 28 | + public function getVertexRoot() |
| 29 | + { |
| 30 | + $root = $this->getVertexPossibleRoot(); |
| 31 | + |
| 32 | + $search = new StrictDepthFirst($root); |
| 33 | + $search->setDirection(static::DIRECTION_CHILDREN); |
| 34 | + |
| 35 | + $num = $search->getNumberOfVertices(); |
| 36 | + |
| 37 | + if ($num !== $this->graph->getNumberOfVertices()) { |
| 38 | + throw new UnexpectedValueException(); |
| 39 | + } |
| 40 | + |
| 41 | + return $root; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * checks if this is a tree |
| 46 | + * |
| 47 | + * @return boolean |
| 48 | + * @uses Graph::isEmpty() |
| 49 | + * @uses self::getVertexRoot() to actually check tree |
| 50 | + */ |
| 51 | + public function isTree() |
| 52 | + { |
| 53 | + if (!$this->graph->isEmpty()) { |
| 54 | + try { |
| 55 | + $this->getVertexRoot(); |
| 56 | + } |
| 57 | + catch (UnexpectedValueException $e) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * get parent vertex for given $vertex |
| 66 | + * |
| 67 | + * @param Vertex $vertex |
| 68 | + * @throws UnderflowException if vertex has no parent (is a root vertex) |
| 69 | + * @throws UnexpectedValueException if vertex has more than one possible parent (check isTree()!) |
| 70 | + * @return Vertex |
| 71 | + */ |
| 72 | + public function getVertexParent(Vertex $vertex) |
| 73 | + { |
| 74 | + $parents = $this->getVerticesParent($vertex); |
| 75 | + if (count($parents) !== 1) { |
| 76 | + if (!$parents) { |
| 77 | + throw new UnderflowException('No parents for given vertex found'); |
| 78 | + } else { |
| 79 | + throw new UnexpectedValueException('More than one parent'); |
| 80 | + } |
| 81 | + } |
| 82 | + return current($parents); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * get array of child vertices for given $vertex |
| 87 | + * |
| 88 | + * @param Vertex $vertex |
| 89 | + * @return Vertex[] |
| 90 | + */ |
| 91 | + abstract public function getVerticesChildren(Vertex $vertex); |
| 92 | + |
| 93 | + abstract protected function getVerticesParent(Vertex $vertex); |
| 94 | + |
| 95 | + protected function isVertexPossibleRoot(Vertex $vertex) |
| 96 | + { |
| 97 | + return (count($this->getVerticesParent($vertex)) === 0); |
| 98 | + } |
| 99 | + |
| 100 | + protected function getVertexPossibleRoot() |
| 101 | + { |
| 102 | + foreach ($this->graph->getVertices() as $vertex) { |
| 103 | + if ($this->isVertexPossibleRoot($vertex)) { |
| 104 | + return $vertex; |
| 105 | + } |
| 106 | + } |
| 107 | + throw new UnderflowException('No possible root found. Either empty graph or no Vertex with proper degree found.'); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * checks if the given $vertex is a leaf (outermost vertex with no children) |
| 112 | + * |
| 113 | + * @param Vertex $vertex |
| 114 | + * @return boolean |
| 115 | + * @uses self::getVerticesChildren() |
| 116 | + */ |
| 117 | + public function isVertexLeaf(Vertex $vertex) |
| 118 | + { |
| 119 | + return (count($this->getVerticesChildren($vertex)) === 0); |
| 120 | + } |
| 121 | + |
| 122 | + public function isVertexInternal(Vertex $vertex) |
| 123 | + { |
| 124 | + return ($this->getVerticesParent($vertex) && $this->getVerticesChildren($vertex)); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * get degree of tree (maximum number of children) |
| 129 | + * |
| 130 | + * @return int |
| 131 | + * @throws UnderflowException for empty graphs |
| 132 | + * @uses Graph::getVertices() |
| 133 | + * @uses self::getVerticesChildren() |
| 134 | + */ |
| 135 | + public function getDegree() |
| 136 | + { |
| 137 | + $max = null; |
| 138 | + foreach ($this->graph->getVertices() as $vertex) { |
| 139 | + $num = count($this->getVerticesChildren($vertex)); |
| 140 | + if ($max === null || $num > $max) { |
| 141 | + $max = $num; |
| 142 | + } |
| 143 | + } |
| 144 | + if ($max === null) { |
| 145 | + throw new UnderflowException('No vertices found'); |
| 146 | + } |
| 147 | + return $max; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * get depth of given $vertex (number of edges between root vertex) |
| 152 | + * |
| 153 | + * root has depth zero |
| 154 | + * |
| 155 | + * @param Vertex $vertex |
| 156 | + * @return int |
| 157 | + * @throws UnderflowException for empty graphs |
| 158 | + * @throws UnexpectedValueException if there's no path to root node (check isTree()!) |
| 159 | + * @uses self::getVertexRoot() |
| 160 | + * @uses self::getVertexParent() for each step |
| 161 | + */ |
| 162 | + public function getDepthVertex(Vertex $vertex) |
| 163 | + { |
| 164 | + $root = $this->getVertexRoot(); |
| 165 | + |
| 166 | + $depth = 0; |
| 167 | + while ($vertex !== $root) { |
| 168 | + $vertex = $this->getVertexParent($vertex); |
| 169 | + ++$depth; |
| 170 | + } |
| 171 | + return $depth; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * get height of this tree (longest downward path to a leaf) |
| 176 | + * |
| 177 | + * a single vertex graph has height zero |
| 178 | + * |
| 179 | + * @return int |
| 180 | + * @throws UnderflowException for empty graph |
| 181 | + * @uses self::getVertexRoot() |
| 182 | + * @uses self::getHeightVertex() |
| 183 | + */ |
| 184 | + public function getHeight() |
| 185 | + { |
| 186 | + return $this->getHeightVertex($this->getVertexRoot()); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * get height of given vertex (longest downward path to a leaf) |
| 191 | + * |
| 192 | + * leafs has height zero |
| 193 | + * |
| 194 | + * @param Vertex $vertex |
| 195 | + * @return int |
| 196 | + * @uses self::getVerticesChildren() to get children of given vertex |
| 197 | + * @uses self::getHeightVertex() to recurse into sub-children |
| 198 | + */ |
| 199 | + public function getHeightVertex(Vertex $vertex) |
| 200 | + { |
| 201 | + $max = 0; |
| 202 | + foreach ($this->getVerticesChildren($vertex) as $vertex) { |
| 203 | + $height = $this->getHeightVertex($vertex) + 1; |
| 204 | + if ($height > $max) { |
| 205 | + $max = $height; |
| 206 | + } |
| 207 | + } |
| 208 | + return $max; |
| 209 | + } |
| 210 | +} |
0 commit comments