Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. void Graph::breadFirstSearch(int vid)
  2. {
  3.     cout <<"The sequence of breadth first search starts from vertex " << vid << " is :" << endl;
  4.  
  5.     queue<Vertex* > search_queue;
  6.     //place starting nodes on the queue
  7.     Vertex* v = vertice.at(vid);
  8.     search_queue.push(v);
  9.    
  10.     //make sure queue isnt empty before beginning
  11.     while (search_queue.size() != 0)
  12.     {
  13.         Vertex* current_vertex = search_queue.front();
  14.         search_queue.pop();
  15.  
  16.         if (current_vertex->isVisited == true)
  17.             continue;
  18.  
  19.         current_vertex->isVisited = true;
  20.  
  21.         cout<< current_vertex->id << endl;
  22.  
  23.         for( vector<Vertex*>::const_iterator it = current_vertex->adj_v.begin(); it != current_vertex->adj_v.end(); ++it )
  24.         {
  25.             Vertex* neighbor_vertex = *it;
  26.  
  27.             if (neighbor_vertex->isVisited == true)
  28.             {
  29.                 continue;
  30.             }
  31.  
  32.             search_queue.push(neighbor_vertex);
  33.         }
  34.     }
  35.  
  36.     cout<<endl;
  37.  
  38. }
  39.  
  40. void Graph::depthFirstSearch(int vid)
  41. {
  42.     cout << "The sequence of depth first search starts from vertex " << vid << " is :" << endl;
  43.  
  44.     stack<Vertex* > search_stack; //
  45.    
  46.     //create vertex to search
  47.     Vertex* v = vertice.at(vid); // pointer to vertex
  48.     search_stack.push(v);       //
  49.  
  50.     while (search_stack.size() != 0)
  51.     {
  52.         Vertex* current_vertex = search_stack.top();
  53.         search_stack.pop();
  54.  
  55.         if (current_vertex->isVisited == true)
  56.             continue;
  57.  
  58.         current_vertex->isVisited = true;
  59.  
  60.         cout << current_vertex->id << endl;
  61.  
  62.         for (vector<Vertex*>::const_iterator it = current_vertex->adj_v.begin(); it != current_vertex->adj_v.end(); ++it)
  63.         {
  64.             Vertex* neighbor_vertex = *it;
  65.  
  66.             if (neighbor_vertex->isVisited == true)
  67.             {
  68.                 continue;
  69.             }
  70.  
  71.             search_stack.push(neighbor_vertex);
  72.         }
  73.     }
  74.  
  75.     cout << endl;
  76.  
  77. }
  78.  
  79. void Graph::Dijkstra(int vid)
  80. {
  81.     // input
  82.     //
  83.     Graph g; //define the graph
  84.     ifstream vertex_file;
  85.     vertex_file.open("vertex.txt", ios::in);
  86.     int id;
  87.     double x, y;
  88.     while (vertex_file >> id >> x >> y)
  89.     {
  90.         Vertex* v = new Vertex(id);
  91.         v->x = x;
  92.         v->y = y;
  93.         g.load(v); //load the vertex into the graph
  94.     }
  95.     vertex_file.close();
  96.     //read adjacency relationship among vertices
  97.     ifstream adjacency_file;
  98.     adjacency_file.open("adjacency.txt", ios::in);
  99.     int adj_1, adj_2;
  100.     while (adjacency_file >> adj_1 >> adj_2)
  101.     {
  102.         Vertex* v_1 = g.vertice.at(adj_1);
  103.         Vertex* v_2 = g.vertice.at(adj_2);
  104.         //calculate the distance between the two vertices
  105.         double distance= v_1->distanceTo(*v_2);
  106.         v_1->addAdjacentVertex(v_2, distance);
  107.         v_2->addAdjacentVertex(v_1, distance);
  108.     }
  109.     adjacency_file.close();
  110.     //start vertex:
  111.     // end vertex:
  112.     cout << "The sequence of Dijkstra search starts from vertex " << 28776 << " is :" << endl;
  113.  
  114.     priority_queue<Vertex* > search_queue;
  115.     //place starting nodes on the queue
  116.     Vertex* v = vertice.at(28776);
  117.     search_queue.push(v);
  118.  
  119.     //make sure queue isnt empty before beginning
  120.     while (search_queue.size() != 0)
  121.     {
  122.         Vertex* current_vertex = search_queue.top();
  123.         search_queue.pop();
  124.  
  125.         if (current_vertex->isVisited == true)
  126.             continue;
  127.  
  128.         current_vertex->isVisited = true;
  129.  
  130.         cout << current_vertex->id << endl;
  131.  
  132.         for (vector<Vertex*>::const_iterator it = current_vertex->adj_v.begin(); it != current_vertex->adj_v.end(); ++it)
  133.         {
  134.             Vertex* neighbor_vertex = *it;
  135.  
  136.             if (neighbor_vertex->isVisited == true)
  137.             {
  138.                 continue;
  139.             }
  140.  
  141.             search_queue.push(neighbor_vertex);
  142.         }
  143.     }
  144.  
  145.     cout << endl;
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement