@@ -1239,10 +1239,14 @@ cpdef tuple find_hamiltonian(G, long max_iter=100000, long reset_bound=30000,
1239
1239
path::
1240
1240
1241
1241
sage: G = graphs. HyperStarGraph( 5, 2)
1242
- sage: fh( G,find_path=False)
1243
- ( False, ['01100', '10100', '00110', '10010', '00011', '10001', '01001', '11000', '01010' ])
1244
- sage: fh( G,find_path=True)
1245
- ( False, ['00110', '10010', '01010', '11000', '01100', '10100', '00101', '10001', '00011' ])
1242
+ sage: G. order( )
1243
+ 10
1244
+ sage: b, P = fh( G,find_path=False)
1245
+ sage: b, len( P)
1246
+ ( False, 9)
1247
+ sage: b, P = fh( G,find_path=True)
1248
+ sage: b, len( P)
1249
+ ( False, 9)
1246
1250
1247
1251
The method can also be used for directed graphs::
1248
1252
@@ -1256,8 +1260,10 @@ cpdef tuple find_hamiltonian(G, long max_iter=100000, long reset_bound=30000,
1256
1260
( False, ['d', 'c', 'b', 'a' ])
1257
1261
sage: H = DiGraph( )
1258
1262
sage: H. add_cycle( 'abcdefgh')
1259
- sage: fh( H)
1260
- ( True, ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'a' ])
1263
+ sage: H. add_edge( 'dg')
1264
+ sage: b, C = fh( H)
1265
+ sage: b, len( C)
1266
+ ( True, 8)
1261
1267
sage: H. delete_edge( 'ab')
1262
1268
sage: fh( H, find_path=False)
1263
1269
( False, ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'a' ])
@@ -1312,6 +1318,13 @@ cpdef tuple find_hamiltonian(G, long max_iter=100000, long reset_bound=30000,
1312
1318
( False, [0, 1, 2, 3 ])
1313
1319
sage: fh( G, find_path=True)
1314
1320
( False, [0, 1, 2, 3 ])
1321
+
1322
+ Check that the method is robust to incomparble vertices::
1323
+
1324
+ sage: G = Graph( [(1, 'a'), ('a', 2), (2, 3), (3, 1) ])
1325
+ sage: b, C = fh( G, find_path=False)
1326
+ sage: b, len( C)
1327
+ ( True, 4)
1315
1328
"""
1316
1329
G._scream_if_not_simple()
1317
1330
@@ -1471,7 +1484,7 @@ cpdef tuple find_hamiltonian(G, long max_iter=100000, long reset_bound=30000,
1471
1484
longest = length
1472
1485
longest_reversed = reverse
1473
1486
1474
- if not directed and not longer:
1487
+ if not directed and not longer and out_degree(sd, path[length - 1 ]) > 1 :
1475
1488
# We revert a cycle to change the extremity of the path
1476
1489
degree = out_degree(sd, path[length - 1 ])
1477
1490
while True :
0 commit comments