Click here to Skip to main content
15,888,984 members
Articles / Database Development / Data Visualization

Millenium Problem Solved ?!

Rate me:
Please Sign up or sign in to vote.
4.86/5 (50 votes)
7 Jan 2023CPOL6 min read 98.6K   585   38   83
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 succession 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).

Introduction

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.

Background

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 happens to have an 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 the 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).

Breaks

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).

Image 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).

Image 3

This kind of exercise made me believe that Prof. Lizhi Du's approach is quite reasonable. Showing this trick to Ivan Fedorov, I made him believe that the 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 of 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 eight 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 does 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 looking for the solution for several hours because it requires billions of backtracking steps.
  • Stop the algorithm (use toolstrip button Stop the search) if you don't want to wait.
  • 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!

Conclusion

If the Math community will adopt 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.

History

  • 21st May, 2017: Initial version

License

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


Written By
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.

Comments and Discussions

 
QuestionWhat is at stake here? Pin
James Hunter Ross7-Jun-17 13:55
James Hunter Ross7-Jun-17 13:55 
AnswerRe: What is at stake here? Pin
Member 113980948-Jun-17 6:39
Member 113980948-Jun-17 6:39 
QuestionDear Professor Alexander Chernosvitov Pin
Lizhi Du5-Jun-17 13:55
Lizhi Du5-Jun-17 13:55 
AnswerRe: Dear Professor Alexander Chernosvitov Pin
Alexander Chernosvitov10-Jun-17 4:00
Alexander Chernosvitov10-Jun-17 4:00 
GeneralRe: Dear Professor Alexander Chernosvitov Pin
Lizhi Du10-Jun-17 12:35
Lizhi Du10-Jun-17 12:35 
AnswerTo all: I am Lizhi Du Pin
Lizhi Du31-May-17 12:48
Lizhi Du31-May-17 12:48 
BugSeems to fail with my test graph Pin
kchris8031-May-17 10:11
kchris8031-May-17 10:11 
GeneralRe: Seems to fail with my test graph Pin
Lizhi Du31-May-17 14:04
Lizhi Du31-May-17 14:04 
Dear Sir: I am Lizhi Du

I do not know which program you run.
Your example is very very easy for my program.
But my program only compute a Hamilton path from vertex 0 to n-1 or from 1 to n.
I give the result.

If anyone say some example cannot be compute, give the example to me, I give your the correct result.

1 2 3 130 131 132 127 128 129 124 125 126 121 122 123 118 119 120 115 116 117 112 113 114 292 293 294 109 110 111 106 107 108 103 104 105 100 101 102 286 287 288 97 98 99 94 95 96 283 284 285 91 92 93 88 89 90 85 86 87 82 83 84 79 80 81 76 77 78 274 275 276 73 74 75 70 71 72 67 68 69 133 134 135 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 280 281 282 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 289 290 291 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 268 269 270 265 266 267 64 65 66 61 62 63 58 59 60 55 56 57 52 53 54 295 296 297 49 50 51 46 47 48 43 44 45 40 41 42 37 38 39 34 35 36 31 32 33 28 29 30 25 26 27 22 23 24 19 20 21 16 17 18 13 14 15 10 11 12 7 8 9 4 5 6 271 272 273 136 137 138 139 140 141 142 143 144 145 146 147 277 278 279 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 298 299 300 190 191 192 193 194 195 196 197 198 301 302 303
GeneralRe: Seems to fail with my test graph Pin
Member 1069906312-Jul-18 2:48
Member 1069906312-Jul-18 2:48 
GeneralRe: Seems to fail with my test graph Pin
Member 113980944-Jun-17 17:55
Member 113980944-Jun-17 17:55 
GeneralRe: Seems to fail with my test graph Pin
Alexander Chernosvitov5-Jun-17 6:30
Alexander Chernosvitov5-Jun-17 6:30 
GeneralRe: Seems to fail with my test graph Pin
Member 113980945-Jun-17 8:02
Member 113980945-Jun-17 8:02 
Questionlink to more papers Pin
Jan Heckman29-May-17 3:09
professionalJan Heckman29-May-17 3:09 
SuggestionI am LiZhi Du, to all Pin
Lizhi Du26-May-17 18:42
Lizhi Du26-May-17 18:42 
GeneralRe: I am LiZhi Du, to all Pin
Alexander Chernosvitov27-May-17 1:30
Alexander Chernosvitov27-May-17 1:30 
GeneralRe: I am LiZhi Du, to all Pin
Lizhi Du27-May-17 12:29
Lizhi Du27-May-17 12:29 
GeneralRe: I am LiZhi Du, to all Pin
Member 1139809427-May-17 9:43
Member 1139809427-May-17 9:43 
GeneralRe: I am LiZhi Du, to all Pin
Lizhi Du27-May-17 12:09
Lizhi Du27-May-17 12:09 
GeneralRe: I am LiZhi Du, to all Pin
Member 1139809428-May-17 3:25
Member 1139809428-May-17 3:25 
GeneralRe: I am LiZhi Du, to all Pin
Lizhi Du28-May-17 4:02
Lizhi Du28-May-17 4:02 
GeneralRe: I am LiZhi Du, to all Pin
johnwiliam6-Jun-17 0:58
johnwiliam6-Jun-17 0:58 
QuestionWorking on a more rigorous explanation for "main branches", but then I see the following on his Researchgate prof. Pin
Member 1139809426-May-17 9:30
Member 1139809426-May-17 9:30 
AnswerRe: Working on a more rigorous explanation for "main branches", but then I see the following on his Researchgate prof. Pin
Alexander Chernosvitov27-May-17 2:59
Alexander Chernosvitov27-May-17 2:59 
GeneralRe: Working on a more rigorous explanation for "main branches", but then I see the following on his Researchgate prof. Pin
Member 1139809427-May-17 6:54
Member 1139809427-May-17 6:54 
GeneralRe: Working on a more rigorous explanation for "main branches", but then I see the following on his Researchgate prof. Pin
Mikolaj Barwicki27-May-17 7:32
Mikolaj Barwicki27-May-17 7:32 

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.