日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

邻接表与邻接矩阵的相互转换

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 邻接表与邻接矩阵的相互转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鄰接表轉鄰接矩陣的算法思想:首先初始化鄰接矩陣。遍歷鄰接表,在依次遍歷頂點vertices[i]的邊鏈表時,修改鄰接矩陣的第i行的元素值。若鏈表邊結點的值為 j,則置鄰接矩陣的edge[i][j]=1。遍歷完鄰接表時,整個轉換過程結束。此算法對于無向圖有向圖均適用。
鄰接矩陣轉鄰接表的算法思想:首先初始化鄰接表的各個邊表結點,將邊表結點的first指針指向NULL。遍歷鄰接矩陣,遍歷到edge[i][j]==1時,將結點值為j的頂點插入到vertices[i]的邊鏈表中。遍歷完鄰接矩陣,整個轉換過程結束。

圖的鄰接表存儲結構定義如下

const int MaxVertexNum = 100; // 圖中頂點數目的最大值 typedef struct ArcNode { //邊表結點int adjvex; // 該弧所指向的頂點的位置struct ArcNode *next; //指向下一條弧的指針// int weight; //網的邊權值// Infotype info; }ArcNode; typedef struct VNode { //頂點表結點ArcNode *first; // 指向第一條依附該頂點的弧的指針 }VNode, AdjList[MaxVertexNum]; typedef struct {AdjList vertices; //鄰接表int vexnum, arcnum; //圖的頂點數和弧數 }ALGraph; // ALGraph是以鄰接表存儲的圖類型

圖的鄰接矩陣存儲結構定義如下

