Structures_Graph
[ class tree: Structures_Graph ] [ index: Structures_Graph ] [ all elements ]

Source for file AcyclicTest.php

Documentation is available at AcyclicTest.php

  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. //
  26. /**
  27.  * This file contains the definition of the Structures_Graph_Manipulator_AcyclicTest graph manipulator.
  28.  * 
  29.  * @see Structures_Graph_Manipulator_AcyclicTest
  30.  * @package Structures_Graph
  31.  */
  32.  
  33. /* dependencies {{{ */
  34. /** */
  35. require_once 'PEAR.php';
  36. /** */
  37. require_once 'Structures/Graph.php';
  38. /** */
  39. require_once 'Structures/Graph/Node.php';
  40. /* }}} */
  41.  
  42. /* class Structures_Graph_Manipulator_AcyclicTest {{{ */
  43. /**
  44.  * The Structures_Graph_Manipulator_AcyclicTest is a graph manipulator
  45.  * which tests whether a graph contains a cycle.
  46.  * 
  47.  * The definition of an acyclic graph used in this manipulator is that of a
  48.  * DAG. The graph must be directed, or else it is considered cyclic, even when
  49.  * there are no arcs.
  50.  *
  51.  * @author        Sérgio Carvalho <sergio.carvalho@portugalmail.com>
  52.  * @copyright    (c) 2004 by Sérgio Carvalho
  53.  * @package Structures_Graph
  54.  */
  55.     /* _nonVisitedInDegree {{{ */
  56.     /**
  57.     *
  58.     * This is a variant of Structures_Graph::inDegree which does
  59.     * not count nodes marked as visited.
  60.     *
  61.     * @access   private
  62.     * @return    integer     Number of non-visited nodes that link to this one
  63.     */
  64.     function _nonVisitedInDegree(&$node{
  65.         $result = 0;
  66.         $graphNodes =$node->_graph->getNodes();
  67.         foreach (array_keys($graphNodesas $key{
  68.             if ((!$graphNodes[$key]->getMetadata('acyclic-test-visited')) && $graphNodes[$key]->connectsTo($node)) $result++;
  69.         }
  70.         return $result;
  71.         
  72.     }
  73.     /* }}} */
  74.  
  75.     /* _isAcyclic {{{ */
  76.     /**
  77.     * @access   private
  78.     */
  79.     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 $keyif (!$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 {{{ */
  118.     /**
  119.     *
  120.     * isAcyclic returns true if a graph contains no cycles, false otherwise.
  121.     *
  122.     * @return    boolean     true iff graph is acyclic
  123.     * @access    public
  124.     */
  125.     function isAcyclic(&$graph{
  126.         // We only test graphs
  127.         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);
  128.         if (!$graph->isDirected()) return false; // Only directed graphs may be acyclic
  129.  
  130.         return Structures_Graph_Manipulator_AcyclicTest::_isAcyclic($graph);
  131.     }
  132.     /* }}} */
  133. }
  134. /* }}} */
  135. ?>

Documentation generated on Thu, 08 May 2008 18:00:04 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.