NYCPHP Meetup

NYPHP.org

[nycphp-talk] Graph Data Structures

Douglas Clifton dwclifton at gmail.com
Wed Aug 17 10:21:21 EDT 2005


Is it mandatory that you use PHP to solve your problem?

If not, then I suggest you consider using Perl. Unless I'm
mistaken, there doesn't seem to be quite the codebase
in PHP for these sorts of abstract computer science
problems. Perl is another story. CPAN contains 1000s
of modules for this kind of thing, and a quick search
revealed this promising package: 

http://search.cpan.org/~jhi/Graph-0.66/lib/Graph.pod

Gasp! Someone posting a message about Perl to a PHP
mailing list? Let me tell you, I was using Perl long before
PHP was nothing but a bunch of CGI scripts (written in
Perl mind you, and later in C).

Also, the languages are very similar, so you may be able
to port the Perl Graph library to PHP. HTH ~d

-- 
Douglas Clifton
dwclifton at gmail.com
http://loadaveragezero.com/
http://loadaveragezero.com/app/s9y/
http://loadaveragezero.com/drx/rss/recent

> ---------- Forwarded message ----------
> From: Jonathan <hendler at simmons.edu>
> To: NYPHP Talk <talk at lists.nyphp.org>
> Date: Wed, 17 Aug 2005 07:17:00 -0400
> Subject: [nycphp-talk] Graph Data Structures
>  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