typedef struct{int Edge[MaxVertexNum][MaxVertexNum]; //鄰接矩陣,邊表int vexnum, arcnum; //圖的當前頂點數和弧數 }MGraph;

鄰接矩陣轉化為鄰接表算法實現如下

void matrix_convert_table(ALGraph &G1, MGraph G2) { // 鄰接矩陣轉化為鄰接表G1.arcnum = G2.arcnum;G1.vexnum = G2.vexnum;for(int i = 1; i <= G1.vexnum; i++) {G1.vertices[i].first = NULL; // 初始化指向第一條依附該頂點的弧的指針}ArcNode *p;for(int i = 1; i <= G2.vexnum; i++) { // 依次遍歷整個鄰接矩陣for(int j = 1; j <= G2.vexnum; j++) {if(G2.Edge[i][j] == 1) {p = (ArcNode *) malloc (sizeof(ArcNode));p -> adjvex = j;p -> next = G1.vertices[i].first;G1.vertices[i].first = p;}}} }

鄰接表轉化為鄰接矩陣算法實現如下

void table_convert_matrix(MGraph &G1, ALGraph G2) { // 鄰接表轉化為鄰接矩陣G1.arcnum = G2.arcnum;G1.vexnum = G2.vexnum;for(int i = 1; i <= G1.vexnum; i++) {for(int j = 1; j <= G1.vexnum; j++) {G1.Edge[i][j] = 0; // 初始化鄰接矩陣}}ArcNode *p;for(int i = 1; i <= G2.vexnum; i++) { //依次遍歷各頂點表結點為頭的邊鏈表p = G2.vertices[i].first; // 取出頂點 i 的第一條出邊while(p) { //遍歷邊鏈表G1.Edge[i][p->adjvex] = 1; p = p -> next; // 取出下一條出邊}} }

我們依然以下面這個圖為例子,便得到了一個輸入樣例:

6 8 1 2 1 4 4 2 2 5 5 4 3 5 3 6 6 6

完整可執行代碼如下:

#include<bits/stdc++.h> using namespace std; //圖的鄰接表存儲結構定義如下 const int MaxVertexNum = 100; // 圖中頂點數目的最大值 typedef struct ArcNode { //邊表結點int adjvex; // 該弧所指向的頂點的位置struct ArcNode *next; //指向下一條弧的指針// int weight; //網的邊權值// Infotype info; }ArcNode; typedef struct VNode { //頂點表結點ArcNode *first; // 指向第一條依附該頂點的弧的指針 }VNode, AdjList[MaxVertexNum]; typedef struct {AdjList vertices; //鄰接表int vexnum, arcnum; //圖的頂點數和弧數 }ALGraph; // ALGraph是以鄰接表存儲的圖類型//圖的鄰接矩陣存儲結構定義如下 typedef struct{int Edge[MaxVertexNum][MaxVertexNum]; //鄰接矩陣,邊表int vexnum, arcnum; //圖的當前頂點數和弧數 }MGraph;void Create_Graph(ALGraph &AG, MGraph &MG) { //創建一個鄰接表和鄰接矩陣scanf("%d%d", &AG.vexnum, &AG.arcnum); //輸入圖的頂點數和邊數MG.arcnum = AG.arcnum;MG.vexnum = AG.vexnum;for(int i = 1; i <= MG.vexnum; i++) { // 初始化鄰接矩陣for(int j = 1; j <= MG.vexnum; j++) {MG.Edge[i][j] = 0;}}for(int i = 1; i <= AG.vexnum; i++) {AG.vertices[i].first = NULL; //初始化第一條依附該頂點的弧的指針為空}ArcNode *p;for(int i = 0; i < AG.arcnum; i++) {int u, v;scanf("%d%d", &u, &v); //u, v表示u有一條邊指向v;MG.Edge[u][v] = 1;p = (ArcNode *) malloc (sizeof(ArcNode)); // p = new ArcNode;p -> adjvex = v;p -> next = AG.vertices[u].first; //用頭插法將v插到結點u的邊表結點中AG.vertices[u].first = p; // 插入后將第一條依附該頂點的弧的指針修改為p} }void matrix_convert_table(ALGraph &G1, MGraph G2) { // 鄰接矩陣轉化為鄰接表G1.arcnum = G2.arcnum;G1.vexnum = G2.vexnum;for(int i = 1; i <= G1.vexnum; i++) {G1.vertices[i].first = NULL; // 初始化指向第一條依附該頂點的弧的指針}ArcNode *p;for(int i = 1; i <= G2.vexnum; i++) { // 依次遍歷整個鄰接矩陣for(int j = 1; j <= G2.vexnum; j++) {if(G2.Edge[i][j] == 1) {p = (ArcNode *) malloc (sizeof(ArcNode));p -> adjvex = j;p -> next = G1.vertices[i].first;G1.vertices[i].first = p;}}} }void table_convert_matrix(MGraph &G1, ALGraph G2) { // 鄰接表轉化為鄰接矩陣G1.arcnum = G2.arcnum;G1.vexnum = G2.vexnum;for(int i = 1; i <= G1.vexnum; i++) {for(int j = 1; j <= G1.vexnum; j++) {G1.Edge[i][j] = 0; // 初始化鄰接矩陣}}ArcNode *p;for(int i = 1; i <= G2.vexnum; i++) { //依次遍歷各頂點表結點為頭的邊鏈表p = G2.vertices[i].first; // 取出頂點 i 的第一條出邊while(p) { //遍歷邊鏈表G1.Edge[i][p->adjvex] = 1; p = p -> next; // 取出下一條出邊}} }void print_matrix(MGraph G) { // 打印鄰接矩陣for(int i = 1; i <= G.vexnum; i++) {for(int j = 1; j <= G.vexnum; j++) {cout << G.Edge[i][j] << " ";}puts("");} } void print_table(ALGraph G) { // 打印鄰接表for(int i = 1; i <= G.vexnum; i++) {printf("(%d) ", i);ArcNode *p = G.vertices[i].first;while(p) {printf("-> %d ", p -> adjvex);p = p -> next;}puts("");} } int main() {MGraph MG1, MG2;ALGraph AG1, AG2;Create_Graph(AG1, MG1);cout << endl;print_matrix(MG1);cout << endl;print_table(AG1);cout << endl;matrix_convert_table(AG2, MG1); // 鄰接矩陣轉鄰接表print_table(AG2); // 打印轉化后的鄰接表cout << endl;table_convert_matrix(MG2, AG1); // 鄰接表轉鄰接矩陣print_matrix(MG2); // 打印轉化后的鄰接矩陣return 0; } /* 測試樣例二: 9 14 1 2 1 3 2 5 2 4 5 3 5 4 5 6 6 5 3 8 8 9 9 6 4 7 7 6 7 9 */

總結

以上是生活随笔為你收集整理的邻接表与邻接矩阵的相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。