| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $level and not $level
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
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 108 | ▶ | $seen_atoms = {} unless defined $seen_atoms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 108 | ▶ | $original_atom = $atom unless defined $original_atom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 60 | 180 | ▶ | next if defined $previous_atom && $previous_atom eq $neighbour_atom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $previous_atom and $previous_atom eq $neighbour_atom
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
| |||||||||||||||||||||||||||||
| 61 | 108 | ▶ | next if !exists $seen_atoms->{$neighbour_atom}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 73 | 72 | for my $neighbour_atom ( $graph->neighbours( $atom ) ) { | |||||||||||||||||||||||||||
| 74 | 138 | ▶ | next if $neighbour_atom ne $original_atom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 108 | ▶ | push @rings, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $min_index and $elements[$i] ge $elements[$min_index]
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]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||