Skip to content

Commit

Permalink
Merge pull request #199 from Schefflera-Arboricola/main
Browse files Browse the repository at this point in the history
added NetworkX tutorial script
  • Loading branch information
MridulS authored Oct 4, 2023
2 parents d4c6aab + 9257a71 commit 16c3dd4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 2023-round-2/Schefflera-Arboricola/nx_tutorial_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import networkx as nx
import matplotlib.pyplot as plt

# Creating a NetworkX DiGraph (Directed Graph) object
G=nx.DiGraph()

# Adding nodes of different types
nodes = [1, 'A', (3, 4), 'B', 5, 'C', (6, 7), 8, 'D', 'E']

G.add_nodes_from(nodes)

# Adding multiple edges between nodes
edges = [(1, 'A'), ('A', (3, 4)), ('A', 5), ((3, 4), 'B'), (5, 'B'), ('B', 8), (8, 'C'), ('C', 'D'), ('D', 'E'), ('E', 1)]

G.add_edges_from(edges)

# Finding the shortest path between all pairs of nodes
shortest_paths=dict(nx.all_pairs_shortest_path(G))

# Printing the shortest paths
for source_node, paths in shortest_paths.items():
for target_node,path in paths.items():
if source_node!=target_node:
print("Shortest path from node",source_node, "to node",target_node, "is : ", path)

# Plotting the graph using networkx.draw
pos=nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_size=500)
plt.title('Directed Graph')
plt.show()

0 comments on commit 16c3dd4

Please sign in to comment.