Bearsampp 2026.9.23
Loading...
Searching...
No Matches
Structures_Graph_Manipulator_AcyclicTest Class Reference

Static Public Member Functions

static isAcyclic (&$graph)

Static Protected Member Functions

static _isAcyclic (&$graph)
static _nonVisitedInDegree (&$node)

Detailed Description

Definition at line 55 of file AcyclicTest.php.

Member Function Documentation

◆ _isAcyclic()

_isAcyclic ( & $graph)
staticprotected

Check if the graph is acyclic

Definition at line 79 of file AcyclicTest.php.

79 {
80 // Mark every node as not visited
81 $nodes =& $graph->getNodes();
82 $nodeKeys = array_keys($nodes);
83 $refGenerator = array();
84 foreach($nodeKeys as $key) {
85 $refGenerator[] = false;
86 $nodes[$key]->setMetadata('acyclic-test-visited', $refGenerator[sizeof($refGenerator) - 1]);
87 }
88
89 // Iteratively peel off leaf nodes
90 do {
91 // Find out which nodes are leafs (excluding visited nodes)
92 $leafNodes = array();
93 foreach($nodeKeys as $key) {
94 if ((!$nodes[$key]->getMetadata('acyclic-test-visited')) && Structures_Graph_Manipulator_AcyclicTest::_nonVisitedInDegree($nodes[$key]) == 0) {
95 $leafNodes[] =& $nodes[$key];
96 }
97 }
98 // Mark leafs as visited
99 for ($i=sizeof($leafNodes) - 1; $i>=0; $i--) {
100 $visited =& $leafNodes[$i]->getMetadata('acyclic-test-visited');
101 $visited = true;
102 $leafNodes[$i]->setMetadata('acyclic-test-visited', $visited);
103 }
104 } while (sizeof($leafNodes) > 0);
105
106 // If graph is a DAG, there should be no non-visited nodes. Let's try to prove otherwise
107 $result = true;
108 foreach($nodeKeys as $key) if (!$nodes[$key]->getMetadata('acyclic-test-visited')) $result = false;
109
110 // Cleanup visited marks
111 foreach($nodeKeys as $key) $nodes[$key]->unsetMetadata('acyclic-test-visited');
112
113 return $result;
114 }
$result

References $result, and _nonVisitedInDegree().

Referenced by isAcyclic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _nonVisitedInDegree()

_nonVisitedInDegree ( & $node)
staticprotected

This is a variant of Structures_Graph::inDegree which does not count nodes marked as visited.

Returns
integer Number of non-visited nodes that link to this one

Definition at line 64 of file AcyclicTest.php.

64 {
65 $result = 0;
66 $graphNodes =& $node->_graph->getNodes();
67 foreach (array_keys($graphNodes) as $key) {
68 if ((!$graphNodes[$key]->getMetadata('acyclic-test-visited')) && $graphNodes[$key]->connectsTo($node)) $result++;
69 }
70 return $result;
71
72 }

References $result.

Referenced by _isAcyclic().

Here is the caller graph for this function:

◆ isAcyclic()

isAcyclic ( & $graph)
static

isAcyclic returns true if a graph contains no cycles, false otherwise.

Returns
boolean true iff graph is acyclic

Definition at line 124 of file AcyclicTest.php.

124 {
125 // We only test graphs
126 if (!is_a($graph, 'Structures_Graph')) return Pear::raiseError('Structures_Graph_Manipulator_AcyclicTest::isAcyclic received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC);
127 if (!$graph->isDirected()) return false; // Only directed graphs may be acyclic
128
130 }
const STRUCTURES_GRAPH_ERROR_GENERIC
Definition Graph.php:37

References _isAcyclic(), and STRUCTURES_GRAPH_ERROR_GENERIC.

Referenced by Structures_Graph_Manipulator_TopologicalSorter\sort(), AcyclicTestTest\testIsAcyclicFalse(), and AcyclicTestTest\testIsAcyclicTrue().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file:
  • E:/Bearsampp-development/sandbox/core/libs/php/pear/pear/Structures/Graph/Manipulator/AcyclicTest.php