line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Graph::RandomPath;
|
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
62393
|
use 5.012000;
|
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
74
|
|
4
|
2
|
|
|
2
|
|
13
|
use strict;
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
66
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings;
|
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
63
|
|
6
|
2
|
|
|
2
|
|
11
|
use base qw(Exporter);
|
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
261
|
|
7
|
2
|
|
|
2
|
|
2625
|
use Graph;
|
|
2
|
|
|
|
|
327843
|
|
|
2
|
|
|
|
|
67
|
|
8
|
2
|
|
|
2
|
|
17
|
use Carp;
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1087
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.01';
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw(
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
) ] );
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @EXPORT = qw(
|
19
|
|
|
|
|
|
|
);
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub create_generator {
|
22
|
40
|
|
|
40
|
1
|
1292986
|
my ($class, $g, $src, $dst, %opt) = @_;
|
23
|
|
|
|
|
|
|
|
24
|
40
|
|
50
|
|
|
260
|
$opt{max_length} //= 64;
|
25
|
|
|
|
|
|
|
|
26
|
40
|
|
|
|
|
200
|
my %to_src = map { $_ => 1 } $src, $g->all_successors($src);
|
|
771
|
|
|
|
|
67739
|
|
27
|
40
|
|
|
|
|
395
|
my %to_dst = map { $_ => 1 } $dst, $g->all_predecessors($dst);
|
|
773
|
|
|
|
|
76450
|
|
28
|
|
|
|
|
|
|
|
29
|
40
|
|
|
|
|
401
|
my $copy = $g->new;
|
30
|
40
|
50
|
66
|
|
|
20166
|
$copy->set_edge_weight($_->[1], $_->[0], 1) for grep {
|
|
7753
|
|
66
|
|
|
313154
|
|
31
|
|
|
|
|
|
|
$to_src{$_->[0]} and $to_src{$_->[1]} and
|
32
|
|
|
|
|
|
|
$to_dst{$_->[0]} and $to_dst{$_->[1]}
|
33
|
|
|
|
|
|
|
} $g->edges;
|
34
|
|
|
|
|
|
|
|
35
|
40
|
|
|
|
|
1737697
|
my $sptg;
|
36
|
|
|
|
|
|
|
|
37
|
40
|
|
|
|
|
122
|
eval {
|
38
|
40
|
|
|
|
|
296
|
$sptg = $copy->SPT_Dijkstra($dst);
|
39
|
|
|
|
|
|
|
};
|
40
|
|
|
|
|
|
|
|
41
|
40
|
50
|
|
|
|
1597924
|
if ($@) {
|
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
|
|
|
|
|
0
|
$sptg = $copy->SPT_Bellman_Ford($dst);
|
46
|
|
|
|
|
|
|
}
|
47
|
|
|
|
|
|
|
|
48
|
40
|
100
|
66
|
|
|
4724
|
Carp::confess "Unable to generate paths for these parameters" unless
|
49
|
|
|
|
|
|
|
(defined $sptg->get_vertex_attribute($src, 'weight') and
|
50
|
|
|
|
|
|
|
$sptg->get_vertex_attribute($src, 'weight') < $opt{max_length});
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
return sub {
|
53
|
360
|
|
|
360
|
|
3343118
|
my @path = ($src);
|
54
|
360
|
|
|
|
|
1541
|
my $target = rand($opt{max_length});
|
55
|
360
|
|
|
|
|
807
|
while (1) {
|
56
|
12131
|
|
|
|
|
48503
|
my $v = $copy->random_predecessor($path[-1]);
|
57
|
|
|
|
|
|
|
|
58
|
12131
|
100
|
100
|
|
|
2975472
|
last if $path[-1] eq $dst and
|
|
|
|
66
|
|
|
|
|
59
|
|
|
|
|
|
|
(not defined $v or @path > $target);
|
60
|
|
|
|
|
|
|
|
61
|
11771
|
|
100
|
|
|
50508
|
my $w = $sptg->get_vertex_attribute($v, 'weight') // 0;
|
62
|
|
|
|
|
|
|
|
63
|
11771
|
100
|
|
|
|
1367906
|
if (@path + $w > $opt{max_length}) {
|
64
|
3599
|
|
|
|
|
11364
|
my $v = $sptg->get_vertex_attribute($v, 'p');
|
65
|
|
|
|
|
|
|
};
|
66
|
|
|
|
|
|
|
|
67
|
11771
|
|
|
|
|
410021
|
push @path, $v;
|
68
|
|
|
|
|
|
|
}
|
69
|
360
|
|
|
|
|
7554
|
@path;
|
70
|
|
|
|
|
|
|
}
|
71
|
36
|
|
|
|
|
8807
|
}
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1;
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__
|