| 1 | package Graph::RandomPath; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.012000; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Graph; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | ) ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 40 | ▶ | sub create_generator { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_generator: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 40 | my ($class, $g, $src, $dst, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 40 | ▶ | $opt{max_length} //= 64; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"max_length"} //= 64
MC/DC0%0/1
$opt{"max_length"} //= 64
$opt{"max_length"}
Truth table50%1/2
A
A= $opt{"max_length"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 40 | my %to_src = map { $_ => 1 } $src, $g->all_successors($src); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 40 | my %to_dst = map { $_ => 1 } $dst, $g->all_predecessors($dst); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 40 | my $copy = $g->new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 40 | $copy->set_edge_weight($_->[1], $_->[0], 1) for grep { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | $to_src{$_->[0]} and $to_src{$_->[1]} and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 7459 | ▶ | $to_dst{$_->[0]} and $to_dst{$_->[1]} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition77%7/9
$to_src{$_->[0]} && $to_src{$_->[1]} && $to_dst{$_->[0]} && $to_dst{$_->[1]}
$to_src{$_->[0]} and $to_src{$_->[1]} and $to_dst{$_->[0]}
$to_src{$_->[0]} and $to_src{$_->[1]}
MC/DC50%2/4
$to_src{$_->[0]} && $to_src{$_->[1]} && $to_dst{$_->[0]} && $to_dst{$_->[1]}
$to_src{$_->[0]}
$to_src{$_->[1]}
$to_dst{$_->[0]}
$to_dst{$_->[1]}
Truth table60%3/5
A and B and C && D
A= $to_src{$_->[0]}B= $to_src{$_->[1]}C= $to_dst{$_->[0]}D= $to_dst{$_->[1]}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } $g->edges; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 40 | my $sptg; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 40 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 40 | $sptg = $copy->SPT_Dijkstra($dst); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 40 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # This is here in case the module is updated to allow user- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # supplied weights for the edges, which might then be nega- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # tive and require a different shortest path algorithm. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $sptg = $copy->SPT_Bellman_Ford($dst); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | Carp::confess "Unable to generate paths for these parameters" unless | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | (defined $sptg->get_vertex_attribute($src, 'weight') and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 40 | ▶ | $sptg->get_vertex_attribute($src, 'weight') < $opt{max_length}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sptg->get_vertex_attribute($src, "weight") and $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
MC/DC50%1/2
defined $sptg->get_vertex_attribute($src, "weight") and $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
defined $sptg->get_vertex_attribute($src, "weight")
$sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
Truth table66%2/3
A and B
A= defined $sptg->get_vertex_attribute($src, "weight")B= $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 350 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 350 | my @path = ($src); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 350 | my $target = rand($opt{max_length}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 350 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 11478 | my $v = $copy->random_predecessor($path[-1]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 11478 | ▶ | last if $path[-1] eq $dst and | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$path[-1] eq $dst and !defined($v) || @path > $target
not defined $v or @path > $target
MC/DC100%3/3
$path[-1] eq $dst and !defined($v) || @path > $target
$path[-1] eq $dst
not defined $v
@path > $target
Truth table100%4/4
A and not(B) or C
A= $path[-1] eq $dstB= not defined $vC= @path > $target
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | (not defined $v or @path > $target); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 11128 | ▶ | my $w = $sptg->get_vertex_attribute($v, 'weight') // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$sptg->get_vertex_attribute($v, "weight") // 0
MC/DC100%1/1
$sptg->get_vertex_attribute($v, "weight") // 0
$sptg->get_vertex_attribute($v, "weight")
Truth table100%2/2
A
A= $sptg->get_vertex_attribute($v, "weight")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 11128 | ▶ | if (@path + $w > $opt{max_length}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 3427 | my $v = $sptg->get_vertex_attribute($v, 'p'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 11128 | push @path, $v; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 350 | @path; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 35 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||