Advertisement
mamamaria

NearestNeighborSearch.cpp

Nov 2nd, 2021
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. // NearestNeighborSearch.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <ctime>
  7. #include <random>
  8.  
  9. const int N = 30;
  10.  
  11. using namespace std;
  12. //default_random_engine eng(time(0));
  13. default_random_engine eng;
  14.  
  15. using Graph = vector<vector<double>>;
  16. using Visit = vector <bool>;
  17.  
  18. Graph GraphGenerator() {
  19.     Graph graph(N);
  20.     for (int i = 0; i < graph.size(); i++) {
  21.         graph[i].resize(N);
  22.     }
  23.     uniform_real_distribution<double> distr(1, 10);
  24.     for (int i = 0; i < graph.size(); i++) {
  25.         for (int j = 0; j <= i; j++) {
  26.             if (i == j) {
  27.                 graph[i][j] = 0;
  28.                 graph[j][i] = 0;
  29.                 continue;
  30.             }
  31.             double dist = distr(eng);
  32.             graph[i][j] = dist;
  33.             graph[j][i] = graph[i][j];
  34.         }
  35.     }
  36.     return graph;
  37. }
  38. Visit VisitGenerator() {
  39.     Visit visit(N);
  40.     for (int i = 0; i < N; i++)
  41.         visit[i] = false;
  42.     cout << endl;
  43.     return visit;
  44. }
  45. void GraphPrinter(Graph graph) {
  46.     for (int i = 0; i < graph.size(); i++) {
  47.         for (int j = 0; j < graph.size(); j++)
  48.             cout << graph[i][j] << " ";
  49.         cout << endl;
  50.     }
  51.     cout << endl;
  52. }
  53. void VisitPrinter(Visit visit) {
  54.         for (int i = 0; i < visit.size(); i++)
  55.             cout << visit[i] << " ";
  56.         cout << endl;
  57. }
  58.  
  59. int NearestWayFinder(Graph graph, int index, double &wayLength, Visit& visit) {
  60.     double minimalWay = INFINITY;
  61.     int minimalWayIndex = 0;
  62.     for (int i=0; i<N; i++)
  63.         if (graph[index][i] < minimalWay && visit[i]==false) {
  64.             minimalWay = graph[index][i];
  65.             minimalWayIndex = i;
  66.         }
  67.     wayLength += minimalWay;
  68.     return (minimalWayIndex);
  69. }
  70. vector<int> Visited;
  71. double TravelingSalesmanNeighbour(Graph &graph, Visit &visit) {
  72.     int visitedCounter = 1;
  73.     int currentIndex = 0, nearestIndex = 0;  double wayLength=0;
  74.     visit[0] = true; Visited.push_back(0);
  75.     while (visitedCounter != N) {
  76.         nearestIndex = NearestWayFinder(graph, nearestIndex, wayLength, visit);
  77.         visitedCounter++;
  78.         visit[nearestIndex] = true; Visited.push_back(nearestIndex);
  79.     }
  80.     wayLength += graph[0][nearestIndex];
  81.     return wayLength;
  82. }
  83.  
  84. int main() {
  85.     srand(time(0));
  86.     Graph graph = GraphGenerator();
  87.     Visit visit = VisitGenerator();
  88.     GraphPrinter(graph);
  89.     VisitPrinter(visit);
  90.     cout << TravelingSalesmanNeighbour(graph, visit) << endl;
  91.     VisitPrinter(visit);
  92.     for (int i = 0; i < Visited.size(); i++)
  93.         cout << Visited[i] << " ";
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement