convert_adjacency_matrix_to_costmatrix: Converts an adjacency matrix to a costmatrix

View source: R/convert_adjacency_matrix_to_costmatrix.r

convert_adjacency_matrix_to_costmatrixR Documentation

Converts an adjacency matrix to a costmatrix

Description

Takes an adjacency matrix as input and returns the corresponding costmatrix.

Usage

convert_adjacency_matrix_to_costmatrix(adjacency_matrix)

Arguments

adjacency_matrix

A labelled square matrix with zeroes denoting non-adjacencies and ones denoting adjacencies.

Details

This function is intended for internal use, but as it also generalizes to solving a general graph theory problem - generating a distance matrix corresponding to each shortest path between vertices of a connected graph represented as an adjacency matrix - it is made available explicitly here.

The process is best understood with an example. Imagine we have a graph like this:

  0-1-2
  |
  3

I.e., we have four labelled vertices, 0-3, and three edges (connections) between them:

  0-1
  1-2
  0-3

Note: here we assume symmetry, 0-1 = 1-0.

Graphs like this can be explicitly captured as adjacency matrices, where a one denotes two vertices are "adjacent" (connected by an edge) and a zero that they are not.

    _________________
    | 0 | 1 | 2 | 3 |
---------------------
| 0 | 0 | 1 | 0 | 1 |
---------------------
| 1 | 1 | 0 | 1 | 0 |
---------------------
| 2 | 0 | 1 | 0 | 0 |
---------------------
| 3 | 1 | 0 | 0 | 0 |
---------------------

But what such matrices do not tell us is how far every vertex-to-vertex path is in terms of edge counts. E.g., the path length from vertex 3 to vertex 2.

This function simply takes the adjacency matrix and returns the corresponding costmatrix, corresponding to every minimum vertex-to-vertex path length.

Value

An object of class costMatrix.

Author(s)

Graeme T. Lloyd graemetlloyd@gmail.com

See Also

convert_state_tree_to_adjacency_matrix, linklocate_bracket_positions

Examples


# Build the example adjacency matrix for the graph above:
adjacency_matrix <- matrix(
  data = c(
    0, 1, 0, 1,
    1, 0, 1, 0,
    0, 1, 0, 0,
    1, 0, 0, 0
  ),
  nrow = 4,
  ncol = 4,
  dimnames = list(0:3, 0:3)
)

# Convert this to a costmatrix:
convert_adjacency_matrix_to_costmatrix(
  adjacency_matrix = adjacency_matrix
)


graemetlloyd/Claddis documentation built on May 9, 2024, 8:07 a.m.