Why do generated graphs in NetworkX start at 0 and not 1?

Viewed 249

I'm a novice in learning NetworkX. When I learned how to create a network via coding, taking network G as an example, the textbook told me to use code "G.add_nodes_from(1,2,3,4)." However, when it comes to subnetworks, the book gave an example. The code said:

K5 = nx.complete_graph(5)
clique = nx.subgraph(K5, (0,1,2))

My question is: for this network, why does the number of nodes start from 0? It is 0,1,2,3,4. Why is it not "1,2,3,4,5"? I'm confused.

Thanks for all the help in advance!

1 Answers

This is just by design. It is mentioned in the docs:

Node labels are the integers 0 to n-1.

This is also coherent with indices in python (and other languages in general). The first index in python lists and other indexable iterables is always 0.

Related