File Coverage

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


line stmt bran cond sub pod time code
1             package Graph::Subgraph;
2              
3 7     7   495458 use warnings;
  7         51  
  7         196  
4 7     7   29 use strict;
  7         11  
  7         248  
5             our $VERSION = '0.03';
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         9  
  7         316  
57 7     7   2665 use parent 'Graph';
  7         1678  
  7         30  
58              
59             sub subgraph {
60 7     7 1 2850 my $self = shift;
61 7         11 my ($src, $dst);
62 7 100       17 if (!ref $_[0]) {
63 2         5 $src = $dst = [ @_ ];
64             # no check here
65             } else {
66 5         7 $src = shift;
67 5   66     13 $dst = shift || $src;
68 5 100       28 croak "Extra arguments in subgraph"
69             if @_;
70 4 100 100     33 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 3         8 my $subg = $self->new;
78              
79             # iterate over $src and $dst sets, copying edges when needed
80 3         600 foreach my $s (@$src) {
81 6 50       28 $self->has_vertex($s) or next;
82 6         38 $subg->add_vertex($s);
83 6         154 my @edges;
84 6         11 foreach my $d (@$dst) {
85 18 50       208 $self->has_edge($s, $d) and push @edges, $s, $d;
86             };
87 6         91 $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 3         11 foreach my $d (@$dst) {
92 6 50       114 $self->has_vertex($d) and $subg->add_vertex($d);
93             };
94              
95 3         57 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 "Found Graph->subgraph, it is safe to remove deprecated 'use Graph::Subgraph;' now";
104             } else {
105             carp "Graph::Subgraph is deprecated, please upgrade Graph to >=0.97";
106 7     7   550628 no warnings 'redefine', 'once'; ## no critic
  7         16  
  7         523  
107             *Graph::subgraph = \&subgraph;
108             };
109              
110             =head1 AUTHOR
111              
112             Konstantin S. Uvarin, C<< >>
113              
114             =head1 BUGS
115              
116             This module should be merged into Graph.
117              
118             Please report any bugs or feature requests to C, or through
119             the web interface at L. I will be notified, and then you'll
120             automatically be notified of progress on your bug as I make changes.
121              
122             =head1 SUPPORT
123              
124             You can find documentation for this module with the perldoc command.
125              
126             perldoc Graph::Subgraph
127              
128              
129             You can also look for information at:
130              
131             =over 4
132              
133             =item * github
134              
135             L
136              
137             =item * RT: CPAN's request tracker
138              
139             L
140              
141             =item * AnnoCPAN: Annotated CPAN documentation
142              
143             L
144              
145             =item * CPAN Ratings
146              
147             L
148              
149             =item * Search CPAN
150              
151             L
152              
153             =back
154              
155             =head1 LICENSE AND COPYRIGHT
156              
157             Copyright 2012 Konstantin S. Uvarin.
158              
159             This program is free software; you can redistribute it and/or modify it
160             under the terms of either: the GNU General Public License as published
161             by the Free Software Foundation; or the Artistic License.
162              
163             See http://dev.perl.org/licenses/ for more information.
164              
165             =cut
166              
167             1; # End of Graph::Subgraph