数据结构:图的实现--邻接矩阵
来源:互联网 发布:php开源网站 编辑:程序博客网 时间:2024/05/02 00:53
图的实现:邻接矩阵
为了表现图中顶点之间的关联,我们可以使用邻接矩阵来实现图结构。所谓的邻接矩阵,就是一个反应边与边之间联系的二维数组。这个二维数组我们用matrix[numV][numV]表示,其中numV是顶点数。
对于无权图
若顶点Vi和Vj之间有边,则matrix[Vi][Vj]=1;否则matrix[Vi][Vj]=0。
对于有权图
若顶点Vi和Vj之间有边,且权值为weight,则matrix[Vi][Vj]=weight;否则matrix[Vi][Vj]=0或MAXWEIGHT(取最小权值或最大权值)。
下面给出一个实例
类定义
#include<iostream>#include<iomanip>using namespace std;//最大权值#define MAXWEIGHT 100//用邻接矩阵实现图class Graph{private://是否带权bool isWeighted;//是否有向bool isDirected;//顶点数int numV;//边数int numE;//邻接矩阵int **matrix;public:/*构造方法numV是顶点数,isWeighted是否带权值,isDirected是否有方向*/Graph(int numV, bool isWeighted = false, bool isDirected = false);//建图void createGraph();//析构方法~Graph();//获取顶点数int getVerNums(){return numV;}//获取边数int getEdgeNums(){return numE;}//设置指定边的权值void setEdgeWeight(int tail, int head, int weight);//打印邻接矩阵void printAdjacentMatrix();//检查输入bool check(int i, int j, int w = 1);};类实现
/*构造方法numV是顶点数,isWeighted是否带权值,isDirected是否有方向*/Graph::Graph(int numV, bool isWeighted, bool isDirected){while (numV <= 0){cout << "输入的顶点数不正确!,重新输入 ";cin >> numV;}this->numV = numV;this->isWeighted = isWeighted;this->isDirected = isDirected;matrix = new int*[numV]; //指针数组int i, j;for (i = 0; i < numV; i++)matrix[i] = new int[numV];//对图进行初始化if (!isWeighted) //无权图{//所有权值初始化为0for (i = 0; i < numV; i++)for (j = 0; j < numV; j++)matrix[i][j] = 0;}else //有权图{//所有权值初始化为最大权值for (i = 0; i < numV; i++)for (j = 0; j < numV; j++)matrix[i][j] = MAXWEIGHT;}}//建图void Graph::createGraph(){cout << "输入边数 ";while (cin >> numE && numE < 0)cout << "输入有误!,重新输入 ";int i, j, w;if (!isWeighted) //无权图{if (!isDirected) //无向图{cout << "输入每条边的起点和终点:\n";for (int k = 0; k < numE; k++){cin >> i >> j;while (!check(i, j)){cout << "输入的边不对!重新输入\n";cin >> i >> j;}matrix[i][j] = matrix[j][i] = 1;}}else //有向图{cout << "输入每条边的起点和终点:\n";for (int k = 0; k < numE; k++){cin >> i >> j;while (!check(i, j)){cout << "输入的边不对!重新输入\n";cin >> i >> j;}matrix[i][j] = 1;}}}else //有权图{if (!isDirected) //无向图{cout << "输入每条边的起点、终点和权值:\n";for (int k = 0; k < numE; k++){cin >> i >> j >> w;while (!check(i, j, w)){cout << "输入的边不对!重新输入\n";cin >> i >> j >> w;}matrix[i][j] = matrix[j][i] = w;}}else //有向图{cout << "输入每条边的起点、终点和权值:\n";for (int k = 0; k < numE; k++){cin >> i >> j >> w;while (!check(i, j, w)){cout << "输入的边不对!重新输入\n";cin >> i >> j >> w;}matrix[i][j] = w;}}}}//析构方法Graph::~Graph(){int i = 0;for (i = 0; i < numV; i++)delete[] matrix[i];delete[]matrix;}//设置指定边的权值void Graph::setEdgeWeight(int tail, int head, int weight){if (isWeighted){while (!check(tail, head, weight)){cout << "输入不正确,重新输入边的起点、终点和权值 ";cin >> tail >> head >> weight;}if (isDirected)matrix[tail][head] = weight;elsematrix[tail][head] = matrix[head][tail] = weight;}else{while (!check(tail, head, 1)){cout << "输入不正确,重新输入边的起点、终点 ";cin >> tail >> head;}if (isDirected)matrix[tail][head] = 1-matrix[tail][head];elsematrix[tail][head] = matrix[head][tail] = 1 - matrix[tail][head];}}//输入检查bool Graph::check(int i, int j, int w){if (i >= 0 && i < numV && j >= 0 && j < numV && w > 0 && w <= MAXWEIGHT)return true;elsereturn false;}//打印邻接矩阵void Graph::printAdjacentMatrix(){int i, j;cout.setf(ios::left);cout << setw(4) << " ";for (i = 0; i < numV; i++)cout << setw(4) << i;cout << endl;for (i = 0; i < numV; i++){cout << setw(4) << i;for (j = 0; j < numV; j++)cout << setw(4) << matrix[i][j];cout << endl;}}主函数
int main(){cout << "******使用邻接矩阵实现图结构***by David***" << endl;bool isDirected, isWeighted;int numV;cout << "建图" << endl;cout << "输入顶点数 ";cin >> numV;cout << "边是否带权值,0(不带) or 1(带) ";cin >> isWeighted;cout << "是否是有向图,0(无向) or 1(有向) ";cin >> isDirected;Graph graph(numV, isWeighted, isDirected);cout << "这是一个";isDirected ? cout << "有向、" : cout << "无向、";isWeighted ? cout << "有权图" << endl : cout << "无权图" << endl;graph.createGraph();cout << "打印邻接矩阵" << endl;graph.printAdjacentMatrix();cout << endl;int tail, head, weight;cout << "修改指定边的权值" << endl;if (isWeighted) //针对有权图{cout << "输入边的起点、终点和权值 ";cin >> tail >> head >> weight;graph.setEdgeWeight(tail, head, weight);}else //针对无权图{cout << "输入边的起点、终点 ";cin >> tail >> head;graph.setEdgeWeight(tail, head, 1);}cout << "修改成功!" << endl;cout << "打印邻接矩阵" << endl;graph.printAdjacentMatrix();system("pause");return 0;}运行
实例一
实例二
完整代码下载:图的实现:邻接矩阵
转载请注明出处,本文地址:http://blog.csdn.net/zhangxiangdavaid/article/details/38321327
若有所帮助,顶一个哦!
专栏目录:
- 数据结构与算法目录
- c指针
4 0
- 数据结构:图的实现--邻接矩阵
- 数据结构:图的实现--邻接矩阵
- 数据结构---图的邻接矩阵实现
- 数据结构:图的邻接矩阵存储实现
- 【数据结构】图的邻接矩阵存储实现
- 图的邻接矩阵数据结构代码实现
- 数据结构---图的邻接矩阵
- 【数据结构】Dijkstra求最短路径的图的邻接矩阵的实现
- 数据结构经典算法汇总___图的邻接矩阵实现
- 数据结构的C实现_图_邻接矩阵表示
- 数据结构(C实现)------- 图的邻接矩阵表示
- 数据结构(C实现)------- 图的邻接矩阵表示
- java数据结构-图的实现 采用数组和邻接矩阵
- 38. 数据结构笔记之三十八图的邻接矩阵存储实现
- 数据结构之图的邻接矩阵
- 数据结构,图的邻接矩阵表示
- 数据结构-图的邻接矩阵表示
- 【数据结构】图的存储--邻接矩阵
- 模拟鼠标的单双击
- hdu 大菲波数
- KM算法(最优匹配)
- 安卓实现选项卡
- 使用eclipse的MAT对程序内存进行分析
- 数据结构:图的实现--邻接矩阵
- Hibernate中的缓存
- 2^x mod n = 1 【杭电-HDOJ-1395】 附题
- 2^x mod n = 1
- myeclipse安装svn插件的多种方式
- 如果写一个android支持的html文件
- ios view抖动动画(相当实用)
- PHP源码加密,以前只知道zend guard,今天才知道,原来还有个 ioncube
- [算法] 已知在平面坐标系内有N个点,求离开给定坐标距离最近的10个点