Using the Python library NetworkX to find all Hamiltonian cycles in a graph

For the purposes of a lecture in graph theory, I created the following example of a Hamiltonian graph consisting of the complete graphs K_3, K_4 and K_5 joined pairwise by an edge:

The Python library NetworkX can be used to perform many of the calculations that arise in graph theory and networks and I used it in this case to list and count all the Hamiltonian cycles in the above graph. In the present post, I want to quickly record the Python code I used for this (in a Jupyter Notebook):

Published by Dr Christian P. H. Salas

Mathematics Lecturer

Leave a comment