jkeenan1 has asked for the wisdom of the Perl Monks concerning the following question:
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:
When I run this, I get this output:$ 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})); }
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:$ 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
... the test will PASS because it is phrased in the negative. Instead, the test FAILs:ok(! $g->add_edge( 'Lord' => 'Tornado '), "I expect that I cannot add Lord -> Tornado edge");
$ 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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Graph.pm: How do I represent one-way dependency?
by LanX (Saint) on Feb 18, 2018 at 02:45 UTC | |
by jkeenan1 (Deacon) on Feb 18, 2018 at 03:02 UTC | |
by LanX (Saint) on Feb 18, 2018 at 03:04 UTC | |
by LanX (Saint) on Feb 23, 2018 at 18:36 UTC | |
by pryrt (Abbot) on Feb 23, 2018 at 19:48 UTC | |
|