graph-utils: Graph Utility Functions

graph-utilsR Documentation

Graph Utility Functions

Description

A suite of graph utility functions.

Usage

getOrigin(x)

getConnected(x)

getNonConnected(x)

getTerminal(x)

getMedian(x)

isTree(x)

isDivergent(x)

isLinear(x)

graphDist(
  x,
  ...,
  mean = c("arithmetic", "geometric", "harmonic"),
  weighting = invDistWeighting
)

reorderGraph(x, order, shuffleEdge = FALSE)

Arguments

x

A graph-class object.

...

Further argument(s) to be passed to the weighting function described below.

mean

One of character strings "arithmetic", "geometric", "harmonic", or any unambiguous abbreviation thereof, specifying the type of mean used for averaging the distances when a vertex has multiple ascendent edges.

weighting

A weighting function; it takes a set of distances as its first argument and returns a set of weights summing to 1 (default: invDistWeighting).

order

An integer vector of the vertex indices.

shuffleEdge

A Boolean. Whether to randomly shuffle the order that the edges are stored in the graph-class object (FALSE).

Details

A origin vertex is one having only outgoing edge(s) and no incoming edge, whereas a terminal vertex is one having only incoming edge(s) and no outgoing edge. A median vertex has one incoming edge and one outgoing edge. A non-connected vertex has no edge, whereas a connected vertex may have incoming edge(s), outgoing edge(s), or both.

Reordering a graph with a processOrder attribute will come with a recalculation of the process order, whereas doing so on a graph with a dist attribute cause the pairwise distance matrix to also be reordered.

Value

getOrigin

A vector of integer.

getConnected

A vector of integer.

getNonConnected

A vector of integer.

getTerminal

A vector of integer.

getMedian

A vector of integer.

isTree

A logical stipulating whether the graph is a tree.

isDivergent

A logical stipulating whether the graph has divergence.

isLinear

A logical stipulating whether the graph is a linear sequence of vertices.

graphDist

A pairwise distance matrix such as the one obtained from function dist.

Functions

  • getOrigin(): Get Origin Vertex

    Obtain the origin vert(ex/ices) of a directed graph; an origin vertex is one with no incoming edge.

  • getConnected(): Get Connected Vertex

    Obtain the connected vert(ex/ices) of a graph.

  • getNonConnected(): Get Non-connected Vertex

    Obtain the non-connected connected vert(ex/ices) of a graph.

  • getTerminal(): Get Terminal Vertex

    Obtain the terminal vert(ex/ices) of a directed graph; a terminal vertex is one with no outgoing edge.

  • getMedian(): Get Terminal Vertex

    Obtain the median vert(ex/ices) of a directed graph; a median vertex is one with one incoming edge and one outgoing edge.

  • isTree(): Tree Test

    Testing whether the graph is a tree.

  • isDivergent(): Divergence Test

    Testing whether the graph has divergence.

  • isLinear(): Linearity Test

    Testing whether the graph is a linear sequence.

  • graphDist(): Graph Distance Matrix

    Obtain a matrix of the (average) graph distance among the vertices.

  • reorderGraph(): Reorder Vertices

    Reorder the vertices of a directed graph.

Author(s)

Guillaume Guénard [aut, cre] (<https://orcid.org/0000-0003-0761-3072>), Pierre Legendre [ctb] (<https://orcid.org/0000-0002-3838-3305>) Maintainer: Guillaume Guénard <guillaume.guenard@umontreal.ca>

See Also

graph-class.

Examples

## Create and example graph with 10 vertices and 16 edges:
data.frame(
  species = rep(TRUE,10),
  x = c(2,3,2,4,3,4,2,1,1,0),
  y = c(-2,1,2,0,-0.5,-2,0,-1,1,0),
  row.names = sprintf("V%d",1:10)
) %>%
graph %>%
add.edge(
  from = c(10,10,9,9,8,8,3,7,7,10,2,2,5,1,4,5),
  to = c(9,8,3,7,7,1,2,2,5,2,1,4,4,4,6,6),
  data = data.frame(
    distance = c(1,1,1,1,1,1,1,1,1,4,2,1,1,3,1,1),
    row.names = sprintf("E%d",1:16)
  )
) -> x

getOrigin(x)         ## The graph has a single origin vertex.
getConnected(x)      ## All the vertices
getNonConnected(x)   ## are connected.
getTerminal(x)       ## The graph has a single terminal vertex.
getMedian(x)         ## The graph has a single median vertex.
isTree(x)            ## The graph is not a tree.
isDivergent(x)       ## The graoh has divergences.
isLinear(x)          ## The graph is not a linear vertex sequence.

## The average pairwise distances between the vertices:
graphDist(x)

## Reordering of the vertices:
xr <- reorderGraph(x, order=c(5:1,8,6,7,10,9))
xr

getOrigin(xr)     ## Same origin vertex, but at a different index.
getTerminal(xr)   ## Same terminal vertex, but at a different index.
graphDist(xr)     ## Same distances, but in a different order.

## Comparison between distances obtained using various means and weighting
## parameter:
cmpr <- function(x, y) 200*(x - y)/(x + y)

cmpr(graphDist(x), graphDist(x, mean="geo"))
cmpr(graphDist(x), graphDist(x, mean="har"))
cmpr(graphDist(x), graphDist(x, mean="ari", a=1))
cmpr(graphDist(x), graphDist(x, mean="geo", a=1))
cmpr(graphDist(x), graphDist(x, mean="har", a=1))
cmpr(graphDist(x), graphDist(x, mean="ari", a=2))
cmpr(graphDist(x), graphDist(x, mean="geo", a=2))
cmpr(graphDist(x), graphDist(x, mean="har", a=2))



guenardg/MPSEM documentation built on April 14, 2025, 3:53 p.m.