NAME
Graph::Base - graph base class
SYNOPSIS
use Graph::Directed;use Graph::Undirected;
$d1 = new Graph;$d2 = new Graph::Directed;$u = new Graph::Undirected;
DESCRIPTION
You create new graphs by calling the "new" constructors of classes "Graph", "Graph::Directed", and "Graph::Undirected". The classes "Graph" and "Graph::Directed" are identical. After creating the graph you can modify and explore the graph with following methods.
- new
-
$G = Graph->new(@V)Returns a new graph
$Gwith the optional vertices@V.
- add_vertices
-
$G = $G->add_vertices(@v)Adds the vertices to the graph
$G, returns the graph.
- add_vertex
-
$G = $G->add_vertex($v)Adds the vertex
$vto the graph$G, returns the graph.
- vertices
-
@V = $G->verticesIn list context returns the vertices
@Vof the graph$G. In scalar context returns the number of the vertices.
- has_vertices
-
$G->has_vertices(@v)In list context returns a list which contains the vertex of the vertices
@vif the vertex exists in the graph$Gand undef if it doesn't. In scalar context returns the number of the existing vertices.
- has_vertex
-
$b = $G->has_vertex($v)Returns true if the vertex
$vexists in the graph$Gand false if it doesn't.
- vertex
-
$v = $G->has_vertex($v)Returns the vertex
$vif the vertex exists in the graph$Gor undef if it doesn't.
- directed
-
$b = $G->directed($d)Set the directedness of the graph
$Gto$dor return the current directedness. Directedness defaults to true.
- undirected
-
$b = $G->undirected($d)Set the undirectedness of the graph
$Gto$uor return the current undirectedness. Undirectedness defaults to false.
- has_edge
-
$b = $G->has_edge($u, $v)Return true if the graph
$Ghas the edge between the vertices$u,$v.
- has_edges
-
$G->has_edges($u1, $v1, $u2, $v2, ...)In list context returns a list which contains true for each edge in the graph
$Gdefined by the vertices$u1,$v1, ..., and false for each non-existing edge. In scalar context returns the number of the existing edges.
- has_path
-
$G->has_path($u, $v, ...)Return true if the graph
$Ghas the cycle defined by the vertices$u,$v, ..., false otherwise.
- has_cycle
-
$G->has_cycle($u, $v, ...)Return true if the graph
$Ghas the cycle defined by the vertices$u,$v, ...,false otherwise.
- vertex_set
-
$s = $G->vertex_set($v)Returns the vertex set of the vertex
$vin the graph$G. A "vertex set" is represented by its parent vertex.
- add_edge
-
$G = $G->add_edge($u, $v)Adds the edge defined by the vertices
$u,$v, to the graph$G. Also implicitly adds the vertices. Returns the graph.
- add_edges
-
$G = $G->add_edges($u1, $v1, $u2, $v2, ...)Adds the edge defined by the vertices
$u1,$v1, ..., to the graph$G. Also implicitly adds the vertices. Returns the graph.
- add_path
-
$G->add_path($u, $v, ...)Adds the path defined by the vertices
$u,$v, ..., to the graph$G. Also implicitly adds the vertices. Returns the graph.
- add_cycle
-
$G = $G->add_cycle($u, $v, ...)Adds the cycle defined by the vertices
$u,$v, ..., to the graph$G. Also implicitly adds the vertices. Returns the graph.
- neighbors
-
@n = $G->neighbors($v)Returns the neighbor vertices of the vertex in the graph. (Also 'neighbours' works.)
- successors
-
@s = $G->successors($v)Returns the successor vertices of the vertex in the graph.
- predecessors
-
@p = $G->predecessors($v)Returns the predecessor vertices of the vertex in the graph.
- out_edges
-
@e = $G->out_edges($v)Returns the edges leading out of the vertex
$vin the graph$G. In list context returns the edges as ($start_vertex,$end_vertex) pairs. In scalar context returns the number of the edges.
- in_edges
-
@e = $G->in_edges($v)Returns the edges leading into the vertex
$vin the graph$G. In list context returns the edges as ($start_vertex,$end_vertex) pairs; in scalar context returns the number of the edges.
- edges
-
@e = $G->edges($u, $v)Returns the edges between the vertices
$uand$v, or if$vis undefined, the edges leading into or out of the vertex$u, or if$uis undefined, returns all the edges, of the graph$G. In list context returns the edges as a list of$start_vertex,$end_vertexpairs; in scalar context returns the number of the edges.
- delete_edge
-
$G = $G->delete_edge($u, $v)Deletes an edge defined by the vertices
$u,$vfrom the graph$G. Note that the edge need not actually exist. Returns the graph.
- delete_edges
-
$G = $G->delete_edges($u1, $v1, $u2, $v2, ..)Deletes edges defined by the vertices
$u1,$v1, ..., from the graph$G. Note that the edges need not actually exist. Returns the graph.
- delete_path
-
$G = $G->delete_path($u, $v, ...)Deletes a path defined by the vertices
$u,$v, ..., from the graph$G. Note that the path need not actually exist. Returns the graph.
- delete_cycle
-
$G = $G->delete_cycle($u, $v, ...)Deletes a cycle defined by the vertices
$u,$v, ..., from the graph$G. Note that the cycle need not actually exist. Returns the graph.
- delete_vertex
-
$G = $G->delete_vertex($v)Deletes the vertex
$vand all its edges from the graph$G. Note that the vertex need not actually exist. Returns the graph.
- delete_vertices
-
$G = $G->delete_vertices(@v)Deletes the vertices
@vand all their edges from the graph$G. Note that the vertices need not actually exist. Returns the graph.
- in_degree
-
$d = $G->in_degree($v)Returns the in-degree of the vertex
$vin the graph$G, or, if$vis undefined, the total in-degree of all the vertices of the graph, or undef if the vertex doesn't exist in the graph.
- out_degree
-
$d = $G->out_degree($v)Returns the out-degree of the vertex
$vin the graph$G, or, if$vis undefined, the total out-degree of all the vertices of the graph, of undef if the vertex doesn't exist in the graph.
- degree
-
$d = $G->degree($v)Returns the degree of the vertex
$vin the graph$Gor, if$vis undefined, the total degree of all the vertices of the graph, or undef if the vertex$vdoesn't exist in the graph.
- average_degree
-
$d = $G->average_degreeReturns the average degree of the vertices of the graph
$G.
- is_source_vertex
-
$b = $G->is_source_vertex($v)Returns true if the vertex
$vis a source vertex of the graph$G.
- is_sink_vertex
-
$b = $G->is_sink_vertex($v)Returns true if the vertex
$vis a sink vertex of the graph$G.
- is_isolated_vertex
-
$b = $G->is_isolated_vertex($v)Returns true if the vertex
$vis a isolated vertex of the graph$G.
- is_exterior_vertex
-
$b = $G->is_exterior_vertex($v)Returns true if the vertex
$vis a exterior vertex of the graph$G.
- is_interior_vertex
-
$b = $G->is_interior_vertex($v)Returns true if the vertex
$vis a interior vertex of the graph$G.
- is_self_loop_vertex
-
$b = $G->is_self_loop_vertex($v)Returns true if the vertex
$vis a self-loop vertex of the graph$G.
- source_vertices
-
@s = $G->source_verticesReturns the source vertices
@sof the graph$G.
- sink_vertices
-
@s = $G->sink_verticesReturns the sink vertices
@sof the graph$G.
- isolated_vertices
-
@i = $G->isolated_verticesReturns the isolated vertices
@iof the graph$G.
- exterior_vertices
-
@e = $G->exterior_verticesReturns the exterior vertices
@eof the graph$G.
- interior_vertices
-
@i = $G->interior_verticesReturns the interior vertices
@iof the graph$G.
- self_loop_vertices
-
@s = $G->self_loop_verticesReturns the self-loop vertices
@sof the graph$G.
- density_limits
-
($sparse, $dense, $complete) = $G->density_limitsReturns the density limits for the number of edges in the graph
$G. Note that reaching$completeedges does not really guarantee completeness because we can have multigraphs. The limit of sparse is less than 1/4 of the edges of the complete graph, the limit of dense is more than 3/4 of the edges of the complete graph.
- density
-
$d = $G->densityReturns the density
$dof the graph$G.
- is_sparse
-
$d = $G->is_sparseReturns true if the graph
$Gis sparse.
- is_dense
-
$d = $G->is_denseReturns true if the graph
$Gis dense.
- complete
-
$C = $G->complete;Returns a new complete graph
$Ccorresponding to the graph$G.
- complement
-
$C = $G->complement;Returns a new complement graph
$Ccorresponding to the graph$G.
- copy
-
$C = $G->copy;Returns a new graph
$Ccorresponding to the graph$G.
- transpose
-
$T = $G->transpose;Returns a new transpose graph
$Tcorresponding to the graph$G.
- set_attribute
-
$G->set_attribute($attribute, $value)$G->set_attribute($attribute, $v, $value)$G->set_attribute($attribute, $u, $v, $value)Sets the
$attributeof graph/vertex/edge to$valuebut only if the vertex/edge already exists. Returns true if the attribute is set successfully, false if not.
- get_attribute
-
$value = $G->get_attribute($attribute)$value = $G->get_attribute($attribute, $v)$value = $G->get_attribute($attribute, $u, $v)Returns the
$valueof$attributeof graph/vertex/edge.
- has_attribute
-
$value = $G->has_attribute($attribute)$value = $G->has_attribute($attribute, $v)$value = $G->has_attribute($attribute, $u, $v)Returns the
$valueof$attributeof graph/vertex/edge.
- get_attributes
-
%attributes = $G->get_attributes()%attributes = $G->get_attributes($v)%attributes = $G->get_attributes($u, $v)Returns as a hash all the attribute names and values of graph/vertex/edge.
- delete_attribute
-
$G->delete_attribute($attribute)$G->delete_attribute($attribute, $v)$G->delete_attribute($attribute, $u, $v)Deletes the
$attributeof graph/vertex/edge.
- delete_attributes
-
$G->delete_attributes()$G->delete_attributes($v)$G->delete_attributes($u, $v)Deletes all the attributes of graph/vertex/edge.
- add_weighted_edge
-
$G->add_weighted_edge($u, $w, $v, $a)Adds in the graph
$Gan edge from vertex$uto vertex$vand the edge attribute 'weight' set to$w.
- add_weighted_edges
-
$G->add_weighted_edges($u1, $w1, $v1, $u2, $w2, $v2, ...)Adds in the graph
$Gthe weighted edges.
- add_weighted_path
-
$G->add_weighted_path($v1, $w1, $v2, $w2, ..., $wnm1, $vn)Adds in the graph
$Gthe n edges defined by the path$v1...$vnwith the n-1 'weight' attributes$w1...$wnm1
- MST_Kruskal
-
$MST = $G->MST_Kruskal;Returns Kruskal's Minimum Spanning Tree (as a graph) of the graph
$Gbased on the 'weight' attributes of the edges. (Needs the ->vertex_set() method.)
- edge_classify
-
@C = $G->edge_classify(%param)Returns the edge classification as a list where each element is a triplet [$u,
$v,$class] the$u,$vbeing the vertices of an edge and$classbeing the class. The%paramcan be used to control the search.
- toposort
-
@toposort = $G->toposortReturns the vertices of the graph
$Gsorted topologically.
- strongly_connected_components
-
@S = $G->strongly_connected_componentsReturns the strongly connected components
@Sof the graph$Gas a list of anonymous lists of vertices, each anonymous list containing the vertices belonging to one strongly connected component.
- strongly_connected_graph
-
$T = $G->strongly_connected_graphReturns the strongly connected graph
$Tof the graph$G. The names of the strongly connected components are formed from their constituent vertices by concatenating their names by '+'-characters: "a" and "b" --> "a+b".
- APSP_Floyd_Warshall
-
$APSP = $G->APSP_Floyd_WarshallReturns the All-pairs Shortest Paths graph of the graph
$Gcomputed using the Floyd-Warshall algorithm and the attribute 'weight' on the edges. The returned graph has an edge for each shortest path. An edge has attributes "weight" and "path"; for the length of the shortest path and for the path (an anonymous list) itself.
- TransitiveClosure_Floyd_Warshall
-
$TransitiveClosure = $G->TransitiveClosure_Floyd_WarshallReturns the Transitive Closure graph of the graph
$Gcomputed using the Floyd-Warshall algorithm. The resulting graph has an edge between each *ordered* pair of vertices in which the second vertex is reachable from the first.
- articulation points
-
@A = $G->articulation_points(%param)Returns the articulation points (vertices)
@Aof the graph$G. The%paramcan be used to control the search.
- is_biconnected
-
$b = $G->is_biconnectedReturns true is the graph
$Gis biconnected (has no articulation points), false otherwise.
- largest_out_degree
-
$v = $G->largest_out_degree( @V )Selects the vertex
$vfrom the vertices@Vhaving the largest out degree in the graph$G.
- MST_Prim
-
$MST = $G->MST_Prim($u)Returns Prim's Minimum Spanning Tree (as a graph) of the graph
$Gbased on the 'weight' attributes of the edges. The optional start vertex is$u, if none is given, a hopefully good one (a vertex with a large out degree) is chosen.
- SSSP_Dijkstra
-
$SSSP = $G->SSSP_Dijkstra($s)Returns the Single-source Shortest Paths (as a graph) of the graph
$Gstarting from the vertex$susing Dijktra's SSSP algorithm.
- SSSP_Bellman_Ford
-
$SSSP = $G->SSSP_Bellman_Ford($s)Returns the Single-source Shortest Paths (as a graph) of the graph
$Gstarting from the vertex$susing Bellman-Ford SSSP algorithm. If there are one or more negatively weighted cycles, returns undef.
- SSSP_DAG
-
$SSSP = $G->SSSP_DAG($s)Returns the Single-source Shortest Paths (as a graph) of the DAG
$Gstarting from vertex$s.
- add_capacity_edge
-
$G->add_capacity_edge($u, $w, $v, $a)Adds in the graph
$Gan edge from vertex$uto vertex$vand the edge attribute 'capacity' set to$w.
- add_capacity_edges
-
$G->add_capacity_edges($u1, $w1, $v1, $u2, $w2, $v2, ...)Adds in the graph
$Gthe capacity edges.
- add_capacity_path
-
$G->add_capacity_path($v1, $w1, $v2, $w2, ..., $wnm1, $vn)Adds in the graph
$Gthe n edges defined by the path$v1...$vnwith the n-1 'capacity' attributes$w1...$wnm1
- Flow_Ford_Fulkerson
-
$F = $G->Flow_Ford_Fulkerson($S)Returns the (maximal) flow network of the flow network
$G, parametrized by the state$S. The$Gmust have 'capacity' attributes on its edges.$S->{ source } must contain the source vertex and$S->{ sink } the sink vertex, and most importantly$S->{ next_augmenting_path } must contain an anonymous subroutine which takes$Fand$Sas arguments and returns the next potential augmenting path. Flow_Ford_Fulkerson will do the augmenting. The result graph$Fwill have 'flow' and (residual) 'capacity' attributes on its edges.
- Flow_Edmonds_Karp
-
$F = $G->Flow_Edmonds_Karp($source, $sink)Return the maximal flow network of the graph
$Gbuilt using the Edmonds-Karp version of Ford-Fulkerson. The input graph$Gmust have 'capacity' attributes on its edges; resulting flow graph will have 'capacity' and 'flow' attributes on its edges.
- eq
-
$G->eq($H)Return true if the graphs (actually, their string representations) are identical. This means really identical: they must have identical vertex names and identical edges between the vertices, and they must be similarly directed. (Just isomorphism isn't enough.)
COPYRIGHT
Copyright 1999, O'Reilly & Associates.
This code is distributed under the same copyright terms as Perl itself.