blib/lib/Graph/SSSR.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 109 CC 15 SCAR 43.5
CC
15
Cov
0%
detect_rings
132.0
sort_ring_elements
20.0
get_cycle_base
6.0
CRAP 77.5
1 package Graph::SSSR;
2  
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: Find Smallest Set of Smallest Rings in graphs
7 our $VERSION = '0.1.0'; # VERSION
8  
9 sub get_SSSR
10 {
11 my( $graph, $max_depth ) = @_;
12  
13 return
14 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 {
22 my( $graph, $max_depth ) = @_;
23  
24 my @SSSR = get_SSSR( $graph, $max_depth );
25 my %edge_participation;
26 for my $cycle (@SSSR) {
27 for my $i (0..$#$cycle) {
28 my $edge = join '', $cycle->[$i % @$cycle],
29 $cycle->[($i+1) % @$cycle];
30 $edge_participation{$edge} = [] unless $edge_participation{$edge};
31 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 {
40 my ( $graph, $atom, $original_atom, $previous_atom,
41 $level, $seen_atoms ) = @_;
42  
43 return () if defined $level && !$level;
44  
45 $seen_atoms = {} unless defined $seen_atoms;
46 $original_atom = $atom unless defined $original_atom;
47  
48 my %seen_atoms = ( %$seen_atoms,
49 $atom => { atom => $atom,
50 position => scalar keys %$seen_atoms } );
51  
52 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 for my $neighbour_atom ( $graph->neighbours( $atom ) ) {
59 next if $neighbour_atom eq $original_atom;
60 next if defined $previous_atom && $previous_atom eq $neighbour_atom;
61 next if !exists $seen_atoms->{$neighbour_atom};
62  
63 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 if( scalar keys %seen_atoms > 2 ) {
73 for my $neighbour_atom ( $graph->neighbours( $atom ) ) {
74 next if $neighbour_atom ne $original_atom;
75  
76 # Detect a ring:
77  
78 my @sorted_ring =
79 sort_ring_elements( map { $seen_atoms{$_}->{atom} }
80 sort { $seen_atoms{$a}->{position} <=>
81 $seen_atoms{$b}->{position} }
82 keys %seen_atoms );
83 return @rings, \@sorted_ring;
84 }
85 }
86  
87 # Descend the new path in the neighbourhood graph:
88 for my $neighbour_atom ( $graph->neighbours( $atom ) ) {
89 next if exists $seen_atoms->{$neighbour_atom};
90
91 push @rings,
92 detect_rings( $graph,
93 $neighbour_atom,
94 $original_atom,
95 $atom,
96 defined $level ? $level - 1 : undef,
97 \%seen_atoms );
98 }
99  
100 return @rings;
101 }
102  
103 sub sort_ring_elements
104 {
105 my( @elements ) = @_;
106  
107 return @elements if scalar @elements <= 1;
108  
109 my $min_index;
110 my $reverse;
111 for my $i (0..$#elements) {
112 next if defined $min_index && $elements[$i] ge
113 $elements[$min_index];
114 $min_index = $i;
115 $reverse = $elements[($i-1) % scalar @elements] lt
116 $elements[($i+1) % scalar @elements];
117 }
118  
119 if( $reverse ) {
120 @elements = reverse @elements;
121 $min_index = $#elements - $min_index;
122 }
123  
124 return @elements[$min_index..$#elements],
125 @elements[0..$min_index-1];
126 }
127  
128 1;