{"id":20305075,"url":"https://github.com/supermitch/chinese-postman","last_synced_at":"2025-04-11T14:50:18.488Z","repository":{"id":25901946,"uuid":"29342589","full_name":"supermitch/Chinese-Postman","owner":"supermitch","description":"My Python solution to the Chinese-Postman problem.","archived":false,"fork":false,"pushed_at":"2023-04-05T15:14:13.000Z","size":1089,"stargazers_count":40,"open_issues_count":2,"forks_count":19,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-25T10:51:17.026Z","etag":null,"topics":["circuit","dijkstra","graph","network"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"godotengine/godot","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/supermitch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-16T09:52:00.000Z","updated_at":"2024-12-25T07:35:10.000Z","dependencies_parsed_at":"2022-09-01T13:30:24.167Z","dependency_job_id":null,"html_url":"https://github.com/supermitch/Chinese-Postman","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/supermitch%2FChinese-Postman","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supermitch%2FChinese-Postman/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supermitch%2FChinese-Postman/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supermitch%2FChinese-Postman/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/supermitch","download_url":"https://codeload.github.com/supermitch/Chinese-Postman/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248423999,"owners_count":21101094,"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":["circuit","dijkstra","graph","network"],"created_at":"2024-11-14T17:06:56.795Z","updated_at":"2025-04-11T14:50:18.459Z","avatar_url":"https://github.com/supermitch.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chinese-Postman Solver\n\nI wrote this program to solve the\n[Chinese Postman problem](http://en.wikipedia.org/wiki/Route_inspection_problem).\n\n\u003e The Chinese Postman Problem, or \"route inspection problem\"\n\u003e is to find a shortest closed circuit that visits _every edge_ of a\n\u003e (connected) undirected graph.\n\n## Inspiration\n\nI was inspired to learn about and solve this problem when I thought it would\nbe cool to follow every trail in\n[Pacific Spirit Park](http://en.wikipedia.org/wiki/Pacific_Spirit_Regional_Park)\nin one run.\n\nGiven that the park contains over 73 km of trail, I need to find the optimum\nEularian Path. Otherwise it's going to be a really, really long run!\n\n\n## The Process\n\nThe solution is roughly a three-step process:\n\n1. Determine if the graph has an\n[Eularian Path](http://en.wikipedia.org/wiki/Eulerian_path)\n    (Very easy)\n2. Make the non-Eularian graph Eularian, at the minimum expense\n    (Not so easy)\n3. Find the fudged Eularian path\n    (Pretty easy)\n\n### Solving Minimum Expense\n\nIn order to convert a non- or semi-Eularian graph to an Eularian one,\nyou must eliminate odd nodes (nodes having an odd number of edges.)\n\nTo eliminate an odd node, you need to add another edge to it (essentially\nretracing your steps.) However, this comes as a cost! The goal then is\nto find out which edges to repeat, that eliminate all the odd nodes, with\nthe minimum cost.\n\n1. Find all possible combinations of pairs of odd nodes\n2. Using Dijkstra's Algorithm, find the cost of the minimum path between\nthose pairs\n3. Find which set of paths (depending on how many odd nodes you have)\nthat results in the least total cost\n4. Modify your graph with these new parallel edges\n\nNow you have an Eularian graph with only even nodes, for which an Eularian\nCircuit can be found.\n\n### Solving the Eularian Circuit\n\nSolving the Eularian Circuit (now that we have one) is relatively easy. At\nfirst, I simply walked the edges randomly until I happened to find a route\nthat either dead-ended, or resulted in a circuit. Then I implemented [Fleury's\nAlgorithm](http://en.wikipedia.org/wiki/Eulerian_path#Fleury.27s_algorithm)\nwhich says always choose a non-bridge over a bridge (for obvious\nreasons). Now it takes very few attempts to solve most circuits.\n\nLater I will implement an alternative circuit finding method (Hierholzer's?)\n\n## Usage\n\nThis program will probably run in Python 2.7 and definitely in Python 3.4-3.10\n(Tested recently w/ Python 3.10.6.)\n\n* Usage: `python main.py -h`\n* Specify which graph to load by adding the graph name `python main.py north`\n* Optionally specify the starting node, .e.g `python main.py square 4` would produce\n`[4, 3, 2, 1, 4]`.\n\nYou can find all the graph names in the [data/data.py](data/data.py) file.\n\n\n## Tests\n\nThere are unit tests included, in the `tests` directory.\nYou can run these via\n\n```bash\npython tests/run_tests.py\n```\n\nfrom the root project folder.\n\n## Graph Format\n\n* A graph is defined as a list containing tuples\n* Each tuple in the list represents an edge\n* Edges are defined as `(start node, end node, length)`\n\nFor example, an equilateral triangle like:\n```\n   1\n  / \\\n 2 - 3\n ```\nCould be represented as:\n`triangle = [(1, 2, 1), (2, 3, 1), (3, 1 ,1)]`\n\nIf an edge is [directed](https://en.wikipedia.org/wiki/Directed_graph)\nyou can add an optional fourth argument, `True`, e.g.\n`(1, 2, 5, True)` for a one-way edge from Node 1 to\nNode 2 of length 5.\n\nSee [network.py](chinesepostman/network.py) for the actual implementation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsupermitch%2Fchinese-postman","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsupermitch%2Fchinese-postman","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsupermitch%2Fchinese-postman/lists"}