GraphViz


ABCDone.jpg

Some complicated stuff. Well, a little bit.

But its not really that hard, with php and a couple other things its not too difficult to get a drawing of a graph data structure.

The image here renders from the below code, plus a couple changes to make it a bit more interesting and show extra information, e.g. the red lines with the outline arrows are the critical path.


The listing has `rankdir...LR' while this image was done with the default TB or Top to Bottom orientation.

The picture of the graph is one way to quickly see if the array setups are correct and doing what is expected- with some CLI (Command Line Interface) code, some different php-pear bits, one can take the graph array setup and get some very handy information.

When doing a project identifying the critical path is important since a delay along any critical path(s) will delay the entire project.

If this ABCDone project is a subset of the tasks required for building a house e.g. (a) framing the walls; (b) electrical wiring; (c) plumbing; and (d) drywall; then its easy to see that one should not interfere with  the electricians.

And don't start the drywall before completing the previous tasks- both the plumbers and electricians will be very upset when sheet rock panels go up and block access to incomplete work.
<?php
/*
gv_abcd.php

draw a graph data structure with pear and GraphViz

req: php
req: httpd
req: php-pear-Image-GraphViz

see http://www.graphviz.org and

Borate, S. (February 13, 2012. Building a graph data structure in PHP. [Web log
    comment].  Retrieved from
    http://www.codediesel.com/algorithms/building-a-graph-data-structure-in-php/
    
*/

require_once 'Image/GraphViz.php';

// make a directed acyclic graph render it Left to Right (LR)
$dag = new Image_GraphViz(true,
                           array('rankdir' => 'LR',
                                 'label' => 'ABCDone Project Task Paths', ) ) ;

// array of two element arrays the first [0] element values must be unique
$aNodes = array( [ 'a', 1.0 ],
                 [ 'b', 3.0 ],
                 [ 'c', 2.0 ],
                 [ 'd', 4.0 ], ) ;


foreach($aNodes as $node) {
 
  // add node to the graph with name and weight values
  $dag->addNode( $node[0],
                 array( 'label' =>  "$node[0]<br />" . $node[1], ) );
}

// connections between different nodes
//   'a-b' -> means node 'a' connects with node 'b'
$aVertices = array('a-b', 'a-c', 'b-d', 'c-d', );

/*
// optional array of critical path vertices|edges
$aCritpath = array( 'a-b','b-d' );
*/

foreach($aVertices as $vertex) {

  // go basic
  $colr = "black" ;
  $arro = "normal" ;


/* // or optionally pretty-up the drawing
   // $aCritpath array must be enabled
  // check if this is a critical path vertex|edge
  if ( in_array( $vertex, $aCritpath ) ) {
    $colr = "red" ;
    $arro = "empty" ;
  } else {
    $colr = "black" ;
    $arro = "normal" ;
  }
*/

  // split out the two edges
  $data = preg_split("/-/", $vertex);
  // 
  $dag->addEdge( array( $data[0] => $data[1], ),
                 array( 'color'  => $colr,
                        'arrowhead' => $arro, ) );
}

// disable the ...->image() call to see any `echo..` or `print(...` text
// can not do both. there is always a trade-off.

// draw|render the img
$dag->image();

?>

No comments:

Post a Comment