From b638fbc71faba91710527178bac6dec447ade208 Mon Sep 17 00:00:00 2001 From: Shine wOng <1551885@tongji.edu.cn> Date: Mon, 3 Jun 2019 17:04:09 +0800 Subject: [PATCH] Create Graph.h, Edge.h, Vertex.h, GraphMatrix.h, not finished yet. --- thu_dsa/chp6/Edge.h | 16 ++++++ thu_dsa/chp6/Graph.h | 57 ++++++++++++++++++++ thu_dsa/chp6/GraphMatrix.h | 108 +++++++++++++++++++++++++++++++++++++ thu_dsa/chp6/Vertex.h | 21 ++++++++ 4 files changed, 202 insertions(+) create mode 100644 thu_dsa/chp6/Edge.h create mode 100644 thu_dsa/chp6/Graph.h create mode 100644 thu_dsa/chp6/GraphMatrix.h create mode 100644 thu_dsa/chp6/Vertex.h diff --git a/thu_dsa/chp6/Edge.h b/thu_dsa/chp6/Edge.h new file mode 100644 index 0000000..16df902 --- /dev/null +++ b/thu_dsa/chp6/Edge.h @@ -0,0 +1,16 @@ +#ifndef EDGE_H_ +#define EDGE_H_ + +#include "Graph.h" + +template +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 diff --git a/thu_dsa/chp6/Graph.h b/thu_dsa/chp6/Graph.h new file mode 100644 index 0000000..36ac2f7 --- /dev/null +++ b/thu_dsa/chp6/Graph.h @@ -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 template +class Graph{ +private: + void reset(); + void BFS(int, int&); + void DFS(int, int&); + void BCC(int, int&, Stack&); + bool TSort(int, int&, Stack*); + template 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* tSort(int); + template void pfd(int, PU); +}; + +#endif \ No newline at end of file diff --git a/thu_dsa/chp6/GraphMatrix.h b/thu_dsa/chp6/GraphMatrix.h new file mode 100644 index 0000000..a7399a2 --- /dev/null +++ b/thu_dsa/chp6/GraphMatrix.h @@ -0,0 +1,108 @@ +#ifndef GRAPHMATRIX_H_ +#define GRAPHMATRIX_H_ + +#include "Graph.h" +#include "Edge.h" +#include "Vertex.h" +#include "../chp2/Vector.h" + +template template +class GraphMatrix: public Graph{ +private: + Vector> V; + Vector*>> 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 template +GraphMatrix::GraphMatrix(){ + num_of_vertices = 0; + num_of_edges = 0; + + V = Vector>(); + E = Vector>>(); +} + +//implementations of abstract Graph methods + +//vertex methods +template template +int GraphMatrix::insertVertex(Tv const & data){ + V.push_back(Vertex(data)); + for (int ix = 0; ix != num_of_vertices; ++ix) E[ix].push_back(nullptr); + E.push_back(Vector*>(nullptr, ++num_of_vertices)); + return num_of_vertices - 1; +} + +template template +Te GraphMatrix::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 template +void GraphMatrix::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(data, weight); + + ++num_of_edges; + V[tail].inDegree++; + V[src].outDegree--; +} + +template template +Te GraphMatrix::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 diff --git a/thu_dsa/chp6/Vertex.h b/thu_dsa/chp6/Vertex.h new file mode 100644 index 0000000..b3e024d --- /dev/null +++ b/thu_dsa/chp6/Vertex.h @@ -0,0 +1,21 @@ +#ifndef VERTEX_H_ +#define VERTEX_H_ + +#include "Graph.h" + +template +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