Skip to content

Commit 15fa9fb

Browse files
committed
Add README.md
1 parent 9a7c586 commit 15fa9fb

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Christofides algorithm
2+
3+
The **Christofides algorithm** is an algorithm for finding approximate solutions to the travelling salesman problem, on instances where the distances form a metric space(they are symmetric and obey the triangle inequality).
4+
It is an approximation algorithm that guarantees that its solutions will be within a factor of 3/2 of the optimal solution length, and is named after Nicos Christofides, who published it in 1976. As of 2017, this is the best approximation ratio that has been proven for the traveling salesman problem on general metric spaces, although better approximations are known for some special cases [Wikipedia](https://en.wikipedia.org/wiki/Christofides_algorithm)
5+
6+
7+
8+
**Basic steps of algorithm:**
9+
10+
1. Find a minimum spanning tree **(T)**
11+
2. Find vertexes in **T** with odd degree **(O)**
12+
3. Find minimum weight matching **(M)** edges to **T**
13+
4. Build an Eulerian circuit using the edges of **M** and **T**
14+
5. Make a Hamiltonian circuit by skipping repeated vertexes
15+
16+
17+
18+
19+
## Python implementation
20+
21+
In the file **christofides.py** there is an implementation of algorithm with comments and several test inputs.
22+
23+
You can simply use this code by calling the tsp() function with an array of point(with its coordinates x, y)
24+
25+
**Example:**
26+
27+
```python
28+
# array of points
29+
points = [
30+
[0, 0], [3, 0], [6, 0],
31+
[0, 3], [3, 3], [6, 3],
32+
[0, 6], [3, 6], [6, 6]
33+
]
34+
35+
length, path = tsp(points)
36+
37+
```
38+
39+
40+
41+
If you have some questions or advices, please give me know
42+
43+
## Authors
44+
45+
* **Andrew Zhuravchak** - student of CS@UCU

christofides.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
def tsp(data):
2+
# build a graph
23
G = build_graph(data)
34
print("Graph: ", G)
45

6+
# build a minimum spanning tree
57
MSTree = minimum_spanning_tree(G)
68
print("MSTree: ", MSTree)
79

@@ -13,6 +15,7 @@ def tsp(data):
1315
minimum_weight_matching(MSTree, G, odd_vertexes)
1416
print("Minimum weight matching: ", MSTree)
1517

18+
# find an eulerian tour
1619
eulerian_tour = find_eulerian_tour(MSTree, G)
1720

1821
print("Eulerian tour: ", eulerian_tour)

0 commit comments

Comments
 (0)