Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.90 KB | None | 0 0
  1. //-------------------------------GRAPH.H-----------------------------------
  2. #ifndef Graph_H
  3. #define Graph_H
  4.  
  5. #include <vector>
  6. #include <map>
  7. #include "Vertex.h"
  8.  
  9. using namespace std;
  10.  
  11. class Graph
  12. {
  13. public:
  14.     //store vertices in graph
  15.     map<int, Vertex*> vertice;
  16.  
  17.     //load vertex into graph
  18.     void load(Vertex* v);
  19.  
  20.     //mark two vertices to be adjacent, and record the distance between them
  21.     void makeAdjacency(int v1, int v2, double distance);
  22.  
  23.     //print adjacency matrix
  24.     void printAdjacencyMatrix();
  25.  
  26.     //print adjacency list
  27.     void printAdjacencyList();
  28.  
  29.     //bread first search
  30.     void breadFirstSearch(int v);
  31.  
  32.     //depth first search
  33.     void depthFirstSearch(int v);
  34.  
  35.     //Dijkstras
  36.     void Dijkstra(int v);
  37.  
  38.     //reset the flag
  39.     void restoreVisitedFlag();
  40. };
  41.  
  42. #endif
  43.  
  44. //------------------------------- GRAPH.CPP---------------------------------
  45. //#include "stdafx.h"
  46. #include "Graph.h"
  47. #include <iostream>
  48. #include <algorithm>
  49. #include <queue> // std::priority queue
  50.  
  51. #include <stack>
  52. #include<fstream>
  53. void Graph::load(Vertex* v)
  54. {
  55.     map<int, Vertex*>::iterator iter = vertice.find((*v).id);
  56.  
  57.     if (iter == vertice.end())
  58.     {
  59.         vertice.insert(pair<int, Vertex*> ((*v).id, v));
  60.     }
  61.     else
  62.     {
  63.         cout<<"Fail: the vertex has been inserted into the network!";
  64.     }
  65. }
  66.  
  67. void Graph::makeAdjacency(int v1, int v2, double distance)
  68. {
  69.     map<int, Vertex*>::iterator iter1 = vertice.find(v1);
  70.     map<int, Vertex*>::iterator iter2 = vertice.find(v2);
  71.  
  72.     if (iter1 == vertice.end())
  73.     {
  74.         cout<<"Fail: v1 does not exist in the network!";
  75.         return;
  76.     }
  77.  
  78.     if (iter2 == vertice.end())
  79.     {
  80.         cout<<"Fail: v2 does not exist in the network!";
  81.         return;
  82.     }
  83.  
  84.     Vertex* vertex1 = vertice.at(v1);
  85.     Vertex* vertex2 = vertice.at(v2);
  86.  
  87.     vertex1->addAdjacentVertex(vertex2,distance);
  88.     vertex2->addAdjacentVertex(vertex1,distance);
  89. }
  90.  
  91. void Graph::printAdjacencyMatrix()
  92. {
  93.     cout <<"Adjacency Matrix:"<<endl;
  94.  
  95.     for( map<int, Vertex*>::const_iterator it1 = vertice.begin(); it1 != vertice.end(); ++it1 )
  96.     {
  97.       int key1 = it1->first;
  98.       Vertex* vertex1 = it1->second;
  99.  
  100.         for( map<int, Vertex*>::const_iterator it2 = vertice.begin(); it2 != vertice.end(); ++it2 )
  101.         {
  102.           int key2 = it2->first;
  103.           Vertex* vertex2 = it2->second;
  104.  
  105.           if (vertex1->isAdjacentTo(vertex2))
  106.               cout << "1" << '\t';
  107.           else
  108.               cout << "0" << '\t';
  109.  
  110.         }
  111.         cout <<endl;
  112.     }
  113.  
  114.     cout<<'\n';
  115. }
  116.  
  117. void Graph::printAdjacencyList()
  118. {
  119.     cout <<"Adjacency List:"<<endl;
  120.  
  121.     for( map<int, Vertex*>::const_iterator it1 = vertice.begin(); it1 != vertice.end(); ++it1 )
  122.     {
  123.       int key1 = it1->first;
  124.       Vertex* vertex1 = it1->second;
  125.       cout<<key1<<'\t'<<":"<<'\t';
  126.        
  127.         for( map<int, Vertex*>::const_iterator it2 = vertice.begin(); it2 != vertice.end(); ++it2 )
  128.         {
  129.           int key2 = it2->first;
  130.           Vertex* vertex2 = it2->second;
  131.  
  132.           if (vertex1->isAdjacentTo(vertex2))
  133.               cout << key2 << '\t';
  134.         }
  135.         cout << endl;
  136.     }
  137.     cout << endl;
  138. }
  139.  
  140.  
  141. void Graph::breadFirstSearch(int vid)
  142. {
  143.     cout <<"The sequence of breadth first search starts from vertex " << vid << " is :" << endl;
  144.  
  145.     queue<Vertex* > search_queue;
  146.     //place starting nodes on the queue
  147.     Vertex* v = vertice.at(vid);
  148.     search_queue.push(v);
  149.    
  150.     //make sure queue isnt empty before beginning
  151.     while (search_queue.size() != 0)
  152.     {
  153.         Vertex* current_vertex = search_queue.front();
  154.         search_queue.pop();
  155.  
  156.         if (current_vertex->isVisited == true)
  157.             continue;
  158.  
  159.         current_vertex->isVisited = true;
  160.  
  161.         cout<< current_vertex->id << endl;
  162.  
  163.         for( vector<Vertex*>::const_iterator it = current_vertex->adj_v.begin(); it != current_vertex->adj_v.end(); ++it )
  164.         {
  165.             Vertex* neighbor_vertex = *it;
  166.  
  167.             if (neighbor_vertex->isVisited == true)
  168.             {
  169.                 continue;
  170.             }
  171.  
  172.             search_queue.push(neighbor_vertex);
  173.         }
  174.     }
  175.  
  176.     cout<<endl;
  177.  
  178. }
  179. void Graph::depthFirstSearch(int vid)
  180. {
  181.     cout << "The sequence of depth first search starts from vertex " << vid << " is :" << endl;
  182.  
  183.     stack<Vertex* > search_stack; //
  184.    
  185.     //create vertex to search
  186.     Vertex* v = vertice.at(vid); // pointer to vertex
  187.     search_stack.push(v);       //
  188.  
  189.     while (search_stack.size() != 0)
  190.     {
  191.         Vertex* current_vertex = search_stack.top();
  192.         search_stack.pop();
  193.  
  194.         if (current_vertex->isVisited == true)
  195.             continue;
  196.  
  197.         current_vertex->isVisited = true;
  198.  
  199.         cout << current_vertex->id << endl;
  200.  
  201.         for (vector<Vertex*>::const_iterator it = current_vertex->adj_v.begin(); it != current_vertex->adj_v.end(); ++it)
  202.         {
  203.             Vertex* neighbor_vertex = *it;
  204.  
  205.             if (neighbor_vertex->isVisited == true)
  206.             {
  207.                 continue;
  208.             }
  209.  
  210.             search_stack.push(neighbor_vertex);
  211.         }
  212.     }
  213.  
  214.     cout << endl;
  215.  
  216. }
  217. void Graph::restoreVisitedFlag()
  218. {
  219.     for( map<int, Vertex*>::const_iterator it = vertice.begin(); it != vertice.end(); ++it )
  220.     {
  221.         int key = it->first;
  222.         Vertex* vertex = it->second;
  223.  
  224.         vertex->isVisited = false;
  225.        
  226.     }
  227.  
  228. }
  229. void Graph::Dijkstra(int vid)
  230. {
  231.     // input
  232.     //
  233.     Graph g; //define the graph
  234.     ifstream vertex_file;
  235.     vertex_file.open("vertex.txt", ios::in);
  236.     int id;
  237.     double x, y;
  238.     while (vertex_file >> id >> x >> y)
  239.     {
  240.         Vertex* v = new Vertex(id);
  241.         v->x = x;
  242.         v->y = y;
  243.         g.load(v); //load the vertex into the graph
  244.     }
  245.     vertex_file.close();
  246.     //read adjacency relationship among vertices
  247.     ifstream adjacency_file;
  248.     adjacency_file.open("adjacency.txt", ios::in);
  249.     int adj_1, adj_2;
  250.     while (adjacency_file >> adj_1 >> adj_2)
  251.     {
  252.         Vertex* v_1 = g.vertice.at(adj_1);
  253.         Vertex* v_2 = g.vertice.at(adj_2);
  254.         //calculate the distance between the two vertices
  255.         double distance= v_1->distanceTo(*v_2);
  256.         v_1->addAdjacentVertex(v_2, distance);
  257.         v_2->addAdjacentVertex(v_1, distance);
  258.     }
  259.     adjacency_file.close();
  260.     //start vertex:
  261.     // end vertex:
  262.     cout << "The sequence of Dijkstra search starts from vertex " << 28776 << " is :" << endl;
  263.  
  264.     priority_queue<Vertex* > search_queue;
  265.     //place starting nodes on the queue
  266.     Vertex* v = vertice.at(28776);
  267.     search_queue.push(v);
  268.  
  269.     //make sure queue isnt empty before beginning
  270.     while (search_queue.size() != 0)
  271.     {
  272.         Vertex* current_vertex = search_queue.top();
  273.         search_queue.pop();
  274.  
  275.         if (current_vertex->isVisited == true)
  276.             continue;
  277.  
  278.         current_vertex->isVisited = true;
  279.  
  280.         cout << current_vertex->id << endl;
  281.  
  282.         for (vector<Vertex*>::const_iterator it = current_vertex->adj_v.begin(); it != current_vertex->adj_v.end(); ++it)
  283.         {
  284.             Vertex* neighbor_vertex = *it;
  285.  
  286.             if (neighbor_vertex->isVisited == true)
  287.             {
  288.                 continue;
  289.             }
  290.  
  291.             search_queue.push(neighbor_vertex);
  292.         }
  293.     }
  294.  
  295.     cout << endl;
  296.  
  297. }
  298.  
  299. //---------------------------VERTEX.CPP-----------------------------
  300. /*Vertex: Corner Point Where lines meet*/
  301.  
  302.  
  303.  
  304.  
  305. //#include "stdafx.h"
  306. #include "Vertex.h"
  307. #include <cmath>
  308.  
  309. Vertex::Vertex(int id)
  310. {
  311.     this->id = id;
  312.     this->isVisited = false;
  313. }
  314.  
  315. Vertex::Vertex(int id, double x, double y)
  316. {
  317.     this->id = id;
  318.     this->x = x;
  319.     this->y = y;
  320.  
  321.     this->isVisited = false;
  322. }
  323.  
  324. double Vertex::distanceTo(Vertex v)
  325. {
  326.     double dx = this->x - v.x;
  327.     double dy = this->y - v.y;
  328.     double distance = sqrt(dx * dx + dy * dy );
  329.  
  330.     return distance;
  331. }
  332.  
  333. void Vertex::addAdjacentVertex(Vertex* v, double dis)
  334. {
  335.     if (v->id == id)
  336.     {
  337.         return;
  338.     }
  339.  
  340.     if (adj_v.end() == find (adj_v.begin(), adj_v.end(), v))
  341.     {
  342.         adj_v.push_back(v);
  343.         adj_distance.push_back(dis);
  344.     }
  345.     else
  346.     {
  347.         return;
  348.     }
  349. }
  350.  
  351. bool Vertex::isAdjacentTo(Vertex* v)
  352. {
  353.     vector<Vertex*>::iterator iter = find (adj_v.begin(), adj_v.end(), v);
  354.  
  355.     if (adj_v.end() == iter)
  356.         return false;
  357.     else
  358.         return true;
  359. }
  360.  
  361. void Vertex::deleteAdjacentVertex(Vertex* v)
  362. {
  363.     vector<Vertex*>::iterator iter = find (adj_v.begin(), adj_v.end(), v);
  364.  
  365.  
  366.     if (adj_v.end() == iter)
  367.     {
  368.         return;
  369.     }
  370.     else
  371.     {
  372.         size_t pos = iter - adj_v.begin();
  373.         vector<double>::iterator iter_dis = adj_distance.begin() + pos;
  374.  
  375.         adj_v.erase(iter);
  376.         adj_distance.erase(iter_dis);
  377.     }
  378. }
  379.  
  380. bool Vertex::operator== (const Vertex& v1)
  381. {
  382.         if (id == v1.id)
  383.         {
  384.             return true;
  385.         }
  386.         else
  387.         {
  388.             return false;
  389.         }
  390. }
  391. //---------------------------------- VERTEX.H ------------------------------------
  392.  
  393. #ifndef Vertex_H
  394. #define Vertex_H
  395.  
  396. #include <vector>
  397. #include <algorithm>
  398. using namespace std;
  399.  
  400. class Vertex
  401. {
  402. public:
  403.  
  404.     int id; //vertex id
  405.     double x; //coordinate x
  406.     double y; //coordinate y
  407.  
  408.  
  409.     bool isVisited; //visited flag
  410.  
  411.     //store adjacent vertices
  412.     vector<Vertex*> adj_v;
  413.  
  414.     //store the distances to its adjacent vertices
  415.     vector<double> adj_distance;
  416.  
  417.     //constructor function
  418.     Vertex(int id);
  419.     Vertex(int id, double x, double y);
  420.    
  421.     //distance to another vertex
  422.     double distanceTo(Vertex v);
  423.  
  424.     //add its adjacent vertices
  425.     void addAdjacentVertex(Vertex* v, double dis);
  426.  
  427.     //check vertex v is adjacent
  428.     bool isAdjacentTo(Vertex* v);
  429.  
  430.     //delete its adjacent vertex
  431.     void deleteAdjacentVertex(Vertex* v);
  432.  
  433.     //check if two vertices are the same one
  434.     bool operator == (const Vertex& v1);
  435.    
  436. };
  437.  
  438.  
  439. //----------------------- main.cpp ------------------------------
  440.  
  441. #endif
  442.  
  443. #include <iostream>
  444. #include<fstream>
  445. #include "Graph.h"
  446. #include "Vertex.h"
  447.  
  448. using namespace std;
  449.  
  450.  
  451.  
  452. int main()
  453. {
  454.  
  455.     Vertex v1(1), v2(2), v3(3), v4(4), v5(5), v6(6), v7(7), v8(8);
  456.     Graph g;
  457.  
  458.     //ifstream vertex_file;
  459.     //vertex_file.open("vertex.txt", ios::in);
  460.     //int id;
  461.     //double x, y;
  462.     //while (vertex_file >> id >> x >> y)
  463.     //{
  464.     //  Vertex* v = new Vertex(id);
  465.     //  v->x = x;
  466.     //  v->y = y;
  467.     //  g.load(v); //load the vertex into the graph
  468.     //}
  469.     //vertex_file.close();
  470.     ////read adjacency relationship among vertices
  471.     //ifstream adjacency_file;
  472.     //adjacency_file.open("adjacency.txt", ios::in);
  473.     //int adj_1, adj_2;
  474.     //while (adjacency_file >> adj_1 >> adj_2)
  475.     //{
  476.     //  Vertex* v_1 = g.vertice.at(adj_1);
  477.     //  Vertex* v_2 = g.vertice.at(adj_2);
  478.     //  //calculate the distance between the two vertices
  479.     //  double distance = v_1->distanceTo(*v_2);
  480.     //  v_1->addAdjacentVertex(v_2, distance);
  481.     //  v_2->addAdjacentVertex(v_1, distance);
  482.     //}
  483.     //adjacency_file.close();
  484.  
  485.     g.load(&v1);
  486.     g.load(&v2);
  487.     g.load(&v3);
  488.     g.load(&v4);
  489.     g.load(&v5);
  490.     g.load(&v6);
  491.     g.load(&v7);
  492.     g.load(&v8);
  493.     v1.addAdjacentVertex(&v2, 0);
  494.     v1.addAdjacentVertex(&v7, 0);
  495.     v2.addAdjacentVertex(&v1, 0);
  496.     v2.addAdjacentVertex(&v3, 0);
  497.     v2.addAdjacentVertex(&v4, 0);
  498.     v3.addAdjacentVertex(&v2, 0);
  499.     v3.addAdjacentVertex(&v4, 0);
  500.     v3.addAdjacentVertex(&v5, 0);
  501.     v3.addAdjacentVertex(&v8, 0);
  502.     v4.addAdjacentVertex(&v2, 0);
  503.     v4.addAdjacentVertex(&v3, 0);
  504.     v4.addAdjacentVertex(&v5, 0);
  505.     v5.addAdjacentVertex(&v3, 0);
  506.     v5.addAdjacentVertex(&v4, 0);
  507.     v5.addAdjacentVertex(&v6, 0);
  508.     v5.addAdjacentVertex(&v7, 0);
  509.     v6.addAdjacentVertex(&v5, 0);
  510.     v7.addAdjacentVertex(&v1, 0);
  511.     v7.addAdjacentVertex(&v5, 0);
  512.     v8.addAdjacentVertex(&v3, 0);
  513.     g.printAdjacencyMatrix();
  514.     g.printAdjacencyList();
  515.     g.breadFirstSearch(2);
  516.     g.restoreVisitedFlag();
  517.     g.depthFirstSearch(2);
  518.     g.restoreVisitedFlag();
  519.     getchar();
  520.     //--------------------------------------
  521.  
  522.     g.Dijkstra(28776);
  523.  
  524.  
  525.  
  526.  
  527.     return 0;
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement