next up previous
Next: Bibliography Up: CGI-Perl, GnuPlot, etc - Previous: 0.5 LWP

0.6 Graph Packages

In this section we'll look at a graph package that can be used to construct simple graphs.

Before going into graphs, there's a quick overview of the `Set' package.

[L529guest@biokdd agopu]\$ cat > set.pl
#!/usr/bin/perl
  
use Set::Scalar;
  
my $A = Set::Scalar->new('a', 'b', 'c');
my $B = Set::Scalar->new('b', 'c');
my $C = Set::Scalar->new('c', 'd');
  
print "INTERSECT of A, B, C = ", $A->intersection($B, $C), "\n";
print "UNION of A, B, C = ", $A->union($B, $C), "\n";

In the above code, we've just defined three sets A, B and C with the various elements as shown. Also use of simple operators like intersection() and union() is shown. The output of this script follows the source code of two other scripts (showing graph stuff).

The following two source code snippets show how graphs can be constructed.

[L529guest@biokdd agopu]\$ cat > graph1.pl
#!/usr/bin/perl
  
use Graph::Undirected;
use Graph::DFS;
  
my $g = Graph::Undirected->new;
  
$g->add_edge('a', 'b');
$g->add_edge('a', 'c');
$g->add_edge('a', 'd');
$g->add_edge('d', 'e');
$g->add_edge('f', 'g');
  
  
$dfs = Graph::DFS->new($g);
  
@RA  = $dfs->roots;
print $dfs->roots, "\n";
  
  
%R = $dfs->vertex_roots;
  
pr_root('a');
pr_root('b');
pr_root('f');
  
sub
pr_root
{
        print "root of $_[0] is ",  $R{$_[0]}, "\n";
        print "root of $_[0] is ",  $RA[$R{$_[0]}], "\n";
}

[L529guest@biokdd agopu]\$ cat > graph2.pl
#!/usr/bin/perl
  
use Set::Scalar;
use Graph::Undirected;
use Graph::DFS;
  
my $g = Graph::Undirected->new;
  
$g->add_weighted_edge('a', 1.0, 'b');
$g->add_weighted_edge('a', 1e-1, 'c');
$g->add_weighted_edge('a', 1e-4, 'd');
$g->add_weighted_edge('d', 1e-2, 'e');
$g->add_weighted_edge('f', 1e-3, 'g');
  
print "The density of the graph is ", $g->density, "\n";
  
my $allvset = Set::Scalar->new($g->vertices);
  
foreach $v ($g->vertices) {
        my @ne = $g->neighbors($v);
        my $vset = Set::Scalar->new(@ne);
  
        $g->set_attribute($v, $vset);
        print "The neighbors of $v are @ne \n";
}
  
#see if how many vertices cover the shole graph
$vcover = Set::Scalar->new;
foreach $v ($g->vertices) {
        my $vset = Set::Scalar->new(qw(@ne));
   
        $g->set_attribute($v, $vset);
        print "The neighbors of $v are @ne \n";
}
  
#see if how many vertices cover the shole graph
$vcover = Set::Scalar->new;
foreach $v ($g->vertices) {
        my $vset = Set::Scalar->new(qw(@ne));
   
        my $thisset = $g->get_attribute($v);
  
        my $setdiff = $allvset->difference($thisset);
        $setdiff->delete($v);
        my @notcovered = $setdiff->members;
        if (defined(@notcovered)) {
                print "Vertices [ @notcovered ] are not covered by $v \n";
        }
  
        $vcover->union($thisset);
  
        if ($vcover->is_equal($allvset)) {
                print "All vertices are covered by nodes ";
        }
}
  
$SP = $g->APSP_Floyd_Warshall($v);
#print "Floyd-Warshall graph ", $SP, "\n";
$path = $SP->get_attribute("path", 'a', 'e');
print " The shortest path from a to e is @$path \n";

This is what you can expect to see as output. Do note that these scripts don't spit HTML stuff as part of the output - no CGI object exists either! Yeah! these are essentially perl scripts. It's left as an exercize for you to modify the scripts to make them web-accessible. By now you should be able to do that, otherwise this whole exercize would be one in futility!

Anyway here we go ...

[L529guest@biokdd agopu]\$
[L529guest@biokdd agopu]\$ chmod 755 *.pl
[L529guest@biokdd agopu]\$
[L529guest@biokdd agopu]\$ perl set.pl
INTERSECT of A, B, C = (c)
UNION of A, B, C = (a b c d)

[L529guest@biokdd agopu]\$ perl graph1.pl
af
root of a is 0
root of a is a
root of b is 0
root of b is a
root of f is 1
root of f is f

[L529guest@biokdd agopu]\$ perl graph2.pl
The density of the graph is 0.238095238095238
The neighbors of a are b c d
The neighbors of b are a
The neighbors of c are a
The neighbors of d are e a
The neighbors of e are d
The neighbors of f are g
The neighbors of g are f
The neighbors of a are
The neighbors of b are
The neighbors of c are
The neighbors of d are
The neighbors of e are
The neighbors of f are
The neighbors of g are
Vertices [ e c b g d f ] are not covered by a
Vertices [ e c g d f ] are not covered by b
Vertices [ e g d f ] are not covered by c
Vertices [ e g f ] are not covered by d
Vertices [ g f ] are not covered by e
Vertices [ g ] are not covered by f
Vertices [  ] are not covered by g
All vertices are covered by nodes  The shortest path from a to e is a d e


next up previous
Next: Bibliography Up: CGI-Perl, GnuPlot, etc - Previous: 0.5 LWP
Arvind Gopu 2006-03-24