Skip to content

Commit

Permalink
Create Python script of digraph
Browse files Browse the repository at this point in the history
Create Digraph, plot the graph, and find shortest path between all nodes
  • Loading branch information
arpitadash committed Mar 31, 2022
1 parent 9442669 commit 0b3d453
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions 2022-round-1/arpitadash/nx_tutorial_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
#Adding nodes of int, str and tuple type
G.add_node(1)
G.add_node("string1")
G.add_node((3,2))
G.add_node((6,2))
G.add_node('string2')
G.add_nodes_from([2,3,10])
G.add_nodes_from([2,3,5])
G.add_nodes_from([(4,{'color':'red'})])
#Adding edges between the nodes
G.add_edges_from([(1,2),(1,3),(1,'string1'),(2,3),('string2',1),(1,'string2'),(1,(3,2)),(2,10),((3,2),'string2'),('string2',4)])
# print(G.number_of_nodes())
# print(G.number_of_edges())
G_node_list=G.nodes
G.add_edge(4,1)
G.add_edges_from([(1,2),(1,3),(1,'string1'),(2,3),('string2',1),(1,'string2'),(1,(6,2)),(2,5),((6,2),'string2'),('string2',4)])
#Calculating shortest path between all nodes in the graph
p=nx.shortest_path(G)
print('Printing shortest path')
#Storing existing nodes in the graph
G_node_list = G.nodes
for index,node in enumerate(G_node_list):
print('From',node)
for index2,node2 in enumerate(G_node_list):
if index!=index2 and nx.has_path(G, node, node2):
print('To', node2, ':', p[node][node2])
#Plotting the graph
nx.draw(G,pos=nx.circular_layout(G),with_labels=True)
plt.show()

0 comments on commit 0b3d453

Please sign in to comment.