Title: Traveling Salesman Problem with Python: Greedy and Brute Force - AskPython
Open Graph Title: Traveling Salesman Problem with Python: Greedy and Brute Force - AskPython
X Title: Traveling Salesman Problem with Python: Greedy and Brute Force - AskPython
Description: Last quarter I had to plan a 12-stop delivery loop and the route my gut picked came out 38% longer than the version a small Python script returned in under a
Open Graph Description: Last quarter I had to plan a 12-stop delivery loop and the route my gut picked came out 38% longer than the version a small Python script returned in under a
X Description: Last quarter I had to plan a 12-stop delivery loop and the route my gut picked came out 38% longer than the version a small Python script returned in under a
Opengraph URL: https://www.askpython.com/python/examples/travelling-salesman-problem-python
X: @AskPython
Generator: WordPress 7.0
Domain: www.askpython.com
{"@context":"https://schema.org","@graph":[{"@type":["Person","Organization"],"@id":"https://www.askpython.com/#person","name":"Rnzo1zWtYm","sameAs":["https://twitter.com/AskPython"],"logo":{"@type":"ImageObject","@id":"https://www.askpython.com/#logo","url":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","contentUrl":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","caption":"Rnzo1zWtYm","inLanguage":"en-US"},"image":{"@type":"ImageObject","@id":"https://www.askpython.com/#logo","url":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","contentUrl":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","caption":"Rnzo1zWtYm","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.askpython.com/#website","url":"https://www.askpython.com","name":"Rnzo1zWtYm","publisher":{"@id":"https://www.askpython.com/#person"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.askpython.com/wp-content/uploads/2025/04/Traveling_Salesman_Problem_with_Python___1744317160.png","url":"https://www.askpython.com/wp-content/uploads/2025/04/Traveling_Salesman_Problem_with_Python___1744317160.png","width":"1200","height":"630","caption":"Featured Image For: Traveling Salesman Problem With Python: Greedy And Brute Force","inLanguage":"en-US"},{"@type":["WebPage","FAQPage"],"@id":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#webpage","url":"https://www.askpython.com/python/examples/travelling-salesman-problem-python","name":"Traveling Salesman Problem with Python: Greedy and Brute Force - AskPython","datePublished":"2026-07-06T11:36:56+00:00","dateModified":"2026-07-06T11:36:56+00:00","isPartOf":{"@id":"https://www.askpython.com/#website"},"primaryImageOfPage":{"@id":"https://www.askpython.com/wp-content/uploads/2025/04/Traveling_Salesman_Problem_with_Python___1744317160.png"},"inLanguage":"en-US","mainEntity":[{"@type":"Question","url":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#faq-question-1783337810781","name":"What is the travelling salesman problem in one sentence?","acceptedAnswer":{"@type":"Answer","text":"Given a list of cities and the distance between every pair, find the shortest loop that visits each city once and returns to the start. It is NP-hard in general."}},{"@type":"Question","url":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#faq-question-1783337810782","name":"Is the greedy algorithm always optimal?","acceptedAnswer":{"@type":"Answer","text":"No. Greedy makes a locally optimal choice at every step and can miss the global optimum. On random inputs it typically lands 10% to 25% above the brute-force optimum."}},{"@type":"Question","url":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#faq-question-1783337810783","name":"When is brute force practical?","acceptedAnswer":{"@type":"Answer","text":"Brute force is practical for n up to about 10 or 11. At n=10 the work is 9! = 362,880 permutations. At n=15 the work is 14! = 87 billion and the loop is no longer a practical option."}},{"@type":"Question","url":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#faq-question-1783337810784","name":"What algorithm should I use for n between 12 and 20?","acceptedAnswer":{"@type":"Answer","text":"Held-Karp dynamic programming in O(n\u00b2 \u00b7 2\u207f) gives the exact answer and runs in seconds for n up to 20. Beyond that, switch to OR-Tools or a Lin-Kernighan heuristic."}}]},{"@type":"Person","@id":"https://www.askpython.com/author/rishabh","name":"Rishabh Das","url":"https://www.askpython.com/author/rishabh","image":{"@type":"ImageObject","@id":"https://secure.gravatar.com/avatar/0389ec298723119db63d64b1a087a2492d2b22fb1acd4dceb804e076e8a94465?s=96&d=mm&r=g","url":"https://secure.gravatar.com/avatar/0389ec298723119db63d64b1a087a2492d2b22fb1acd4dceb804e076e8a94465?s=96&d=mm&r=g","caption":"Rishabh Das","inLanguage":"en-US"}},{"@type":"BlogPosting","headline":"Traveling Salesman Problem with Python: Greedy and Brute Force - AskPython","keywords":"Travelling salesman","datePublished":"2026-07-06T11:36:56+00:00","dateModified":"2026-07-06T11:36:56+00:00","author":{"@id":"https://www.askpython.com/author/rishabh","name":"Rishabh Das"},"publisher":{"@id":"https://www.askpython.com/#person"},"description":"Last quarter I had to plan a 12-stop delivery loop and the route my gut picked came out 38% longer than the version a small Python script returned in under a","name":"Traveling Salesman Problem with Python: Greedy and Brute Force - AskPython","@id":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#richSnippet","isPartOf":{"@id":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#webpage"},"image":{"@id":"https://www.askpython.com/wp-content/uploads/2025/04/Traveling_Salesman_Problem_with_Python___1744317160.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.askpython.com/python/examples/travelling-salesman-problem-python#webpage"}}]}
| og:locale | en_US |
| og:type | article |
| og:site_name | AskPython |
| article:section | Python Programming Examples |
| og:image | https://www.askpython.com/wp-content/uploads/2025/04/Traveling_Salesman_Problem_with_Python___1744317160.png |
| og:image:secure_url | https://www.askpython.com/wp-content/uploads/2025/04/Traveling_Salesman_Problem_with_Python___1744317160.png |
| og:image:width | 1200 |
| og:image:height | 630 |
| og:image:alt | Featured Image For: Traveling Salesman Problem With Python: Greedy And Brute Force |
| og:image:type | image/png |
| article:published_time | 2026-07-06T11:36:56+00:00 |
| twitter:card | summary_large_image |
| twitter:creator | @AskPython |
| twitter:image | https://www.askpython.com/wp-content/uploads/2025/04/Traveling_Salesman_Problem_with_Python___1744317160.png |
| twitter:label1 | Written by |
| twitter:data1 | Rishabh Das |
| twitter:label2 | Time to read |
| twitter:data2 | 5 minutes |
| msapplication-TileImage | https://www.askpython.com/wp-content/uploads/2019/12/cropped-favicon-1-270x270.png |
Links:
Viewport: width=device-width, initial-scale=1, maximum-scale=5, viewport-fit=cover
Robots: index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large