blib/lib/Graph/Bipartite.pm

filter: Statementstmt 19.6% 20 / 102 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 22.2% 2 / 9 Podpod 0.0% 0 / 4 totaltotal 12.0% 22 / 183 CC 33 SCAR 48.6
CC
33
Cov
12%
_sbfs
240.0
_rec_sdfs
132.0
maximum_matching
30.0
CRAP 128.4
↑ Summary
1 package Graph::Bipartite;
2 # $Id: Bipartite.pm,v 1.1 2003/05/25 15:03:20 detzold Exp $
3  
4 1 require 5.005_62;
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9  
10 1 our @ISA = qw(Exporter);
11  
12 # Items to export into callers namespace by default. Note: do not export
13 # names by default without a very good reason. Use EXPORT_OK instead.
14 # Do not simply export all your public functions/methods/constants.
15  
16 # This allows declaration use Graph::Bipartite ':all';
17 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18 # will save memory.
19 1 our %EXPORT_TAGS = ( 'all' => [ qw(
20
21 ) ] );
22  
23 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24  
25 1 our @EXPORT = qw(
26
27 );
28 1 our $VERSION = '0.01';
29  
30  
31 # Preloaded methods go here.
32 1 my $n1;
33 my $n2;
34 1 my $n;
35 1 my @neighbours;
36  
37 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
38 0 $n1 = $_[ 1 ];
39 0 $n2 = $_[ 2 ];
40 0 $n = $n1 + $n2;
41 0 ▶ for( my $i = 0; $i < $n; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
42 0 $neighbours[ $i ] = [];
43 }
44 0 my $class = shift;
45 0 my $self = { };
46 0 bless( $self, $class );
47 0 return $self;
48 }
49  
50 0 ▶sub insert_edge {
Subroutine insert_edge: not called CC=1 SCAR=6.9
Pod undocumented
51 0 push( @{ $neighbours[ $_[ 1 ] ] }, $_[ 2 ] );
52 0 push( @{ $neighbours[ $_[ 2 ] ] }, $_[ 1 ] );
53 }
54  
55 0 ▶sub neighbours {
Subroutine neighbours: not called CC=2 SCAR=17.9
Pod undocumented
56 0 ▶ if( scalar( @{ $neighbours[ $_[ 1 ] ] } ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$neighbours[$_[1]];} > 0) 0 0 0
57 0 return scalar( @{ $neighbours[ $_[ 1 ] ] } );
58 }
59 0 0;
60 }
61  
62 1 my @matching;
63  
64 0 ▶sub maximum_matching {
Subroutine maximum_matching: not called CC=5 SCAR=34.0
Pod undocumented
65 0 ▶ for( my $i = 0; $i < $n; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
66 0 $matching[ $i ] = -1;
67 }
68 0 ▶ while( _sbfs() > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sbfs() > 0) 0 0 0
69 0 _sdfs();
70 }
71 0 my %h;
72 0 ▶ for( my $i = 0; $i < $n1; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n1) 0 0 0
73 0 ▶ if( $matching[ $i ] != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$i] != -1) 0 0 0
74 0 $h{ $i } = $matching[ $i ];
75 }
76 }
77 0 %h;
78 }
79  
80 1 my @level;
81  
82 0 ▶sub _sbfs {
Subroutine _sbfs: not called CC=15 SCAR=54.8
83 0 my @queue1;
84 my @queue2;
85 0 ▶ for( my $i = 0; $i < $n1; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n1) 0 0 0
86 0 ▶ if( $matching[ $i ] == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$i] == -1) { } 0 0 0
87 0 $level[ $i ] = 0;
88 0 push( @queue1, $i );
89 } else {
90 0 $level[ $i ] = -1;
91 }
92 }
93 0 ▶ for( my $i = $n1; $i < $n; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
94 0 $level[ $i ] = -1;
95 }
96 0 ▶ while( scalar( @queue1 ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @queue1 > 0) 0 0 0
97 0 $#queue2 = -1;
98 0 my $free = 0;
99 0 ▶ while( scalar( @queue1 ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @queue1 > 0) 0 0 0
100 0 my $v = pop( @queue1 );
101 0 for my $w ( @{ $neighbours[ $v ] } ) {
102 0 ▶ if( $matching[ $v ] != $w && $level[ $w ] == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$v] != $w and $level[$w] == -1) 0 0 0
Condition0%0/3
$matching[$v] != $w and $level[$w] == -1
!ll&&!rl&&r
000
MC/DC0%0/2
$matching[$v] != $w and $level[$w] == -1
$matching[$v] != $w $level[$w] == -1
Truth table0%0/3
A and B
A= $matching[$v] != $wB= $level[$w] == -1
ABresult
0X0
100
111
103 0 $level[ $w ] = $level[ $v ] + 1;
104 0 push( @queue2, $w );
105 0 ▶ if( $matching[ $w ] == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$w] == -1) 0 0 0
106 0 $free = $w;
107 }
108 }
109 }
110 }
111 0 ▶ if( $free > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($free > 0) 0 0 0
112 0 return 1;
113 }
114 0 $#queue1 = -1;
115 0 ▶ while( scalar( @queue2 ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @queue2 > 0) 0 0 0
116 0 my $v = pop( @queue2 );
117 0 for my $w ( @{ $neighbours[ $v ] } ) {
118 0 ▶ if( $matching[ $v ] == $w && $level[ $w ] == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$v] == $w and $level[$w] == -1) 0 0 0
Condition0%0/3
$matching[$v] == $w and $level[$w] == -1
!ll&&!rl&&r
000
MC/DC0%0/2
$matching[$v] == $w and $level[$w] == -1
$matching[$v] == $w $level[$w] == -1
Truth table0%0/3
A and B
A= $matching[$v] == $wB= $level[$w] == -1
ABresult
0X0
100
111
119 0 $level[ $w ] = $level[ $v ] + 1;
120 0 push( @queue1, $w );
121 }
122 }
123 }
124 }
125 0 0;
126 }
127  
128 0 ▶sub _sdfs {
Subroutine _sdfs: not called CC=3 SCAR=24.8
129 0 ▶ for( my $i = 0; $i < $n1; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n1) 0 0 0
130 0 ▶ if( $matching[ $i ] == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$i] == -1) 0 0 0
131 0 _rec_sdfs( $i );
132 }
133 }
134 }
135  
136 0 ▶sub _rec_sdfs {
Subroutine _rec_sdfs: not called CC=11 SCAR=48.8
137 0 my $u = $_[ 0 ];
138 0 ▶ if( $u < $n1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u < $n1) { } 0 0 0
139 0 for my $w ( @{ $neighbours[ $u ] } ) {
140 0 ▶ if( $matching[ $u ] != $w && $level[ $w ] == $level[ $u ] + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$u] != $w and $level[$w] == $level[$u] + 1) 0 0 0
Condition0%0/3
$matching[$u] != $w and $level[$w] == $level[$u] + 1
!ll&&!rl&&r
000
MC/DC0%0/2
$matching[$u] != $w and $level[$w] == $level[$u] + 1
$matching[$u] != $w $level[$w] == $level[$u] + 1
Truth table0%0/3
A and B
A= $matching[$u] != $wB= $level[$w] == $level[$u] + 1
ABresult
0X0
100
111
141 0 ▶ if( _rec_sdfs( $w ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_rec_sdfs($w) == 1) 0 0 0
142 0 $matching[ $u ] = $w;
143 0 $matching[ $w ] = $u;
144 0 $level[ $u ] = -1;
145 0 return 1;
146 }
147 }
148 }
149 } else {
150 0 ▶ if( $matching[ $u ] == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$u] == -1) { } 0 0 0
151 0 $level[ $u ] = -1;
152 0 return 1;
153 } else {
154 0 for my $w ( @{ $neighbours[ $u ] } ) {
155 0 ▶ if( $matching[ $u ] == $w && $level[ $w ] == $level[ $u ] + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching[$u] == $w and $level[$w] == $level[$u] + 1) 0 0 0
Condition0%0/3
$matching[$u] == $w and $level[$w] == $level[$u] + 1
!ll&&!rl&&r
000
MC/DC0%0/2
$matching[$u] == $w and $level[$w] == $level[$u] + 1
$matching[$u] == $w $level[$w] == $level[$u] + 1
Truth table0%0/3
A and B
A= $matching[$u] == $wB= $level[$w] == $level[$u] + 1
ABresult
0X0
100
111
156 0 ▶ if( _rec_sdfs( $w ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_rec_sdfs($w) == 1) 0 0 0
157 0 $level[ $u ] = -1;
158 0 return 1;
159 }
160 }
161 }
162 }
163 }
164 0 $level[ $u ] = -1;
165 0 0;
166 }
167  
168 1 1;
169 __END__
↑ Summary