I have a simple directed network. It could be rivers that merge and separate. Here river A and river B both contribute 50% to the water in downstream river A12, etc.
I want to calculate how large a percent of water in each river comes from different upstream rivers.
I gather that I have to multiply the weights along all paths between vertices and then sum them. But I am unsure how to achieve this in igraph?
library(igraph)
# Network structure
df <- structure(list(pid = c("A1", "A1", "B1", "A11", "A11", "A12", "A12")
, cid = c("A11", "A12", "A12", "A111", "A121", "A121", "A122")
, w = c(1, 0.5, 0.5, 1, 0.6, 0.4, 1))
, row.names = c(NA, -7L)
, class = "data.frame")
df_g <- graph_from_data_frame(df, directed=TRUE)
# Wanted list with each pair of connected vertices and sum of muliplied weights along connections:
wanted_df <- as.data.frame(rbind(c('A1','A11',1)
,c('A1','A12',0.5)
,c('A1','A111',1) # 1*1
,c('A1','A121',0.8) # 1*0.6 + 0.5*0.4
,c('A1','A122',0.5) # 0.5**1
,c('A11','A111',1)
,c('A11','A121',0.6)
,c('A12','A121',0.4)
,c('A12','A122',1)
,c('B1','A12',0.5)
,c('B1','A121',0.2) #0.5*0.4
,c('B1','A122',0.5))) #0.5*1
colnames(wanted_df) <- c('pid','cid','share')