line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package BellmanFord;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24936
|
use 5.008004;
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
6
|
use Carp;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1461
|
|
5
|
|
|
|
|
|
|
#our @ISA = qw(Exporter);
|
6
|
|
|
|
|
|
|
#our @EXPORT = qw();
|
7
|
|
|
|
|
|
|
require Exporter;
|
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export
|
11
|
|
|
|
|
|
|
# # names by default without a very good reason. Use EXPORT_OK instead.
|
12
|
|
|
|
|
|
|
# # Do not simply export all your public functions/methods/constants.
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# # This allows declaration use Algorithm::BellmanFord ':all';
|
15
|
|
|
|
|
|
|
# # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
|
16
|
|
|
|
|
|
|
# # will save memory.
|
17
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw() ] );
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our @EXPORT = qw();
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
######################### variables #######
|
24
|
|
|
|
|
|
|
sub new
|
25
|
|
|
|
|
|
|
{
|
26
|
0
|
|
|
0
|
|
|
$class = shift;
|
27
|
0
|
|
|
|
|
|
$self = {
|
28
|
|
|
|
|
|
|
input_file => shift,
|
29
|
|
|
|
|
|
|
output_file => shift,
|
30
|
|
|
|
|
|
|
};
|
31
|
|
|
|
|
|
|
# Print all the values just for clarification.
|
32
|
0
|
|
|
|
|
|
bless $self, $class;
|
33
|
0
|
|
|
|
|
|
return $self;
|
34
|
|
|
|
|
|
|
}
|
35
|
|
|
|
|
|
|
sub calculate
|
36
|
|
|
|
|
|
|
{
|
37
|
0
|
0
|
|
0
|
|
|
open FILE, "$self->{input_file}" or die $!;
|
38
|
0
|
0
|
|
|
|
|
open FILE2, "$self->{output_file}" or die $!;
|
39
|
0
|
|
|
|
|
|
@lines = ;
|
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
$lines[0] =~/nodes ([0-9]+);links ([0-9]+)/;
|
42
|
0
|
|
|
|
|
|
$totNodes = $1;
|
43
|
0
|
|
|
|
|
|
$links = $2;
|
44
|
0
|
|
|
|
|
|
print "\nTotal Nodes = $totNodes and Links = $links\n";
|
45
|
0
|
|
|
|
|
|
@u1;
|
46
|
0
|
|
|
|
|
|
@v1;
|
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
for ( $i=1; $i<=$links; $i++ )
|
49
|
|
|
|
|
|
|
{
|
50
|
0
|
|
|
|
|
|
$lines[$i] =~/([a-z]),([a-z]),([0-9]+\.[0-9]+)/;
|
51
|
0
|
|
|
|
|
|
$u1[$i-1] = $1;
|
52
|
0
|
|
|
|
|
|
$v1[$i-1] = $2;
|
53
|
0
|
|
|
|
|
|
$wt[$i-1] = $3;
|
54
|
|
|
|
|
|
|
}
|
55
|
0
|
|
|
|
|
|
$nodeStr = $lines[$links + 1];
|
56
|
0
|
|
|
|
|
|
chomp ($nodeStr);
|
57
|
0
|
|
|
|
|
|
@nodes12 = split(',',$nodeStr);
|
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
@startNode;
|
60
|
0
|
|
|
|
|
|
@endNode;
|
61
|
0
|
|
|
|
|
|
$infinity = "122.0";
|
62
|
0
|
|
|
|
|
|
$lenOfFile = @lines;
|
63
|
0
|
|
|
|
|
|
$m = 0;
|
64
|
0
|
|
|
|
|
|
for ($t = $links + 2; $t < $lenOfFile-1; $t++) {
|
65
|
0
|
|
|
|
|
|
$lines[$t] =~/^path ([a-z]),([a-z])/;
|
66
|
0
|
|
|
|
|
|
$startEndNode[$m] = "$1,$2";
|
67
|
0
|
|
|
|
|
|
$m++;
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
foreach $pair (@startEndNode) {
|
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$pair =~/([a-z]),([a-z])/;
|
73
|
0
|
|
|
|
|
|
$root = $1;
|
74
|
0
|
|
|
|
|
|
$destination = $2;
|
75
|
0
|
|
|
|
|
|
print "\nWelcome to My Bellman algorithm we find solution for source = $1 and destination = $2\n";
|
76
|
0
|
0
|
|
|
|
|
open FILE2, ">>output.txt" or die $!;
|
77
|
0
|
|
|
|
|
|
print FILE2 "\nWelcome to My Bellman algorithm we find solution for source = $1 and destination = $2\n";
|
78
|
0
|
|
|
|
|
|
@node = @nodes12;
|
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
%dist;
|
81
|
0
|
|
|
|
|
|
%prev;
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
############################ the algorithm ####
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# first, set all distances to infinity
|
86
|
0
|
|
|
|
|
|
foreach $n (@node) {
|
87
|
0
|
|
|
|
|
|
$dist{$n} = $infinity;
|
88
|
0
|
|
|
|
|
|
$prev{$n}= Null;
|
89
|
|
|
|
|
|
|
}
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# .. except the source
|
92
|
0
|
|
|
|
|
|
$dist{$root} = 0;
|
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
for ($l=0; $l < $totNodes - 1; $l++) {
|
95
|
0
|
|
|
|
|
|
for ($n=0; $n < $links - 1; $n++) {
|
96
|
0
|
|
|
|
|
|
$u = $u1[$n];
|
97
|
0
|
|
|
|
|
|
$v = $v1[$n];
|
98
|
0
|
0
|
|
|
|
|
if ($dist{$u} + $wt[$n] < $dist{$v}) {
|
99
|
0
|
|
|
|
|
|
$dist{$v} = $dist{$u} + $wt[$n];
|
100
|
0
|
|
|
|
|
|
$prev{$v} = $u;
|
101
|
|
|
|
|
|
|
}
|
102
|
|
|
|
|
|
|
}
|
103
|
|
|
|
|
|
|
}
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
##### print the solutions ######
|
108
|
0
|
|
|
|
|
|
$p = 0;
|
109
|
0
|
|
|
|
|
|
$path;
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
format STDOUT =
|
112
|
|
|
|
|
|
|
@<<<<<<<<<<<<<< @>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
113
|
|
|
|
|
|
|
$p, $dist{$p}, $path;
|
114
|
|
|
|
|
|
|
.
|
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
foreach $p (@node) {
|
117
|
0
|
|
|
|
|
|
$t = $p;
|
118
|
0
|
|
|
|
|
|
$path = $t;
|
119
|
0
|
|
|
|
|
|
while ($t ne Null) { $t = $prev{$t}; $path = "$t -> " . $path; }
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
write;
|
121
|
|
|
|
|
|
|
}
|
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
$n = $destination;
|
124
|
0
|
|
|
|
|
|
$t = $n;
|
125
|
0
|
|
|
|
|
|
$path = $t;
|
126
|
0
|
|
|
|
|
|
print "\nThe required solution:\n";
|
127
|
0
|
|
|
|
|
|
while ($t ne Null) { $t = $prev{$t}; $path = "$t -> " . $path; }
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
write;
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
##########writing to output file
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
format FILE2 =
|
135
|
|
|
|
|
|
|
@<<<<<<<<<<<<<< @>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
136
|
|
|
|
|
|
|
$n, $dist{$n}, $path;
|
137
|
|
|
|
|
|
|
.
|
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
foreach $n (@node) {
|
140
|
0
|
|
|
|
|
|
$t = $n;
|
141
|
0
|
|
|
|
|
|
$path = $t;
|
142
|
0
|
|
|
|
|
|
while ($t ne Null) { $t = $prev{$t}; $path = "$t -> " . $path; }
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
write FILE2;
|
144
|
|
|
|
|
|
|
}
|
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
$n = $destination;
|
147
|
0
|
|
|
|
|
|
$t = $n;
|
148
|
0
|
|
|
|
|
|
$path = $t;
|
149
|
0
|
|
|
|
|
|
print FILE2 "\nThe required solution:\n";
|
150
|
0
|
|
|
|
|
|
while ($t ne Null) { $t = $prev{$t}; $path = "$t -> " . $path; }
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
write FILE2;
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
}
|
154
|
|
|
|
|
|
|
}
|
155
|
|
|
|
|
|
|
1;
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__
|