I'm working with a directed graph in R using igraph and I have a specific issue that I'm unable to resolve. Each vertex carries a weight of 1 and I want to calculate the cumulative sum of the vertices taking into account the following conditions:
- "Fiber" routes have priority over 'Micro' routes.
- If there are two 'Fiber' or 'Micro' routes, the physical distance in kilometers determines which one is selected.
- The solution should not involve removing or adding any edges, i.e., even for calculation purposes, all the existing connections should remain intact.
Here is a simplified example of my graph. For convenience I use R, but it can be in Python:
library(igraph)
library(dplyr)
edges <- tribble(
~from, ~to, ~tipo, ~distance_km, ~color, ~width,
"A", "B", "Fiber", 10, "black", 2,
"B", "C", "Fiber", 5, "black", 2,
"B", "C", "Fiber", 6, "gray", 0.5,
"A", "C", "Micro", 5, "gray", 0.5,
"C", "D", "Micro", 1, "black", 2,
"C", "D", "Micro", 2, "gray", 0.5
)
edges <- edges %>%
mutate(label = paste0(tipo, " (", distance_km, ")"))
g <- graph_from_data_frame(edges, directed = TRUE)
V(g)$name <- paste0(V(g)$name, " (", 1:4, ")")
plot(g, edge.label = E(g)$label)
How can I calculate the cumulative sum of the vertices following the conditions described above?
In the next image you can see in black the paths that I expect the algorithm must decide to achieve the cumulative sum.
Any guidance or help would be greatly appreciated.