Create Graph.h, Edge.h, Vertex.h, GraphMatrix.h, not finished yet.
This commit is contained in:
16
thu_dsa/chp6/Edge.h
Normal file
16
thu_dsa/chp6/Edge.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef EDGE_H_
|
||||
#define EDGE_H_
|
||||
|
||||
#include "Graph.h"
|
||||
|
||||
template <typename Te>
|
||||
class Edge{
|
||||
Te data;
|
||||
double weight = 1;
|
||||
Etype type = UNDETERMINED;
|
||||
|
||||
Edge(Te data) : data(data) {}
|
||||
Edge(Te data, double w) : data(data), weight(w) {}
|
||||
};
|
||||
|
||||
#endif
|
||||
57
thu_dsa/chp6/Graph.h
Normal file
57
thu_dsa/chp6/Graph.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef GRAPH_H_
|
||||
#define GRAPH_H_
|
||||
|
||||
#include "../chp2/Vector.h"
|
||||
#include "../chp4/Stack.h"
|
||||
|
||||
typedef enum { UNDISCOVERED, DISCOVERED, VISITED } VStatus;
|
||||
typedef enum { UNDETERMINED, TREE, BACKWARD, FORWARD, CROSS } Etype;
|
||||
|
||||
//abstract class Graph
|
||||
template <typename Tv> template <typename Te>
|
||||
class Graph{
|
||||
private:
|
||||
void reset();
|
||||
void BFS(int, int&);
|
||||
void DFS(int, int&);
|
||||
void BCC(int, int&, Stack<int>&);
|
||||
bool TSort(int, int&, Stack<Tv>*);
|
||||
template <typename PU> void PFS(int, PU);
|
||||
|
||||
public:
|
||||
int num_of_vertices;
|
||||
int num_of_edges;
|
||||
|
||||
//vertex methods
|
||||
virtual int insertVertex(Tv const&) = 0; //return the id number of the new vertex
|
||||
virtual Tv removeVertex(int) = 0;
|
||||
virtual Tv& vertex(int) = 0;
|
||||
virtual int inDegree(int) = 0;
|
||||
virtual int outDegree(int) = 0;
|
||||
virtual int firstNeighbor(int) = 0;
|
||||
virtual int nextNeighbor(int, int) = 0;
|
||||
virtual int& dtime(int) = 0;
|
||||
virtual int& ftime(int) = 0;
|
||||
virtual int& parent(int) = 0;
|
||||
virtual int& priority(int) = 0;
|
||||
virtual VStatus& status(int) = 0;
|
||||
|
||||
//directed egde methods
|
||||
virtual bool exists(int, int) = 0;
|
||||
virtual void insertEdge(Te const&, int, int, int) = 0;
|
||||
virtual Te removeEdge(int, int) = 0;
|
||||
virtual Etype& type(int, int) = 0;
|
||||
virtual Te& edge(int, int) = 0;
|
||||
virtual int& weight(int, int) = 0;
|
||||
|
||||
//Graph related algorithms
|
||||
void bfs(int);
|
||||
void dfs(int);
|
||||
void bcc(int);
|
||||
void prim(int);
|
||||
void dijkstra(int);
|
||||
Stack<Tv>* tSort(int);
|
||||
template <typename PU> void pfd(int, PU);
|
||||
};
|
||||
|
||||
#endif
|
||||
108
thu_dsa/chp6/GraphMatrix.h
Normal file
108
thu_dsa/chp6/GraphMatrix.h
Normal file
@@ -0,0 +1,108 @@
|
||||
#ifndef GRAPHMATRIX_H_
|
||||
#define GRAPHMATRIX_H_
|
||||
|
||||
#include "Graph.h"
|
||||
#include "Edge.h"
|
||||
#include "Vertex.h"
|
||||
#include "../chp2/Vector.h"
|
||||
|
||||
template <typename Tv> template <typename Te>
|
||||
class GraphMatrix: public Graph<Tv, Te>{
|
||||
private:
|
||||
Vector<Vertex<Tv>> V;
|
||||
Vector<Vector<Edge<Te>*>> E;
|
||||
|
||||
public:
|
||||
//constructor
|
||||
GraphMatrix();
|
||||
|
||||
//deconstructor
|
||||
~GraphMatrix();
|
||||
|
||||
//implementations of abstract Graph methods
|
||||
|
||||
//vertex methods
|
||||
int insertVertex(Tv const& data); //return the id number of the new vertex
|
||||
Tv removeVertex(int vertexId);
|
||||
Tv& vertex(int vertexId) { return V[vertexId].data; }
|
||||
int inDegree(int vertexId) { return V[vertexId].inDegree; }
|
||||
int outDegree(int vertexId) { return V[vertexId].outDegree; }
|
||||
int firstNeighbor(int vertexId);
|
||||
int nextNeighbor(int vertexId, int curr);
|
||||
int& dtime(int vertexId) { return V[vertexId].dtime; }
|
||||
int& ftime(int vertexId) { return V[vertexId].ftime; }
|
||||
int& parent(int vertexId) { return V[vertexId].parent; }
|
||||
int& priority(int vertexId) { return V[vertexId].priority; }
|
||||
VStatus& status(int vertexId) { return V[vertexId].status; }
|
||||
|
||||
//directed edge methods
|
||||
bool exists(int src, int tail) { return E[src][tail] != nullptr; }
|
||||
void insertEdge(Te const &data, int weight, int src, int tail);
|
||||
Te removeEdge(int src, int tail);
|
||||
Etype& type(int src, int tail) { return E[src][tail]->type; }
|
||||
Te& edge(int src, int tail) { return E[src][tail]->data; }
|
||||
int& weight(int src, int tail) { return E[src][tail]->weight; }
|
||||
};
|
||||
|
||||
/*---------------Implementations---------------*/
|
||||
|
||||
//constructor
|
||||
template <typename Tv> template <typename Te>
|
||||
GraphMatrix<Tv, Te>::GraphMatrix(){
|
||||
num_of_vertices = 0;
|
||||
num_of_edges = 0;
|
||||
|
||||
V = Vector<Vertex<Tv>>();
|
||||
E = Vector<Vector<Edge<Te>>>();
|
||||
}
|
||||
|
||||
//implementations of abstract Graph methods
|
||||
|
||||
//vertex methods
|
||||
template <typename Tv> template <typename Te>
|
||||
int GraphMatrix<Tv, Te>::insertVertex(Tv const & data){
|
||||
V.push_back(Vertex<Tv>(data));
|
||||
for (int ix = 0; ix != num_of_vertices; ++ix) E[ix].push_back(nullptr);
|
||||
E.push_back(Vector<Edge<Te>*>(nullptr, ++num_of_vertices));
|
||||
return num_of_vertices - 1;
|
||||
}
|
||||
|
||||
template <typename Tv> template <typename Te>
|
||||
Te GraphMatrix<Tv, Te>::removeVertex(int vertexId){
|
||||
for (int ix = 0; ix != num_of_vertices; ++ix) {
|
||||
//remove edges
|
||||
if(exists(ix, vertexId) removeEdge(ix, vertexId);
|
||||
if(exists(vertexId, ix) removeEdge(vertexId, ix);
|
||||
E[ix].pop(vertexId);
|
||||
}
|
||||
E.pop(vertexId);
|
||||
--num_of_vertices;
|
||||
return V.pop(vertexId).data;
|
||||
}
|
||||
|
||||
//edge methods
|
||||
template <typename Tv> template <typename Te>
|
||||
void GraphMatrix<Tv, Te>::insertEdge(Te const &data, int weight, int src, int tail){
|
||||
if (src < 0 || src >= num_of_vertices || tail < 0 || tail >= num_of_vertices) return;
|
||||
if (exists(src, tail)) return;
|
||||
|
||||
E[src][tail] = new Edge<Te>(data, weight);
|
||||
|
||||
++num_of_edges;
|
||||
V[tail].inDegree++;
|
||||
V[src].outDegree--;
|
||||
}
|
||||
|
||||
template <typename Tv> template <typename Te>
|
||||
Te GraphMatrix<Tv, Te>::removeEdge(int src, int tail){
|
||||
Te edgeBak = edge(src, tail);
|
||||
delete edge(src, tail);
|
||||
edge(src, tail) = nullptr;
|
||||
|
||||
--num_of_edges;
|
||||
V[tail].inDegree--;
|
||||
V[src].outDegree--;
|
||||
return edgeBak;
|
||||
}
|
||||
|
||||
#endif
|
||||
21
thu_dsa/chp6/Vertex.h
Normal file
21
thu_dsa/chp6/Vertex.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef VERTEX_H_
|
||||
#define VERTEX_H_
|
||||
|
||||
#include "Graph.h"
|
||||
|
||||
template <typename Tv>
|
||||
class Vertex{
|
||||
public:
|
||||
Tv data;
|
||||
VStatus status = UNDISCOVERED;
|
||||
int parent = -1;
|
||||
int dTime = -1;
|
||||
int fTime = -1;
|
||||
int priority = 0;
|
||||
int inDegree = 0;
|
||||
int outDegree = 0;
|
||||
|
||||
Vertex(Tv data) : data(data) {}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user