-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.h
240 lines (182 loc) · 5.45 KB
/
Graph.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#ifndef FICHAJUNG_GRAPH_H
#define FICHAJUNG_GRAPH_H
#include <vector>
#include <queue>
#include <list>
#include <limits>
#include <cmath>
#include <iostream>
#include "MutablePriorityQueue.h"
using namespace std;
class Edge;
class Graph;
class Vertex;
/************************* Vertex **************************/
class Vertex {
int id; // vertex id
double x; // x coordinate
double y; // y coordinate
vector<Edge*> adj; // outgoing edges
double dist = 0;
Vertex* path = nullptr;
int prevPlace = -1;
int queueIndex = 0; // required by MutablePriorityQueue
bool visited = false; // auxiliary field
//bool processing = false; // auxiliary field
//void addEdge(Vertex *dest, double w);
public:
Vertex(int newId, double newX, double newY);
~Vertex();
int getId() const;
double getDist() const;
Vertex *getPath() const;
vector<Edge*> getAdj() const;
bool getVisited() const;
int getPrevPlace() const;
void setDist(double distance);
void setPath(Vertex *newPath);
void addAdj(Edge *edge);
void setVisited(bool v);
void setPrevPlace(int placeId);
bool operator<(Vertex & vertex) const; // // required by MutablePriorityQueue
//friend class Graph;
friend class MutablePriorityQueue<Vertex>;
};
// ? Vertex::Vertex(T in): info(in) {}
/*
* Auxiliary function to add an outgoing edge to a vertex (this),
* with a given destination vertex (d) and edge weight (w).
*/
/*
void Vertex::addEdge(Vertex *d, double w) {
adj.push_back(Edge(d, w));
}
bool Vertex::operator<(Vertex& vertex) const {
return this->dist < vertex.dist;
}
Vertex::getInfo() const {
return this->info;
}
double Vertex<T>::getDist() const {
return this->dist;
}
Vertex *Vertex::getPath() const {
return this->path;
}*/
/********************** Edge ****************************/
class Edge {
int id;
Vertex* dest; // destination vertex
double weight; // edge weight
public:
Edge(int newId, Vertex* d, double w);
int getId();
Vertex* getDest();
double getWeight();
void updateWeight(double newWeight);
friend class Graph;
friend class Vertex;
};
/*************************** Graph **************************/
class Graph {
vector<Vertex *> vertexSet; // vertex set
vector<Edge*> edgeSet; // edge set
public:
Vertex *findVertex(int id);
//bool addVertex(const T &in);
//bool addEdge(const T &sourc, const T &dest, double w);
Graph();
int getNumVertex() const;
vector<Vertex *> getVertexSet() const;
vector<Edge *> getEdgeSet() const;
void addVertex(Vertex* newV);
void addEdge(Edge* newE);
void bestCircuit(int startVertexId, int endVertexId, vector<int> interestIds);
double g(Vertex &start, Vertex &end, vector<Vertex*> &path);
void dijkstra(int initial_id);
};
class NonExistentVertex{
int id;
public:
NonExistentVertex(int idV){ id= idV; }
// Operador << para mensagem de erro que apanha o objeto lançado pela exceção.
friend ostream & operator<<(ostream &out, const NonExistentVertex &nonExistentVertex){
out << "Vertex non existent! id: " << nonExistentVertex.id << endl;
return out;
}
};
/* CLASS STUFF, MIGHT BE USEFUL LATER
*
*
*
*
// Fp05 - single source
void unweightedShortestPath(const T &s); //TODO...
void dijkstraShortestPath(const T &s); //TODO...
void bellmanFordShortestPath(const T &s); //TODO...
vector<T> getPathTo(const T &dest) const; //TODO...
// Fp05 - all pairs
void floydWarshallShortestPath(); //TODO...
vector<T> getfloydWarshallPath(const T &origin, const T &dest) const; //TODO...
};
template <class T>
int Graph<T>::getNumVertex() const {
return vertexSet.size();
}
template <class T>
vector<Vertex<T> *> Graph<T>::getVertexSet() const {
return vertexSet;
}
*/
/*
* Auxiliary function to find a vertex with a given content.
*/
/*
template <class T>
Vertex<T> * Graph<T>::findVertex(const T &in) const {
for (auto v : vertexSet)
if (v->info == in)
return v;
return NULL;
}
*/
/*
* Adds a vertex with a given content or info (in) to a graph (this).
* Returns true if successful, and false if a vertex with that content already exists.
*/
/*
template <class T>
bool Graph<T>::addVertex(const T &in) {
if ( findVertex(in) != NULL)
return false;
vertexSet.push_back(new Vertex<T>(in));
return true;
}
*/
/*
* Adds an edge to a graph (this), given the contents of the source and
* destination vertices and the edge weight (w).
* Returns true if successful, and false if the source or destination vertex does not exist.
*/
/*
template <class T>
bool Graph<T>::addEdge(const T &sourc, const T &dest, double w) {
auto v1 = findVertex(sourc);
auto v2 = findVertex(dest);
if (v1 == NULL || v2 == NULL)
return false;
v1->addEdge(v2,w);
return true;
}
*/
/**************** Single Source Shortest Path algorithms ************/
/*
template<class T>
void Graph<T>::unweightedShortestPath(const T &orig) {
}
template<class T>
void Graph<T>::dijkstraShortestPath(const T &origin) {
// TODO
}
*/
#endif //FICHAJUNG_GRAPH_H