首页 > 编程知识 正文

无向图的邻接矩阵是一个,邻接表无向图

时间:2023-05-05 20:00:00 阅读:174230 作者:966

图的数据节点是多对多的关系。 一个节点有多个前驱,也有多个后继。 也有有向图,不区分前驱和后继,只要节点之间有邻接关系即可。 因此,描述这种数据关系需要新的数据结构。

图中有顶点集和边集。 顶点表示数据节点,而边表示数据顶点之间的相邻关系。

描述图的数据结构多种多样,bilibili中的小甲鱼老师也说过,程序编程没有唯一的答案。 能解决问题就好了。 因为编程的自由度比数学领域更大,像数学题一样没有唯一的正确答案。 有几种解法。 程序解决问题,简单易懂是很好的程序。 在前人的基础上做一些改变,做一些改进,也是一个更好的过程。

图可以用邻接矩阵来描述。 包含存储所有顶点的数组成员和存储顶点之间相邻关系的二维矩阵。 矩阵元素值表示与两个下标对应的两个顶点是否为相邻关系,对于加权边,矩阵元素值表示相应边的权重,有表示顶点数的成员和表示边数的成员。 此类结构变量可以代表图上的所有数据信息。

图也可以用邻表表示。 对于稀疏图来说,用邻接矩阵存储边是因为太浪费内存了。 邻接表的结构成员包括一维数组、顶点总数和边总数成员,一维数组由单链表标题组成。 每个标头也是一个结构变量。 一个成员存储顶点,另一个成员存储指向相邻表节点的指针。 相邻的表节点也是结构变量,一个成员存储与表头顶点相邻的另一个顶点在表头数组中的下标,一个成员存储相应边的权重,最后一个成员是指向下一个表节点的指针。 这样,图中的所有信息也被存储在这种邻接表结构变量中。

本例题是练习,包含以下函数功能:

函数createGraphAdjMatrix根据图的所有碎片已知信息构造邻接矩阵。

函数createGraphAdjList根据图的片断的所有已知信息制作邻接表。

函数matrixToList根据邻接矩阵制作邻接表。

根据邻接表创建邻接矩阵的函数listToMatrix; 其实是给结构的每个成员赋值。

函数dispalyGraphAdjMatrix从邻接矩阵中输出图表的所有信息,用矩阵表示。

函数displayGrapgAdjList从相邻的表中输出图表的所有信息,并将其表示为线性表。 这2个输出的目的,也是为了检测是否正确制作了图的结构体变量。

完整的代码如下所示。 第一个包含主函数的文件代码:

# include iostream # include stdio.husingnamespacestd; # definemaxvertex 15 # define infini 65555 structgraphadjamatrix { char vertexes [ max vertex ]; int edges[MAXVERTEX][MAXVERTEX]; int numVertexes; int numEdges; (; structadjalistnode { intindexofvertex; int weightOfEdge AdjaListNode* pt; (; struct AdjListHead {char vertex; AdjaListNode* pt; (; structgraphadjalist { adjlistheadvertexes [ max vertex ]; int numVertexes; int numEdges; (; externvoidcreategraphadjmatrix graphadjmatrix,int numVertexes,int numEdges,int edges[][6], char verter externvoidcreategraphadjlist (graphadjalistgraphadjlist,int numVertexes,int numEdges,int edges[][6], char vertexes [ ] externvoidmatrixtolist (graphadjamatrixgraphadjmatrix,GraphAdjaList graphAdjList; externvoidlisttomatrix (graphadjalistgraphadjlist,GraphAdjaMatrix graphAdjMatrix ); externvoiddispalygraphadjmatrix graphadjmatrix; externvoiddisplaygrapgadjlist (graphadjalistgraphadjlist; int main () {GraphAdjaMatrix graphAdjMatrix; GraphAdjaList graphAdjList; int numVertexes=6,numEdges=10; int edges [ ] [6]={ 0,5,INFINI,7,INFINI,INFINI},{8,INFINI,0,4,INFINI,INFINI,INFINI},

NI},{3,INFINI,INFINI,INFINI,1,0} };char vertexes[] = {'a','b','c','d','e','f'};createGraphAdjMatrix(graphAdjMatrix,numVertexes,numEdges,edges,vertexes);createGraphAdjList(graphAdjList,numVertexes,numEdges,edges,vertexes);GraphAdjaMatrix graphAdjMatrixNew;GraphAdjaList graphAdjListNew;matrixToList(graphAdjMatrix,graphAdjListNew);listToMatrix(graphAdjList,graphAdjMatrixNew);dispalyGraphAdjMatrix(graphAdjMatrixNew);cout << endl;displayGrapgAdjList(graphAdjListNew);return 0;}

  接着是各函数所在源文件代码:

