Advertisement
Guest User

Graph Problem

a guest
Apr 19th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.82 KB | None | 0 0
  1. // Main.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <vector>
  9. #include <fstream>
  10. #include <cctype>
  11. #include <queue>
  12. #include <stack>
  13. #include "Graph.h"
  14.  
  15. using namespace std;
  16.  
  17. template <class VertexType>
  18. void DepthFirstSearch(Graph<VertexType>, VertexType, VertexType);
  19.  
  20. bool is_number( const std::string & );
  21.  
  22. int main (int argc, char ** argv)
  23. {
  24.     std::vector<std::string> tokens;
  25.     std::string full_input;
  26.     Graph<std::string> airport_routes;
  27.     /*bool go_on = true;*/
  28.  
  29.     /*std::cout << "<File name> <Current City> <Destination City>" << std::endl;*/
  30.  
  31.     // Get User Input
  32.     std::cout << "find_route " << std::ends;
  33.     std::getline(std::cin, full_input);
  34.     std::string buffer;
  35.     std::stringstream split_string(full_input);
  36.        
  37.     while (split_string >> buffer)
  38.         tokens.push_back(buffer);
  39.  
  40.     // Getting parts of the input string
  41.     std::string file_name = tokens.at(0);
  42.     std::string from_city = tokens.at(1);
  43.     std::string to_city   = tokens.at(2);
  44.     tokens.clear();
  45.        
  46.     // Reading file
  47.     ifstream file_reader(file_name);
  48.     if (!file_reader.is_open())
  49.         std::cerr << "Could not open file" << std::endl;
  50.  
  51.     // Tokenize file
  52.     std::string tok;
  53.     while( file_reader >> tok)
  54.         tokens.push_back(tok);
  55.  
  56.     // Deleting the end of file
  57.     tokens.erase(tokens.end()-3, tokens.end());
  58.        
  59.     // Filtering out city names
  60.     std::vector<std::string> city_names;
  61.     for (auto str : tokens)
  62.     {
  63.         if (!is_number(str))
  64.         {
  65.             if (city_names.empty())
  66.             {
  67.                 city_names.push_back(str);
  68.             } else {
  69.                 if(std::find(city_names.begin(), city_names.end(), str) == city_names.end())
  70.                     city_names.push_back(str);
  71.             }
  72.         }
  73.     }  
  74.  
  75.     // Adding City Names
  76.     for (auto var: city_names)
  77.     {
  78.         /*std::cout << "City being added: " << var << std::endl;*/
  79.         airport_routes.addVertex(var);
  80.     }
  81.  
  82.     // Closing file reader
  83.     file_reader.close();
  84.        
  85.     // Adding edges
  86.     for(std::size_t i = 0; i+2 < tokens.size(); i += 3)
  87.     {
  88.         auto from_place = tokens.at(i);
  89.         auto to_place = tokens.at(i+1);
  90.         int  weight = std::stoi(tokens.at(i+2));
  91.         /*std::cout << "Adding Edge: " << from_place << " " << to_place << " " << weight << std::endl;*/
  92.         airport_routes.addEdge( from_place, to_place, weight);
  93.     }
  94.  
  95.     DepthFirstSearch(airport_routes, from_city, to_city);
  96.  
  97.     system("pause");
  98.     return 0;
  99. }
  100.  
  101. bool is_number( const std::string & str)
  102. {
  103.     std::string::const_iterator str_it = str.begin();
  104.     while (str_it != str.end() && std::isdigit(*str_it))
  105.         str_it++;
  106.     return !str.empty() && str_it == str.end();
  107. }
  108.  
  109. template <class VertexType>
  110. void DepthFirstSearch(Graph<VertexType> routes, VertexType from_vertex, VertexType to_vertex)
  111. {
  112.     stack<VertexType> mystack;
  113.     queue<VertexType> vertexQ;
  114.     std::vector<std::vector<VertexType>> path_vector;
  115.     int total_weight = 0;
  116.     int found_count = 0;
  117.  
  118.     /*stringstream ss;*/
  119.  
  120.     bool found = false;
  121.     VertexType vertex;
  122.     VertexType item;
  123.  
  124.     routes.clearMarks();
  125.  
  126.     mystack.push(from_vertex); // Adding Location to stack
  127.  
  128.     do
  129.     {  
  130.         vertex = mystack.top(); // Get top location
  131.         mystack.pop(); // Getting rid of top location
  132.         if (vertex == to_vertex) // If location == destination, then stop;
  133.         {
  134.             found = true;
  135.             found_count++;
  136.         } else {
  137.             if (!routes.isMarked(vertex)) // If route has not been traveled to
  138.             {
  139.                 routes.markVertex(vertex); // Mark place
  140.                 routes.goToVertices(vertex, vertexQ); // Add adjacent positions
  141.                 while (!vertexQ.empty()) // While there are still positions left to test
  142.                 {
  143.                     item = vertexQ.front(); // Get top position
  144.                     vertexQ.pop(); // Get rid of top position from stack
  145.                     if (!routes.isMarked(item)) // If top position is not marked,
  146.                     {
  147.                         mystack.push(item); // Add to-visit stack
  148.                         std::cout << "From " << vertex << " to " << item << " : " << routes.weight(vertex, item) << std::endl;
  149.                     }
  150.                 }
  151.                 std::cout << std::endl;
  152.             }
  153.         }
  154.     } while (!mystack.empty());
  155.  
  156.     if(!found)
  157.     {
  158.         std::cout << "Distance: infinity" << std::endl;
  159.         std::cout << "Route:" << std::endl;
  160.         std::cout << "None" << std::endl;
  161.     } else {
  162.         std::cout << "Found this many times: " << found_count << std::endl;
  163.     }
  164.    
  165. }
  166.  
  167. // Graph.h
  168.  
  169. #ifndef GRAPH_H_
  170. #define GRAPH_H_
  171.  
  172. #include <queue>
  173. #include <vector>
  174. #include <algorithm>
  175.  
  176. using namespace std;
  177. const int NULL_EDGE = 0;
  178.  
  179. template <class VertexType>
  180. class Graph
  181. {
  182.     int num_vertices;
  183.     int max_vertices;
  184.     VertexType * vertices;
  185.     int edges[50][50];
  186.     bool marks[50];
  187. public:
  188.     Graph(void);
  189.     Graph(int);
  190.     ~Graph(void) { delete [] vertices; /*delete [] marks;*/ };
  191.     void makeEmpty();
  192.     void isEmpty();
  193.     void isFull();
  194.     void addVertex(VertexType);
  195.     void addEdge(VertexType, VertexType, int);
  196.     int  weight(VertexType, VertexType);
  197.     void goToVertices(VertexType, std::vector<VertexType> &);
  198.     void goToVertices(VertexType, std::queue<VertexType> &);
  199.     void clearMarks();
  200.     void markVertex(VertexType);
  201.     bool isMarked(VertexType);
  202.     /*void printVertices();*/
  203.     /*void printEdges();*/
  204. };
  205.  
  206. // Graph.cpp
  207.  
  208. #ifndef GRAPH_CPP_
  209. #define GRAPH_CPP_
  210.  
  211. #include "Graph.h"
  212.  
  213. template <class VertexType>
  214. bool Graph<VertexType>::isMarked( VertexType vertex)
  215. {
  216.     return marks[indexIs(vertices, vertex)];
  217. }
  218.  
  219. template <class VertexType>
  220. void Graph<VertexType>::markVertex( VertexType vertex )
  221. {
  222.     int index_val = indexIs(vertices, vertex);
  223.     marks[index_val] = true;
  224. }
  225.  
  226. template <class VertexType>
  227. void Graph<VertexType>::clearMarks()
  228. {
  229.     for(int i = 0; i < num_vertices; i++)
  230.         marks[i] = false;
  231. }
  232.  
  233. template <class VertexType>
  234. void Graph<VertexType>::goToVertices( VertexType vertex, std::vector<VertexType> & adjVertices)
  235. {
  236.     int fromIndex = indexIs(vertices, vertex);
  237.     for (int toIndex = 0; toIndex < num_vertices ; toIndex++)
  238.     {
  239.         if (edges[fromIndex][toIndex] != NULL_EDGE)
  240.             adjVertices.push_back(vertices[toIndex]);
  241.     }
  242. }
  243.  
  244. template <class VertexType>
  245. void Graph<VertexType>::goToVertices( VertexType vertex, std::queue<VertexType> & adjVertices)
  246. {
  247.     int fromIndex = indexIs(vertices, vertex);
  248.     for (int toIndex = 0; toIndex < num_vertices ; toIndex++)
  249.     {
  250.         if (edges[fromIndex][toIndex] != NULL_EDGE)
  251.             adjVertices.push(vertices[toIndex]);
  252.     }
  253. }
  254.  
  255. template <class VertexType>
  256. int Graph<VertexType>::weight( VertexType from_vertex, VertexType to_vertex)
  257. {
  258.     int row;
  259.     int col;
  260.  
  261.     row = indexIs(vertices, from_vertex);
  262.     col = indexIs(vertices, to_vertex);
  263.  
  264.     return edges[row][col];
  265. }
  266.  
  267. template <class VertexType>
  268. void Graph<VertexType>::addEdge( VertexType from_vertex, VertexType to_vertex, int w)
  269. {
  270.     int row = indexIs(vertices, from_vertex);
  271.     int col = indexIs(vertices, to_vertex);
  272.  
  273.     edges[row][col] = w;
  274. }
  275.  
  276. template <class VertexType>
  277. int indexIs(VertexType * vertices, VertexType vertex)
  278. {
  279.     int index = 0;
  280.     while (!(vertex == vertices[index]))
  281.         index++;
  282.     return index;
  283. }
  284.  
  285. template <class VertexType>
  286. void Graph<VertexType>::addVertex( VertexType vertex )
  287. {
  288.     vertices[num_vertices] = vertex;
  289.     for (int i = 0; i < num_vertices; i++)
  290.     {
  291.         edges[num_vertices][i] = NULL_EDGE;
  292.         edges[i][num_vertices] = NULL_EDGE;
  293.     }
  294.     num_vertices++;
  295. }
  296.  
  297. template <class VertexType>
  298. void Graph<VertexType>::makeEmpty()
  299. {
  300.     num_vertices = 0;
  301.     auto tempVert = vertices;
  302.     vertices = new VertexType[50];
  303.     delete [] tempVert;
  304. }
  305.  
  306. template <class VertexType>
  307. Graph<VertexType>::Graph()
  308. {
  309.     num_vertices = 0;
  310.     max_vertices = 50;
  311.     vertices = new VertexType[50];
  312.     /*marks = new bool[50];*/
  313.  
  314.     for (int i = 0; i < 50; i++)
  315.     {
  316.         marks[i] = false;
  317.     }
  318.  
  319.     int j;
  320.     for (int i = 0; i < 50; i++)
  321.     {
  322.         j = i;
  323.         edges[i][j] = 0;
  324.     }
  325. }
  326.  
  327. template <class VertexType>
  328. Graph<VertexType>::Graph(int m_vert)
  329. {
  330.     num_vertices = 0;
  331.     this->max_vertices = m_vert;
  332.     vertices = new VertexType[m_vert];
  333.     /*marks = new bool[m_vert];*/
  334.     for (int i = 0; i < 50; i++)
  335.     {
  336.         marks[i] = false;
  337.     }
  338.  
  339.     int j;
  340.     for (int i = 0; i < 50; i++)
  341.     {
  342.         j = i;
  343.         edges[i][j] = 0;
  344.     }
  345. }
  346.  
  347. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement