0

I have an undirected graph, and I want to iteratively remove each serial edge and replace it with a new edge. The weight of the new edge represents the number of spanning trees, and should be computed as follows: T_new = (1/a+b) * T_old, where a and b are the weights of the removed edges, T_new is the number of spanning trees in current iteration and T_old is the number of spanning trees in the previous iteration. This equation changes iteratively, as the graph changes, so if we have 4 iterations we will have 4 equations, each one is in terms of the previous one. We stop once the graph has no more serial edges. If the final graph is composed of 1 edge, the weight of that edge is the last T_new, and we will have a numerical value of T-old. Otherwise, we should have T_old in terms of T_new. Here is an attached image explaining what I said in case it is not well explained. Here is the part of my code describing the problem :

** PS : I only need the part where the equation changes in every iteration, not the things to do to remove and add new edges and so on.here is an example :enter image description here **

import networkx as nx 
    def fun2(G) :

    L1=  G.degree()  
    print(L1)
    L= list(L1) 
    for x in L :
        if G.degree(x[0]) == 2 : #if the adjacent edges to x[0] are serial 
             ... do somthing(remove edges and add new one with new weight)
        #T-new = 1/(a+b) T-old   here the equation should change


def tau(G) : # it should return T_old which is the number of spanning trees of the initial graph 
    if G.number_of_edges() == 1 :
        T_new = list(G.edges(data=True))[0][2]['weight']
        T_old = (a+b) * t_new
    else : 
        T_new = 1/(a+b) * tau(G) 
        T_old =  (a+b) * t_new
    return t_old
learner
  • 33
  • 7
  • One question: fun2 is doing one step of graph transformation and there is outside code not shown doing iterative cals? Or is everything supposed to be happening inside? Also, where does the first T-old come from, a constant? – mkiever Jan 13 '19 at 20:21
  • @mkiever , in the loop for , for each element in the list of nodes if the condition is true (degree of a node = 2 ) , we will do a sequence of transformation , and at the end of the iteration (before going to next iteration) there should be the equation i want .because i will need it in the next iteration equation since it will be in terms of it . – learner Jan 13 '19 at 21:49
  • the T-old is what we are seeking, so i will be having a bunch of recursive equations(one from every iteration) , once there are no nodes of degree 2 , we will find ourselves with the final equation which is in terms of the previous one ,and the previous one is in terms of the one before it (,and so on , until we have the last T-new in terms of the very first T-old that we are looking for .(by replacing all equations in the last one) – learner Jan 13 '19 at 21:53
  • one more thing to add , if the final graph is composed of 1 edge , then the weight of that edge is the last T-new . so we will have a numerical value of T-old ,otherwise , we should have T-old in terms of T-new – learner Jan 13 '19 at 21:58
  • This really needs a [mcve]. We need to see an example starting network, and what you expect the final network to be. – Joel Jan 17 '19 at 00:51

1 Answers1

0

No recursion is needed, as we change the graph as we go. Here's a solution:

import networkx as nx

G = nx.Graph()
G.add_weighted_edges_from([(0,1,5),(0,2,10),(2,3,30)])

for node in list(G.nodes()):
    if G.degree(node) == 2:
        neighbors = list(G.neighbors(node))
        a = G.get_edge_data(neighbors[0], node)['weight']
        b = G.get_edge_data(neighbors[1], node)['weight']
        w = (a * b) / (a + b)
        G.add_edge(neighbors[0], neighbors[1], weight=w)
        G.remove_node(node)
zohar.kom
  • 1,765
  • 3
  • 12
  • 28
  • Thank you for the quick response ,but i would like to say that i mentioned in the preious description of my problem that i DON't need the part where i should remove the serial edges and add new ones with new weights .I Need to compute T-old basing on its value in the whole previous iterations . – learner Jan 15 '19 at 14:38
  • I must say I don't get what you are trying to do, please try to edit your question and add a small and clear example. I followed the image you added and that's what the code does. In the current code, eventually you get a graph with a single edge and it's weight equals the final T_new value. You can access it by calling `list(G.edges(data=True))[0][2]['weight']`. I didn't understand what is the desired behavior in case there are multiple edges. – zohar.kom Jan 15 '19 at 14:53
  • I'm in fact trying to compute the number of spanning trees of a graph (T_old)which is changing in terms of the number of spanning trees of the new graph created after each iteration T_new(after removing every serial edges),.I will try to explain the problem more clearly and modify my question . – learner Jan 15 '19 at 16:31
  • Here i updated my code to show the function Tau(G) which supposed to do the recursion and gives the result i'm looking for – learner Jan 15 '19 at 17:57