Click here to Skip to main content
13,202,706 members (54,128 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 21 May 2017

Millenium Problem Solved ?!

, 21 Jun 2017
Rate this:
Please Sign up or sign in to vote.
Implementation of a polynomial time algorithm searching Hamilton cycles in an undirected graph

Update info

The code was updated.

  • Now the application allows testing the validity of a path for being a Hamiltonian cycle of the current graph. The current graph is the graph which was opened from file or created by hand. See the class CycleTester and its usage.
  • The path may be read from a *.path-file or *.txt-file (see the button 'Open Ham Path'). This file must contain a succesion of comma separated (or space separated) numbers of graph nodes. Nodes numbering starts with zero.
  • One more view mode was added (click 'Switch View' button several times).


Most of you heard about Seven Millennium Prize Problems established by (The Clay Mathematics Institute of Cambridge) with $1 million allocated to the solution of each problem. It seems that one of these problems was solved by professor (Lizhi Du), Wuhan University of Science, China.


I read his article a year ago, but could not decipher the logic of the algorithm (and for sure, I doubted the real success). Later I tried to implement the algorithm but after some efforts gave up. Unfortunately the author does not open his C++ code.

There is a great advantage of being a teacher - you can give a task to a student, and if he is happened to have exceptional mind (moreover, a great patience, belief in his own abilities, belief that a teacher does not doubt a success), then you can win. And this happened to me and my student Ivan Fedorov. He worked very hard for a month (or more), putting aside all other obligations and finally succeeded. He managed to get the logic of written words (description of the algorithm that you can find using the link above) and convert it to a working code. But first, you should be sure that solution is possible.

Main Idea

The main idea of prof. Lizhi Du's approach is to present a graph (any undirected graph) as a ring of nodes (see Fig.1) and try to mend the breaks in successive chain of nodes. A break (or breakpoint by the author's terminology) is the absence of an edge between two neighboring nodes (along the ring chain).


If you managed to mend all the breaks, then Hamiltonian cycle is found. It is just the succession of nodes along the ring. Long ago I developed an app that allows to redraw any graph in a RingView fashion and drag its nodes all over. If you try (using the application which accompanies this article) to intuitively drag the nodes of some graph trying to mend all the breaks, quite often you can succeed.

See Fig.2 where one break was mended by dragging down the node #8 (compare Fig.1 and Fig.2).

Fig 3. shows the final result of mending all the breaks by dragging several nodes. Compare Fig.3 and Fig.1. Pay attention to nodes' numbers. Consider the fact that while dragging, you don't change the graph structure (or adjacency matrix). As you see all the breaks were mended and now we have a Hamiltonian cycle (it is the succession of nodes along the ellipsoidal ring).

This kind of exercises made me believe that prof. Lizhi Du's approach is quite reasonable. Showing this trick to Ivan Fedorov I made him believe that solution is near. All you have to do is to convert intuitive dragging manipulations to some strict programming logic. Oh, don't forget to use the description of the Lizhi Du's algorithm on several pages of PDF document.

Inspired by this logic Ivan started working. The resulting code turned out to be very impressive in a sense that it worked! But it was not so good in a sense that it was compressed into two big functions with many repetitions and very-hard-to-follow logic. After two days pondering over the problem and wrestling with the code, I hope the code became more readable and manageable. Now it is encapsulated in a class that has 8 methods (each of them implements a separate step of the algorithm). The code works a little faster than the original (two functions approach).

Points of Interest

Even now I am not sure that can clearly explain all the magic (especially the logic of methods named TrySecond, TryThird and CutAndInsert). I think I perceived and can explain the rotational technique used to change the graph structure, but nevertheless I am not 100% sure that I could explain all the actions. Ivan says that he shares my opinion in spite of the fact that it was he who resurrected the code from honest but rather verbose Lizhi Du's document (see the link to his article above).

Code Usage

Data files have an extension '*.gv' (GraphView Files). It is the simplest format (the list of adjacent nodes for each of the graph nodes). If you change the filter in OpenFileDialog to '*.hcp', you can open many of the existing in the net graph files. The format of such files is very simple -- a list of edges (pairs of connected nodes). I placed only two such files in Data folder of the project. The file named 'TestGraph_11_4.hcp' does not contain Hamiltonian cycle, but prof. Lizhi Du has found a Hamiltonian path in this graph. Note, our implementation does not search for Hamiltonian paths (only cycles are at stake).

