From 4881c32296fde5a9394458b4daee14779ef7e6b9 Mon Sep 17 00:00:00 2001 From: Harsh Sharma Date: Thu, 13 Oct 2022 10:03:38 +0530 Subject: [PATCH] digraph python script task --- 2022-round-2/hrshshrm/nx_tutorial_script.py | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2022-round-2/hrshshrm/nx_tutorial_script.py diff --git a/2022-round-2/hrshshrm/nx_tutorial_script.py b/2022-round-2/hrshshrm/nx_tutorial_script.py new file mode 100644 index 0000000..f523322 --- /dev/null +++ b/2022-round-2/hrshshrm/nx_tutorial_script.py @@ -0,0 +1,32 @@ +import networkx as nx + +# Create a NetworkX DiGraph graph object +DG = nx.DiGraph() + +# Add nodes of multiple types to this graph object +listOfNodes = [ 1, 7, ("NetworkX", "Outreachy"), "DiGraph", ("Agent", 47), "2B" ] +DG.add_nodes_from(listOfNodes) + +# Add multiple edges between these nodes +listOfEdges = [ + (1, 7), + (1, "2B"), + (1, ("Agent", 47)), + (7, "2B"), + (7, "DiGraph"), + (("NetworkX", "Outreachy"), "DiGraph"), + (("NetworkX", "Outreachy"), 7), + (("NetworkX", "Outreachy"), ("Agent", 47)), + (("Agent", 47), 1), + ("2B", "DiGraph"), + ("2B", ('NetworkX', 'Outreachy'),) +] +DG.add_edges_from(listOfEdges) + +# Find the shortest path between all pairs of nodes in this graph and print them. +shortestPaths = dict(nx.all_pairs_shortest_path(DG)) +for key in shortestPaths: + print(key, shortestPaths[key]) + +# Plot the graph using networkx.draw +nx.draw(DG, nx.shell_layout(DG), with_labels=True, font_weight="bold")