資料結構(C實現)------- 圖的鄰接矩陣表示

來源:互聯網
上載者:User

標籤:圖的鄰接表標記法   圖的鄰接表格儲存體   

[本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.net/jesson20121020]

   圖的鄰接表標記法類似於樹的孩子鏈表標記法,就是對圖中的每個頂點vi,將所有鄰接於vi的頂點連結成一個單鏈表,這個單鏈表就稱為頂點vi的鄰接表。在鄰接表中有兩種結點結構:頭結點(vexdata,firstarc)、表結點(adjvex,nextarc)。

   其中,表頭結點由頂點域(vexdata)和指向第一條鄰接邊的指標域(firstarc)構成;表結點由鄰接點域(adjvex)和指向下一條鄰接邊的指標域(nextarc)構成。

   對於一個具有n個頂點、e條邊的圖G,若G是無向圖,則它的鄰接表需要n個表前端節點組成的順序表和2e個結點組成的n個鏈表;若G是有向圖,則它的鄰接表需要n個表頭結點組成的順序表和e個結點組成的n個鏈表。因此圖的鄰接表標記法的空間複雜度為S(n,e) = O(n+e)。若圖中邊的數目遠遠小於n^2,即圖為稀疏圖,則這時用鄰接表表示要比用鄰接矩陣表示節省空間的。


演算法實現:

   圖的鄰接表格儲存體結構描述如下:

#define MAX_VERTEX_NUM 50typedef enum {DG, UDG} GraphType;typedef char VertexType;//表節點typedef struct ArcNode {int adjvex; //鄰接節點int weight; //邊權重struct ArcNode *nextarc; //下一個節點指標} ArcNode, *ArcPtr;//前端節點typedef struct {VertexType vexdata;int id;ArcPtr firstarc;} VNode;//前端節點數組typedef struct {VNode vertices[MAX_VERTEX_NUM];int vexnum, arcnum;GraphType type;} ALGraph;

建立圖的鄰接表的演算法描述如下:

   (1) 輸入圖的類型(無向圖或有向圖)

   (2) 輸入圖的頂點數,邊數

   (3) 輸入所有頂點的字元資訊,並初始化所有鏈表的頭指標為空白指標NULL。

   (4) 輸入邊的資訊,產生邊表結點,建立圖的鄰接表,注意區分是圖的類型,另外,如果是有權圖,鄰接矩陣儲存其邊的權重,這裡是無權圖

演算法原始碼如下:

