Skip to content

Commit

Permalink
nx tutorial script
Browse files Browse the repository at this point in the history
  • Loading branch information
Ytemiloluwa committed Oct 6, 2023
1 parent 9ddd9ef commit 788d1dc
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2023-round-2/Ytemiloluwa/nx_tutorial_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import networkx as nx
import matplotlib.pyplot as plt

# Create a DiGraph graph object
DG = nx.DiGraph()

# Adding nodes of multiple types (int, str, tuple)
DG.add_nodes_from([2, 'nodeA', (1, 1), 3, 'nodeB', (2, 2), 4, 'nodeC', (3, 3), 5])

# Adding multiple edges between nodes
edges = [(2, 'nodeA'), ('nodeA', (1, 1)), (3, 'nodeB'), ('nodeB', (2, 2)), (4, 'nodeC'), ('nodeC', (3, 3)), (5, 2), (5, 'nodeC')]
DG.add_edges_from(edges)

# Finding the shortest path between all pairs of nodes in this graph.
shortestPath = dict(nx.all_pairs_shortest_path(DG))
for source, path in shortestPath.items():
for destination, path in path.items():
print("Shortest path from", source, "to ", destination, "is -> ", path)

# Plotting the graph using networkx.draw
pos = nx.spring_layout(DG)
nx.draw(DG, pos, with_labels=True, node_size=500)
plt.title("A DiGraph")
plt.show()

0 comments on commit 788d1dc

Please sign in to comment.