File Coverage

blib/lib/Graph/Subgraph.pm
Criterion Covered Total %
statement 34 34 100.0
branch 12 12 100.0
condition 5 6 83.3
subroutine 6 6 100.0
pod 1 1 100.0
total 58 59 98.3


line stmt bran cond sub pod time code
1 7     7   348164 use warnings;
  7         18  
  7         254  
2 7     7   44 use strict;
  7         11  
  7         496  
3              
4             package Graph::Subgraph;
5              
6             =head1 NAME
7              
8             Graph::Subgraph - A subgraph() method for Graph module.
9              
10             =head1 VERSION
11              
12             Version 0.02
13              
14             =cut
15              
16             our $VERSION = '0.0204';
17              
18             =head1 SYNOPSIS
19              
20             use Graph;
21             use Graph::Subgraph;
22              
23             my $foo = Graph->new();
24             $foo->add_edges(qw(x y y z));
25             my $bar = $foo->subgraph(['x', 'y']);
26             # $bar is now 'x-y'
27              
28             =head1 METHODS
29              
30             The only method resides in the Graph package (not Graph::Subgraph)
31             so that any descendant of Graph can call it.
32              
33             =head2 subgraph( \@src, [ \@dst ] );
34              
35             =head2 subgraph( @src );
36              
37             Returns a subgraph of the original graph induced by two sets of vertices.
38              
39             A vertex is copied if and only if it belongs to one of the sets. An edge is
40             copied if and only if it starts in the first set and ends in the second set.
41              
42             If only one set is given, it is used as both. (So that is "subgraph induced
43             by a set of vertices").
44              
45             The sets may be given as one or two array references, or list.
46              
47             The properties of the original graph (directedness etc.) are preserved,
48             however the properties of vertices and edges are not.
49              
50             B This method has a computational complexity of O(N(src)*N(dst)).
51              
52             In theory, O(N(egdes_in_initial_graph)) is also possible, and the algorithm
53             should choose whichever is better. This is not done yet.
54              
55             Feel free to file a bug report if there's anything faster.
56              
57             =cut
58              
59 7     7   39 use Carp;
  7         29  
  7         923  
60 7     7   8434 use Graph;
  7         995896  
  7         2008  
61              
62             sub subgraph {
63 22     22 1 14122 my $self = shift;
64 22         36 my ($src, $dst);
65 22 100       70 if (!ref $_[0]) {
66 2         10 $src = $dst = [ @_ ];
67             # no check here
68             } else {
69 20         135 $src = shift;
70 20   66     104 $dst = shift || $src;
71 20 100       102 croak "Extra arguments in subgraph"
72             if @_;
73 19 100 100     149 croak "Arguments of subgraph must be array references"
74             unless ref $src eq 'ARRAY' and ref $dst eq 'ARRAY';
75             };
76              
77             # Now we'll use undocumented feature of Graph.
78             # As the source tells, new() will copy properties but not vertices/edges
79             # if called this way
80 18         81 my $subg = $self->new;
81              
82             # iterate over $src and $dst sets, copying edges when needed
83 18         4017 foreach my $s (@$src) {
84 148 100       1801 $self->has_vertex($s) or next;
85 44         366 $subg->add_vertex($s);
86 44         1154 my @edges;
87 44         82 foreach my $d (@$dst) {
88 112 100       1736 $self->has_edge($s, $d) and push @edges, $s, $d;
89             };
90 44         669 $subg->add_edges(@edges); # don't call too often, keep memory usage linear
91             };
92              
93             # now add orphaned vertices from the dst set
94 18         416 foreach my $d (@$dst) {
95 156 100       2034 $self->has_vertex($d) and $subg->add_vertex($d);
96             };
97              
98 18         435 return $subg;
99             }
100              
101             # FIXME UGLY HACK
102             # Now plant the subgraph method into Graph.
103             # Warn if method is present in Graph, but still override it
104             carp "Found subgraph method in Graph, Graph::Subgraph is now deprecated"
105             if Graph->can('subgraph');
106              
107             {
108 7     7   69 no warnings 'redefine', 'once'; ## no critic
  7         16  
  7         1956  
109             *Graph::subgraph = \&subgraph;
110             };
111             =head1 AUTHOR
112              
113             Konstantin S. Uvarin, C<< >>
114              
115             =head1 BUGS
116              
117             This module should be merged into Graph.
118              
119             Please report any bugs or feature requests to C, or through
120             the web interface at L. I will be notified, and then you'll
121             automatically be notified of progress on your bug as I make changes.
122              
123             =head1 SUPPORT
124              
125             You can find documentation for this module with the perldoc command.
126              
127             perldoc Graph::Subgraph
128              
129              
130             You can also look for information at:
131              
132             =over 4
133              
134             =item * github
135              
136             L
137              
138             =item * RT: CPAN's request tracker
139              
140             L
141              
142             =item * AnnoCPAN: Annotated CPAN documentation
143              
144             L
145              
146             =item * CPAN Ratings
147              
148             L
149              
150             =item * Search CPAN
151              
152             L
153              
154             =back
155              
156             =head1 LICENSE AND COPYRIGHT
157              
158             Copyright 2012 Konstantin S. Uvarin.
159              
160             This program is free software; you can redistribute it and/or modify it
161             under the terms of either: the GNU General Public License as published
162             by the Free Software Foundation; or the Artistic License.
163              
164             See http://dev.perl.org/licenses/ for more information.
165              
166             =cut
167              
168             1; # End of Graph::Subgraph