From dcd1ff771a3d408cf2bd470cdc3d21c76f70b24c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20L=C3=B6ffler?= Date: Tue, 27 Aug 2024 17:08:04 +0200 Subject: [PATCH] Add 'remove.duplicate.edges' function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a function that takes a network as input and simply removes all edges that are exact duplicates of each other. Works towards #138. Signed-off-by: Maximilian Löffler --- util-networks.R | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/util-networks.R b/util-networks.R index da1b1da6..8eb82182 100644 --- a/util-networks.R +++ b/util-networks.R @@ -1993,6 +1993,44 @@ delete.isolates = function(network) { return(network.no.isolates) } +#' Remove duplicate edges from the given network. +#' +#' Keep exactly one edge from all equivalence classes of edges over identity. +#' This function retains all set network, vertex, and edge attributes. +#' +#' @param network the given network +#' +#' @return the simplified network +remove.duplicate.edges = function(network) { + + logging::logdebug("remove.duplicate.edges: starting.") + + seen = list() + duplicates = c() + edges = igraph::as_data_frame(network, "edges") + + ## iterate over all edges and collect duplicates + for (i in seq_len(nrow(edges))) { + + ## get current edge without rowname + current.edge = edges[i, ] + rownames(current.edge) = NULL + + ## check whether the current edge is a duplicate + if (any(sapply(seen, function(edge) identical(edge, current.edge)))) { + duplicates = append(duplicates, i) + } else { + seen = append(seen, list(current.edge)) + } + } + + ## remove all duplicates + network = igraph::delete_edges(network, duplicates) + + logging::logdebug("remove.duplicate.edges: finished.") + return(network) +} + ## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / ## Multi-network views -----------------------------------------------------