37,742
社区成员
发帖
与我相关
我的任务
分享package Graph::Base;
use vars qw(@ISA);
require Exporter;
@ISA = qw(Exporter);use vars qw(@ISA);
require Exporter;
@ISA = qw(Exporter);use base qw(Exporter);package Graph::Base;
use vars qw(@ISA);
require Exporter;
@ISA = qw(Exporter);
# new
#
# $G = Graph->new(@V)
#
# Returns a new graph $G with the optional vertices @V.
#
sub new {
my $class = shift;
my $G = { };
bless $G, $class;
$G->add_vertices(@_) if @_;
return $G;
}
Page 291
# add_vertices
#
# $G = $G->add_vertices(@v)
#
# Adds the vertices to the graph $G, returns the graph.
#
sub add_vertices {
my ($G, @v) = @_;
@{ $G->{ V } }{ @v } = @v;
return $G;
}
# add_vertex
#
# $G = $G->add_vertex($v)
#
# Adds the vertex $v to the graph $G, returns the graph.
#
sub add_vertex {
my ($G, $v) = @_;
return $G->add_vertices($v);
}
# vertices
#
# @V = $G->vertices
#
# In list context returns the vertices @V of the graph $G.
# In scalar context (implicitly) returns the number of the vertices.
#
sub vertices {
my $G = shift;
my @V = exists $G->{ V } ? values %{ $G->{ V } } : ();
return @V;
}
# has_vertex
#
# $b = $G->has_vertex($v)
#
# Returns true if the vertex $v exists in
# the graph $G and false if it doesn't.
#
sub has_vertex {
my ($G, $v) = @_;
return exists $G->{ V }->{ $v };
}