I am attempting to use the Graph module from CPAN for the first time. I am having trouble understanding how the module implements the concept of a "directed graph".

Suppose I have 3 nodes, "Jolt", "Lord", and "Tornado". I want to indicate that "Tornado" depends on each of "Jolt" and "Lord". Based on the Graph distribution's t/06_new.t and t/18_add_edge.t, I believe I can say that with the following program:

$ cat graph.t #!/usr/bin/env perl use strict; use warnings; use Graph; use Test::More qw( no_plan ); my @vertices = qw( Jolt Lord Tornado ); my $g = Graph->new(vertices => [ @vertices ]); ok(defined $g, "Constructor returned defined value"); isa_ok($g, 'Graph'); for my $v (@vertices) { ok($g->has_vertex($v), "vertex $v"); } my @edges = ( [ 'Tornado' => 'Jolt' ], [ 'Tornado' => 'Lord' ], ); for my $e (@edges) { ok($g->add_edge(@{$e}), sprintf("Add edge %-12s -> %s", @{$e})); }
When I run this, I get this output:
$ prove -v graph.t graph.t .. ok 1 - Constructor returned defined value ok 2 - An object of class 'Graph' isa 'Graph' ok 3 - vertex Jolt ok 4 - vertex Lord ok 5 - vertex Tornado ok 6 - Add edge Tornado -> Jolt ok 7 - Add edge Tornado -> Lord 1..7 ok All tests successful. Files=1, Tests=7, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.11 cusr + 0.00 csys = 0.13 CPU) Result: PASS
So far so good. But to me "depends on" means that I should not be able to add an edge from "Jolt" to "Tornado" or from "Lord" to "Tornado" because I've already established the other direction for those edges. So my expectation is that, I when I add the following unit test:
ok(! $g->add_edge( 'Lord' => 'Tornado '), "I expect that I cannot add Lord -> Tornado edge");
... the test will PASS because it is phrased in the negative. Instead, the test FAILs:
$ prove -v graph.t graph.t .. ok 1 - Constructor returned defined value ok 2 - An object of class 'Graph' isa 'Graph' ok 3 - vertex Jolt ok 4 - vertex Lord ok 5 - vertex Tornado ok 6 - Add edge Tornado -> Jolt ok 7 - Add edge Tornado -> Lord not ok 8 - I expect that I cannot add Lord -> Tornado edge # Failed test 'I expect that I cannot add Lord -> Tornado edge' # at graph.t line 23. 1..8 # Looks like you failed 1 test of 8. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/8 subtests Test Summary Report ------------------- graph.t (Wstat: 256 Tests: 8 Failed: 1) Failed test: 8 Non-zero exit status: 1 Files=1, Tests=8, 0 wallclock secs ( 0.01 usr 0.01 sys + 0.11 cusr + 0.00 csys = 0.13 CPU) Result: FAIL

So, what am I not grokking about Graph and directed graphs? Is it possible to implement what I want with Graph, or is some other module better for this?

Thank you very much.

Jim Keenan

In reply to Graph.pm: How do I represent one-way dependency? by jkeenan1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.