Bearsampp 2026.9.23
Loading...
Searching...
No Matches
AcyclicTest.php
Go to the documentation of this file.
1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3// +-----------------------------------------------------------------------------+
4// | Copyright (c) 2003 Sérgio Gonçalves Carvalho |
5// +-----------------------------------------------------------------------------+
6// | This file is part of Structures_Graph. |
7// | |
8// | Structures_Graph is free software; you can redistribute it and/or modify |
9// | it under the terms of the GNU Lesser General Public License as published by |
10// | the Free Software Foundation; either version 2.1 of the License, or |
11// | (at your option) any later version. |
12// | |
13// | Structures_Graph is distributed in the hope that it will be useful, |
14// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16// | GNU Lesser General Public License for more details. |
17// | |
18// | You should have received a copy of the GNU Lesser General Public License |
19// | along with Structures_Graph; if not, write to the Free Software |
20// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
21// | 02111-1307 USA |
22// +-----------------------------------------------------------------------------+
23// | Author: Sérgio Carvalho <sergio.carvalho@portugalmail.com> |
24// +-----------------------------------------------------------------------------+
25//
32
33/* dependencies {{{ */
35require_once 'PEAR.php';
37require_once 'Structures/Graph.php';
39require_once 'Structures/Graph/Node.php';
40/* }}} */
41
42/* class Structures_Graph_Manipulator_AcyclicTest {{{ */
56 /* _nonVisitedInDegree {{{ */
64 protected static function _nonVisitedInDegree(&$node) {
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 }
73 /* }}} */
74
75 /* _isAcyclic {{{ */
79 protected static function _isAcyclic(&$graph) {
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 }
115 /* }}} */
116
117 /* isAcyclic {{{ */
124 public static function isAcyclic(&$graph) {
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 }
131 /* }}} */
132}
133/* }}} */
134?>
const STRUCTURES_GRAPH_ERROR_GENERIC
Definition Graph.php:37
$result