#include<iostream>#include<stdio.h>using namespace std;#define MAXVERTEX 15#define INFINI 65555struct GraphAdjaMatrix {char vertexes[MAXVERTEX];int edges[MAXVERTEX][MAXVERTEX];int numVertexes;int numEdges;};struct AdjaListNode {int indexOfVertex;int weightOfEdge;AdjaListNode* pt;};struct AdjListHead {char vertex;AdjaListNode* pt;};struct GraphAdjaList {AdjListHead vertexes[MAXVERTEX];int numVertexes;int numEdges;};void createGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix,int numVertexes, int numEdges, int edges[][6], char vertexes[]) {graphAdjMatrix.numVertexes = numVertexes;graphAdjMatrix.numEdges = numEdges;for (int i = 0; i < numVertexes; i++)graphAdjMatrix.vertexes[i] = vertexes[i];for (int row = 0; row < numVertexes; row++)for (int column = 0; column < numVertexes; column++)graphAdjMatrix.edges[row][column] = edges[row][column];}void createGraphAdjList(GraphAdjaList &graphAdjList,int numVertexes, int numEdges, int edges[][6], char vertexes[]){graphAdjList.numEdges = numEdges;graphAdjList.numVertexes = numVertexes;for (int i = 0; i < MAXVERTEX; i++)graphAdjList.vertexes[i].pt = NULL;for (int i = 0; i < numVertexes; i++)graphAdjList.vertexes[i].vertex = vertexes[i];AdjaListNode* ptTail = NULL,*ptNew;int i, j;for ( i = 0; i < numVertexes; i++) for (j = 0; j < numVertexes; j++) if (edges[i][j] != 0 && edges[i][j] != INFINI) {ptNew = new AdjaListNode;ptNew->indexOfVertex = j;ptNew->weightOfEdge = edges[i][j];if (graphAdjList.vertexes[i].pt == NULL) {ptNew->pt = NULL;graphAdjList.vertexes[i].pt = ptNew;ptTail = ptNew;}else {ptNew->pt = ptTail->pt;ptTail->pt = ptNew;ptTail = ptNew;}}}void matrixToList(GraphAdjaMatrix &graphAdjMatrix,GraphAdjaList &graphAdjList) {graphAdjList.numEdges = graphAdjMatrix.numEdges;graphAdjList.numVertexes = graphAdjMatrix.numVertexes;for (int i = 0; i < MAXVERTEX; i++)graphAdjList.vertexes[i].pt = NULL;for (int i = 0; i < graphAdjList.numVertexes; i++)graphAdjList.vertexes[i].vertex = graphAdjMatrix.vertexes[i];AdjaListNode* ptTail = NULL, * ptNew;int i, j;for (i = 0; i < graphAdjList.numVertexes; i++)for (j = 0; j < graphAdjList.numVertexes; j++)if (graphAdjMatrix.edges[i][j] != 0 && graphAdjMatrix.edges[i][j] != INFINI) {ptNew = new AdjaListNode;ptNew->indexOfVertex = j;ptNew->weightOfEdge = graphAdjMatrix.edges[i][j];if (graphAdjList.vertexes[i].pt == NULL) {ptNew->pt = NULL;graphAdjList.vertexes[i].pt = ptNew;ptTail = ptNew;}else {ptNew->pt = ptTail->pt;ptTail->pt = ptNew;ptTail = ptNew;}}}void listToMatrix(GraphAdjaList &graphAdjList,GraphAdjaMatrix &graphAdjMatrix) {graphAdjMatrix.numEdges = graphAdjList.numEdges;graphAdjMatrix.numVertexes = graphAdjList.numVertexes;for (int i = 0; i < graphAdjMatrix.numVertexes; i++)graphAdjMatrix.vertexes[i] = graphAdjList.vertexes[i].vertex;int row, column;//对邻接矩阵的初始化,主对角线为0,其余统统为无穷大for(row = 0 ; row < graphAdjMatrix.numVertexes ; row++)for(column = 0 ; column < graphAdjMatrix.numVertexes ; column++)if (row == column) graphAdjMatrix.edges[row][column] = 0 ;elsegraphAdjMatrix.edges[row][column] = INFINI;AdjaListNode* pt;for (row = 0; row < graphAdjMatrix.numVertexes; row++) {pt = graphAdjList.vertexes[row].pt;while (pt != NULL) {column = pt->indexOfVertex;graphAdjMatrix.edges[row][column] = pt->weightOfEdge;pt = pt->pt;}}}void dispalyGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix) {cout << "adjacensy matrix :" << endl;int row,column;printf("%3c",' ');for (row = 0; row < graphAdjMatrix.numVertexes; row++)printf("%3c",graphAdjMatrix.vertexes[row]);printf("n");for (row = 0; row < graphAdjMatrix.numVertexes; row++) {printf("%-3c", graphAdjMatrix.vertexes[row]);for (column = 0; column < graphAdjMatrix.numVertexes; column++)if (graphAdjMatrix.edges[row][column] == INFINI)printf("%3s", "∞");elseprintf("%3d",graphAdjMatrix.edges[row][column]);cout << endl;}}void displayGrapgAdjList(GraphAdjaList &graphAdjList) {cout << "graph adjacency list :" << endl;AdjaListNode* pt;for (int i = 0; i < graphAdjList.numVertexes; i++) {printf("%2c:",graphAdjList.vertexes[i].vertex);pt = graphAdjList.vertexes[i].pt;while (pt != NULL) {printf("%5d(%d)",pt->indexOfVertex,pt->weightOfEdge);pt = pt->pt;}cout << endl;}}

测试结果如下:

谢谢阅读

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。