1

I generate a complete graph with arbitrary nodes. Each edge has two attributes where the second one is the function of the first one. (for example, when the first one is x, the second one is 1/x). How can I add these attributes to edges?

def create_random_topology(size):     
    G = nx.generators.complete_graph(size)
    attfirst={x:random.randint(100,500) for x in G.edges()}
    nx.set_edge_attributes(G,    name="PR", values=attfirst)

    #attsecond= 1/attfirst

I want to have this graph, e.g {(0,1):{PR= 10 , BW=1/10}, (0,2):{PR=5 , BW=1/5}, (1,2):{PR=2 , BW=1/2)}} Because the graph has variable size, I won't have access to the edge with the exact name of its node, for example (1,2).

1 Answer 1

1

You could obviously craft another dictionary from attfirst:

nx.set_edge_attributes(G, name='BW', {e: 1/v for e, v in attfirst.items()})

Assuming this dictionary is no longer existing, you could retrieve the value from G.edges(data=True):

nx.set_edge_attributes(G, name='BW', values={tuple(e): 1/data['PR']
                                             for *e, data in G.edges(data=True)})

Example edge data (for size=5):

G.edges(data=True)

EdgeDataView([(0, 1, {'PR': 295, 'BW': 0.003389830508474576}),
              (0, 2, {'PR': 155, 'BW': 0.0064516129032258064}),
              (0, 3, {'PR': 452, 'BW': 0.0022123893805309734}),
              (0, 4, {'PR': 293, 'BW': 0.0034129692832764505}),
              (1, 2, {'PR': 169, 'BW': 0.005917159763313609}),
              (1, 3, {'PR': 204, 'BW': 0.004901960784313725}),
              (1, 4, {'PR': 487, 'BW': 0.002053388090349076}),
              (2, 3, {'PR': 322, 'BW': 0.003105590062111801}),
              (2, 4, {'PR': 440, 'BW': 0.0022727272727272726}),
              (3, 4, {'PR': 310, 'BW': 0.0032258064516129032})])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.