[nycphp-talk] Graph Data Structures
Dan Cech
dcech at phpwerx.net
Wed Aug 17 09:55:57 EDT 2005
Kenneth,
That seems to be a very elegant solution to the problem, top stuff!
Dan
Kenneth Downs wrote:
> Johathan,
>
> Have you considered putting the nodes into one array, and the arcs into a
> second?
>
> $nodes = array('a'=>array(..info..),'b'=>array(..info..));
>
> $arcs = array(array('a','b'),array('b','a'))
>
> This allows each node's info to be stored only once, and allows you to
> then treat the arcs cleanly, allowing or disallowing any combo you may
> choose.
>
> You may have to write a little of your code to walk through things, but
> you'll have complete integrity and control.
>
>
>>I'm trying to formulate a question out of this. If there isn't one here,
>>I hope the read is interesting.
>>My goal is to create the simplest efficient graph data structures - that
>>allow for cycles.
>>
>>The reason one would want cycles in a graph is the following:
>>a->b
>>and
>>b->a
>>(or b->a again with another arc (also known as a hypergraph))
>>or
>>a->a
>>
>> where '->' is an arc
>>Even if the arcs are labled, the data in 'a' is something I don't want
>>to duplicate.
>>
>>I am using php version 4.3.11
>>
>>If I try to do this with a simple php array:
>>
>> $a = array();
>> $b = array();
>>
>> $a['b'] = & $b;
>> $b['a'] = & $a;
>>
>> print_r($a);
>>
>>Array
>>(
>> [a] => Array
>> (
>> [b] => Array
>> (
>> [a] => Array
>> *RECURSION*
>> )
>>
>> )
>>
>>)
>>
>>
>>I get this recursion error. Or, perhaps this is not an error at all. But
>>I can't seem to use this function:
>>
>> function recursive_print($array)
>> {
>> foreach($array as $key => $value)
>> {
>> if (is_array($value))
>> {
>> echo $key .' <hr /> ' .recursive_print($value);
>> }
>> else
>> {
>> echo 'end'.$value;
>> }
>> }
>> }
>>
>>So I went to the PEAR site - http://pear.php.net/package/Structures_Graph
>>This pear package doesn't throw any errors but it also seems to balk -
>>although I am not sure the *RECURSION* will affect functionality
>>
>>
>> include 'Structures/Graph.php';
>> $directedGraph =& new Structures_Graph(true);
>> $nodeOne =& new Structures_Graph_Node();
>> $nodeTwo =& new Structures_Graph_Node();
>>
>>
>> $directedGraph->addNode(&$nodeOne);
>> $directedGraph->addNode(&$nodeTwo);
>>
>>
>> $nodeOne->connectTo($nodeTwo);
>> $nodeTwo->connectTo($nodeOne);
>>
>>
>>Inside the code I found a comment about the Zend engine before the data
>>structure procedes to iteratively loop through the the nodes to see if
>>there are duplicates.
>> /*
>> 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
>> */
>>
>>Even so, print_r produces many recursion warnings.
>>
>>Maybe I am just trying to use a hammer for a screwdriver. But can anyone
>>offer any insight here?
>>
>>Thanks,
>>
>>- Jonathan Hendler
More information about the talk
mailing list