NYCPHP Meetup

NYPHP.org

[nycphp-talk] Graph Data Structures

Jonathan hendler at simmons.edu
Wed Aug 17 07:17:00 EDT 2005


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



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20050817/99833e49/attachment.html>


More information about the talk mailing list