Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit 3d2c498

Browse files
committed
ApproxTSPTour.cpp
1 parent 7691804 commit 3d2c498

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

ApproxTSPTour.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// algorithm - some algorithms in "Introduction to Algorithms", third edition
3+
// Copyright (C) 2018 lxylxy123456
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef MAIN
20+
#define MAIN
21+
#define MAIN_ApproxTSPTour
22+
#endif
23+
24+
#ifndef FUNC_ApproxTSPTour
25+
#define FUNC_ApproxTSPTour
26+
27+
#include "utils.h"
28+
29+
#include "MST.cpp"
30+
#include "SegmentsIntersect.cpp"
31+
32+
template <typename GT, typename T>
33+
void TreeWalk(GT& G, T current, T* last, std::vector<T>& H) {
34+
H.push_back(current);
35+
for (auto i = G.edges_from(current); !i.end(); i++)
36+
if (!last || i.d() != *last)
37+
TreeWalk(G, i.d(), &current, H);
38+
}
39+
40+
template <typename GT, typename T, typename WT>
41+
void ApproxTSPTour(GT& G, umap_WT& c, std::vector<T>& H) {
42+
T r = *G.V.begin();
43+
umap<T, MSTPrimInfo<T, WT>> MST_info;
44+
MSTPrim(G, c, r, MST_info);
45+
GT MST_Graph(false);
46+
for (auto i = G.V.begin(); i != G.V.end(); i++) {
47+
const MSTPrimInfo<T, WT>& info = MST_info[*i];
48+
if (!info.pi_nil)
49+
MST_Graph.add_edge(*i, info.pi);
50+
}
51+
H.reserve(G.V.size() + 1);
52+
TreeWalk<GT, T>(MST_Graph, r, nullptr, H);
53+
H.push_back(r);
54+
}
55+
#endif
56+
57+
#ifdef MAIN_ApproxTSPTour
58+
int main(int argc, char *argv[]) {
59+
const size_t n = get_argv(argc, argv, 1, 200);
60+
const size_t m = get_argv(argc, argv, 2, 10);
61+
const bool dir = 0;
62+
std::vector<int> b;
63+
random_integers(b, -n, n, m * 2);
64+
using T = size_t;
65+
using WT = double;
66+
GraphAdjList<size_t> G(dir);
67+
umap_WT c;
68+
std::vector<Vector<T>> S;
69+
S.reserve(m);
70+
for (size_t i = 0; i < m; i++) {
71+
S.push_back(Vector<T>(b[2 * i + 0], b[2 * i + 1]));
72+
for (size_t j = 0; j < i; j++) {
73+
G.add_edge(i, j);
74+
c[Edge<T>(i, j, false)] = (S[i] - S[j]).Length();
75+
}
76+
}
77+
std::vector<size_t> H;
78+
ApproxTSPTour(G, c, H);
79+
std::cout << "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>"
80+
<< std::endl;
81+
std::cout << "<svg height=\"" << 2 * n << "\" width=\"" << 2 * n << "\">"
82+
<< std::endl;
83+
std::cout << "\t<rect fill=\"#ffffff\" x=\"0\" y=\"0\" width=\"" << 2 * n
84+
<< "\" height=\"" << 2 * n << "\"/>" << std::endl;
85+
std::cout << "\t<polygon stroke=\"#000000\" fill=\"#cccccc\" points=\"";
86+
for (auto i = H.begin(); i != H.end(); i++)
87+
std::cout << n + S[*i].x + 1 << "," << n + S[*i].y + 1 << " ";
88+
std::cout << "\"/>" << std::endl;
89+
for (size_t i = 0; i < m; i++)
90+
std::cout << "\t<circle cx=\"" << n + S[i].x + 1 << "\" cy=\""
91+
<< n + S[i].y + 1 << "\" r=\"2\"/>" << std::endl;
92+
std::cout << "</svg>" << std::endl;
93+
return 0;
94+
}
95+
#endif
96+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
| 33 | JarvisMarch.cpp | Jarvis March | 1038 |
224224
| 33 | ClosestPairPoints.cpp | Closest Pair Points | 1043 |
225225
| 35 | ApproxVertexCover.cpp | Approx Vertex Cover | 1109 |
226+
| 35 | ApproxTSPTour.cpp | Approx TSP Tour | 1112 |
226227

227228
# Supplementary Files
228229
* `utils.h`: Utils

0 commit comments

Comments
 (0)