1 package Graph
::BitMatrix
;
5 # $SIG{__DIE__ } = sub { use Carp; confess };
6 # $SIG{__WARN__} = sub { use Carp; confess };
8 sub _V
() { 2 } # Graph::_V()
9 sub _E
() { 3 } # Graph::_E()
10 sub _i
() { 3 } # Index to path.
11 sub _s
() { 4 } # Successors / Path to Index.
14 my ($class, $g, %opt) = @_;
17 my $Z = "\0" x
(($V + 7) / 8);
18 my %V; @V{ @V } = 0 .. $#V;
19 my $bm = bless [ [ ( $Z ) x
$V ], \
%V ], $class;
22 if (exists $opt{connect_edges
}) {
23 $connect_edges = $opt{connect_edges
};
24 delete $opt{connect_edges
};
26 $connect_edges = 1 unless defined $connect_edges;
27 Graph
::_opt_unknown
(\
%opt);
29 # for (my $i = 0; $i <= $#V; $i++) {
31 # for (my $j = 0; $j <= $#V; $j++) {
32 # vec($bm0->[$i], $j, 1) = 1 if $g->has_edge($u, $V[$j]);
35 my $Vi = $g->[_V
]->[_i
];
36 my $Ei = $g->[_E
]->[_i
];
37 if ($g->is_undirected) {
38 for my $e (keys %{ $Ei }) {
39 my ($i0, $j0) = @
{ $Ei->{ $e } };
40 my $i1 = $V{ $Vi->{ $i0 } };
41 my $j1 = $V{ $Vi->{ $j0 } };
42 vec($bm0->[$i1], $j1, 1) = 1;
43 vec($bm0->[$j1], $i1, 1) = 1;
46 for my $e (keys %{ $Ei }) {
47 my ($i0, $j0) = @
{ $Ei->{ $e } };
48 vec($bm0->[$V{ $Vi->{ $i0 } }], $V{ $Vi->{ $j0 } }, 1) = 1;
57 my ($i, $j) = map { $m->[1]->{ $_ } } ($u, $v);
58 vec($m->[0]->[$i], $j, 1) = 1 if defined $i && defined $j;
63 my ($i, $j) = map { $m->[1]->{ $_ } } ($u, $v);
64 vec($m->[0]->[$i], $j, 1) = 0 if defined $i && defined $j;
69 my ($i, $j) = map { $m->[1]->{ $_ } } ($u, $v);
70 defined $i && defined $j ?
vec($m->[0]->[$i], $j, 1) : undef;
74 my ($m, $u) = splice @_, 0, 2;
78 return unless defined $i;
81 vec($m0->[$i], $j, 1) = 1 if defined $j;
86 my ($m, $u) = splice @_, 0, 2;
90 return unless defined $i;
93 vec($m0->[$i], $j, 1) = 0 if defined $j;
98 my ($m, $u) = splice @_, 0, 2;
102 return () x
@_ unless defined $i;
106 push @r, defined $j ?
(vec($m0->[$i], $j, 1) ?
1 : 0) : undef;
112 my ($m, $u, $v) = @_;
122 Graph::BitMatrix - create and manipulate a V x V bit matrix of graph G
126 use Graph::BitMatrix;
128 my $g = Graph::Directed->new;
129 $g->add_...(); # build $g
130 my $m = Graph::BitMatrix->new($g, %opt);
134 $m->get_row($u, $v1, $v2, ..., $vn)
135 $m->set_row($u, $v1, $v2, ..., $vn)
136 $m->unset_row($u, $v1, $v2, ..., $vn)
141 This class enables creating bit matrices that compactly describe
142 the connected of the graphs.
150 Create a bit matrix from a Graph $g. The C<%opt>, if present,
151 can have the following options:
159 If true or if not present, set the bits in the bit matrix that
160 correspond to edges. If false, do not set any bits. In either
161 case the bit matrix of V x V bits is allocated.
167 =head2 Object Methods
173 Return true if the bit matrix has a "one bit" between the vertices
174 $u and $v; in other words, if there is (at least one) a vertex going from
175 $u to $v. If there is no vertex and therefore a "zero bit", return false.
179 Set the bit between the vertices $u and $v; in other words, connect
180 the vertices $u and $v by an edge. The change does not get mirrored
181 back to the original graph. Returns nothing.
185 Unset the bit between the vertices $u and $v; in other words, disconnect
186 the vertices $u and $v by an edge. The change does not get mirrored
187 back to the original graph. Returns nothing.
189 =item get_row($u, $v1, $v2, ..., $vn)
191 Test the row at vertex C<u> for the vertices C<v1>, C<v2>, ..., C<vn>
192 Returns a list of I<n> truth values.
194 =item set_row($u, $v1, $v2, ..., $vn)
196 Sets the row at vertex C<u> for the vertices C<v1>, C<v2>, ..., C<vn>,
197 in other words, connects the vertex C<u> to the vertices C<vi>.
198 The changes do not get mirrored back to the original graph.
201 =item unset_row($u, $v1, $v2, ..., $vn)
203 Unsets the row at vertex C<u> for the vertices C<v1>, C<v2>, ..., C<vn>,
204 in other words, disconnects the vertex C<u> from the vertices C<vi>.
205 The changes do not get mirrored back to the original graph.
210 Return the list of vertices in the bit matrix.
216 The algorithm used to create the matrix is two nested loops, which is
217 O(V**2) in time, and the returned matrices are O(V**2) in space.
219 =head1 AUTHOR AND COPYRIGHT
221 Jarkko Hietaniemi F<jhi@iki.fi>
225 This module is licensed under the same terms as Perl itself.