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
maxloeffler committed Sep 10, 2024
1 parent 74c4dd2 commit dcd1ff7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions util-networks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------
Expand Down

0 comments on commit dcd1ff7

Please sign in to comment.