| 1 | package Graph::Fast; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | our $VERSION = "0.02"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use List::Util qw(min); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Hash::PriorityQueue; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 5 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 17 | 5 | ▶ | my $queue_maker = exists($args{queue_maker}) ? $args{queue_maker} : sub { Hash::PriorityQueue->new() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 5 | return bless({ | |||||||||||||||||||||||||||
| 19 | vertices => {}, | ||||||||||||||||||||||||||||
| 20 | edges => [], | ||||||||||||||||||||||||||||
| 21 | _queue_maker => $queue_maker, | ||||||||||||||||||||||||||||
| 22 | }, $class); | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 21 | ▶ | sub count_edges { | ||||||||||||||||||||||||||
|
Subroutine
count_edges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||
| 27 | 21 | return scalar(@{$self->{edges}}); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 21 | ▶ | sub count_vertices { | ||||||||||||||||||||||||||
|
Subroutine
count_vertices: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||
| 32 | 21 | return scalar(keys(%{$self->{vertices}})); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 15 | ▶ | sub add_vertex { | ||||||||||||||||||||||||||
|
Subroutine
add_vertex: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 15 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 15 | ▶ | if (!exists($self->{vertices}->{$name})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 15 | $self->{vertices}->{$name} = { name => $name, edges_in => {}, edges_out => {} }; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 15 | return $self->{vertices}->{$name}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | ▶ | sub del_vertex { | ||||||||||||||||||||||||||
|
Subroutine
del_vertex: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 6 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | ▶ | if (exists($self->{vertices}->{$name})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 6 | ▶ | @{$self->{edges}} = grep { $_->{from} ne $name and $_->{to} ne $name } @{$self->{edges}}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$_->{'from'} ne $name && $_->{'to'} ne $name
MC/DC100%2/2
$_->{'from'} ne $name && $_->{'to'} ne $name
$_->{'from'} ne $name
$_->{'to'} ne $name
Truth table100%3/3
A && B
A= $_->{'from'} ne $nameB= $_->{'to'} ne $name
| |||||||||||||||||||||||||||||
| 49 | 6 | foreach my $in_edge (keys %{$self->{vertices}->{$name}->{edges_in}}) { | |||||||||||||||||||||||||||
| 50 | 8 | delete($self->{vertices}->{$in_edge}->{edges_out}->{$name}); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 6 | foreach my $out_edge (keys %{$self->{vertices}->{$name}->{edges_out}}) { | |||||||||||||||||||||||||||
| 53 | 4 | delete($self->{vertices}->{$out_edge}->{edges_in}->{$name}); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 6 | delete($self->{vertices}->{$name}); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 9 | ▶ | sub dijkstra_worker { | ||||||||||||||||||||||||||
|
Subroutine
dijkstra_worker: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 9 | my ($self, $from, $to) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | my $vert = $self->{vertices}; | |||||||||||||||||||||||||||
| 63 | 9 | my $suboptimal = $self->{_queue_maker}->(); | |||||||||||||||||||||||||||
| 64 | 9 | $suboptimal->insert($_, $self->{d_suboptimal}->{$_}) foreach (keys(%{$self->{d_suboptimal}})); | |||||||||||||||||||||||||||
| 65 | 9 | $self->{d_dist}->{$_} = -1 foreach (@{$self->{d_unvisited}}); | |||||||||||||||||||||||||||
| 66 | 9 | $self->{d_dist}->{$from} = 0; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 9 | while (1) { | |||||||||||||||||||||||||||
| 69 | # find the smallest unvisited node | ||||||||||||||||||||||||||||
| 70 | 31 | ▶ | my $current = $suboptimal->pop() // last; | ||||||||||||||||||||||||||
|
Condition100%2/2
$suboptimal->pop // (last)
MC/DC100%1/1
$suboptimal->pop // (last)
$suboptimal->pop
Truth table100%2/2
A
A= $suboptimal->pop
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # update all neighbors | ||||||||||||||||||||||||||||
| 73 | 22 | foreach my $edge (values %{$vert->{$current}->{edges_out}}) { | |||||||||||||||||||||||||||
| 74 | 19 | ▶ | if (($self->{d_dist}->{$edge->{to}} == -1) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"d_dist"}{$edge->{"to"}} == -1 or $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
MC/DC100%2/2
$self->{"d_dist"}{$edge->{"to"}} == -1 or $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
$self->{"d_dist"}{$edge->{"to"}} == -1
$self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
Truth table100%3/3
A or B
A= $self->{"d_dist"}{$edge->{"to"}} == -1B= $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
| |||||||||||||||||||||||||||||
| 75 | ($self->{d_dist}->{$edge->{to}} > ($self->{d_dist}->{$current} + $edge->{weight}) )) { | ||||||||||||||||||||||||||||
| 76 | $suboptimal->update( | ||||||||||||||||||||||||||||
| 77 | $edge->{to}, | ||||||||||||||||||||||||||||
| 78 | $self->{d_dist}->{$edge->{to}} = $self->{d_dist}->{$current} + $edge->{weight} | ||||||||||||||||||||||||||||
| 79 | 16 | ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # trace the path from the destination to the start | ||||||||||||||||||||||||||||
| 85 | 9 | my @path = (); | |||||||||||||||||||||||||||
| 86 | 9 | my $current = $to; | |||||||||||||||||||||||||||
| 87 | 9 | ▶ | NODE: while ($current ne $from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 11 | foreach my $edge (values %{$vert->{$current}->{edges_in}}) { | |||||||||||||||||||||||||||
| 89 | 13 | ▶ | if ($self->{d_dist}->{$current} == $self->{d_dist}->{$edge->{from}} + $edge->{weight}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 7 | $current = $edge->{from}; | |||||||||||||||||||||||||||
| 91 | 7 | unshift(@path, $edge); | |||||||||||||||||||||||||||
| 92 | 7 | next NODE; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | # getting here means we found no predecessor - there is none. | ||||||||||||||||||||||||||||
| 96 | # so there's no path. | ||||||||||||||||||||||||||||
| 97 | 4 | return (); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 5 | return @path; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 9 | ▶ | sub dijkstra_first { | ||||||||||||||||||||||||||
|
Subroutine
dijkstra_first: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 9 | my ($self, $from, $to) = @_; | |||||||||||||||||||||||||||
| 105 | 9 | $self->{d_from} = $from; | |||||||||||||||||||||||||||
| 106 | 9 | $self->{d_dist} = {}; | |||||||||||||||||||||||||||
| 107 | 9 | $self->{d_unvisited} = [ grep { $_ ne $from } keys(%{$self->{vertices}}) ]; | |||||||||||||||||||||||||||
| 108 | 9 | $self->{d_suboptimal} = { $from => 0 }; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 9 | dijkstra_worker($self, $from, $to); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 5 | ▶ | sub dijkstra_continue { | ||||||||||||||||||||||||||
|
Subroutine
dijkstra_continue: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 5 | my ($self, $from, $to, $del_to) = @_; | |||||||||||||||||||||||||||
| 115 | # instead of reinitializing, it should invoke the worker after initializing | ||||||||||||||||||||||||||||
| 116 | # to a state that assumes that an edge to $del_to has just been deleted. | ||||||||||||||||||||||||||||
| 117 | 5 | goto &dijkstra_first; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 9 | ▶ | sub dijkstra { | ||||||||||||||||||||||||||
|
Subroutine
dijkstra: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 9 | my ($self, $from, $to, $del_to) = @_; | |||||||||||||||||||||||||||
| 122 | 9 | ▶ | if (!defined($self->{d_from}) or $self->{d_from} ne $from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $self->{"d_from"} or $self->{"d_from"} ne $from
MC/DC100%2/2
not defined $self->{"d_from"} or $self->{"d_from"} ne $from
not defined $self->{"d_from"}
$self->{"d_from"} ne $from
Truth table100%3/3
not(A) or B
A= not defined $self->{"d_from"}B= $self->{"d_from"} ne $from
| |||||||||||||||||||||||||||||
| 123 | 4 | goto &dijkstra_first; | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | 5 | goto &dijkstra_continue; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub recursive_dijkstra { | ||||||||||||||||||||||||||
|
Subroutine
recursive_dijkstra: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($self, $from, $to, $level, $del_to) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | my @d = ([ $self->dijkstra($from, $to, $del_to) ]); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (!defined($d[0]->[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | return (); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($level > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | foreach (0..(@{$d[0]}-1)) { | |||||||||||||||||||||||||||
| 139 | # from copies of the graph, remove one edge from the result path, | ||||||||||||||||||||||||||||
| 140 | # and continue finding paths on that tree. | ||||||||||||||||||||||||||||
| 141 | 0 | my $ffffuuuu = $self->{_queue_maker}; | |||||||||||||||||||||||||||
| 142 | 0 | $self->{_queue_maker} = "omg"; | |||||||||||||||||||||||||||
| 143 | 0 | my $g2 = dclone($self); | |||||||||||||||||||||||||||
| 144 | 0 | $g2->{_queue_maker} = $self->{_queue_maker} = $ffffuuuu; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | $g2->del_edge($d[0]->[$_]->{from}, $d[0]->[$_]->{to}); | |||||||||||||||||||||||||||
| 147 | 0 | my @new = $g2->recursive_dijkstra($from, $to, $level - 1, $d[0]->[$_]->{to}); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # add all new paths, unless they are already present in the result set | ||||||||||||||||||||||||||||
| 150 | 0 | foreach my $n (@new) { | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | push(@d, $n) unless (grep { $n ~~ $_ } @d); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | @d; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 31 | ▶ | sub add_edge { | ||||||||||||||||||||||||||
|
Subroutine
add_edge: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 31 | my ($self, $from, $to, $weight, $user_data) = @_; | |||||||||||||||||||||||||||
| 161 | 31 | $self->del_edge($from => $to); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 31 | ▶ | my $edge = { from => $from, to => $to, weight => $weight, (defined($user_data) ? (user_data => $user_data) : ()) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 31 | push(@{$self->{edges}}, $edge); | |||||||||||||||||||||||||||
| 166 | 31 | ▶ | ($self->{vertices}->{$from} // $self->add_vertex($from))->{edges_out}->{$to} = $edge; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"vertices"}{$from} // $self->add_vertex($from)
MC/DC0%0/2
$self->{"vertices"}{$from} // $self->add_vertex($from)
$self->{"vertices"}{$from}
$self->add_vertex($from)
Truth table33%1/3
A // B
A= $self->{"vertices"}{$from}B= $self->add_vertex($from)
| |||||||||||||||||||||||||||||
| 167 | 31 | ▶ | ($self->{vertices}->{$to } // $self->add_vertex($to ))->{edges_in }->{$from} = $edge; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"vertices"}{$to} // $self->add_vertex($to)
MC/DC0%0/2
$self->{"vertices"}{$to} // $self->add_vertex($to)
$self->{"vertices"}{$to}
$self->add_vertex($to)
Truth table66%2/3
A // B
A= $self->{"vertices"}{$to}B= $self->add_vertex($to)
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 33 | ▶ | sub del_edge { | ||||||||||||||||||||||||||
|
Subroutine
del_edge: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 33 | my ($self, $from, $to) = @_; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # find the edge. assume it only exists once -> only delete the first. | ||||||||||||||||||||||||||||
| 174 | # while we're at it, delete the edge from the source vertex... | ||||||||||||||||||||||||||||
| 175 | 33 | my $e = $self->{vertices}->{$from}->{edges_out}->{$to}; | |||||||||||||||||||||||||||
| 176 | 33 | ▶ | return undef if (!defined($e)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 2 | delete($self->{vertices}->{$from}->{edges_out}->{$to}); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # now search it in the destination vertex' list, delete it there | ||||||||||||||||||||||||||||
| 180 | # also only delete the first matching one here (though now there | ||||||||||||||||||||||||||||
| 181 | # shouldn't be any duplicates at all because now we're matching the | ||||||||||||||||||||||||||||
| 182 | # actual edge, not just its endpoints like above. | ||||||||||||||||||||||||||||
| 183 | 2 | delete($self->{vertices}->{$to}->{edges_in}->{$from}); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # and remove it from the graph's vertex list | ||||||||||||||||||||||||||||
| 186 | 2 | @{$self->{edges}} = grep { $_ != $e } @{$self->{edges}} | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 6 | 1; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||