CGL::AnyGraph(V)
Instance methods
Add an edge between vertices u and v.
The given vertices are automatically added if they are not already part of the graph.
A weight and/or a label can be associated to the edge if the concrete class supports it.
Add the given edge to the graph.
See #add_edge(u : V, v : V, weight, label)
Add a single vertex (a.k.a. node) to this graph.
g = CGL::Graph(String).new
g.add_vertex("Hello")
Returns an iterator over vertices from the given source v in a breadth-first search (DFS).
Yields vertices from the given source v in a breadth-first search (DFS).
Returns a deep copy of self.
Similar to #dup, but duplicates the nodes and edges attributes as well.
Count all simple paths between the two given vertices, where a simple path is a path with no repeated nodes.
Returns the degree of the given vertex v.
For directed graphs, the value equals #out_degree_of.
For undirected graphs, the value is the sum of #in_degree_of and
#in_degree_of.
Returns the density of self.
Self loops are counted in the total number of edges so graphs with self loops can have density higher than 1.
Returns an iterator over vertices from the given source v in a depth-first search (DFS).
Yields vertices from the given source v in a depth-first search (DFS).
Yields all vertices of self, which are traversed following a
depth-first search (DFS) on the whole graph.
Returns an iterator over all vertices of self, which are traversed
following a depth-first search (DFS) on the whole graph.
Yields each vertex adjacent to u in the graph.
g = Graph(String).new(edges: [{"b", "a"}, {"a", "c"}])
g.each_adjacent("a") do |v|
puts v
end
Output:
b
c
For directed graphs, adjacent vertices are found following outgoing edges.
g = DiGraph(String).new(edges: [{"b", "a"}, {"a", "c"}])
g.each_adjacent("a") do |v|
puts v
end
Output:
c
Returns an iterator over each vertex adjacent to u in the graph.
Returns an edge data structure between u and v if present in the graph, otherwise returns the value of the given block.
Returns an edge data structure between u and v if present in the
graph, otherwise raises an EdgeError.
Returns an edge data structure between u and v if present in the
graph, otherwise returns nil.
Whether the edge between u and v with the given attributes is part of the graph.
Returns the incoming degree of the given vertex v.
For undirected graphs, the value equals #degree_of.
Returns the label associated with the given edge if it exists, raises
EdgeError otherwise
Returns the label associated with the given edge if it exists, otherwise
returns nil.
Returns the outgoing degree of the given vertex v.
For undirected graphs, the value equals #degree_of.
Remove given vertex v from this graph. Edges incident to v are also removed.
Raises a GraphError if vertex is not part of the graph.
g = CGL::Graph(String).new(edges: [{"a", "b"}, {"b", "c"}])
g.size # => 2
g.remove_vertex("b")
g.size # => 0
Returns a shortest path between the given vertices.
Note: Tries to select the most appropriate algorithm for self.
Returns the shortest weighted path between the given vertices.
Raises if self is not a weighted graph.
Note: Uses Dijkstra's algorithm. Not appropriate for graphs with negative weights and/or negative cycles.
Returns the shortest unweighted path between the given vertices.
Returns a subgraph containing the given edges.
If copy is set to true, the vertices as well as edge attributes are
deep copies, otherwise they are shallow copies.
Returns a subgraph containing the given vertices as well as the existing edges between those vertices.
If copy is set to true, the vertices as well as edge attributes are
deep copies, otherwise they are shallow copies.
Returns the weight associated with the given edge if it exists, otherwise
EdgeError otherwise.
Returns the weight associated with the given edge if it exists, otherwise
returns nil.