View source: R/convert_adjacency_matrix_to_costmatrix.r
convert_adjacency_matrix_to_costmatrix | R Documentation |
Takes an adjacency matrix as input and returns the corresponding costmatrix.
convert_adjacency_matrix_to_costmatrix(adjacency_matrix)
adjacency_matrix |
A labelled square matrix with zeroes denoting non-adjacencies and ones denoting adjacencies. |
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.
An object of class costMatrix
.
Graeme T. Lloyd graemetlloyd@gmail.com
convert_state_tree_to_adjacency_matrix, locate_bracket_positions
# 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
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.