'How to add weights to grid_2d_graph in Python
Can anyone please help how to add weights to 2d_graph in python. My objective is to add a probability to each edge in python and simulate and check whether there is connection still exists between two nodes.
Step 1 For Example I drawn a 10X10 Graph using networkx.
G = nx.grid_2d_graph(10,10)
plt.figure(figsize=(5,5))
pos = {(x,y):(y,-x) for x,y in G.nodes()}
nx.draw(G, pos=pos, node_color='pink',
with_labels=True, node_size=600)
Step 2 I have removed some edges using following code manually.
G.remove_edge((9,0),(9,1))
G.remove_edge((8,1),(7,1))
G.remove_edge((6,3),(5,3))
G.remove_edge((4,4),(3,4))
G.remove_edge((3,4),(3,5))
G.remove_edge((3,6),(2,6))
G.remove_edge((2,7),(1,7))
G.remove_edge((1,7),(1,8))
G.remove_edge((1,8),(1,9))
G.remove_edge((1,9),(0,9))
G.remove_edge((8,1),(9,1))
G.remove_edge((9,1),(9,2))
G.remove_edge((0,8),(0,9))
G.remove_edge((1,7),(0,7))
G.remove_edge((3,5),(2,5))
G.remove_edge((3,2),(3,3))
G.remove_edge((8,0),(7,0))
G.remove_edge((7,2),(6,2))
G.remove_edge((2,2),(1,2))
G.remove_edge((0,2),(0,3))
G.remove_edge((0,3),(0,4))
G.remove_edge((1,2),(1,3))
G.remove_edge((1,3),(0,3))
nx.draw(G, pos=pos, node_color='pink',
with_labels=True, node_size=600)
Step 3: My objective is to add a probability to all the edges and see which one is still intact and not intact. Instead of Step 2 I want to automate using Step 3 by fixing probability to each edge and simulate it to check whether there is still connection between two nodes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|