Skip to content

Commit

Permalink
Add 'remove.duplicate.edges' function
Browse files Browse the repository at this point in the history
Add a function that takes a network as input and simply removes all
edges that are exact duplicates of each other.

Works towards se-sic#138.

Signed-off-by: Maximilian Löffler <[email protected]>
  • Loading branch information
MaLoefUDS committed Aug 27, 2024
1 parent c773e63 commit 9f95d05
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions util-networks.R
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,43 @@ delete.isolates = function(network) {
return(network.no.isolates)
}

#' Remove duplicate edges from the given network.
#'
#' 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 -----------------------------------------------------
Expand Down

0 comments on commit 9f95d05

Please sign in to comment.