# assigning weights from bottom to top in graph python

 I have a graph which has following paths {1,2,3,4},{1,2,3,5},{1,6} I have to assign weights such that from leaf to root: 1. All the leaf nodes get weight of A 2. If a node has one child: Then the weight of the node becomes A*weight of its single child 3. If a node has two or more children then weight of that node is: weight of its child1*weight of child2*A*B e.g the output is final weights of node: 4: A, 5: A, 6: A, weight of 3: (A*A*A)*B , weight of of 2: A*(A*A*A*B), and weight of 1: Weight of 2* weight of 6*A*B I am working with a code in python Not getting how to call this in recursion: I have three dictionaries: parent_of[node['id']]=parent # parent of a node as value child_of[node['id']]=children # the list of children of a node no_child_of[node['id']]=len(child_of[node['id']]) # number of children #assigning weights to leaf for c in no_child_of.keys(): if no_child_of[c]==0: weight[c]=A for c in weight.keys(): k=parent_of[c] if len(child_of[k])==1: weight[k]=weight[c]*A else: for i in range(len(child_of[k])): weight[k]=weight[k]*weight