Trim graph with a binary filter
binary_search_filter_1 <- function(g, low, high) {
x<-g
if ( high < low ) {
return(NULL)
}else {
mid <- floor((low + high) / 2)
x<-igraph::delete.edges(g, which(E(g)$weight <= weights[mid]))
cat("low:", low, " mid:", mid, " high:", high, " mid_weight:", weights[mid], " components:", count_components(x), " edges:", ecount(x), "\n")
if ((mid - low) == 0){
x<-igraph::delete.edges(g, which(E(g)$weight <= weights[mid-1]))
cat("Final: low:", low - 1, " mid:", mid - 1, " high:", high - 1, " mid_weight:", weights[mid - 1], " components:",count_components(x), " edges:", ecount(x), "\n")
return(list(weight=mid - 1, graph=x))
exit
}
if (((high - low) == 0)){
x<-igraph::delete.edges(g, which(E(g)$weight <= weights[mid+1]))
cat("Final: low:", low, " mid:", mid, " high:", high, " mid_weight:", weights[mid], " components:",count_components(x), " edges:", ecount(x), "\n")
return(list(weight=mid , graph=x))
exit
}
if (!(is.connected(x))){
binary_search_filter_1(g, low, mid - 1)
}else if (is.connected(x)){
binary_search_filter_1(g, mid + 1, high)
}
}
}
weights <- unique(sort(E(G)$weight, decreasing = FALSE))
binary_search_filter_1(g = G, low = 1, high = length(weights))$graph