Advertisement
Garey

Untitled

Mar 11th, 2018
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. //  main.cpp
  2. //  Find shortest distance between two towns
  3. //
  4.  
  5. #include <unordered_map>
  6. #include <vector>
  7. #include <limits>
  8. #include <algorithm>
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. class Graph
  14. {
  15.     unordered_map<char, const unordered_map<char, int>> vertices;
  16.    
  17. public:
  18.     void add_vertex(char name, const unordered_map<char, int>& edges)
  19.     {
  20.         // Insert the connected nodes in unordered map
  21.         vertices.insert(unordered_map<char, const unordered_map<char, int>>::value_type(name, edges));
  22.     }
  23.    
  24.     vector<char> shortest_path(char start, char finish)
  25.     {
  26.         // Second arguments -> distances
  27.         // Find the smallest distance in the already in closed list and push it in -> previous
  28.         unordered_map<char, int> distances;
  29.         unordered_map<char, char> previous;
  30.         vector<char> nodes; // Open list
  31.         vector<char> path; // Closed list
  32.        
  33.         auto comparator = [&] (char left, char right) { return distances[left] > distances[right]; };
  34.        
  35.         for (auto& vertex : vertices)
  36.         {
  37.             if (vertex.first == start)
  38.             {
  39.                 distances[vertex.first] = 0;
  40.             }
  41.             else
  42.             {
  43.                 distances[vertex.first] = numeric_limits<int>::max();
  44.             }
  45.            
  46.             nodes.push_back(vertex.first);
  47.             push_heap(begin(nodes), end(nodes), comparator);
  48.         }
  49.        
  50.         while (!nodes.empty())
  51.         {
  52.             pop_heap(begin(nodes), end(nodes), comparator);
  53.             char smallest = nodes.back();
  54.             nodes.pop_back();
  55.            
  56.             std::cout << "Open list: ";
  57.             for( std::vector<char>::const_iterator i = nodes.begin(); i != nodes.end(); ++i)
  58.                 std::cout << *i << ' ';
  59.             std::cout << std::endl;
  60.            
  61.             if (smallest == finish)
  62.             {
  63.                 while (previous.find(smallest) != end(previous))
  64.                 {
  65.                     path.push_back(smallest);
  66.                     smallest = previous[smallest];
  67.                     std::cout << "Closed list: ";
  68.                     for( std::vector<char>::const_iterator i = path.begin(); i != path.end(); ++i)
  69.                         std::cout << *i << ' ';
  70.                     std::cout << std::endl;
  71.                 }
  72.                
  73.                 break;
  74.             }
  75.            
  76.             if (distances[smallest] == numeric_limits<int>::max())
  77.             {
  78.                 break;
  79.             }
  80.            
  81.             for (auto& neighbor : vertices[smallest])
  82.             {
  83.                 int alt = distances[smallest] + neighbor.second;
  84.                 if (alt < distances[neighbor.first])
  85.                 {
  86.                     distances[neighbor.first] = alt;
  87.                     previous[neighbor.first] = smallest;
  88.                     make_heap(begin(nodes), end(nodes), comparator);
  89.                 }
  90.             }
  91.         }
  92.        
  93.         return path;
  94.     }
  95. };
  96.  
  97. int main()
  98. {
  99.     cout << "@author: Stf Kolew" << endl << endl;
  100.     int seq = 0;
  101.     char init_node = 'A';
  102.     char dest_node = 'G';
  103.    
  104.     Graph g;
  105.     g.add_vertex('A', {{'B', 1}, {'C', 4}, {'F', 2}});
  106.     g.add_vertex('B', {{'E', 2}});
  107.     g.add_vertex('C', {{'G', 2}, {'D', 4}});
  108.     g.add_vertex('D', {});
  109.     g.add_vertex('E', {{'D', 3}});
  110.     g.add_vertex('F', {{'C', 1}, {'G', 4}});
  111.     g.add_vertex('G', {{'E', 5}});
  112.    
  113.     cout << "As initial node: " << init_node << endl;
  114.     cout << "As goal node: " << dest_node << endl;
  115.    
  116.     for (char vertex : g.shortest_path(init_node, dest_node))
  117.     {
  118.         cout << "Solution path from goal sequence : " << seq << " Node : " << vertex << endl;
  119.         seq++;
  120.     }
  121.    
  122.     cout << "Solution path from goal sequence : " << seq << " Node : " << init_node << endl;
  123.    
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement