I am starting to work with IDE Jupyter && Python 3.6 and a question has arisen. I have to draw through the IDE, a Hamiltonian path in the Petersen subgraph, but I do not know how to do it.
I show information about said graph:
- Graph of Petersen: https://en.wikipedia.org/wiki/Petersen_graph
- Hypohamiltonian graph: https://en.wikipedia.org/wiki/Hypohamiltonian_graph
Any idea of how you can make the comments?
Thank you very much.
To compute the Hamiltonian graph in Petersen graph we can use the solution from this answer
I've forgotten whether or not Petersen graphs are isomorphic to any of their vertex permutations so I will assume they are not. Therefore, instead of searching for pairs of vertices which form the ends of the path we will add two new vertices connected to every vertex of the original graph. So if a Hamiltonian path exists in the original graph, it will exist in this extended graph -- just cut off the two extra vertices (-1) and (-2).
Now we can apply the algorithm from the post:
Since this algorithm returns list of ALL paths between given vertices we will filter them only to Hamiltonian paths and cut off the extra vertices.
Surely, this can be more efficient, but I leave the optimizations to either you or someone else. For such a small graph as Petersen it works quickly enough in my opinion.
DRAWING
We randomly choose one path and store it in
ham_path
variable.Then we will use the
networkx
package to draw the graph and the chosen path.We create a
networkx
graph, and each edge that is in Hamiltonian path will be colored red and bold. On the other hand, every other edge will be thinner and black. We also do not want the extra vertices in our drawing.