Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main.cpp
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <algorithm>
- #include <iterator>
- #include <vector>
- #include <fstream>
- #include <cctype>
- #include <queue>
- #include <stack>
- #include "Graph.h"
- using namespace std;
- template <class VertexType>
- void DepthFirstSearch(Graph<VertexType>, VertexType, VertexType);
- bool is_number( const std::string & );
- int main (int argc, char ** argv)
- {
- std::vector<std::string> tokens;
- std::string full_input;
- Graph<std::string> airport_routes;
- /*bool go_on = true;*/
- /*std::cout << "<File name> <Current City> <Destination City>" << std::endl;*/
- // Get User Input
- std::cout << "find_route " << std::ends;
- std::getline(std::cin, full_input);
- std::string buffer;
- std::stringstream split_string(full_input);
- while (split_string >> buffer)
- tokens.push_back(buffer);
- // Getting parts of the input string
- std::string file_name = tokens.at(0);
- std::string from_city = tokens.at(1);
- std::string to_city = tokens.at(2);
- tokens.clear();
- // Reading file
- ifstream file_reader(file_name);
- if (!file_reader.is_open())
- std::cerr << "Could not open file" << std::endl;
- // Tokenize file
- std::string tok;
- while( file_reader >> tok)
- tokens.push_back(tok);
- // Deleting the end of file
- tokens.erase(tokens.end()-3, tokens.end());
- // Filtering out city names
- std::vector<std::string> city_names;
- for (auto str : tokens)
- {
- if (!is_number(str))
- {
- if (city_names.empty())
- {
- city_names.push_back(str);
- } else {
- if(std::find(city_names.begin(), city_names.end(), str) == city_names.end())
- city_names.push_back(str);
- }
- }
- }
- // Adding City Names
- for (auto var: city_names)
- {
- /*std::cout << "City being added: " << var << std::endl;*/
- airport_routes.addVertex(var);
- }
- // Closing file reader
- file_reader.close();
- // Adding edges
- for(std::size_t i = 0; i+2 < tokens.size(); i += 3)
- {
- auto from_place = tokens.at(i);
- auto to_place = tokens.at(i+1);
- int weight = std::stoi(tokens.at(i+2));
- /*std::cout << "Adding Edge: " << from_place << " " << to_place << " " << weight << std::endl;*/
- airport_routes.addEdge( from_place, to_place, weight);
- }
- DepthFirstSearch(airport_routes, from_city, to_city);
- system("pause");
- return 0;
- }
- bool is_number( const std::string & str)
- {
- std::string::const_iterator str_it = str.begin();
- while (str_it != str.end() && std::isdigit(*str_it))
- str_it++;
- return !str.empty() && str_it == str.end();
- }
- template <class VertexType>
- void DepthFirstSearch(Graph<VertexType> routes, VertexType from_vertex, VertexType to_vertex)
- {
- stack<VertexType> mystack;
- queue<VertexType> vertexQ;
- std::vector<std::vector<VertexType>> path_vector;
- int total_weight = 0;
- int found_count = 0;
- /*stringstream ss;*/
- bool found = false;
- VertexType vertex;
- VertexType item;
- routes.clearMarks();
- mystack.push(from_vertex); // Adding Location to stack
- do
- {
- vertex = mystack.top(); // Get top location
- mystack.pop(); // Getting rid of top location
- if (vertex == to_vertex) // If location == destination, then stop;
- {
- found = true;
- found_count++;
- } else {
- if (!routes.isMarked(vertex)) // If route has not been traveled to
- {
- routes.markVertex(vertex); // Mark place
- routes.goToVertices(vertex, vertexQ); // Add adjacent positions
- while (!vertexQ.empty()) // While there are still positions left to test
- {
- item = vertexQ.front(); // Get top position
- vertexQ.pop(); // Get rid of top position from stack
- if (!routes.isMarked(item)) // If top position is not marked,
- {
- mystack.push(item); // Add to-visit stack
- std::cout << "From " << vertex << " to " << item << " : " << routes.weight(vertex, item) << std::endl;
- }
- }
- std::cout << std::endl;
- }
- }
- } while (!mystack.empty());
- if(!found)
- {
- std::cout << "Distance: infinity" << std::endl;
- std::cout << "Route:" << std::endl;
- std::cout << "None" << std::endl;
- } else {
- std::cout << "Found this many times: " << found_count << std::endl;
- }
- }
- // Graph.h
- #ifndef GRAPH_H_
- #define GRAPH_H_
- #include <queue>
- #include <vector>
- #include <algorithm>
- using namespace std;
- const int NULL_EDGE = 0;
- template <class VertexType>
- class Graph
- {
- int num_vertices;
- int max_vertices;
- VertexType * vertices;
- int edges[50][50];
- bool marks[50];
- public:
- Graph(void);
- Graph(int);
- ~Graph(void) { delete [] vertices; /*delete [] marks;*/ };
- void makeEmpty();
- void isEmpty();
- void isFull();
- void addVertex(VertexType);
- void addEdge(VertexType, VertexType, int);
- int weight(VertexType, VertexType);
- void goToVertices(VertexType, std::vector<VertexType> &);
- void goToVertices(VertexType, std::queue<VertexType> &);
- void clearMarks();
- void markVertex(VertexType);
- bool isMarked(VertexType);
- /*void printVertices();*/
- /*void printEdges();*/
- };
- // Graph.cpp
- #ifndef GRAPH_CPP_
- #define GRAPH_CPP_
- #include "Graph.h"
- template <class VertexType>
- bool Graph<VertexType>::isMarked( VertexType vertex)
- {
- return marks[indexIs(vertices, vertex)];
- }
- template <class VertexType>
- void Graph<VertexType>::markVertex( VertexType vertex )
- {
- int index_val = indexIs(vertices, vertex);
- marks[index_val] = true;
- }
- template <class VertexType>
- void Graph<VertexType>::clearMarks()
- {
- for(int i = 0; i < num_vertices; i++)
- marks[i] = false;
- }
- template <class VertexType>
- void Graph<VertexType>::goToVertices( VertexType vertex, std::vector<VertexType> & adjVertices)
- {
- int fromIndex = indexIs(vertices, vertex);
- for (int toIndex = 0; toIndex < num_vertices ; toIndex++)
- {
- if (edges[fromIndex][toIndex] != NULL_EDGE)
- adjVertices.push_back(vertices[toIndex]);
- }
- }
- template <class VertexType>
- void Graph<VertexType>::goToVertices( VertexType vertex, std::queue<VertexType> & adjVertices)
- {
- int fromIndex = indexIs(vertices, vertex);
- for (int toIndex = 0; toIndex < num_vertices ; toIndex++)
- {
- if (edges[fromIndex][toIndex] != NULL_EDGE)
- adjVertices.push(vertices[toIndex]);
- }
- }
- template <class VertexType>
- int Graph<VertexType>::weight( VertexType from_vertex, VertexType to_vertex)
- {
- int row;
- int col;
- row = indexIs(vertices, from_vertex);
- col = indexIs(vertices, to_vertex);
- return edges[row][col];
- }
- template <class VertexType>
- void Graph<VertexType>::addEdge( VertexType from_vertex, VertexType to_vertex, int w)
- {
- int row = indexIs(vertices, from_vertex);
- int col = indexIs(vertices, to_vertex);
- edges[row][col] = w;
- }
- template <class VertexType>
- int indexIs(VertexType * vertices, VertexType vertex)
- {
- int index = 0;
- while (!(vertex == vertices[index]))
- index++;
- return index;
- }
- template <class VertexType>
- void Graph<VertexType>::addVertex( VertexType vertex )
- {
- vertices[num_vertices] = vertex;
- for (int i = 0; i < num_vertices; i++)
- {
- edges[num_vertices][i] = NULL_EDGE;
- edges[i][num_vertices] = NULL_EDGE;
- }
- num_vertices++;
- }
- template <class VertexType>
- void Graph<VertexType>::makeEmpty()
- {
- num_vertices = 0;
- auto tempVert = vertices;
- vertices = new VertexType[50];
- delete [] tempVert;
- }
- template <class VertexType>
- Graph<VertexType>::Graph()
- {
- num_vertices = 0;
- max_vertices = 50;
- vertices = new VertexType[50];
- /*marks = new bool[50];*/
- for (int i = 0; i < 50; i++)
- {
- marks[i] = false;
- }
- int j;
- for (int i = 0; i < 50; i++)
- {
- j = i;
- edges[i][j] = 0;
- }
- }
- template <class VertexType>
- Graph<VertexType>::Graph(int m_vert)
- {
- num_vertices = 0;
- this->max_vertices = m_vert;
- vertices = new VertexType[m_vert];
- /*marks = new bool[m_vert];*/
- for (int i = 0; i < 50; i++)
- {
- marks[i] = false;
- }
- int j;
- for (int i = 0; i < 50; i++)
- {
- j = i;
- edges[i][j] = 0;
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement