Learn R Programming

rdecision (version 1.1.2)

Graph: An undirected graph

Description

An R6 class to represent a graph (from discrete mathematics).

Arguments

Author

Andrew Sims andrew.sims@newcastle.ac.uk

Methods


Method new()

Create a new Graph object from sets of nodes and edges.

Usage

Graph$new(V, E)

Arguments

V

A list of Nodes.

E

A list of Edges.

Returns

A Graph object.


Method order()

Return the order of the graph (number of vertices).

Usage

Graph$order()

Returns

Order of the graph (integer).


Method size()

Return the size of the graph (number of edges).

Usage

Graph$size()

Returns

Size of the graph (integer).


Method vertex_along()

Sequence of vertex indices.

Usage

Graph$vertex_along()

Details

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.

Returns

A numeric vector.


Method vertex_index()

Find the index of a vertex in the graph.

Usage

Graph$vertex_index(v)

Arguments

v

A vertex.

Returns

Index of v. The vertices are normally stored in the same order they are specified in new, but this cannot be guaranteed. This function returns the same index as used in the adjacency matrix and NA if v is not a vertex, or is a vertex that is not in the graph.


Method vertex_at()

Find the vertex at a given index.

Usage

Graph$vertex_at(index)

Arguments

index

Index of a vertex in the graph, as an integer. The indices of the vertices normally run from 1 to the order of the graph, and are normally in the same sequence as the list of vertices, V, supplied when the graph was created. However, these assumptions are not guaranteed to hold for future versions of the package, and it is recommended to supply an index value that has been provided by another class function such as vertex_index, vertex_along and graph_adjacency_matrix.

Details

The inverse of function vertex_index. The function will raise an abort signal if the supplied index is not a vertex.

Returns

the node (vertex) with the specified index.


Method has_vertex()

Test whether a vertex is an element of the graph.

Usage

Graph$has_vertex(v)

Arguments

v

Subject vertex.

Returns

TRUE if v is an element of V(G).


Method edge_along()

Sequence of edge indices.

Usage

Graph$edge_along()

Details

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.

Returns

A numeric vector.


Method edge_index()

Find the index of an edge in a graph.

Usage

Graph$edge_index(e)

Arguments

e

Subject edge.

Returns

Index of e. The edges are normally stored in the same order they are specified in new, but this cannot be guaranteed. This function returns the same index returned in other functions and NA if the edge is not in the graph.


Method edge_at()

Find the edge at a given index.

Usage

Graph$edge_at(index)

Arguments

index

Index of a edge in the graph, as an integer. The indices of the edges normally run from 1 to the size of the graph, and are normally in the same sequence as the list of edges, E, supplied when the graph was created. However, these assumptions are not guaranteed to hold for future versions of the package, and it is recommended to supply an index value that has been provided by another class function such as edge_index and edge_along.

Details

The inverse of function dge_index. The function will raise an abort signal if the supplied index is not an edge.

Returns

the edge with the specified index.


Method has_edge()

Test whether an edge is element of the graph.

Usage

Graph$has_edge(e)

Arguments

e

Subject edge.

Returns

TRUE if e is an element of \(E(G)\).


Method graph_adjacency_matrix()

Compute the adjacency matrix for the graph.

Usage

Graph$graph_adjacency_matrix(boolean = FALSE)

Arguments

boolean

If TRUE, the adjacency matrix is logical, each cell is FALSE, TRUE.

Details

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).

Returns

A square numeric 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 labels, or the node indices if not.


Method is_simple()

Is this a simple graph?

Usage

Graph$is_simple()

Details

A simple graph has no self loops or multi-edges.

Returns

TRUE if simple, FALSE if not.


Method is_connected()

Test whether the graph is connected.

Usage

Graph$is_connected()

Details

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.

Returns

TRUE if connected, FALSE if not.


Method is_acyclic()

Checks for the presence of a cycle in the graph.

Usage

Graph$is_acyclic()

Details

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.

Returns

TRUE if no cycles detected.


Method is_tree()

Compute whether the graph is connected and acyclic.

Usage

Graph$is_tree()

Returns

TRUE if the graph is a tree; FALSE if not.


Method degree()

The degree of a vertex in the graph.

Usage

Graph$degree(v)

Arguments

v

The subject node.

Details

The number of incident edges.

Returns

Degree of the vertex, integer.


Method neighbours()

Find the neighbours of a node.

Usage

Graph$neighbours(v)

Arguments

v

The subject node.

Details

A property of the graph, not the node. Does not include self, even in the case of a loop to self.

Returns

A list of nodes which are joined to the subject.


Method as_DOT()

Export a representation of the graph in DOT format.

Usage

Graph$as_DOT()

Details

Writes the representation in the graphviz DOT language (http://graphviz.org/doc/info/lang.html) for drawing with one of the graphviz tools including dot (Gansner, 1993).

Returns

A character vector. Intended for passing to writeLines for saving as a text file.


Method clone()

The objects of this class are cloneable with this method.

Usage

Graph$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Details

Encapsulates and provides methods for computation and checking of undirected graphs. Graphs are systems of vertices connected in pairs by edges. A base class.

References

Gansner ER, Koutsofios E, North SC, Vo K-P. A technique for drawing directed graphs. IEEE Transactions on Software Engineering, 1993;19:214–30, 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, tools:::Rd_expr_doi("10.1201/b16132")