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 * The Graph.php file contains the definition of the Structures_Graph class
29 * @see Structures_Graph
30 * @package Structures_Graph
33 /* dependencies {{{ */
34 /** PEAR base classes */
35 require_once 'PEAR.php';
37 require_once 'Structures/Graph/Node.php';
40 define('STRUCTURES_GRAPH_ERROR_GENERIC', 100);
42 /* class Structures_Graph {{{ */
44 * The Structures_Graph class represents a graph data structure.
46 * A Graph is a data structure composed by a set of nodes, connected by arcs.
47 * Graphs may either be directed or undirected. In a directed graph, arcs are
48 * directional, and can be traveled only one way. In an undirected graph, arcs
49 * are bidirectional, and can be traveled both ways.
51 * @author Sérgio Carvalho <sergio.carvalho@portugalmail.com>
52 * @copyright (c) 2004 by Sérgio Carvalho
53 * @package Structures_Graph
56 class Structures_Graph
{
61 var $_nodes = array();
65 var $_directed = false;
73 * @param boolean Set to true if the graph is directed. Set to false if it is not directed. (Optional, defaults to true)
76 function Structures_Graph($directed = true) {
77 $this->_directed
= $directed;
84 * Return true if a graph is directed
86 * @return boolean true if the graph is directed
89 function isDirected() {
90 return (boolean
) $this->_directed
;
97 * Add a Node to the Graph
99 * @param Structures_Graph_Node The node to be added.
102 function addNode(&$newNode) {
104 if (!is_a($newNode, 'Structures_Graph_Node')) return Pear
::raiseError('Structures_Graph::addNode received an object that is not a Structures_Graph_Node', STRUCTURES_GRAPH_ERROR_GENERIC
);
105 // Graphs are node *sets*, so duplicates are forbidden. We allow nodes that are exactly equal, but disallow equal references.
106 foreach($this->_nodes
as $key => $node) {
108 ZE1 equality operators choke on the recursive cycle introduced by the _graph field in the Node object.
109 So, we'll check references the hard way (change $this->_nodes[$key] and check if the change reflects in
112 $savedData = $this->_nodes
[$key];
113 $referenceIsEqualFlag = false;
114 $this->_nodes
[$key] = true;
115 if ($node === true) {
116 $this->_nodes
[$key] = false;
117 if ($node === false) $referenceIsEqualFlag = true;
119 $this->_nodes
[$key] = $savedData;
120 if ($referenceIsEqualFlag) return Pear
::raiseError('Structures_Graph::addNode received an object that is a duplicate for this dataset', STRUCTURES_GRAPH_ERROR_GENERIC
);
122 $this->_nodes
[] =& $newNode;
123 $newNode->setGraph($this);
127 /* removeNode (unimplemented) {{{ */
130 * Remove a Node from the Graph
132 * @todo This is unimplemented
133 * @param Structures_Graph_Node The node to be removed from the graph
136 function removeNode(&$node) {
143 * Return the node set, in no particular order. For ordered node sets, use a Graph Manipulator insted.
146 * @see Structures_Graph_Manipulator_TopologicalSorter
147 * @return array The set of nodes in this graph
149 function &getNodes() {
150 return $this->_nodes
;