diff --git a/NAMESPACE b/NAMESPACE index 4d26ef7..7965bea 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ export(mcMSTPrim) export(mutEdgeExchange) export(mutSubgraphMST) export(mutUniformPruefer) +export(nodelistToEdgelist) export(permutationToCharVec) export(permutationToEdgelist) export(plotEdgeFrequency) diff --git a/R/transformations.R b/R/transformations.R index 0c7fc3b..a9218f3 100644 --- a/R/transformations.R +++ b/R/transformations.R @@ -98,6 +98,28 @@ edgeListToCharVec = function(edgelist, n = NULL) { return(cv) } +#' Convert sequence of nodes to edge list. +#' +#' @param nodelist [\code{integer}]\cr +#' Sequence of nodes. +#' @template ret_edgelist +#' @examples +#' # first generate a random permutation, e.g., representing +#' # a roundtrip tour in a graph +#' nodelist = sample(1:8) +#' # now convert into an edge list +#' nodelistToEdgelist(nodelist) +#' @family transformation functions +#' @export +nodelistToEdgelist = function(nodelist) { + n = length(nodelist) + edgelist = matrix(NA, nrow = 2L, ncol = n - 1L) + for (i in 1:(n - 1L)) { + edgelist[, i] = nodelist[i:(i + 1L)] + } + return(edgelist) +} + #' Convert characteristic vector to edge list. #' #' @template arg_charvec diff --git a/man/charVecToEdgelist.Rd b/man/charVecToEdgelist.Rd index 57e98f5..c769890 100644 --- a/man/charVecToEdgelist.Rd +++ b/man/charVecToEdgelist.Rd @@ -24,6 +24,7 @@ edgelist = charVecToEdgelist(prueferToCharVec(pcode)) } \seealso{ Other transformation functions: \code{\link{edgeListToCharVec}}, + \code{\link{nodelistToEdgelist}}, \code{\link{permutationToCharVec}}, \code{\link{permutationToEdgelist}}, \code{\link{prueferToCharVec}}, diff --git a/man/edgeListToCharVec.Rd b/man/edgeListToCharVec.Rd index 266b6c3..fcf17ee 100644 --- a/man/edgeListToCharVec.Rd +++ b/man/edgeListToCharVec.Rd @@ -31,6 +31,7 @@ print(cvec) } \seealso{ Other transformation functions: \code{\link{charVecToEdgelist}}, + \code{\link{nodelistToEdgelist}}, \code{\link{permutationToCharVec}}, \code{\link{permutationToEdgelist}}, \code{\link{prueferToCharVec}}, diff --git a/man/nodelistToEdgelist.Rd b/man/nodelistToEdgelist.Rd new file mode 100644 index 0000000..e39afdb --- /dev/null +++ b/man/nodelistToEdgelist.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transformations.R +\name{nodelistToEdgelist} +\alias{nodelistToEdgelist} +\title{Convert sequence of nodes to edge list.} +\usage{ +nodelistToEdgelist(nodelist) +} +\arguments{ +\item{nodelist}{[\code{integer}]\cr +Sequence of nodes.} +} +\value{ +[\code{matrix}] Edge list. +} +\description{ +Convert sequence of nodes to edge list. +} +\examples{ +# first generate a random permutation, e.g., representing +# a roundtrip tour in a graph +nodelist = sample(1:8) +# now convert into an edge list +nodelistToEdgelist(nodelist) +} +\seealso{ +Other transformation functions: \code{\link{charVecToEdgelist}}, + \code{\link{edgeListToCharVec}}, + \code{\link{permutationToCharVec}}, + \code{\link{permutationToEdgelist}}, + \code{\link{prueferToCharVec}}, + \code{\link{prueferToEdgeList}} +} diff --git a/man/permutationToCharVec.Rd b/man/permutationToCharVec.Rd index 5b616ca..0170d1b 100644 --- a/man/permutationToCharVec.Rd +++ b/man/permutationToCharVec.Rd @@ -30,6 +30,7 @@ permutationToCharVec(perm, n = 10) \seealso{ Other transformation functions: \code{\link{charVecToEdgelist}}, \code{\link{edgeListToCharVec}}, + \code{\link{nodelistToEdgelist}}, \code{\link{permutationToEdgelist}}, \code{\link{prueferToCharVec}}, \code{\link{prueferToEdgeList}} diff --git a/man/permutationToEdgelist.Rd b/man/permutationToEdgelist.Rd index c1801b0..887d3b8 100644 --- a/man/permutationToEdgelist.Rd +++ b/man/permutationToEdgelist.Rd @@ -27,6 +27,7 @@ permutationToEdgelist(perm) \seealso{ Other transformation functions: \code{\link{charVecToEdgelist}}, \code{\link{edgeListToCharVec}}, + \code{\link{nodelistToEdgelist}}, \code{\link{permutationToCharVec}}, \code{\link{prueferToCharVec}}, \code{\link{prueferToEdgeList}} diff --git a/man/prueferToCharVec.Rd b/man/prueferToCharVec.Rd index 2eb71ee..2914451 100644 --- a/man/prueferToCharVec.Rd +++ b/man/prueferToCharVec.Rd @@ -26,6 +26,7 @@ print(prueferToCharVec(pcode)) \seealso{ Other transformation functions: \code{\link{charVecToEdgelist}}, \code{\link{edgeListToCharVec}}, + \code{\link{nodelistToEdgelist}}, \code{\link{permutationToCharVec}}, \code{\link{permutationToEdgelist}}, \code{\link{prueferToEdgeList}} diff --git a/man/prueferToEdgeList.Rd b/man/prueferToEdgeList.Rd index 78b1d86..a9f725c 100644 --- a/man/prueferToEdgeList.Rd +++ b/man/prueferToEdgeList.Rd @@ -27,6 +27,7 @@ print(edgelist) \seealso{ Other transformation functions: \code{\link{charVecToEdgelist}}, \code{\link{edgeListToCharVec}}, + \code{\link{nodelistToEdgelist}}, \code{\link{permutationToCharVec}}, \code{\link{permutationToEdgelist}}, \code{\link{prueferToCharVec}} diff --git a/tests/testthat/test_transformations.R b/tests/testthat/test_transformations.R index 10f9402..32137e7 100644 --- a/tests/testthat/test_transformations.R +++ b/tests/testthat/test_transformations.R @@ -56,3 +56,13 @@ test_that("trafo for roundtrip tours work well", { expect_true(all(cvec1 %in% c(0, 1))) } }) + +test_that("trafo between node list and edge list works", { + for (i in 1:10L) { + n = sample(2:10, 1L) + nodelist = sample(1:10, size = n, replace = FALSE) + edgelist = nodelistToEdgelist(nodelist) + expect_matrix(edgelist, nrows = 2L, ncols = n - 1L, mode = "numeric") + expect_true(all(as.integer(edgelist) %in% nodelist)) + } +})