I previously asked a question about multiplying through weights in networkx to find an overall share of nodes in a directed graph. The solution provided works well if there is just one path between 2 nodes but fails if there is more than path. A simple example:
import pandas as pd
data = pd.DataFrame({'shop': ['S1', 'S1', 'S2', 'S2', 'S3'],
'owner': ['S2', 'S3', 'O1', 'O2', 'O1'],
'share': [0.8, 0.2, 0.5, 0.5, 1.0]})
owner shop share
0 S2 S1 0.8
1 S3 S1 0.2
2 O1 S2 0.5
3 O2 S2 0.5
4 O1 S3 1.0
create the graph:
import networkx as nx
G = nx.from_pandas_edgelist(data,'shop','owner',edge_attr = ('share'),
create_using=nx.DiGraph())
pos=nx.spring_layout(G, k = 0.5, iterations = 20)
node_labels = {node:node for node in G.nodes()}
nx.draw_networkx(G, pos, labels = node_labels, arrowstyle = '-|>',
arrowsize = 20, font_size = 15, font_weight = 'bold')
To get the share O1 has of S1, the 2 paths need to be multiplied and then added. The previous solution fails to do this. Is there a way of doing this?