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. |
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. |
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. |
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 |
22 // +-----------------------------------------------------------------------------+
23 // | Author: Sérgio Carvalho <sergio.carvalho@portugalmail.com> |
24 // +-----------------------------------------------------------------------------+
27 * This file contains the definition of the Structures_Graph_Manipulator_TopologicalSorter class.
29 * @see Structures_Graph_Manipulator_TopologicalSorter
30 * @package Structures_Graph
33 /* dependencies {{{ */
35 require_once 'PEAR.php';
37 require_once 'Structures/Graph.php';
39 require_once 'Structures/Graph/Node.php';
41 require_once 'Structures/Graph/Manipulator/AcyclicTest.php';
44 /* class Structures_Graph_Manipulator_TopologicalSorter {{{ */
46 * The Structures_Graph_Manipulator_TopologicalSorter is a manipulator
47 * which is able to return the set of nodes in a graph, sorted by topological
50 * A graph may only be sorted topologically iff it's a DAG. You can test it
51 * with the Structures_Graph_Manipulator_AcyclicTest.
53 * @author Sérgio Carvalho <sergio.carvalho@portugalmail.com>
54 * @copyright (c) 2004 by Sérgio Carvalho
55 * @see Structures_Graph_Manipulator_AcyclicTest
56 * @package Structures_Graph
58 class Structures_Graph_Manipulator_TopologicalSorter
{
59 /* _nonVisitedInDegree {{{ */
62 * This is a variant of Structures_Graph::inDegree which does
63 * not count nodes marked as visited.
66 * @return integer Number of non-visited nodes that link to this one
68 function _nonVisitedInDegree(&$node) {
70 $graphNodes =& $node->_graph
->getNodes();
71 foreach (array_keys($graphNodes) as $key) {
72 if ((!$graphNodes[$key]->getMetadata('topological-sort-visited')) && $graphNodes[$key]->connectsTo($node)) $result++
;
83 function _sort(&$graph) {
84 // Mark every node as not visited
85 $nodes =& $graph->getNodes();
86 $nodeKeys = array_keys($nodes);
87 $refGenerator = array();
88 foreach($nodeKeys as $key) {
89 $refGenerator[] = false;
90 $nodes[$key]->setMetadata('topological-sort-visited', $refGenerator[sizeof($refGenerator) - 1]);
93 // Iteratively peel off leaf nodes
94 $topologicalLevel = 0;
96 // Find out which nodes are leafs (excluding visited nodes)
98 foreach($nodeKeys as $key) {
99 if ((!$nodes[$key]->getMetadata('topological-sort-visited')) && Structures_Graph_Manipulator_TopologicalSorter
::_nonVisitedInDegree($nodes[$key]) == 0) {
100 $leafNodes[] =& $nodes[$key];
103 // Mark leafs as visited
104 $refGenerator[] = $topologicalLevel;
105 for ($i=sizeof($leafNodes) - 1; $i>=0; $i--) {
106 $visited =& $leafNodes[$i]->getMetadata('topological-sort-visited');
108 $leafNodes[$i]->setMetadata('topological-sort-visited', $visited);
109 $leafNodes[$i]->setMetadata('topological-sort-level', $refGenerator[sizeof($refGenerator) - 1]);
112 } while (sizeof($leafNodes) > 0);
114 // Cleanup visited marks
115 foreach($nodeKeys as $key) $nodes[$key]->unsetMetadata('topological-sort-visited');
122 * sort returns the graph's nodes, sorted by topological order.
124 * The result is an array with
125 * as many entries as topological levels. Each entry in this array is an array of nodes within
126 * the given topological level.
128 * @return array The graph's nodes, sorted by topological order.
131 function sort(&$graph) {
132 // We only sort graphs
133 if (!is_a($graph, 'Structures_Graph')) return Pear
::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC
);
134 if (!Structures_Graph_Manipulator_AcyclicTest
::isAcyclic($graph)) return Pear
::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an graph that has cycles', STRUCTURES_GRAPH_ERROR_GENERIC
);
136 Structures_Graph_Manipulator_TopologicalSorter
::_sort($graph);
139 // Fill out result array
140 $nodes =& $graph->getNodes();
141 $nodeKeys = array_keys($nodes);
142 foreach($nodeKeys as $key) {
143 if (!array_key_exists($nodes[$key]->getMetadata('topological-sort-level'), $result)) $result[$nodes[$key]->getMetadata('topological-sort-level')] = array();
144 $result[$nodes[$key]->getMetadata('topological-sort-level')][] =& $nodes[$key];
145 $nodes[$key]->unsetMetadata('topological-sort-level');