{"id":19975924,"url":"https://github.com/jfjlaros/hamilton","last_synced_at":"2026-02-16T02:36:59.737Z","repository":{"id":80229617,"uuid":"54911364","full_name":"jfjlaros/hamilton","owner":"jfjlaros","description":"Hamiltonian path and cycle finder.","archived":false,"fork":false,"pushed_at":"2022-04-08T13:27:25.000Z","size":427,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-12T09:47:25.322Z","etag":null,"topics":["cycle","hamiltonian","knight-tour","path"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jfjlaros.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-28T17:48:04.000Z","updated_at":"2022-08-17T18:24:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"6a4e3f46-240d-4c47-86d3-686ae37cc1da","html_url":"https://github.com/jfjlaros/hamilton","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfjlaros%2Fhamilton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfjlaros%2Fhamilton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfjlaros%2Fhamilton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfjlaros%2Fhamilton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jfjlaros","download_url":"https://codeload.github.com/jfjlaros/hamilton/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241411473,"owners_count":19958746,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cycle","hamiltonian","knight-tour","path"],"created_at":"2024-11-13T03:20:58.807Z","updated_at":"2026-02-16T02:36:54.719Z","avatar_url":"https://github.com/jfjlaros.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hamiltonian path finder\nThis library provides functions to find a Hamiltonian path or cycle in a graph\nthat is induced by a rectangular board and a list of moves.\n\nFor a more colourful explanation, see this [notebook](heatmap.ipynb).\n\n```python\n\u003e\u003e\u003e import yaml\n\u003e\u003e\u003e\n\u003e\u003e\u003e from hamilton import Hamilton\n\u003e\u003e\u003e\n\u003e\u003e\u003e # Load the ruleset from a file and make a new class instance using an\n\u003e\u003e\u003e # 8 by 8 board starting at (0, 0).\n\u003e\u003e\u003e moves = yaml.load(open('knight.yml'))['moves']\n\u003e\u003e\u003e knight = Hamilton(moves, 8, 8, 0, 0)\n\u003e\u003e\u003e\n\u003e\u003e\u003e # Find a Hamiltonian path.\n\u003e\u003e\u003e knight.solve()\nTrue\n```\n\nThe result is stored in `knight.board` and can be pretty-printed by calling the\n`__str__` function.\n\n```python\n\u003e\u003e\u003e print knight\n  1 26 15 24 29 50 13 32\n 16 23 28 51 14 31 64 49\n 27  2 25 30 63 60 33 12\n 22 17 52 59 44 57 48 61\n  3 42 21 56 53 62 11 34\n 18 39 54 43 58 45  8 47\n 41  4 37 20 55  6 35 10\n 38 19 40  5 36  9 46  7\n\n```\n\nThe starting position can be altered using the `reset` function.\n\n```python\n\u003e\u003e\u003e knight.reset(5, 5)\n\u003e\u003e\u003e knight.solve()\nTrue\n\u003e\u003e\u003e print knight\n 47 64 13 54 27  6 11  8\n 14 55 48 61 12  9 26  5\n 63 46 57 30 53 28  7 10\n 56 15 62 49 60 31  4 25\n 45 58 43 52 29 50 21 32\n 16 37 40 59 42  1 24  3\n 39 44 35 18 51 22 33 20\n 36 17 38 41 34 19  2 23\n\n```\n\nFinding a Hamiltonian cycle can be done by passing `closed=True` to the constructor, or by using the `reset` function.\n\n```python\n\u003e\u003e\u003e knight.reset(5, 5, closed=True)\n\u003e\u003e\u003e knight.solve()\nTrue\n\u003e\u003e\u003e print knight\n 47 62 13 54 27  6 11  8\n 14 55 48 63 12  9 26  5\n 61 46 57 30 53 28  7 10\n 56 15 60 49 64 31  4 25\n 45 58 43 52 29 50 21 32\n 16 37 40 59 42  1 24  3\n 39 44 35 18 51 22 33 20\n 36 17 38 41 34 19  2 23\n\n```\n\nTo see how many times backtracking was needed, use the `retries` member\nvariable.\n\n```python\n\u003e\u003e\u003e knight.retries\n1\n```\n\n## Command line interface\nMake a 10 by 10 board and use the rules defined in `metita.yml` to find a\nHamiltonian path starting at position (0, 0).\n\n```bash\n$ hamilton metita.yml 10 10 0 0\n   1  55  43  16  93  42  70 100  41  35\n  58  18   3  57  66   4  39  65   5  38\n  44  15  96  54  69  97  94  36  71  99\n   2  56  63  17  92  64  67  85  40  34\n  59  19  45  77  95  29  80  98   6  37\n  52  14  89  53  68  88  91  33  72  86\n  23  49  62  28  81  78  27  84  79  10\n  60  20  46  76  90  30  73  87   7  32\n  51  13  24  50  12  25  82  11  26  83\n  22  48  61  21  47  75   8  31  74   9\n\nNumber of retries: 0\n```\n\nTo find a closed path, use the `-c` option.\n\n```bash\n$ hamilton -c metita.yml 10 10 0 0\n   1  55  43  16  93  42  70  98  41  35\n  58  18   3  57  66   4  39  65   5  38\n  44  15 100  54  69  99  94  36  71  97\n   2  56  63  17  92  64  67  85  40  34\n  59  19  45  77  95  29  80  96   6  37\n  52  14  89  53  68  88  91  33  72  86\n  23  49  62  28  81  78  27  84  79  10\n  60  20  46  76  90  30  73  87   7  32\n  51  13  24  50  12  25  82  11  26  83\n  22  48  61  21  47  75   8  31  74   9\n\nNumber of retries: 1\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjfjlaros%2Fhamilton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjfjlaros%2Fhamilton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjfjlaros%2Fhamilton/lists"}