Code Explanation:
import networkx as nx:
This imports the networkx library and assigns it the alias nx. NetworkX is used for creating and working with graphs and networks.
G = nx.Graph():
This creates an empty graph object G. At this point, the graph does not contain any nodes or edges.
G.add_nodes_from([1, 2, 3]):
The add_nodes_from() method adds multiple nodes to the graph. Here, nodes with the identifiers 1, 2, and 3 are added to the graph G. After this step, the graph contains three nodes: 1, 2, and 3.
print(len(G.nodes)):
The nodes attribute of the graph contains a list of all the nodes in the graph. len(G.nodes) gives the number of nodes present in the graph.
Since three nodes were added, len(G.nodes) will return 3.
Final Output:
The output will be the number of nodes in the graph, which is 3.
C: 3
0 Comments:
Post a Comment