# install.packages("dodgr") # run once if needed library(dodgr) # --- 1. Perfect full binary tree, 3 levels, with extra upstream node X ------ # X # | # A # / \ # B C # / \ / \ # D E F G # # Edges: parent -> child graph <- data.frame( edge_id = 1:7, from = c("X", "A","A", "B","B", "C","C"), to = c("A", "B","C", "D","E", "F","G"), weight = rep(1, 7), # routing weight distance = rep(1, 7) # distance metric ) # --- 2. OD pairs with flows at the LEAVES ----------------------------------- # Origin: the new upstream node "X" # Destinations: the four leaves "D", "E", "F", "G" from <- "X" to <- c("D", "E", "F", "G") # Flows matrix: nrow = length(from) = 1, ncol = length(to) = 4 # X->D = 5, X->E = 5, X->F = 10, X->G = 10 flows <- matrix(c(5, 5, 10, 10), nrow = length(from)) # --- 3. Aggregate flows along shortest paths -------------------------------- agg <- dodgr_flows_aggregate( graph = graph, from = from, to = to, flows = flows, contract = FALSE, # avoid small-graph contraction issues norm_sums = FALSE # <-- KEY CHANGE ) # --- 4. Inspect results ------------------------------------------------------ # Show only edges with non-zero flow agg[agg$flow > 0, ] # The flow between X and A is simply the 'flow' value on edge X -> A, # which will be the sum of all leaf flows: 5 + 5 + 10 + 10 = 30.