MoreUtils/SSSR.pm

filter: Statementstmt 81.1% 43 / 53 Branchbran 85.7% 24 / 28 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 4 totaltotal 82.0% 87 / 106 CC 24 SCAR 25.6
CC
24
Cov
84%
get_cycle_base
20.0
detect_rings
16.0
sort_ring_elements
6.0
CRAP 12.9
« MoreUtils/Line.pm ↑ Summary MoreUtils/Smooth/Intermediate.pm »
1 package Graph::MoreUtils::SSSR;
2  
3 # ABSTRACT: Find the Smallest Set of Smallest Rings in graph
4 8 our $VERSION = '0.3.0'; # VERSION
5  
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub SSSR
10 2 ▶{
Subroutine SSSR: called CC=1 SCAR=0.0
Pod undocumented
11 2 my( $graph, $max_depth ) = @_;
12  
13 return
14 2 map { detect_rings( $graph, $_, undef, undef, $max_depth ) }
15 $graph->vertices;
16 }
17  
18 # This subroutine will return cycle base not containing 1-vertex-connected graphs.
19 # TODO: Finish
20 sub get_cycle_base
21 0 ▶{
Subroutine get_cycle_base: not called CC=4 SCAR=30.0
Pod undocumented
22 0 my( $graph, $max_depth ) = @_;
23  
24 0 my @SSSR = SSSR( $graph, $max_depth );
25 0 my %edge_participation;
26 0 for my $cycle (@SSSR) {
27 0 for my $i (0..$#$cycle) {
28 0 my $edge = join '', $cycle->[$i % @$cycle],
29 $cycle->[($i+1) % @$cycle];
30 0 ▶ $edge_participation{$edge} = [] unless $edge_participation{$edge};
Branch: 1 branch
BranchTrueFalseTotal
unless $edge_participation{$edge} 0 0 0
31 0 push @{$edge_participation{$edge}}, $cycle;
32 }
33 }
34  
35 # TODO: Cycle through all mutual edges and perform cycle addition
36 }
37  
38 sub detect_rings
39 120 ▶{
Subroutine detect_rings: called CC=16 SCAR=27.7
Pod undocumented
40 120 my ( $graph, $atom, $original_atom, $previous_atom,
41 $level, $seen_atoms ) = @_;
42  
43 120 ▶ return () if defined $level && !$level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $level and not $level 12 108 120
Condition100%3/3
defined $level and not $level
!ll&&!rl&&r
664212
MC/DC100%2/2
defined $level and not $level
defined $level not $level
Truth table100%3/3
A and not(B)
A= defined $levelB= not $level
ABresult
0X0
100
111
44  
45 108 ▶ $seen_atoms = {} unless defined $seen_atoms;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seen_atoms 12 96 108
46 108 ▶ $original_atom = $atom unless defined $original_atom;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $original_atom 12 96 108
47  
48 108 my %seen_atoms = ( %$seen_atoms,
49 $atom => { atom => $atom,
50 position => scalar keys %$seen_atoms } );
51  
52 108 my @rings;
53  
54 # First, look if we have Nachbarpunkte of the current path
55 # _different_ from the original atom. If yes, we will discard this
56 # cycle since it could be closed in a shorter way:
57  
58 108 for my $neighbour_atom ( $graph->neighbours( $atom ) ) {
59 216 ▶ next if $neighbour_atom eq $original_atom;
Branch: 1 branch
BranchTrueFalseTotal
if $neighbour_atom eq $original_atom 36 180 216
60 180 ▶ next if defined $previous_atom && $previous_atom eq $neighbour_atom;
Branch: 1 branch
BranchTrueFalseTotal
if defined $previous_atom and $previous_atom eq $neighbour_atom 72 108 180
Condition100%3/3
defined $previous_atom and $previous_atom eq $neighbour_atom
!ll&&!rl&&r
248472
MC/DC100%2/2
defined $previous_atom and $previous_atom eq $neighbour_atom
defined $previous_atom $previous_atom eq $neighbour_atom
Truth table100%3/3
A and B
A= defined $previous_atomB= $previous_atom eq $neighbour_atom
ABresult
0X0
100
111
61 108 ▶ next if !exists $seen_atoms->{$neighbour_atom};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $seen_atoms->{$neighbour_atom} 108 0 108
62  
63 0 return @rings;
64 }
65  
66 # If no Nachbarpunkte are found in the previous search, let's look
67 # if we can close the ring. If we do so, we set the
68 # $Nachbarpunkte_detected flag, so that the search for rings does
69 # not go on (the current atom and the original atom would be
70 # Nachbarpunkte in any larger cycle containing the current path:
71  
72 108 ▶ if( scalar keys %seen_atoms > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %seen_atoms > 2) 72 36 108
73 72 for my $neighbour_atom ( $graph->neighbours( $atom ) ) {
74 138 ▶ next if $neighbour_atom ne $original_atom;
Branch: 1 branch
BranchTrueFalseTotal
if $neighbour_atom ne $original_atom 126 12 138
75  
76 # Detect a ring:
77  
78 my @sorted_ring =
79 72 sort_ring_elements( map { $seen_atoms{$_}->{atom} }
80 12 sort { $seen_atoms{$a}->{position} <=>
81 103 $seen_atoms{$b}->{position} }
82 keys %seen_atoms );
83 12 return @rings, \@sorted_ring;
84 }
85 }
86  
87 # Descend the new path in the neighbourhood graph:
88 96 for my $neighbour_atom ( $graph->neighbours( $atom ) ) {
89 192 ▶ next if exists $seen_atoms->{$neighbour_atom};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen_atoms->{$neighbour_atom} 84 108 192
90
91 108 ▶ push @rings,
Branch: 1 branch
BranchTrueFalseTotal
defined $level ? : 48 60 108
92 detect_rings( $graph,
93 $neighbour_atom,
94 $original_atom,
95 $atom,
96 defined $level ? $level - 1 : undef,
97 \%seen_atoms );
98 }
99  
100 96 return @rings;
101 }
102  
103 sub sort_ring_elements
104 12 ▶{
Subroutine sort_ring_elements: called CC=6 SCAR=17.9
Pod undocumented
105 12 my( @elements ) = @_;
106  
107 12 ▶ return @elements if scalar @elements <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @elements <= 1 0 12 12
108  
109 12 my $min_index;
110 my $reverse;
111 12 for my $i (0..$#elements) {
112 72 ▶ next if defined $min_index && $elements[$i] ge
Branch: 1 branch
BranchTrueFalseTotal
if defined $min_index and $elements[$i] ge $elements[$min_index] 40 32 72
Condition100%3/3
defined $min_index and $elements[$i] ge $elements[$min_index]
!ll&&!rl&&r
122040
MC/DC100%2/2
defined $min_index and $elements[$i] ge $elements[$min_index]
defined $min_index $elements[$i] ge $elements[$min_index]
Truth table100%3/3
A and B
A= defined $min_indexB= $elements[$i] ge $elements[$min_index]
ABresult
0X0
100
111
113 $elements[$min_index];
114 32 $min_index = $i;
115 32 $reverse = $elements[($i-1) % scalar @elements] lt
116 $elements[($i+1) % scalar @elements];
117 }
118  
119 12 ▶ if( $reverse ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) 6 6 12
120 6 @elements = reverse @elements;
121 6 $min_index = $#elements - $min_index;
122 }
123  
124 12 return @elements[$min_index..$#elements],
125 @elements[0..$min_index-1];
126 }
127  
128 8 1;
« MoreUtils/Line.pm ↑ Summary MoreUtils/Smooth/Intermediate.pm »