邻接矩阵转换为邻接表;邻接表转换为邻接矩阵
生活随笔
收集整理的這篇文章主要介紹了
邻接矩阵转换为邻接表;邻接表转换为邻接矩阵
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.兩種存儲結構(鄰接表和鄰接矩陣)
//圖的兩種存儲結構 #define INF 32767 //定義∞ #define MAXV 100 //最大頂點個數 typedef char InfoType;//以下定義鄰接矩陣類型 typedef struct { int no; //頂點編號InfoType info; //頂點其他信息 } VertexType; //頂點類型 typedef struct { int edges[MAXV][MAXV]; //鄰接矩陣數組int n,e; //頂點數,邊數VertexType vexs[MAXV]; //存放頂點信息 } MatGraph; //完整的圖鄰接矩陣類型//以下定義鄰接表類型 typedef struct ANode { int adjvex; //該邊的鄰接點編號struct ANode *nextarc; //指向下一條邊的指針int weight; //該邊的相關信息,如權值(用整型表示) } ArcNode; //邊結點類型 typedef struct Vnode { InfoType info; //頂點其他信息int count; //存放頂點入度,僅僅用于拓撲排序ArcNode *firstarc; //指向第一條邊 } VNode; //鄰接表頭結點類型 typedef struct { VNode adjlist[MAXV]; //鄰接表頭結點數組int n,e; //圖中頂點數n和邊數e } AdjGraph; //完整的圖鄰接表類型2.圖的基本運算
//圖的基本運算算法 #include <stdio.h> #include <malloc.h> #include "graph.h" //------------------------------------------------------------ //----鄰接矩陣的基本運算算法---------------------------------- //------------------------------------------------------------ void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e) //創建圖的鄰接矩陣 {int i,j;g.n=n; g.e=e;for (i=0;i<g.n;i++)for (j=0;j<g.n;j++)g.edges[i][j]=A[i][j]; } void DispMat(MatGraph g) //輸出鄰接矩陣g {int i,j;for (i=0;i<g.n;i++){for (j=0;j<g.n;j++)if (g.edges[i][j]!=INF)printf("%4d",g.edges[i][j]);elseprintf("%4s","∞");printf("\n");} } //------------------------------------------------------------//------------------------------------------------------------ //----鄰接表的基本運算算法------------------------------------ //------------------------------------------------------------ void CreateAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e) //創建圖的鄰接表 {int i,j;ArcNode *p;G=(AdjGraph *)malloc(sizeof(AdjGraph));for (i=0;i<n;i++) //給鄰接表中所有頭結點的指針域置初值G->adjlist[i].firstarc=NULL;for (i=0;i<n;i++) //檢查鄰接矩陣中每個元素for (j=n-1;j>=0;j--)if (A[i][j]!=0 && A[i][j]!=INF) //存在一條邊{ p=(ArcNode *)malloc(sizeof(ArcNode)); //創建一個結點pp->adjvex=j;p->weight=A[i][j];p->nextarc=G->adjlist[i].firstarc; //采用頭插法插入結點pG->adjlist[i].firstarc=p;}G->n=n; G->e=n; } void DispAdj(AdjGraph *G) //輸出鄰接表G {int i;ArcNode *p;for (i=0;i<G->n;i++){p=G->adjlist[i].firstarc;printf("%3d: ",i);while (p!=NULL){printf("%3d[%d]→",p->adjvex,p->weight);p=p->nextarc;}printf("∧\n");} } void DestroyAdj(AdjGraph *&G) //銷毀圖的鄰接表 { int i;ArcNode *pre,*p;for (i=0;i<G->n;i++) //掃描所有的單鏈表{ pre=G->adjlist[i].firstarc; //p指向第i個單鏈表的首結點if (pre!=NULL){ p=pre->nextarc;while (p!=NULL) //釋放第i個單鏈表的所有邊結點{ free(pre);pre=p; p=p->nextarc;}free(pre);}}free(G); //釋放頭結點數組 } //------------------------------------------------------------3.相互轉換
#include "graph.cpp" void MatToList(MatGraph g,AdjGraph *&G) //將鄰接矩陣g轉換成鄰接表G { int i,j;ArcNode *p;G=(AdjGraph *)malloc(sizeof(AdjGraph));for (i=0;i<g.n;i++) //將鄰接表中所有頭結點的指針域置初值G->adjlist[i].firstarc=NULL;for (i=0;i<g.n;i++) //檢查鄰接矩陣中每個元素for (j=g.n-1;j>=0;j--)if (g.edges[i][j]!=0 && g.edges[i][j]!=INF) //存在一條邊{ p=(ArcNode *)malloc(sizeof(ArcNode)); //創建一個邊結點pp->adjvex=j; p->weight= g.edges[i][j];p->nextarc=G->adjlist[i].firstarc; //采用頭插法插入結點pG->adjlist[i].firstarc=p;}G->n=g.n;G->e=g.e; } void ListToMat(AdjGraph *G,MatGraph &g) //將鄰接表G轉換成鄰接矩陣g { int i;ArcNode *p;for (i=0;i<G->n;i++) //掃描所有的單鏈表{ p=G->adjlist[i].firstarc; //p指向第i個單鏈表的首結點while (p!=NULL) //掃描第i個單鏈表{ g.edges[i][p->adjvex]=p->weight;p=p->nextarc;}}g.n=G->n;g.e=G->e; }int main() {MatGraph g;AdjGraph *G;int A[MAXV][MAXV]={{0,1,0,1,1},{1,0,1,1,0},{0,1,0,1,1},{1,1,1,0,1},{1,0,1,1,0}};int n=5, e=8;CreateMat(g,A,n,e); //建立《教程》中圖8.1(a)的鄰接矩陣printf("圖G的鄰接矩陣:\n");DispMat(g); //輸出鄰接矩陣gprintf("將g轉換為鄰接表G\n");MatToList(g,G); //輸出鄰接表Gprintf("圖G的鄰接表:\n");DispAdj(G);DestroyAdj(G); //銷毀鄰接表CreateAdj(G,A,n,e); //建立《教程》中圖8.1(a)的鄰接表printf("圖G的鄰接表:\n");DispAdj(G); //輸出鄰接表Gprintf("將G轉換為鄰接矩陣g\n");ListToMat(G,g);DispMat(g); //輸出鄰接矩陣gDestroyAdj(G); //銷毀鄰接表return 1; }總結
以上是生活随笔為你收集整理的邻接矩阵转换为邻接表;邻接表转换为邻接矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: web前端细解cookie那些事
- 下一篇: OpenSSL 创建自签名证书