Permalink
Browse files

Initial import into PEAR CVS

git-svn-id: http://svn.php.net/repository/pear/packages/Structures_Graph/trunk@158015 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
Sergio Carvalho
Sergio Carvalho committed May 5, 2004
1 parent b3687cd commit 91edafc07090c47aabc9dfa3a503ff9bae6cf43f
Showing with 4,282 additions and 0 deletions.
  1. +504 −0 LICENSE
  2. +156 −0 Structures/Graph.php
  3. +137 −0 Structures/Graph/Manipulator/AcyclicTest.php
  4. +157 −0 Structures/Graph/Manipulator/TopologicalSorter.php
  5. +339 −0 Structures/Graph/Node.php
  6. +8 −0 docs/generate.sh
  7. +243 −0 docs/html/Structures_Graph/Structures_Graph.html
  8. +105 −0 docs/html/Structures_Graph/Structures_Graph_Manipulator_AcyclicTest.html
  9. +107 −0 docs/html/Structures_Graph/Structures_Graph_Manipulator_TopologicalSorter.html
  10. +549 −0 docs/html/Structures_Graph/Structures_Graph_Node.html
  11. +119 −0 docs/html/Structures_Graph/_Structures_Graph_Manipulator_AcyclicTest_php.html
  12. +133 −0 docs/html/Structures_Graph/_Structures_Graph_Manipulator_TopologicalSorter_php.html
  13. +105 −0 docs/html/Structures_Graph/_Structures_Graph_Node_php.html
  14. +136 −0 docs/html/Structures_Graph/_Structures_Graph_php.html
  15. +75 −0 docs/html/Structures_Graph/tutorial_Structures_Graph.pkg.html
  16. +36 −0 docs/html/classtrees_Structures_Graph.html
  17. +339 −0 docs/html/elementindex.html
  18. +336 −0 docs/html/elementindex_Structures_Graph.html
  19. +16 −0 docs/html/errors.html
  20. +24 −0 docs/html/index.html
  21. +53 −0 docs/html/li_Structures_Graph.html
  22. +32 −0 docs/html/media/banner.css
  23. +134 −0 docs/html/media/stylesheet.css
  24. +27 −0 docs/html/packages.html
  25. +21 −0 docs/html/todolist.html
  26. +98 −0 docs/tutorials/Structures_Graph/Structures_Graph.pkg
  27. +16 −0 genpackage.xml.pl
  28. +51 −0 package.sh
  29. +5 −0 publish.sh
  30. 0 tests/README
  31. +39 −0 tests/all-tests.php
  32. +182 −0 tests/testCase/BasicGraph.php
View
504 LICENSE

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,156 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
+// +-----------------------------------------------------------------------------+
+// | Copyright (c) 2003 Sérgio Gonçalves Carvalho |
+// +-----------------------------------------------------------------------------+
+// | This file is part of Structures_Graph. |
+// | |
+// | Structures_Graph is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU Lesser General Public License as published by |
+// | the Free Software Foundation; either version 2.1 of the License, or |
+// | (at your option) any later version. |
+// | |
+// | Structures_Graph is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+// | GNU Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public License |
+// | along with Structures_Graph; if not, write to the Free Software |
+// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
+// | 02111-1307 USA |
+// +-----------------------------------------------------------------------------+
+// | Author: Sérgio Carvalho <sergio.carvalho@portugalmail.com> |
+// +-----------------------------------------------------------------------------+
+//
+/**
+ * The Graph.php file contains the definition of the Structures_Graph class
+ *
+ * @see Structures_Graph
+ * @package Structures_Graph
+ */
+
+/* dependencies {{{ */
+/** PEAR base classes */
+require_once 'PEAR.php';
+/** Graph Node */
+require_once 'Structures/Graph/Node.php';
+/* }}} */
+
+define('STRUCTURES_GRAPH_ERROR_GENERIC', 100);
+
+/* class Structures_Graph {{{ */
+/**
+ * The Structures_Graph class represents a graph data structure.
+ *
+ * A Graph is a data structure composed by a set of nodes, connected by arcs.
+ * Graphs may either be directed or undirected. In a directed graph, arcs are
+ * directional, and can be traveled only one way. In an undirected graph, arcs
+ * are bidirectional, and can be traveled both ways.
+ *
+ * @author Sérgio Carvalho <sergio.carvalho@portugalmail.com>
+ * @copyright (c) 2004 by Sérgio Carvalho
+ * @package Structures_Graph
+ */
+/* }}} */
+class Structures_Graph {
+ /* fields {{{ */
+ /**
+ * @access private
+ */
+ var $_nodes = array();
+ /**
+ * @access private
+ */
+ var $_directed = false;
+ /* }}} */
+
+ /* Constructor {{{ */
+ /**
+ *
+ * Constructor
+ *
+ * @param boolean Set to true if the graph is directed. Set to false if it is not directed. (Optional, defaults to true)
+ * @access public
+ */
+ function Structures_Graph($directed = true) {
+ $this->_directed = $directed;
+ }
+ /* }}} */
+
+ /* isDirected {{{ */
+ /**
+ *
+ * Return true if a graph is directed
+ *
+ * @return boolean true if the graph is directed
+ * @access public
+ */
+ function isDirected() {
+ return (boolean) $this->_directed;
+ }
+ /* }}} */
+
+ /* addNode {{{ */
+ /**
+ *
+ * Add a Node to the Graph
+ *
+ * @param Structures_Graph_Node The node to be added.
+ * @access public
+ */
+ function addNode(&$newNode) {
+ // We only add nodes
+ if (!is_a($newNode, 'Structures_Graph_Node'))
+ Pear::raiseError('Structures_Graph::addNode received an object that is not a Structures_Graph_Node', STRUCTURES_GRAPH_ERROR_GENERIC);
+ // Graphs are node *sets*, so duplicates are forbidden. We allow nodes that are exactly equal, but disallow equal references.
+ foreach($this->_nodes as $key => $node) {
+ /*
+ ZE1 equality operators choke on the recursive cycle introduced by the _graph field in the Node object.
+ So, we'll check references the hard way
+ */
+ $savedData = $this->_nodes[$key];
+ $referenceIsEqualFlag = false;
+ $this->_nodes[$key] = true;
+ if ($node === true) {
+ $this->_nodes[$key] = false;
+ if ($node === false)
+ $referenceIsEqualFlag = true;
+ }
+ $this->_nodes[$key] = $savedData;
+ if ($referenceIsEqualFlag)
+ Pear::raiseError('Structures_Graph::addNode received an object that is a duplicate for this dataset', STRUCTURES_GRAPH_ERROR_GENERIC);
+ }
+ $this->_nodes[] =& $newNode;
+ $newNode->setGraph($this);
+ }
+ /* }}} */
+
+ /* removeNode (unimplemented) {{{ */
+ /**
+ *
+ * Remove a Node from the Graph
+ *
+ * @todo This is unimplemented
+ * @param Structures_Graph_Node The node to be removed from the graph
+ * @access public
+ */
+ function removeNode(&$node) {
+ }
+ /* }}} */
+
+ /* getNodes {{{ */
+ /**
+ *
+ * Return the node set, in no particular order. For ordered node sets, use a Graph Manipulator insted.
+ *
+ * @access public
+ * @see Structures_Graph_Manipulator_TopologicalSorter
+ * @return array The set of nodes in this graph
+ */
+ function &getNodes() {
+ return $this->_nodes;
+ }
+ /* }}} */
+}
+?>
@@ -0,0 +1,137 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
+// +-----------------------------------------------------------------------------+
+// | Copyright (c) 2003 Sérgio Gonçalves Carvalho |
+// +-----------------------------------------------------------------------------+
+// | This file is part of Structures_Graph. |
+// | |
+// | Structures_Graph is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU Lesser General Public License as published by |
+// | the Free Software Foundation; either version 2.1 of the License, or |
+// | (at your option) any later version. |
+// | |
+// | Structures_Graph is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+// | GNU Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public License |
+// | along with Structures_Graph; if not, write to the Free Software |
+// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
+// | 02111-1307 USA |
+// +-----------------------------------------------------------------------------+
+// | Author: Sérgio Carvalho <sergio.carvalho@portugalmail.com> |
+// +-----------------------------------------------------------------------------+
+//
+/**
+ * This file contains the definition of the Structures_Graph_Manipulator_AcyclicTest graph manipulator.
+ *
+ * @see Structures_Graph_Manipulator_AcyclicTest
+ * @package Structures_Graph
+ */
+
+/* dependencies {{{ */
+/** */
+require_once 'PEAR.php';
+/** */
+require_once 'Structures/Graph.php';
+/** */
+require_once 'Structures/Graph/Node.php';
+/* }}} */
+
+/* class Structures_Graph_Manipulator_AcyclicTest {{{ */
+/**
+ * The Structures_Graph_Manipulator_AcyclicTest is a graph manipulator
+ * which tests whether a graph contains a cycle.
+ *
+ * The definition of an acyclic graph used in this manipulator is that of a
+ * DAG. The graph must be directed, or else it is considered cyclic, even when
+ * there are no arcs.
+ *
+ * @author Sérgio Carvalho <sergio.carvalho@portugalmail.com>
+ * @copyright (c) 2004 by Sérgio Carvalho
+ * @package Structures_Graph
+ */
+class Structures_Graph_Manipulator_AcyclicTest {
+ /* _nonVisitedInDegree {{{ */
+ /**
+ *
+ * This is a variant of Structures_Graph::inDegree which does
+ * not count nodes marked as visited.
+ *
+ * @access private
+ * @return integer Number of non-visited nodes that link to this one
+ */
+ function _nonVisitedInDegree(&$node) {
+ $result = 0;
+ $graphNodes =& $node->_graph->getNodes();
+ foreach (array_keys($graphNodes) as $key) {
+ if ((!$graphNodes[$key]->getMetadata('acyclic-test-visited')) && $graphNodes[$key]->connectsTo($node)) $result++;
+ }
+ return $result;
+
+ }
+ /* }}} */
+
+ /* _isAcyclic {{{ */
+ /**
+ * @access private
+ */
+ function _isAcyclic(&$graph) {
+ // Mark every node as not visited
+ $nodes =& $graph->getNodes();
+ $nodeKeys = array_keys($nodes);
+ $refGenerator = array();
+ foreach($nodeKeys as $key) {
+ $refGenerator[] = false;
+ $nodes[$key]->setMetadata('acyclic-test-visited', $refGenerator[sizeof($refGenerator) - 1]);
+ }
+
+ // Iteratively peel off leaf nodes
+ do {
+ // Find out which nodes are leafs (excluding visited nodes)
+ $leafNodes = array();
+ foreach($nodeKeys as $key) {
+ if ((!$nodes[$key]->getMetadata('acyclic-test-visited')) && Structures_Graph_Manipulator_AcyclicTest::_nonVisitedInDegree($nodes[$key]) == 0) {
+ $leafNodes[] =& $nodes[$key];
+ }
+ }
+ // Mark leafs as visited
+ for ($i=sizeof($leafNodes) - 1; $i>=0; $i--) {
+ $visited =& $leafNodes[$i]->getMetadata('acyclic-test-visited');
+ $visited = true;
+ $leafNodes[$i]->setMetadata('acyclic-test-visited', $visited);
+ }
+ } while (sizeof($leafNodes) > 0);
+
+ // If graph is a DAG, there should be no non-visited nodes. Let's try to prove otherwise
+ $result = true;
+ foreach($nodeKeys as $key) if (!$nodes[$key]->getMetadata('acyclic-test-visited')) $result = false;
+
+ // Cleanup visited marks
+ foreach($nodeKeys as $key) $nodes[$key]->unsetMetadata('acyclic-test-visited');
+
+ return $result;
+ }
+ /* }}} */
+
+ /* isAcyclic {{{ */
+ /**
+ *
+ * isAcyclic returns true if a graph contains no cycles, false otherwise.
+ *
+ * @return boolean true iff graph is acyclic
+ * @access public
+ */
+ function isAcyclic(&$graph) {
+ // We only test graphs
+ if (!is_a($graph, 'Structures_Graph'))
+ Pear::raiseError('Structures_Graph_Manipulator_AcyclicTest::isAcyclic received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC);
+ if (!$graph->isDirected()) return false; // Only directed graphs may be acyclic
+
+ return Structures_Graph_Manipulator_AcyclicTest::_isAcyclic($graph);
+ }
+ /* }}} */
+}
+/* }}} */
+?>
Oops, something went wrong.

0 comments on commit 91edafc

Please sign in to comment.