Advertisement
mrDIMAS

Dijkstra pathfinder. OOP C++11

Nov 12th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. const float Infinite = FLT_MAX;
  8.  
  9. class GraphVertex;
  10.  
  11. struct Edge {
  12.     GraphVertex * destVertex;
  13.     float distToDestVertex;
  14.  
  15.     Edge() {
  16.         destVertex = nullptr;
  17.         distToDestVertex = Infinite;
  18.     }
  19.  
  20.     Edge( GraphVertex * destinationVertex, float distanceToDestinationVertex ) {
  21.         destVertex = destinationVertex;
  22.         distToDestVertex = distanceToDestinationVertex;
  23.     }
  24. };
  25.  
  26. class GraphVertex {
  27. private:
  28.     bool used; 
  29.     float distanceFromBegin;
  30.     GraphVertex * ancestor;
  31.     vector< Edge > edges;  
  32.  
  33.     float DistanceToVertex( GraphVertex * vertex ) {
  34.         return sqrtf( x * vertex->x + y * vertex->y + z * vertex->z );
  35.     }
  36. public:
  37.     friend class Route;
  38.  
  39.     float x;
  40.     float y;
  41.     float z;
  42.  
  43.     GraphVertex( float vX, float vY, float vZ ) {
  44.         x = vX;
  45.         y = vY;
  46.         z = vZ;
  47.         ClearState();
  48.     }
  49.  
  50.     void ClearState( ) {
  51.         ancestor = nullptr;
  52.         used = false;
  53.         distanceFromBegin = Infinite;
  54.     }
  55.  
  56.     void AddEdge( GraphVertex * vertex ) {
  57.         edges.push_back( Edge( vertex, DistanceToVertex( vertex )));
  58.         vertex->edges.push_back( Edge( this, DistanceToVertex( vertex )));
  59.     }
  60. };
  61.  
  62. class Route {
  63. private:
  64.     vector< GraphVertex* > graph;
  65. public:
  66.     Route() {
  67.  
  68.     }
  69.  
  70.     ~Route() {
  71.         for( auto vertex : graph ) {
  72.             delete vertex;
  73.         }
  74.     }
  75.  
  76.     void AddPoint( GraphVertex * newVertex ) {
  77.         graph.push_back( newVertex );
  78.     }
  79.  
  80.     GraphVertex * GetPoint( int i ) {
  81.         if( i < 0 || i >= graph.size() ) {
  82.             return nullptr;
  83.         } else {
  84.             return graph[i];
  85.         }
  86.     }
  87.  
  88.     int GetPointCount( ) {
  89.         return graph.size();
  90.     }
  91.  
  92.     void Build( GraphVertex * begin, GraphVertex * end, vector< GraphVertex* > & outPoints ) {
  93.         outPoints.clear();
  94.         // clear state of all vertices
  95.         for( auto vertex : graph ) {
  96.             vertex->ClearState();
  97.         }
  98.         // set begin graph vertex
  99.         begin->distanceFromBegin = 0;
  100.  
  101.         for( int i = 0; i < graph.size(); i++ ) {
  102.             // get nearest vertex
  103.             GraphVertex * nearest = nullptr;
  104.             for( auto vertex : graph ) {
  105.                 if( vertex->used ) {
  106.                     continue;
  107.                 } else if( !nearest ) {
  108.                     nearest = vertex;
  109.                 } else if( vertex->distanceFromBegin < nearest->distanceFromBegin ) {
  110.                     nearest = vertex;
  111.                 }
  112.             }
  113.  
  114.             if( nearest->distanceFromBegin >= Infinite )
  115.                 break;
  116.  
  117.             nearest->used = true;
  118.  
  119.             // relaxation
  120.             for( auto & edge : nearest->edges ) {
  121.                 if( nearest->distanceFromBegin + edge.distToDestVertex < edge.destVertex->distanceFromBegin ) {
  122.                     edge.destVertex->distanceFromBegin = nearest->distanceFromBegin + edge.distToDestVertex;
  123.                     edge.destVertex->ancestor = nearest;
  124.                 }
  125.             }
  126.         }
  127.  
  128.         // restore path to dest vertex
  129.         for( GraphVertex * v = end; v != begin; v = v->ancestor ) {
  130.             outPoints.push_back (v);
  131.         }
  132.  
  133.         outPoints.push_back( begin );
  134.  
  135.         reverse( outPoints.begin(), outPoints.end() );
  136.     }
  137. };
  138.  
  139. int main() {
  140.     // route test
  141.     vector< GraphVertex* > vertex;
  142.    
  143.     vertex.push_back( new GraphVertex( 0, 0, 0 ));
  144.     vertex.push_back( new GraphVertex( 1, 2, 0 ));
  145.     vertex.push_back( new GraphVertex( 1, 5, 0 ));
  146.     vertex.push_back( new GraphVertex( 4, 4, 0 ));
  147.     vertex.push_back( new GraphVertex( 4, 2, 0 ));
  148.     vertex.push_back( new GraphVertex( 7, 2, 0 ));
  149.  
  150.     vertex[0]->AddEdge( vertex[1] );   
  151.  
  152.     vertex[1]->AddEdge( vertex[2] );   
  153.     vertex[1]->AddEdge( vertex[4] );   
  154.  
  155.     vertex[2]->AddEdge( vertex[3] );   
  156.     vertex[3]->AddEdge( vertex[4] );   
  157.  
  158.     vertex[4]->AddEdge( vertex[5] );   
  159.  
  160.     Route testRoute;
  161.  
  162.     for( auto v : vertex )
  163.         testRoute.AddPoint( v );
  164.  
  165.     GraphVertex * begin = vertex[1];
  166.     GraphVertex * end = vertex[3];
  167.  
  168.     vector< GraphVertex* > out;
  169.     testRoute.Build( begin, end, out );
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement