Graph | R Documentation |
An R6 class to represent a graph (from discrete mathematics).
Encapsulates and provides methods for computation and checking of undirected graphs. Graphs are systems of vertices connected in pairs by edges. A base class.
A GML stream as a character vector.
new()
Create a new Graph
object from sets of nodes
and edges.
Graph$new(V, E)
V
An unordered set of Nodes, as a list.
E
An unordered set of Edges, as a list.
A Graph
object.
order()
Return the order of the graph (number of vertices).
Graph$order()
Order of the graph (integer).
size()
Return the size of the graph (number of edges).
Graph$size()
Size of the graph (integer).
vertexes()
A list of all the Node objects in the graph.
Graph$vertexes()
The list of Node objects is returned in the same order as their
indexes understood by vertex_index
, vertex_at
and
vertex_along
, which is not necessarily the same order in which
they were supplied in the V
argument to new
.
vertex_along()
Sequence of vertex indices.
Graph$vertex_along()
Similar to base::seq_along
, this function provides
the indices of the vertices in the graph. It is intended for use by
graph algorithms which iterate vertices.
A numeric vector of indices from 1 to the order of the graph.
The vertex at index i
is not guaranteed to be the same vertex at
V[[i]]
of the argument V
to new
(i.e., the order in
which the vertices are stored internally within the class may differ
from the order in which they were supplied).
vertex_index()
Find the index of a vertex in the graph.
Graph$vertex_index(v)
v
A vertex, or list of vertexes.
Index of v. The index of vertex v
is the one
used internally to the class object, which is not necessarily the same as
the order of vertices in the V
argument of new
. NA
if v is not a vertex, or is a vertex that is not in the graph.
vertex_at()
Find the vertex at a given index.
Graph$vertex_at(index, as_list = FALSE)
index
Index of vertex in the graph, as an integer, or vector of integers.
as_list
Boolean. If TRUE the method returns list of Nodes,
even if the length of index
is 1.
The inverse of function vertex_index
. The function will
raise an abort signal if all the supplied indexes are not vertexes. The
function is vectorized, but for historical compatibility the return
object is a single Node
if index
is a scalar. The
return object can be guaranteed to be a list if as_list
is set.
Node at index
if index
is a scalar, a list of Nodes
at the values of index
if index
is a vector, or an empty
list if index is an empty array.
has_vertex()
Test whether a vertex is an element of the graph.
Graph$has_vertex(v)
v
Vertex, or list of vertices.
A logical scalar or logical vector the same length as v if v is a vector, with TRUE if v is an element of V(G).
vertex_label()
Find label of vertexes at index i.
Graph$vertex_label(iv)
iv
Index of vertex, or vector of indexes.
Label(s) of vertex at index i
edges()
A list of all the Edge objects in the graph.
Graph$edges()
The list of Edge objects is returned in the same order as their
indexes understood by edge_index
, edge_at
and
edge_along
, which is not necessarily the same order in which they
were supplied in the E
argument to new
.
edge_along()
Sequence of edge indices.
Graph$edge_along()
Similar to base::seq_along
, this function provides
the indices of the edges in the graph. It is intended for use by
graph algorithms which iterate edges. It is equivalent to
seq_along(g$edges())
, where g
is a graph.
A numeric vector of indices from 1 to the size of the graph.
The edge at index i
is not guaranteed to be the same edge at
E[[i]]
of the argument E
to new
(i.e., the order in
which the edges are stored internally within the class may differ
from the order in which they were supplied).
edge_index()
Find the index of an edge in a graph.
Graph$edge_index(e)
e
An edge object, or list of edge objects.
The index of edge e
is the one used internally to the
class object, which is not necessarily the same as the
order of edges in the E
argument of new
.
Index of e
. NA
if e is not an edge, or is an
edge that is not in the graph.
edge_at()
Find the edge at a given index.
Graph$edge_at(index, as_list = FALSE)
index
Index of edge in the graph, as an integer, vector of integers, or list of integers.
as_list
Boolean. If TRUE the method returns list of Edges,
even if the length of index
is 1.
The inverse of function edge_index
. The function will
raise an abort signal if the supplied index is not an edge. The
function is vectorized, but for historical compatibility the return
object is a single Edge
if index
is a scalar. The
return object can be guaranteed to be a list if as_list
is set.
The edge, or list of edges, with the specified index.
has_edge()
Test whether an edge is an element of the graph.
Graph$has_edge(e)
e
Edge or list of edges.
Logical vector with each element TRUE
if the corresponding
element of e
is an element of E(G)
.
edge_label()
Find label of edge at index i
Graph$edge_label(ie)
ie
Index of edge, or vector of indexes.
Label of edge at index i, or character vector with the labels at
indexes ie
.
graph_adjacency_matrix()
Compute the adjacency matrix for the graph.
Graph$graph_adjacency_matrix(boolean = FALSE)
boolean
If TRUE
, the adjacency matrix is logical, each
cell is {FALSE
, TRUE
}.
Each cell contains the
number of edges joining the two vertexes, with the convention of
self loops being counted twice, unless binary
is TRUE
when
cells are either 0 (not adjacent) or 1 (adjacent).
A square integer matrix with the number of rows and columns equal to the order of the graph. The rows and columns are labelled with the node labels, if all the nodes in the graph have unique labels, or the node indices if not.
is_simple()
Is this a simple graph?
Graph$is_simple()
A simple graph has no self loops or multi-edges.
TRUE
if simple, FALSE
if not.
is_connected()
Test whether the graph is connected.
Graph$is_connected()
Graphs with no vertices are considered unconnected; graphs with 1 vertex are considered connected. Otherwise a graph is connected if all nodes can be reached from an arbitrary starting point. Uses a depth first search.
TRUE
if connected, FALSE
if not.
is_acyclic()
Checks for the presence of a cycle in the graph.
Graph$is_acyclic()
Uses a depth-first search from each node to detect the presence of back edges. A back edge is an edge from the current node joining a previously detected (visited) node, that is not the parent node of the current one.
TRUE
if no cycles detected.
is_tree()
Compute whether the graph is connected and acyclic.
Graph$is_tree()
TRUE
if the graph is a tree; FALSE
if not.
degree()
The degree of a vertex in the graph.
Graph$degree(v)
v
The subject node.
The number of incident edges.
Degree of the vertex, integer.
neighbours()
Find the neighbours of a node.
Graph$neighbours(v)
v
The subject node (scalar, not a list).
A property of the graph, not the node. Does not include self, even in the case of a loop to self.
A list of nodes which are joined to the subject.
as_DOT()
Export a representation of the graph in DOT format.
Graph$as_DOT(rankdir = "LR", width = 7, height = 7)
rankdir
One of "LR" (default), "TB", "RL" or "BT".
width
of the drawing, in inches
height
of the drawing, in inches
Writes the representation in the graphviz
DOT language
(https://graphviz.org/doc/info/lang.html) for drawing with one
of the graphviz
tools including dot
(Gansner, 1993).
A character vector. Intended for passing to writeLines
for saving as a text file.
as_gml()
Exports the digraph as a Graph Modelling Language (GML) stream.
Graph$as_gml()
Intended to work with the igraph or DiagrammeR packages, which are able to import GML files.
clone()
The objects of this class are cloneable with this method.
Graph$clone(deep = FALSE)
deep
Whether to make a deep clone.
Andrew Sims andrew.sims@newcastle.ac.uk
Gansner ER, Koutsofios E, North SC, Vo K-P. A technique for drawing directed graphs. IEEE Transactions on Software Engineering, 1993;19:214–30, \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1109/32.221135")}.
Gross JL, Yellen J, Zhang P. Handbook of Graph Theory. Second edition, Chapman and Hall/CRC.; 2013, \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1201/b16132")}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.