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