|
| _dfs ($fromNode, $toNode, $graph, &$visited=[], $stack=[]) |
|
| _assertNode ($node, $mustExist) |
|
Graph data structure
Definition at line 11 of file Graph.php.
◆ __construct()
__construct |
( |
array |
$nodes, |
|
|
array |
$relations |
|
) |
| |
Validate consistency of the declared structure and assign it to the object state
- Parameters
-
array | $nodes | plain array with node identifiers |
array | $relations | array of 2-item plain arrays, which represent relations of nodes "from" "to" |
Definition at line 47 of file Graph.php.
49 foreach ($nodes as $node) {
51 $this->_nodes[$node] = $node;
53 foreach ($relations as $pair) {
54 list($fromNode, $toNode) = $pair;
_assertNode($node, $mustExist)
addRelation($fromNode, $toNode)
◆ _assertNode()
_assertNode |
( |
|
$node, |
|
|
|
$mustExist |
|
) |
| |
|
protected |
Verify existence or non-existence of a node
- Parameters
-
string | int | $node | |
bool | $mustExist | |
- Returns
- void
- Exceptions
-
Definition at line 224 of file Graph.php.
226 if (isset($this->_nodes[$node])) {
228 throw new \InvalidArgumentException(
"Graph node '{$node}' already exists'.");
232 throw new \InvalidArgumentException(
"Graph node '{$node}' does not exist.");
◆ _dfs()
_dfs |
( |
|
$fromNode, |
|
|
|
$toNode, |
|
|
|
$graph, |
|
|
& |
$visited = [] , |
|
|
|
$stack = [] |
|
) |
| |
|
protected |
Recursive sub-routine of dfs()
- Parameters
-
string | int | $fromNode | |
string | int | $toNode | |
array | $graph | |
array | &$visited | |
array | $stack | |
- Returns
- array http://en.wikipedia.org/wiki/Depth-first_search
Definition at line 195 of file Graph.php.
197 $stack[] = $fromNode;
198 $visited[$fromNode] = $fromNode;
199 if (isset($graph[$fromNode][$toNode])) {
203 if (isset($graph[$fromNode])) {
204 foreach ($graph[$fromNode] as $node) {
205 if (!isset($visited[$node])) {
206 $result = $this->
_dfs($node, $toNode, $graph, $visited, $stack);
_dfs($fromNode, $toNode, $graph, &$visited=[], $stack=[])
◆ addRelation()
addRelation |
( |
|
$fromNode, |
|
|
|
$toNode |
|
) |
| |
Set a relation between nodes
- Parameters
-
string | int | $fromNode | |
string | int | $toNode | |
- Returns
- $this
- Exceptions
-
Definition at line 67 of file Graph.php.
69 if ($fromNode == $toNode) {
70 throw new \InvalidArgumentException(
"Graph node '{$fromNode}' is linked to itself.");
74 $this->_from[$fromNode][$toNode] = $toNode;
75 $this->_to[$toNode][$fromNode] = $fromNode;
_assertNode($node, $mustExist)
◆ dfs()
dfs |
( |
|
$fromNode, |
|
|
|
$toNode, |
|
|
|
$mode = self::DIRECTIONAL |
|
) |
| |
"Depth-first search" of a path between nodes
Returns path as array of nodes or empty array if path does not exist. Only first found path is returned. It will be not necessary the shortest or optimal in any way.
- Parameters
-
string | int | $fromNode | |
string | int | $toNode | |
int | $mode | |
- Returns
- array
Definition at line 177 of file Graph.php.
_dfs($fromNode, $toNode, $graph, &$visited=[], $stack=[])
_assertNode($node, $mustExist)
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
getRelations($mode=self::DIRECTIONAL)
◆ findCycle()
findCycle |
( |
|
$node = null , |
|
|
|
$firstOnly = true |
|
) |
| |
Find a cycle in the graph
Returns first/all found cycle Optionally may specify a node to return a cycle if it is in any
- Parameters
-
string | int | $node | |
boolean | $firstOnly | found only first cycle |
- Returns
- array
Definition at line 116 of file Graph.php.
118 $nodes =
null === $node ? $this->_nodes : [$node];
120 foreach ($nodes as $node) {
dfs($fromNode, $toNode, $mode=self::DIRECTIONAL)
◆ findPathsToReachableNodes()
findPathsToReachableNodes |
( |
|
$rootNode, |
|
|
|
$mode = self::DIRECTIONAL |
|
) |
| |
Find paths to reachable nodes from root node
Returns array of paths, key is destination node and value is path (an array) from rootNode to destination node Eg. dest => [root, A, dest] means root -> A -> dest
- Parameters
-
string | int | $rootNode | |
int | $mode | |
- Returns
- array
Definition at line 143 of file Graph.php.
148 $visited = [$rootNode => $rootNode];
149 $paths[$rootNode] = [$rootNode];
151 $node = array_shift(
$queue);
152 if (!empty($graph[$node])) {
153 foreach ($graph[$node] as $child) {
154 if (!isset($visited[$child])) {
156 $paths[$child][] = $child;
157 $visited[$child] = $child;
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
getRelations($mode=self::DIRECTIONAL)
◆ getRelations()
getRelations |
( |
|
$mode = self::DIRECTIONAL | ) |
|
Export relations between nodes. Can return inverse relations
- Parameters
-
- Returns
- array
- Exceptions
-
Definition at line 86 of file Graph.php.
95 foreach ($this->_to as $idTo => $relations) {
96 foreach ($relations as $idFrom) {
97 $graph[$idTo][$idFrom] = $idFrom;
102 throw new \InvalidArgumentException(
"Unknown search mode: '{$mode}'");
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
◆ $_from
◆ $_nodes
◆ $_to
◆ DIRECTIONAL
◆ INVERSE
◆ NON_DIRECTIONAL
const NON_DIRECTIONAL = 3 |
The documentation for this class was generated from the following file: