Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Graph Class Reference

Public Member Functions

 __construct (array $nodes, array $relations)
 
 addRelation ($fromNode, $toNode)
 
 getRelations ($mode=self::DIRECTIONAL)
 
 findCycle ($node=null, $firstOnly=true)
 
 findPathsToReachableNodes ($rootNode, $mode=self::DIRECTIONAL)
 
 dfs ($fromNode, $toNode, $mode=self::DIRECTIONAL)
 

Data Fields

const DIRECTIONAL = 1
 
const INVERSE = 2
 
const NON_DIRECTIONAL = 3
 

Protected Member Functions

 _dfs ($fromNode, $toNode, $graph, &$visited=[], $stack=[])
 
 _assertNode ($node, $mustExist)
 

Protected Attributes

 $_nodes = []
 
 $_from = []
 
 $_to = []
 

Detailed Description

Graph data structure

Definition at line 11 of file Graph.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( array  $nodes,
array  $relations 
)

Validate consistency of the declared structure and assign it to the object state

Parameters
array$nodesplain array with node identifiers
array$relationsarray of 2-item plain arrays, which represent relations of nodes "from" "to"

Definition at line 47 of file Graph.php.

48  {
49  foreach ($nodes as $node) {
50  $this->_assertNode($node, false);
51  $this->_nodes[$node] = $node;
52  }
53  foreach ($relations as $pair) {
54  list($fromNode, $toNode) = $pair;
55  $this->addRelation($fromNode, $toNode);
56  }
57  }
_assertNode($node, $mustExist)
Definition: Graph.php:224
addRelation($fromNode, $toNode)
Definition: Graph.php:67

Member Function Documentation

◆ _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.

225  {
226  if (isset($this->_nodes[$node])) {
227  if (!$mustExist) {
228  throw new \InvalidArgumentException("Graph node '{$node}' already exists'.");
229  }
230  } else {
231  if ($mustExist) {
232  throw new \InvalidArgumentException("Graph node '{$node}' does not exist.");
233  }
234  }
235  }

◆ _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.

196  {
197  $stack[] = $fromNode;
198  $visited[$fromNode] = $fromNode;
199  if (isset($graph[$fromNode][$toNode])) {
200  $stack[] = $toNode;
201  return $stack;
202  }
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);
207  if ($result) {
208  return $result;
209  }
210  }
211  }
212  }
213  return [];
214  }
_dfs($fromNode, $toNode, $graph, &$visited=[], $stack=[])
Definition: Graph.php:195

◆ 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.

68  {
69  if ($fromNode == $toNode) {
70  throw new \InvalidArgumentException("Graph node '{$fromNode}' is linked to itself.");
71  }
72  $this->_assertNode($fromNode, true);
73  $this->_assertNode($toNode, true);
74  $this->_from[$fromNode][$toNode] = $toNode;
75  $this->_to[$toNode][$fromNode] = $fromNode;
76  return $this;
77  }
_assertNode($node, $mustExist)
Definition: Graph.php:224

◆ 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.

178  {
179  $this->_assertNode($fromNode, true);
180  $this->_assertNode($toNode, true);
181  return $this->_dfs($fromNode, $toNode, $this->getRelations($mode));
182  }
_dfs($fromNode, $toNode, $graph, &$visited=[], $stack=[])
Definition: Graph.php:195
_assertNode($node, $mustExist)
Definition: Graph.php:224
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
getRelations($mode=self::DIRECTIONAL)
Definition: Graph.php:86

◆ 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$firstOnlyfound only first cycle
Returns
array

Definition at line 116 of file Graph.php.

117  {
118  $nodes = null === $node ? $this->_nodes : [$node];
119  $results = [];
120  foreach ($nodes as $node) {
121  $result = $this->dfs($node, $node);
122  if ($result) {
123  if ($firstOnly) {
124  return $result;
125  } else {
126  $results[] = $result;
127  }
128  }
129  }
130  return $results;
131  }
dfs($fromNode, $toNode, $mode=self::DIRECTIONAL)
Definition: Graph.php:177
$results
Definition: popup.phtml:13

◆ 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.

144  {
145  $graph = $this->getRelations($mode);
146  $paths = [];
147  $queue = [$rootNode];
148  $visited = [$rootNode => $rootNode];
149  $paths[$rootNode] = [$rootNode];
150  while (!empty($queue)) {
151  $node = array_shift($queue);
152  if (!empty($graph[$node])) {
153  foreach ($graph[$node] as $child) {
154  if (!isset($visited[$child])) {
155  $paths[$child] = $paths[$node];
156  $paths[$child][] = $child;
157  $visited[$child] = $child;
158  $queue[] = $child;
159  }
160  }
161  }
162  }
163  return $paths;
164  }
$queue
Definition: queue.php:21
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
getRelations($mode=self::DIRECTIONAL)
Definition: Graph.php:86
$paths
Definition: _bootstrap.php:83

◆ getRelations()

getRelations (   $mode = self::DIRECTIONAL)

Export relations between nodes. Can return inverse relations

Parameters
int$mode
Returns
array
Exceptions

Definition at line 86 of file Graph.php.

87  {
88  switch ($mode) {
89  case self::DIRECTIONAL:
90  return $this->_from;
91  case self::INVERSE:
92  return $this->_to;
94  $graph = $this->_from;
95  foreach ($this->_to as $idTo => $relations) {
96  foreach ($relations as $idFrom) {
97  $graph[$idTo][$idFrom] = $idFrom;
98  }
99  }
100  return $graph;
101  default:
102  throw new \InvalidArgumentException("Unknown search mode: '{$mode}'");
103  }
104  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

Field Documentation

◆ $_from

$_from = []
protected

Definition at line 32 of file Graph.php.

◆ $_nodes

$_nodes = []
protected

#- #-

Definition at line 25 of file Graph.php.

◆ $_to

$_to = []
protected

Definition at line 39 of file Graph.php.

◆ DIRECTIONAL

const DIRECTIONAL = 1

#+ Search modes

Definition at line 16 of file Graph.php.

◆ INVERSE

const INVERSE = 2

Definition at line 18 of file Graph.php.

◆ NON_DIRECTIONAL

const NON_DIRECTIONAL = 3

Definition at line 20 of file Graph.php.


The documentation for this class was generated from the following file: