Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luistelmocosta/03e1ca5287bf2251f7103b96eb48324f to your computer and use it in GitHub Desktop.
Save luistelmocosta/03e1ca5287bf2251f7103b96eb48324f to your computer and use it in GitHub Desktop.
/*
* ================================================================================================
* Class Vertex
* ================================================================================================
*/
template <class T>
class Vertex {
T info;
vector<Edge<T> > adj;
bool visited;
bool processing;
int indegree;
int dist;
public:
Vertex(T in);
friend class Graph<T>;
void addEdge(Vertex<T> *dest, double distance, int line);
bool removeEdgeTo(Vertex<T> *d);
T getInfo() const;
T*getInfoPointer(){
return &info;
}
void setInfo(T info);
int getDist() const;
int getIndegree() const;
int getAdjSize(){
return adj.size();
}
Vertex* path;
};
template <class T>
struct vertex_greater_than {
bool operator()(Vertex<T> * a, Vertex<T> * b) const {
return a->getDist() > b->getDist();
}
};
template <class T>
bool Vertex<T>::removeEdgeTo(Vertex<T> *d) {
d->indegree--; //adicionado do exercicio 5
typename vector<Edge<T> >::iterator it= adj.begin();
typename vector<Edge<T> >::iterator ite= adj.end();
while (it!=ite) {
if (it->dest == d) {
adj.erase(it);
return true;
}
else it++;
}
return false;
}
template <class T>
Vertex<T>::Vertex(T in): info(in), visited(false), processing(false), indegree(0), dist(0) {
path = NULL;
}
template <class T>
void Vertex<T>::addEdge(Vertex<T> *dest, double distance, int line) {
Edge<T> edgeD(dest, distance, line);
adj.push_back(edgeD);
}
template <class T>
T Vertex<T>::getInfo() const {
return this->info;
}
template <class T>
int Vertex<T>::getDist() const {
return this->dist;
}
template <class T>
void Vertex<T>::setInfo(T info) {
this->info = info;
}
template <class T>
int Vertex<T>::getIndegree() const {
return this->indegree;
}
/* ================================================================================================
* Class Edge
* ================================================================================================
*/
template <class T>
class Edge {
Vertex<T> * dest;
double distance;
int line;
public:
Edge(Vertex<T> *d, double dis, int l);
friend class Graph<T>;
friend class Vertex<T>;
};
template <class T>
Edge<T>::Edge(Vertex<T> *d, double dis, int l){
dest = d;
distance = dis;
line = l;
}
/* ================================================================================================
* Class Graph
* ================================================================================================
*/
template <class T>
class Graph {
vector<Vertex<T> *> vertexSet;
void dfs(Vertex<T> *v, vector<T> &res) const;
//exercicio 5
int numCycles;
void dfsVisit(Vertex<T> *v);
void dfsVisit();
void getPathTo(Vertex<T> *origin, list<T> &res);
public:
bool addVertex(const T &in);
bool addEdge(const T &sourc, const T &dest, double dis, int line);
bool removeVertex(const T &in);
bool removeEdge(const T &sourc, const T &dest);
vector<T> dfs() const;
vector<T> bfs(Vertex<T> *v) const;
int maxNewChildren(Vertex<T> *v, T &inf) const;
vector<Vertex<T> * > getVertexSet() const;
int getNumVertex() const;
vector<Vertex<T>*> bestPathFrom(const T &s);
void dijkstraShortestPath(const T &s);
//exercicio 5
Vertex<T>* getVertex(const T &v) const;
void resetIndegrees();
vector<Vertex<T>*> getSources() const;
int getNumCycles();
vector<T> topologicalOrder();
vector<T> getPath(const T &origin, const T &dest);
void unweightedShortestPath(const T &v);
bool isDAG();
T getVertexbyId(int id);
T*getVertexbyIdPointer(int id);
int getFirstId(string nome){
for(int i = 0; i < vertexSet.size(); i++){
if(vertexSet[i]->getInfo().getNome() == nome && vertexSet[i]->getInfo().getId() >= 0){
return vertexSet[i]->getInfo().getId();
}
}
return -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment