-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
88 lines (75 loc) · 2.66 KB
/
Graph.cpp
File metadata and controls
88 lines (75 loc) · 2.66 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
77
78
79
80
81
82
83
84
85
86
87
88
#include "Graph.h"
void Graph::printGraph() {
std::for_each(vertexes.begin(), vertexes.end(), [](const Vertex& vertex){
std::cout << vertex << std::endl;
});
std::cout << "S: " << s->getId()<< std::endl;
std::cout << "T: " << t->getId()<< std::endl;
}
void Graph::buildGraph() {
int vertexesNum, edgesNum, sId, tId;
std::cin >> vertexesNum;
std::cin >> edgesNum;
std::cin >> sId;
std::cin >> tId;
validateInput(vertexesNum, edgesNum, sId, tId);
makeEmptyGraph(vertexesNum);
this->s = &vertexes[sId -1];
this->t = &vertexes[tId -1];
int src, dest, weight;
for(int i = 0; i< edgesNum; ++i){
std::cin >> src >> dest >> weight;
if(src <= 0 || src > vertexesNum || dest <= 0 || dest > vertexesNum || weight < 0)
throw std::invalid_argument("invalid input");
addEdge(src, dest,weight);
}
}
void Graph::validateInput(int numOfVertexes, int numOfEdges, int sId, int tId) {
if(numOfVertexes <= 0)
throw std::invalid_argument("invalid input");
if(numOfEdges <= 0 )
throw std::invalid_argument("invalid input");
if(sId <=0 || sId > numOfVertexes || tId <= 0 || tId > numOfVertexes)
throw std::invalid_argument("invalid input");
}
void Graph::makeEmptyGraph(int numOfVertexes) {
for(int i = 0; i < numOfVertexes; ++i){
this->vertexes.emplace_back(i + 1);
}
}
std::list<int> Graph::getAdjList(int vertexId) {
std::list<int> adjLst;
const std::list<Edge>& vertexNeighbours = this->vertexes[vertexId].getEdges();
std::for_each(vertexNeighbours.begin(), vertexNeighbours.end(), [&adjLst](const Edge& e){
adjLst.push_back(e.getDest());
});
return adjLst;
}
void Graph::addEdge(int src, int dest, int weight) {
this->vertexes[src -1].addEdge(dest,weight);
}
void Graph::removeEdge(int src, int dest) {
std::list<Edge>& edges = this->vertexes[src -1].getEdges();
auto found = (std::find_if(edges.begin(), edges.end(), [&src, &dest](const Edge& e){
return e.getSrc() == src && e.getDest() == dest;
}));
if(found != this->vertexes[src -1].getEdges().end())
edges.remove(*found);
}
const Vertex& Graph::getS() const {
return *s;
}
const Vertex& Graph::getT() const {
return *t;
}
Edge* Graph::getEdge(int src, int dest) {
Edge e(src,dest,0);
std::list<Edge>& refLst = this->vertexes[src -1].getEdges();
auto res = std::find_if(refLst.begin(), refLst.end() ,[&src, &dest](const Edge& edge){
return edge.getSrc() == src && edge.getDest() == dest;
});
return (res == refLst.end()) ? nullptr : &(*res);
}
std::vector<Vertex> &Graph::getVertexes() {
return vertexes;
}