-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreedy.cpp
67 lines (50 loc) · 1.75 KB
/
Greedy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "pch.h"
#include "Graph.h"
// SalesmanTrackGreedy =========================================================
CTrack FindShortestTrack(CGraph& graph, const CVertex* startVertex, const CVertex* endVertex)
{
CTrack result(&graph);
auto currentVertex = endVertex;
while (true) {
result.AddFirst(currentVertex->m_pDijkstraPrevious);
if (currentVertex->m_pDijkstraPrevious->m_pOrigin == startVertex) {
break;
}
currentVertex = currentVertex->m_pDijkstraPrevious->m_pOrigin;
}
return result;
}
CTrack SalesmanTrackGreedy(CGraph& graph, CVisits &visits)
{
CTrack result(&graph);
// First element
auto currentVertex = visits.m_Vertices.front();
// Create candidates
std::list<CVertex*> candidates(visits.m_Vertices);
candidates.pop_back();
candidates.pop_front();
while(!candidates.empty()) {
// Calculate the minimum cost tracks from a start point
DijkstraQueue(graph, currentVertex);
// Get min cost candidate
CVertex* minCostVertex = candidates.front();
for (const auto candidate : candidates) {
if (minCostVertex->m_DijkstraDistance > candidate->m_DijkstraDistance) {
minCostVertex = candidate;
}
}
// Find shortest path from currentVertex to minCostVertex
CTrack track = FindShortestTrack(graph, currentVertex, minCostVertex);
result.Append(track);
// Remove it as possible candidate
candidates.remove(minCostVertex);
// Update current vertex
currentVertex = minCostVertex;
}
// Calculate the minimum cost tracks from a
DijkstraQueue(graph, currentVertex);
// Find shortest track to the last vertex to visit
CTrack finalTrack = FindShortestTrack(graph, currentVertex, visits.m_Vertices.back());
result.Append(finalTrack);
return result;
}