From a6528cc50bb2ebc509cd4ede4deda1a112c45d57 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Wed, 4 Jan 2017 14:31:20 -0500 Subject: Store vertices in Graph --- src/graph.h | 25 ++++++++++++------------- src/parse.h | 11 ----------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/graph.h b/src/graph.h index 8c04606..b4b300b 100644 --- a/src/graph.h +++ b/src/graph.h @@ -6,31 +6,30 @@ class Graph { public: - Graph() : graph_() {} + Graph() : vertices_(), graph_() {} void add_vertex(int vertex) { - graph_[vertex] = std::vector(); + if (vertices_.find(vertex) == vertices_.end()) { + vertices_.insert(vertex); + graph_[vertex] = std::vector(); + } } void add_edge(int from, int to) { + add_vertex(from); + add_vertex(to); graph_[from].push_back(to); } - std::set get_vertices() { - std::set vertices; - - for (auto vertex : graph_) { - vertices.insert(vertex.first); - } - - return vertices; + const std::set & get_vertices() const { + return vertices_; } - - std::vector const& get_neighbors(int vertex) { - return graph_[vertex]; + const std::vector & get_neighbors(int vertex) const { + return graph_.find(vertex)->second; } private: + std::set vertices_; std::map> graph_; }; diff --git a/src/parse.h b/src/parse.h index ec86302..52b8520 100644 --- a/src/parse.h +++ b/src/parse.h @@ -22,7 +22,6 @@ private: Graph graph_; std::ifstream input_file_; int number_edges_; - std::set vertices_; void parse_number_edges_() { input_file_ >> number_edges_; @@ -33,16 +32,6 @@ private: input_file_ >> from >> to; - if (vertices_.find(from) == vertices_.end()) { - graph_.add_vertex(from); - vertices_.insert(from); - } - - if (vertices_.find(to) == vertices_.end()) { - graph_.add_vertex(to); - vertices_.insert(to); - } - graph_.add_edge(from, to); } }; -- cgit v1.2.3