C++邻接矩阵实现有向图、无向图
2017-05-03 19:49
323 查看
/*********************邻接矩阵实现无向图******************/ class MatrixUDG { private: char mVexs[MAX];//顶点集合 int mVexBum;//顶点数 int mEdgNum;//边数 int mMatrix[MAX][MAX];//邻接矩阵 char readChar();//读取一个字符 int getPosition(char ch);//返回ch在矩阵中中的位置 public: MatrixUDG();//创建图(自己输入数据) MatrixUDG(const char *vexs, int vlen,char edges[][2], int elen);//创建图(用已提供的数据) ~MatrixUDG(); void print();//打印矩阵队列图 }; //读取一个字符 char MatrixUDG::readChar() { char ch; cout << "input a ch: "; cin >> ch; return ch; } //返回ch在矩阵中的位置 int MatrixUDG::getPosition(char ch) { for (int i = 0; i < this->mVexBum; i++) { if (this->mVexs[i] == ch) return i; } return -1; } //创建图(自己输入数据) MatrixUDG::MatrixUDG() { char c1, c2; int p1, p2; cout << "构造函数" << endl; cout << "input vertex number: "; cin >> mVexBum; cout << "input edge number: "; cin >> mEdgNum; if (this->mVexBum < 1 || this->mEdgNum<1 || (this->mEdgNum>(this->mVexBum*(this->mVexBum - 1)))) { cout << "input error" << endl; return; } //初始化顶点 for (int i = 0; i < this->mVexBum; i++) this->mVexs[i] = readChar(); //初始化边:邻接矩阵赋值 for (int i = 0; i < this->mEdgNum; i++) { cout << "edge(" << i << ")" << endl; c1 = readChar(); c2 = readChar(); p1 = getPosition(c1); p2 = getPosition(c2); if (p1 == -1 || p2 == -1) { cout << "input error: invalid edge!" << endl; return; } this->mMatrix[p1][p2] = 1; this->mMatrix[p2][p1] = 1; } for (int i = 0; i < this->mVexBum; i++) { for (int j = 0; j < this->mVexBum; j++) { if (this->mMatrix[i][j] != 1) this->mMatrix[i][j] = 0; } } } //创建图用已提供的数据 MatrixUDG::MatrixUDG(const char *vexs, int vlen, char edges[][2], int elen) { cout << "构造函数" << endl; int p1, p2; this->mVexBum = vlen; this->mEdgNum = elen; //初始化顶点 for (int i = 0; i < this->mVexBum; i++) this->mVexs[i] = vexs[i]; //初始化边 for (int i = 0; i < this->mEdgNum; i++) { p1 = getPosition(edges[i][0]); p2 = getPosition(edges[i][1]); this->mMatrix[p1][p2] = 1; this->mMatrix[p2][p1] = 1; } } //析构函数 MatrixUDG::~MatrixUDG() { cout << "析构函数" << endl; } //打印邻接矩阵 void MatrixUDG::print() { cout << "打印矩阵队列图" << endl; int i, j; for (i = 0; i < this->mVexBum; i++) { for (j = 0; j < this->mVexBum; j++) cout << this->mMatrix[i][j] << " "; cout << endl; } } //测试程序 int main() { MatrixUDG Grap; Grap.print(); return 0; } /*********************邻接矩阵实现有向图******************/ //有向图类 template<class Type> class DirectedGraph { private: Type vexs[MAX];//顶点表 int arc[MAX][MAX];//邻接矩阵 int numVertexes;//图中当前顶点数 int numEdges;//图中当前边数 int getPosition(Type el);//返回el在邻接矩阵中的位置 Type readType(); public: DirectedGraph();//创建图 ~DirectedGraph();//析构函数 void print();//打印图的邻接矩阵 }; //返会el在连接矩阵中的位置 template<class Type> int DirectedGraph<Type>::getPosition(Type el) { for (int i = 0; i < this->numVertexes; i++) { if (this->vexs[i] == el) return i; } return -1; } //从外界输入数据 template<class Type> Type DirectedGraph<Type>::readType() { Type data; cout << "input a data: "; cin >> data; return data; } //创建图(自己输入数据) template<class Type> DirectedGraph<Type>::DirectedGraph() { Type c1, c2; int p1, p2; cout << "input numVertexes: "; cin >> this->numVertexes; cout << "input numEdges: "; cin >> this->numEdges; if (this->numVertexes < 1 || this->numEdges<1 || (this->numEdges> this->numVertexes*(this->numVertexes - 1))) { cout << "input error: invalid input!" << endl; return; } //初始化顶点 for (int i = 0; i < this->numVertexes; i++) this->vexs[i] = readType(); //初始化边 for (int i = 0; i < this->numEdges; i++) { cout << "edge(" << i << ")" << endl; cout << "input arcHead: "; c1 = readType(); cout << "input arcTail: "; c2 = readType(); p1 = getPosition(c1); p2 = getPosition(c2); if (p1 == -1 || p2 == -1) { cout << "input invalid" << endl; return; } this->arc[p1][p2] = 1; } for (int i = 0; i < this->numVertexes; i++) { for (int j = 0; j < this->numVertexes; j++) { if (this->arc[i][j] != 1) this->arc[i][j] = 0; } } } //析构函数 template<class Type> DirectedGraph<Type>::~DirectedGraph() { cout << "析构函数" << endl; } //打印邻接矩阵 template<class Type> void DirectedGraph<Type>::print() { for (int i = 0; i < this->numVertexes; i++) { for (int j = 0; j < this->numVertexes; j++) cout << this->arc[i][j] << " "; cout << endl; } } //测试程序 int main() { DirectedGraph<char> graph; graph.print(); return 0; }
相关文章推荐
- 图(有向图,无向图)的邻接矩阵表示C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency matrix
- 我对Prim算法用于求无向图的最小生成树的理解 (C++实现)
- 无权图无向图【邻接矩阵存储】深度搜索和广度搜索--Java实现
- 基于矩阵合同变换算法的无向图同构识别——C++代码实现
- 有向图和无向图的数组C++实现
- 有向图和无向图用邻接矩阵储存及代码实现
- 邻接矩阵(有向图,无向图实现的差异)
- 图 简单c++邻接矩阵 无向图
- 利用邻接矩阵存储无向图,并实现BFS(非递归) DFS(递归+非递归)两种遍历
- 数据结构_图_邻接多重表做存储结构遍历无向图_C++实现
- C++实现图的邻接矩阵的创建以及其深度优先遍历和广度优先遍历
- C++ 图结构邻接矩阵简单实现
- 图的邻接矩阵表示 DFS 和BFS C++实现
- 图的邻接矩阵的C++实现
- 数据结构,图的邻接矩阵创建,邻接矩阵与邻接表的交换,两种表的输出,过程用C++实现
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- 图的实现 邻接矩阵+无向图
- 使用C++邻接矩阵实现图的存储、Prim、Kruskal算法
- 【数据结构】拾遗(一):图的邻接矩阵创建以及其深广度遍历C++实现
- 数据结构之用邻接矩阵实现赋值无向图,有向图,无向图