void create_AG(ALGraph *AG) {ArcPtr p;int i, j, k, type;VertexType v1, v2;printf("Please input graph type UG(0) or UDG(1) :");scanf("%d", &type);if (type == 0)AG->type = DG;else if (type == 1)AG->type = UDG;else {printf("Please input correct graph type UG(0) or UDG(1)!");return;}printf("please input vexnum:");scanf("%d", &AG->vexnum);printf("please input arcnum:");scanf("%d", &AG->arcnum);getchar();for (i = 1; i <= AG->vexnum; i++) {printf("please input the %dth vex(char) : ", i);scanf("%c", &AG->vertices[i].vexdata);getchar();AG->vertices[i].firstarc = NULL;}for (k = 1; k <= AG->arcnum; k++) {printf("please input the %dth arc v1(char) v2(char) :", k);scanf("%c %c", &v1, &v2);i = getIndexOfVexs(v1, AG);j = getIndexOfVexs(v2, AG);//根據圖的類型建立鄰接表if (AG->type == DG) { //有向圖p = (ArcPtr) malloc(sizeof(ArcNode));p->adjvex = j;p->nextarc = AG->vertices[i].firstarc;AG->vertices[i].firstarc = p;} else { //無向圖p = (ArcPtr) malloc(sizeof(ArcNode));p->adjvex = j;p->nextarc = AG->vertices[i].firstarc;AG->vertices[i].firstarc = p;p = (ArcPtr) malloc(sizeof(ArcNode));p->adjvex = i;p->nextarc = AG->vertices[j].firstarc;AG->vertices[j].firstarc = p;}getchar();}}
演算法說明:

   該演算法的時間複雜度為O(n+e)。若輸入邊的兩個頂點字元,需要通過尋找才能得到頂點在圖中的位置,則演算法的時間複雜度為O(n*e)。值得注意的是,一個圖的鄰接矩陣表示是唯一的,但其鄰接表表示不唯一,這是因為在鄰接表標記法中,各邊表結點的連結次序取決於建立鄰接表的演算法以及邊的輸入次序。例如,在該演算法中,每產生一個邊表結點,均插在對應鏈表的表頭位置。

完整代碼如下:

/* ============================================================================ Name        : ALGraph.c Author      :  Version     : Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include <stdio.h>#define MAX_VERTEX_NUM 50typedef enum {DG, UDG} GraphType;typedef char VertexType;//表節點typedef struct ArcNode {int adjvex; //鄰接節點int weight; //邊權重struct ArcNode *nextarc; //下一個節點指標} ArcNode, *ArcPtr;//前端節點typedef struct {VertexType vexdata;int id;ArcPtr firstarc;} VNode;//前端節點數組typedef struct {VNode vertices[MAX_VERTEX_NUM];int vexnum, arcnum;GraphType type;} ALGraph;/** * 根據頂點字元得到在頂點數組中的下標 */int getIndexOfVexs(char vex, ALGraph *AG) {int i;for (i = 1; i <= AG->vexnum; i++) {if (AG->vertices[i].vexdata == vex) {return i;}}return 0;}/** * 建立鄰接表 */void create_AG(ALGraph *AG) {ArcPtr p;int i, j, k, type;VertexType v1, v2;printf("Please input graph type UG(0) or UDG(1) :");scanf("%d", &type);if (type == 0)AG->type = DG;else if (type == 1)AG->type = UDG;else {printf("Please input correct graph type UG(0) or UDG(1)!");return;}printf("please input vexnum:");scanf("%d", &AG->vexnum);printf("please input arcnum:");scanf("%d", &AG->arcnum);getchar();for (i = 1; i <= AG->vexnum; i++) {printf("please input the %dth vex(char) : ", i);scanf("%c", &AG->vertices[i].vexdata);getchar();AG->vertices[i].firstarc = NULL;}for (k = 1; k <= AG->arcnum; k++) {printf("please input the %dth arc v1(char) v2(char) :", k);scanf("%c %c", &v1, &v2);i = getIndexOfVexs(v1, AG);j = getIndexOfVexs(v2, AG);//根據圖的類型建立鄰接表if (AG->type == DG) { //有向圖p = (ArcPtr) malloc(sizeof(ArcNode));p->adjvex = j;p->nextarc = AG->vertices[i].firstarc;AG->vertices[i].firstarc = p;} else { //無向圖p = (ArcPtr) malloc(sizeof(ArcNode));p->adjvex = j;p->nextarc = AG->vertices[i].firstarc;AG->vertices[i].firstarc = p;p = (ArcPtr) malloc(sizeof(ArcNode));p->adjvex = i;p->nextarc = AG->vertices[j].firstarc;AG->vertices[j].firstarc = p;}getchar();}}/** * 輸出圖的相關資訊 */void print_AG(ALGraph AG) {ArcPtr p;int i;if (AG.type == DG) {printf("Graph type: Direct graph\n");} else {printf("Graph type: Undirect graph\n");}printf("Graph vertex number: %d\n", AG.vexnum);printf("Graph arc number: %d\n", AG.arcnum);printf("Vertex set :\n");for (i = 1; i <= AG.vexnum; i++)printf("%c\t", AG.vertices[i].vexdata);printf("\nAdjacency List:\n");for (i = 1; i <= AG.vexnum; i++) {printf("%d", i);p = AG.vertices[i].firstarc;while (p != NULL) {printf("-->%d", p->adjvex);p = p->nextarc;}printf("\n");}}int main(void) {ALGraph AG;create_AG(&AG);print_AG(AG);return EXIT_SUCCESS;}

執行結果:

Please input graph type UG(0) or UDG(1) :1please input vexnum:4please input arcnum:4please input the 1th vex(char) : aplease input the 2th vex(char) : bplease input the 3th vex(char) : cplease input the 4th vex(char) : dplease input the 1th arc v1(char) v2(char) :a bplease input the 2th arc v1(char) v2(char) :a cplease input the 3th arc v1(char) v2(char) :a dplease input the 4th arc v1(char) v2(char) :b dGraph type: Undirect graphGraph vertex number: 4Graph arc number: 4Vertex set :abcdAdjacency List:1-->4-->3-->22-->4-->13-->14-->2-->1

   以上實現了圖的鄰接表標記法,用鄰接表表示圖,可以實現的基本有(1)求圖中任一頂點的度(2)判定圖中任意兩個頂點之間是否有邊相連等操作。

資料結構(C實現)------- 圖的鄰接矩陣表示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.