While playing with the application, first open a file (Ctrl+O) with the small digit prefixes and try to use older known algorithms (Posa, Roberts and Flores, Recursive Backtracking). They were implemented by me several years ago. By the way, Posa's algorithm not always finds the solution, for it selects nodes randomly (description of algorithm is  here). Try it several times on simple graphs and it sometimes will find a solution (may be different each time). More capable, but not so fast, is the algorithm created by Roberts and Flores (you may find its description in the net). It surely will find a Hamiltonian cycle or will give a message that one does not exist.

You can circularly toggle the currently selected algorithm by pressing right arrow key. Press space to start the search. You also can see the inner cooking (details) of searching algorithm by setting a delay of animation (use toolstrip button with the tooltip: Change Animation Delay). Don't forget to set the delay time to zero when you open some bigger graphs (the files, after 09 Knight.gv). This graph corresponds to the famous Knight's tour problem which is mentioned in Wikipedia. In order to see the essence of Lizhi Du's algorithm (and the purpose of this article) do the following steps:

  • Open the file 09 Knight.gv,
  • Select the algorithm RobertsFlores
  • Clear the delay (use button Change Animation Delay).
  • Press space and go drink some coffee. Computer will be busy for several hours because finding a solution requires billions of backtracking steps.
  • Stop the algorithm if you you don't want to wait (use toolstrip button Stop the search).
  • Select the algorithm LizhiDu and press spacebar.

The solution will be found instantly (if you did not forget to clear the delay). The algorithm requires only 234 steps. That's fantastic!


If the Math community will adopt the professor Lizhi Du's proof of the solution the famous P vs NP Problem, then as (the authorities warn us), all Public Key RSA cryptosystems will go down. Should it be troubling news for us? I don't know, I always neglected security problems. The mathematicians would have to invent something new.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander Chernosvitov
Instructor / Trainer Peter the Great St.Petersburg Polytechnic Universi
Russian Federation Russian Federation
Peter the Great St. Petersburg Polytechnic University professor,
Microsoft Authorized Educational Center trainer,
Microsoft Certified Professional (C# Desktop Apps and MFC)
Lectures on OOP, C# and C++, Windows programming with C# and C++.
Have long practice and experience in finding the right way to formulate and numerically solve differential equations.

You may also be interested in...


Comments and Discussions

QuestionA famous German man has a paper on arxiv said: P!=NP, my oppinion Pin
Lizhi Du17-Aug-17 16:49
memberLizhi Du17-Aug-17 16:49 
SuggestionHi, every friends: Pin
Lizhi Du2-Jul-17 23:20
memberLizhi Du2-Jul-17 23:20 
QuestionLet's test it on other NP complete problems Pin
Member 1327397822-Jun-17 5:56
memberMember 1327397822-Jun-17 5:56 
AnswerRe: Let's test it on other NP complete problems Pin
Lizhi Du22-Jun-17 22:26
memberLizhi Du22-Jun-17 22:26 
GeneralRe: Let's test it on other NP complete problems Pin
Yang Kok Wah23-Jun-17 1:24
memberYang Kok Wah23-Jun-17 1:24 
GeneralRe: Let's test it on other NP complete problems Pin
Lizhi Du23-Jun-17 2:27
memberLizhi Du23-Jun-17 2:27 
GeneralRe: Let's test it on other NP complete problems Pin
Claude Chaunier23-Jun-17 6:01
memberClaude Chaunier23-Jun-17 6:01 
GeneralRe: Let's test it on other NP complete problems Pin
Lizhi Du23-Jun-17 11:39
memberLizhi Du23-Jun-17 11:39 
GeneralRe: Let's test it on other NP complete problems Pin
Claude Chaunier24-Jun-17 3:10
memberClaude Chaunier24-Jun-17 3:10 
GeneralRe: Let's test it on other NP complete problems Pin
Lizhi Du23-Jun-17 2:18
memberLizhi Du23-Jun-17 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 21 Jun 2017
Article Copyright 2017 by Alexander Chernosvitov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid