Graph-MoreUtils-0.2.0/0000775000200400020040000000000014534622232014356 5ustar andriusandriusGraph-MoreUtils-0.2.0/dist.ini0000644000200400020040000000074514534622232016026 0ustar andriusandriusname = Graph-MoreUtils author = Andrius Merkys license = LGPL_3_0 copyright_holder = Andrius Merkys copyright_year = 2020-2023 version = 0.2.0 [@Filter] -bundle = @Basic -remove = License [AutoMetaResources] homepage = https://search.cpan.org/dist/%{dist} repository.github = user:merkys bugtracker.github = user:merkys [MetaJSON] [OurPkgVersion] [Prereqs] Graph = 0 Scalar::Util = 0 Set::Object = 0 [Prereqs / Test] -phase = test Test::More = 0 Graph-MoreUtils-0.2.0/LICENSE0000644000200400020040000001674314534622232015374 0ustar andriusandrius GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. Graph-MoreUtils-0.2.0/Changes0000644000200400020040000000014014534622232015642 0ustar andriusandrius0.2.0 2023-12-08 - Implementing graph_replace(). 0.1.0 2023-10-17 - Initial release. Graph-MoreUtils-0.2.0/README0000644000200400020040000000051314534622232015233 0ustar andriusandriusThis archive contains the distribution Graph-MoreUtils, version 0.2.0: Utilities for graphs This software is Copyright (c) 2020-2023 by Andrius Merkys. This is free software, licensed under: The GNU Lesser General Public License, Version 3, June 2007 This README file was generated by Dist::Zilla::Plugin::Readme v6.012. Graph-MoreUtils-0.2.0/MANIFEST0000644000200400020040000000107114534622232015504 0ustar andriusandrius# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012. Changes LICENSE MANIFEST META.json META.yml Makefile.PL README dist.ini lib/Graph/MoreUtils.pm lib/Graph/MoreUtils/Line.pm lib/Graph/MoreUtils/Line/SelfLoopVertex.pm lib/Graph/MoreUtils/Replace.pm lib/Graph/MoreUtils/SSSR.pm lib/Graph/MoreUtils/Smooth.pm lib/Graph/MoreUtils/Smooth/Intermediate.pm t/01_use.t t/SSSR/01_simple.t t/line/01_use.t t/line/02_simple.t t/line/03_type_check.t t/line/04_triangle.t t/line/05_S3.t t/line/06_multiedged.t t/smoothed/01_use.t t/smoothed/02_simple.t Graph-MoreUtils-0.2.0/META.yml0000644000200400020040000000134214534622232015625 0ustar andriusandrius--- abstract: 'Utilities for graphs' author: - 'Andrius Merkys ' build_requires: Test::More: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010' license: lgpl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Graph-MoreUtils requires: Graph: '0' Scalar::Util: '0' Set::Object: '0' resources: bugtracker: https://github.com/merkys/graph-moreutils/issues homepage: https://search.cpan.org/dist/Graph-MoreUtils repository: git://github.com/merkys/graph-moreutils.git version: 0.2.0 x_generated_by_perl: v5.30.0 x_serialization_backend: 'YAML::Tiny version 1.73' Graph-MoreUtils-0.2.0/t/0000775000200400020040000000000014534622232014621 5ustar andriusandriusGraph-MoreUtils-0.2.0/t/SSSR/0000775000200400020040000000000014534622232015413 5ustar andriusandriusGraph-MoreUtils-0.2.0/t/SSSR/01_simple.t0000644000200400020040000000036214534622232017370 0ustar andriusandrius#!/usr/bin/perl use strict; use warnings; use Graph::MoreUtils qw( SSSR ); use Graph::Undirected; use Test::More tests => 2; my $g = Graph::Undirected->new; $g->add_cycle( 'A'..'F' ); is scalar SSSR( $g ), 12; is scalar SSSR( $g, 4 ), 0; Graph-MoreUtils-0.2.0/t/line/0000775000200400020040000000000014534622232015550 5ustar andriusandriusGraph-MoreUtils-0.2.0/t/line/04_triangle.t0000644000200400020040000000052314534622232020043 0ustar andriusandrius#!/usr/bin/perl use strict; use warnings; use Graph::MoreUtils qw( line ); use Graph::Undirected; use Test::More tests => 3; my $g = Graph::Undirected->new; $g->add_edges( [ 'A', 'B' ], [ 'B', 'C' ], [ 'A', 'C' ] ); my $l = line( $g ); is( $l->vertices, 3 ); is( $l->edges, 3 ); is( (grep { $l->degree( $_ ) == 2 } $l->vertices), 3 ); Graph-MoreUtils-0.2.0/t/line/02_simple.t0000644000200400020040000000242014534622232017523 0ustar andriusandrius#!/usr/bin/perl use strict; use warnings; use Graph::MoreUtils qw( line ); use Graph::Undirected; use Test::More tests => 13; my( $g, $l, @edges ); # Path of three vertices, one edge has an attribute $g = Graph::Undirected->new; $g->add_edges( [ 'A', 'B' ], [ 'B', 'C' ] ); $g->set_edge_attribute( 'A', 'B', 'color', 'red' ); $l = line( $g ); is( $l->vertices, 2 ); is( $l->edges, 1 ); is( (grep { defined $_->{color} && $_->{color} eq 'red' } $l->vertices), 1 ); @edges = $l->edges; is( $l->get_edge_attribute( @{$edges[0]}, 'original_vertex' ), 'B' ); $l = line( $g, { loop_end_vertices => 1 } ); is( $l->vertices, 4 ); is( $l->edges, 3 ); @edges = $l->edges; is( join( ',', sort map { $l->get_edge_attribute( @$_, 'original_vertex' ) } $l->edges ), 'A,B,C' ); # Two vertices with two parallel edges $g = Graph::Undirected->new( multiedged => 1 ); $g->add_edges( [ 'A', 'B' ], [ 'A', 'B' ] ); $l = line( $g ); is( $l->vertices, 2 ); is( $l->edges, 1 ); $l = line( $g, { loop_end_vertices => 1 } ); is( $l->vertices, 2 ); is( $l->edges, 1 ); # Single vertex with three self-loops $g = Graph::Undirected->new( multiedged => 1 ); $g->add_edges( [ 'A', 'A' ], [ 'A', 'A' ], [ 'A', 'A' ] ); $l = line( $g ); is( $l->vertices, 3 ); is( $l->edges, 3 ); Graph-MoreUtils-0.2.0/t/line/06_multiedged.t0000644000200400020040000000060414534622232020363 0ustar andriusandrius#!/usr/bin/perl use strict; use warnings; use Graph::MoreUtils qw( line ); use Graph::Undirected; use Test::More tests => 5; my $g = Graph::Undirected->new( multiedged => 1 ); $g->add_edges( [ 'A', 'B' ], [ 'A', 'B' ] ); is( $g->vertices, 2 ); is( $g->edges, 2 ); my $l = line( $g ); is( $l->vertices, 2 ); is( $l->edges, 1 ); is( (grep { $l->degree( $_ ) == 1 } $l->vertices), 2 ); Graph-MoreUtils-0.2.0/t/line/03_type_check.t0000644000200400020040000000046414534622232020357 0ustar andriusandrius#!/usr/bin/perl use strict; use warnings; use Graph; use Graph::MoreUtils qw( line ); use Test::More tests => 1; my $error; eval { my $g = Graph->new; my $l = line( $g ); }; if( $@ ) { $@ =~ s/\n$//; $error = $@; } is( $error, 'only Graph::Undirected and its derivatives are accepted' ); Graph-MoreUtils-0.2.0/t/line/05_S3.t0000644000200400020040000000052314534622232016524 0ustar andriusandrius#!/usr/bin/perl use strict; use warnings; use Graph::MoreUtils qw( line ); use Graph::Undirected; use Test::More tests => 3; my $g = Graph::Undirected->new; $g->add_edges( [ 'A', 'B' ], [ 'A', 'C' ], [ 'A', 'D' ] ); my $l = line( $g ); is( $l->vertices, 3 ); is( $l->edges, 3 ); is( (grep { $l->degree( $_ ) == 2 } $l->vertices), 3 ); Graph-MoreUtils-0.2.0/t/line/01_use.t0000644000200400020040000000010714534622232017025 0ustar andriusandriususe Test::More tests => 1; BEGIN { use_ok('Graph::MoreUtils::Line') }; Graph-MoreUtils-0.2.0/t/smoothed/0000775000200400020040000000000014534622232016443 5ustar andriusandriusGraph-MoreUtils-0.2.0/t/smoothed/02_simple.t0000644000200400020040000000143714534622232020425 0ustar andriusandrius#!/usr/bin/perl use strict; use warnings; use Graph::MoreUtils qw( smooth ); use Graph::Undirected; use Test::More tests => 8; my( $g, @edges ); # Path of three vertices, one edge has an attribute $g = Graph::Undirected->new; $g->add_path( 'A'..'Z' ); smooth( $g ); is( $g->vertices, 2 ); is( $g->edges, 1 ); is( join( ',', @{$g->get_edge_attribute( 'A', 'Z', 'intermediate' )} ), join( ',', 'B'..'Y' ) ); $g = Graph::Undirected->new; $g->add_path( 'Z', 'B'..'Y', 'A' ); smooth( $g ); is( $g->vertices, 2 ); is( $g->edges, 1 ); is( join( ',', @{$g->get_edge_attribute( 'A', 'Z', 'intermediate' )} ), join( ',', reverse 'B'..'Y' ) ); $g = Graph::Undirected->new; $g->add_edges( [ 'A', 'B' ], [ 'B', 'C' ], [ 'C', 'A' ] ); smooth( $g ); is( $g->vertices, 3 ); is( $g->edges, 3 ); Graph-MoreUtils-0.2.0/t/smoothed/01_use.t0000644000200400020040000000011314534622232017715 0ustar andriusandriususe Test::More tests => 1; BEGIN { use_ok( 'Graph::MoreUtils::Smooth' ) }; Graph-MoreUtils-0.2.0/t/01_use.t0000644000200400020040000000010314534622232016072 0ustar andriusandriususe Test::More tests => 1; BEGIN { use_ok( 'Graph::MoreUtils' ) }; Graph-MoreUtils-0.2.0/Makefile.PL0000644000200400020040000000212214534622232016323 0ustar andriusandrius# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.012. use strict; use warnings; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "Utilities for graphs", "AUTHOR" => "Andrius Merkys ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "Graph-MoreUtils", "LICENSE" => "lgpl", "NAME" => "Graph::MoreUtils", "PREREQ_PM" => { "Graph" => 0, "Scalar::Util" => 0, "Set::Object" => 0 }, "TEST_REQUIRES" => { "Test::More" => 0 }, "VERSION" => "0.2.0", "test" => { "TESTS" => "t/*.t t/SSSR/*.t t/line/*.t t/smoothed/*.t" } ); my %FallbackPrereqs = ( "Graph" => 0, "Scalar::Util" => 0, "Set::Object" => 0, "Test::More" => 0 ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); Graph-MoreUtils-0.2.0/lib/0000775000200400020040000000000014534622232015124 5ustar andriusandriusGraph-MoreUtils-0.2.0/lib/Graph/0000775000200400020040000000000014534622232016165 5ustar andriusandriusGraph-MoreUtils-0.2.0/lib/Graph/MoreUtils/0000775000200400020040000000000014534622232020110 5ustar andriusandriusGraph-MoreUtils-0.2.0/lib/Graph/MoreUtils/Smooth/0000775000200400020040000000000014534622232021361 5ustar andriusandriusGraph-MoreUtils-0.2.0/lib/Graph/MoreUtils/Smooth/Intermediate.pm0000644000200400020040000000074014534622232024330 0ustar andriusandriuspackage Graph::MoreUtils::Smooth::Intermediate; # ABSTRACT: Container for intermediate vertices our $VERSION = '0.2.0'; # VERSION use strict; use warnings; use Scalar::Util qw( blessed ); sub new { my $class = shift; my $self = [ map { blessed $_ && $_->isa( Graph::MoreUtils::Smooth::Intermediate:: ) ? @$_ : $_ } @_ ]; return bless $self, $class; } sub reverse { my( $self ) = @_; @$self = reverse @$self; return $self; } 1; Graph-MoreUtils-0.2.0/lib/Graph/MoreUtils/Line.pm0000644000200400020040000000541514534622232021340 0ustar andriusandriuspackage Graph::MoreUtils::Line; # ABSTRACT: Generate line graphs our $VERSION = '0.2.0'; # VERSION use strict; use warnings; use Graph::MoreUtils::Line::SelfLoopVertex; use Graph::Undirected; use Scalar::Util qw( blessed ); sub line { my( $graph, $options ) = @_; if( !blessed $graph || !$graph->isa( Graph::Undirected:: ) ) { die 'only Graph::Undirected and its derivatives are accepted' . "\n"; } $options = {} unless $options; # Collect all edges prior to converting them to vertices: my @originals; my @new_vertices; if( $graph->is_multiedged ) { for my $unique_edge ($graph->unique_edges) { for my $edge ($graph->get_multiedge_ids( @$unique_edge )) { push @originals, $unique_edge; push @new_vertices, $graph->get_edge_attributes_by_id( @$unique_edge, $edge ) || {}; } } } else { # Have to do this in for cycle to maintain relation between the # parallel arrays: for my $edge ($graph->edges) { push @originals, $edge; push @new_vertices, $graph->get_edge_attributes( @$edge ) || {}; } } # Collect adjacent edges for every vertice my $adjacency = {}; for my $i (0..$#originals) { push @{$adjacency->{$originals[$i]->[0]}}, $new_vertices[$i]; # Self-loops have to be detected and not added once again next if $originals[$i]->[0] eq $originals[$i]->[1]; push @{$adjacency->{$originals[$i]->[1]}}, $new_vertices[$i]; } # Create the line graph my $line_graph = Graph::Undirected->new( refvertexed => 1 ); $line_graph->add_vertices( @new_vertices ); for my $vertex (sort keys %$adjacency) { for my $i (0..$#{$adjacency->{$vertex}}-1) { for my $j ($i+1..$#{$adjacency->{$vertex}}) { $line_graph->set_edge_attribute( $adjacency->{$vertex}[$i], $adjacency->{$vertex}[$j], 'original_vertex', $vertex ); } } } # Add self-loops for end vertices if requested if( $options->{loop_end_vertices} ) { for my $vertex ($graph->vertices) { next if $graph->degree( $vertex ) != 1; # Adjacency matrix will only have one item $line_graph->set_edge_attribute( $adjacency->{$vertex}[0], Graph::MoreUtils::Line::SelfLoopVertex->new, 'original_vertex', $vertex ); } } return $line_graph; } 1; Graph-MoreUtils-0.2.0/lib/Graph/MoreUtils/Smooth.pm0000644000200400020040000000401714534622232021717 0ustar andriusandriuspackage Graph::MoreUtils::Smooth; # ABSTRACT: Generate smoothed graphs our $VERSION = '0.2.0'; # VERSION use strict; use warnings; use Graph::MoreUtils::Smooth::Intermediate; use Graph::Undirected; use Scalar::Util qw( blessed ); sub smooth { my( $graph ) = @_; if( !blessed $graph || !$graph->isa( Graph::Undirected:: ) ) { die 'only Graph::Undirected and its derivatives are accepted' . "\n"; } for ($graph->vertices) { next unless $graph->degree( $_ ) == 2; my( $a, $b ) = sort $graph->neighbours( $_ ); # do not reduce cycles of three vertices: next if $graph->has_edge( $a, $b ); my $intermediate; if( $graph->has_edge_attribute( $a, $_, 'intermediate' ) && $graph->has_edge_attribute( $b, $_, 'intermediate' ) ) { $intermediate = Graph::MoreUtils::Smooth::Intermediate->new( $_ lt $a ? $graph->get_edge_attribute( $a, $_, 'intermediate' )->reverse : $graph->get_edge_attribute( $a, $_, 'intermediate' ), $_, $_ gt $b ? $graph->get_edge_attribute( $b, $_, 'intermediate' )->reverse : $graph->get_edge_attribute( $b, $_, 'intermediate' ) ); } elsif( $graph->has_edge_attribute( $a, $_, 'intermediate' ) ) { $intermediate = $graph->get_edge_attribute( $a, $_, 'intermediate' ); $intermediate->reverse if $a gt $_; # getting natural order push @$intermediate, $_; } elsif( $graph->has_edge_attribute( $b, $_, 'intermediate' ) ) { $intermediate = $graph->get_edge_attribute( $b, $_, 'intermediate' ); $intermediate->reverse if $_ gt $b; # getting natural order unshift @$intermediate, $_; } else { $intermediate = Graph::MoreUtils::Smooth::Intermediate->new( $_ ); } $graph->delete_vertex( $_ ); $graph->set_edge_attribute( $a, $b, 'intermediate', $intermediate ); } return $graph; } 1; Graph-MoreUtils-0.2.0/lib/Graph/MoreUtils/Replace.pm0000644000200400020040000000160114534622232022015 0ustar andriusandriuspackage Graph::MoreUtils::Replace; # ABSTRACT: Replace one on more vertices with a given one. our $VERSION = '0.2.0'; # VERSION use strict; use warnings; use Set::Object qw( set ); sub replace { my( $graph, $new, @old ) = @_; $graph->add_vertex( $new ); my $old = set( @old ); for my $edge (grep { ($old->has( $_->[0] ) && !$old->has( $_->[1] )) || ($old->has( $_->[1] ) && !$old->has( $_->[0] )) } $graph->edges) { my( $vertex, $neighbour ) = $old->has( $edge->[0] ) ? @$edge : reverse @$edge; next if $graph->has_edge( $new, $neighbour ); $graph->add_edge( $new, $neighbour ); next unless $graph->has_edge_attributes( @$edge ); $graph->set_edge_attributes( $new, $neighbour, $graph->get_edge_attributes( @$edge ) ); } $graph->delete_vertices( @old ); return $graph; } 1; Graph-MoreUtils-0.2.0/lib/Graph/MoreUtils/SSSR.pm0000644000200400020040000000745714534622232021253 0ustar andriusandriuspackage Graph::MoreUtils::SSSR; # ABSTRACT: Find the Smallest Set of Smallest Rings in graph our $VERSION = '0.2.0'; # VERSION use strict; use warnings; sub SSSR { my( $graph, $max_depth ) = @_; return map { detect_rings( $graph, $_, undef, undef, $max_depth ) } $graph->vertices; } # This subroutine will return cycle base not containing 1-vertex-connected graphs. # TODO: Finish sub get_cycle_base { my( $graph, $max_depth ) = @_; my @SSSR = SSSR( $graph, $max_depth ); my %edge_participation; for my $cycle (@SSSR) { for my $i (0..$#$cycle) { my $edge = join '', $cycle->[$i % @$cycle], $cycle->[($i+1) % @$cycle]; $edge_participation{$edge} = [] unless $edge_participation{$edge}; push @{$edge_participation{$edge}}, $cycle; } } # TODO: Cycle through all mutual edges and perform cycle addition } sub detect_rings { my ( $graph, $atom, $original_atom, $previous_atom, $level, $seen_atoms ) = @_; return () if defined $level && !$level; $seen_atoms = {} unless defined $seen_atoms; $original_atom = $atom unless defined $original_atom; my %seen_atoms = ( %$seen_atoms, $atom => { atom => $atom, position => scalar keys %$seen_atoms } ); my @rings; # First, look if we have Nachbarpunkte of the current path # _different_ from the original atom. If yes, we will discard this # cycle since it could be closed in a shorter way: for my $neighbour_atom ( $graph->neighbours( $atom ) ) { next if $neighbour_atom eq $original_atom; next if defined $previous_atom && $previous_atom eq $neighbour_atom; next if !exists $seen_atoms->{$neighbour_atom}; return @rings; } # If no Nachbarpunkte are found in the previous search, let's look # if we can close the ring. If we do so, we set the # $Nachbarpunkte_detected flag, so that the search for rings does # not go on (the current atom and the original atom would be # Nachbarpunkte in any larger cycle containing the current path: if( scalar keys %seen_atoms > 2 ) { for my $neighbour_atom ( $graph->neighbours( $atom ) ) { next if $neighbour_atom ne $original_atom; # Detect a ring: my @sorted_ring = sort_ring_elements( map { $seen_atoms{$_}->{atom} } sort { $seen_atoms{$a}->{position} <=> $seen_atoms{$b}->{position} } keys %seen_atoms ); return @rings, \@sorted_ring; } } # Descend the new path in the neighbourhood graph: for my $neighbour_atom ( $graph->neighbours( $atom ) ) { next if exists $seen_atoms->{$neighbour_atom}; push @rings, detect_rings( $graph, $neighbour_atom, $original_atom, $atom, defined $level ? $level - 1 : undef, \%seen_atoms ); } return @rings; } sub sort_ring_elements { my( @elements ) = @_; return @elements if scalar @elements <= 1; my $min_index; my $reverse; for my $i (0..$#elements) { next if defined $min_index && $elements[$i] ge $elements[$min_index]; $min_index = $i; $reverse = $elements[($i-1) % scalar @elements] lt $elements[($i+1) % scalar @elements]; } if( $reverse ) { @elements = reverse @elements; $min_index = $#elements - $min_index; } return @elements[$min_index..$#elements], @elements[0..$min_index-1]; } 1; Graph-MoreUtils-0.2.0/lib/Graph/MoreUtils/Line/0000775000200400020040000000000014534622232020777 5ustar andriusandriusGraph-MoreUtils-0.2.0/lib/Graph/MoreUtils/Line/SelfLoopVertex.pm0000644000200400020040000000126714534622232024262 0ustar andriusandriuspackage Graph::MoreUtils::Line::SelfLoopVertex; use strict; use warnings; # ABSTRACT: Marker for artificial self-loops our $VERSION = '0.2.0'; # VERSION sub new { my( $class ) = @_; return bless {}, $class; } 1; __END__ =pod =head1 NAME Graph::MoreUtils::Line::SelfLoopVertex - marker for artificial self-loops =head1 DESCRIPTION With C option L adds self-loops on pendant vertices thus preserving them from getting "lost". Instances of Graph::MoreUtils::Line::SelfLoopVertex are used to represent the artificial self-loops in the resulting line graphs. =head1 SEE ALSO perl(1) =head1 AUTHORS Andrius Merkys, Emerkys@cpan.orgE =cut Graph-MoreUtils-0.2.0/lib/Graph/MoreUtils.pm0000644000200400020040000000505414534622232020450 0ustar andriusandriuspackage Graph::MoreUtils; # ABSTRACT: Utilities for graphs our $VERSION = '0.2.0'; # VERSION =head1 NAME Graph::MoreUtils - utilities for graphs =head1 SYNOPSIS use Graph::MoreUtils qw( line ); use Graph::Undirected; my $G = Graph::Undirected->new; # Greate graph here # Get line graph for $G: my $L = line( $G ); =cut use strict; use warnings; use parent Exporter::; use Graph::MoreUtils::Line; use Graph::MoreUtils::Replace; use Graph::MoreUtils::SSSR; use Graph::MoreUtils::Smooth; our @EXPORT_OK = qw( SSSR graph_replace line smooth ); =head2 C Finds the Smallest Set of Smallest Rings (SSSR) in L objects. Thus it should work with any L object. The code is largely taken from the C package (L). The algorithm returns a superset of minimum cycle basis of a graph in order to produce deterministic results. As a result it does not succumb to the counterexample of oxabicyclo[2.2.2]octane (L, section "SSSR and Uniqueness"). The algorithm has means to control the maximum size of rings included in the SSSR to reduce its complexity. The default value of C stands for no limit. =cut sub SSSR { &Graph::MoreUtils::SSSR::SSSR } =head2 C Replaces one or more vertices (C<@old>) in the graph with a given one (C<$new>). All edges between the replaced vertices are removed and all edges with other vertices become a reconnected to the new one. =cut sub graph_replace { &Graph::MoreUtils::Replace::replace } =head2 C Generates line graphs for L objects. Line graph is constructed nondestructively and returned from the call. Both simple and multiedged graphs are supported. Call accepts additional options hash. Currently only one option is supported, C, which treats the input graph as having self-loops on pendant vertices, that is, increasing the degrees of vertices having degrees of 1. Thus they are not "lost" during line graph construction. In the resulting line graph these self-loops are represented as instances of L. =cut sub line { &Graph::MoreUtils::Line::line } =head2 C Smooths the given graph by collating vertices of degree 2. =cut sub smooth { &Graph::MoreUtils::Smooth::smooth } =head1 SEE ALSO perl(1) =head1 AUTHORS Andrius Merkys, Emerkys@cpan.orgE =cut 1; Graph-MoreUtils-0.2.0/META.json0000644000200400020040000000243614534622232016002 0ustar andriusandrius{ "abstract" : "Utilities for graphs", "author" : [ "Andrius Merkys " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010", "license" : [ "lgpl_3_0" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Graph-MoreUtils", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Graph" : "0", "Scalar::Util" : "0", "Set::Object" : "0" } }, "test" : { "requires" : { "Test::More" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/merkys/graph-moreutils/issues" }, "homepage" : "https://search.cpan.org/dist/Graph-MoreUtils", "repository" : { "type" : "git", "url" : "git://github.com/merkys/graph-moreutils.git", "web" : "https://github.com/merkys/graph-moreutils" } }, "version" : "0.2.0", "x_generated_by_perl" : "v5.30.0", "x_serialization_backend" : "Cpanel::JSON::XS version 4.19" }