-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
76 lines (55 loc) · 1.91 KB
/
Copy pathGraph.cpp
File metadata and controls
76 lines (55 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "Graph.h"
// ========== PRIVATE METHODS ==========
void Graph::read_adjacency_list_and_matrix(const std::string &file_name, const std::string &graph_type) {
std::ifstream file(file_name);
int no_nodes, current_node, current_neighbour, indexing;
file >> no_nodes >> no_edges_ >> indexing;
adjacency_list_.resize(no_nodes);
adjacency_matrix_.resize(no_nodes);
for (int i = 0; i < no_nodes; ++i) {
adjacency_matrix_[i].resize(no_nodes);
}
for (int i = 0; i < no_edges_; i++) {
file >> current_node >> current_neighbour;
current_node -= indexing;
current_neighbour -= indexing;
adjacency_matrix_[current_node][current_neighbour]++;
adjacency_list_[current_node].push_back(current_neighbour);
if (graph_type == "undirected") {
adjacency_list_[current_neighbour].push_back(current_node);
adjacency_matrix_[current_neighbour][current_node]++;
}
}
}
// ========== PUBLIC METHODS ==========
Graph::Graph() {
no_edges_ = 0;
adjacency_list_ = {};
}
Graph::Graph(const std::string &file_name, const std::string &graph_type) {
graph_type_ = graph_type;
read_adjacency_list_and_matrix(file_name, graph_type);
}
int Graph::size() const {
return adjacency_list_.size();
}
const std::vector<int> &Graph::operator[](const int index) const {
return adjacency_list_[index];
}
const std::vector<std::vector<int>> &Graph::get_adjacency_matrix() const {
return adjacency_matrix_;
}
int Graph::get_no_edges() const {
return no_edges_;
}
const std::string &Graph::get_graph_type() const {
return graph_type_;
}
Graph::Graph(const std::vector<std::pair<int, int>> &edges_list, int no_nodes) {
adjacency_list_.resize(no_nodes);
for (auto edge : edges_list) {
int node = edge.first;
int neighbour = edge.second;
adjacency_list_[node].push_back(neighbour);
}
}