1. 程式人生 > >C || 圖的四種儲存結構實現

C || 圖的四種儲存結構實現

1. 陣列表示法:

#include <stdio.h>
#include <limits.h>

#define INFINITY INT_MAX
#define Maxvex 100

typedef struct graph {
	int vexs[Maxvex+1];  //頂點
	int AdjMatrix[Maxvex+1][Maxvex+1]; //鄰接矩陣
	int vexsnum, edgenum; //頂點數,邊數
}Graph;

void CreateGraph(Graph *G) {
	printf("請輸入頂點數,邊數:");
	scanf("%d %d",
&G->vexsnum, &G->edgenum); for (int i = 1; i <= G->vexsnum; i++) { scanf("%d",&G->vexs[i]); for (int j = 1; j <= G->vexsnum; j++) { G->AdjMatrix[i][j] = INFINITY; } } int row, col, w; for (int i = 1; i <= G->edgenum; i++) { printf("輸入邊座標i,j,權值:"
); scanf("%d%d%d",&row, &col, &w); G->AdjMatrix[row][col] = w; G->AdjMatrix[col][row] = w; } } int main () { Graph G; CreateGraph(&G); return 0; }

2.鄰接表

#include <stdio.h>
#include <stdlib.h>

#define Vertex_Num 100
typedef int VertexType;

//儲存弧的結點
typedef
struct ArcNode { int adjvex; //該弧所指向的頂點的位置 int info; //該弧的權值 struct ArcNode *next; //指向下一條弧的指標 }ArcNode; //儲存頂點的結點 typedef struct VNode { VertexType data; ArcNode *first;//指向第一條依附該頂點的弧的指標 }VNode, AdjList[Vertex_Num]; //圖 typedef struct { AdjList vertices; int vexnum, arcnum; //圖的頂點數和弧數 }Graph; void CreateGraph(Graph *G) { printf("請輸入頂點數,邊數:\n"); scanf("%d %d", &G->vexnum, &G->arcnum); printf("請輸入頂點\n"); for (int i = 1; i <= G->vexnum; i++) { scanf("%d",&G->vertices[i].data); G->vertices[i].first = NULL; } printf("請選擇你要建立的儲存型別,1表示鄰接表,0表示逆鄰接表:\n"); int choice; scanf("%d",&choice); int vex1, vex2, weight; if (choice != 1 && choice != 0) { printf("輸入有錯,請重新輸入\n"); scanf("%d",&choice); } if (choice == 1) { for (int j =1; j <= G->arcnum; j++) { printf("請輸入邊的兩個頂點以及權值:\n"); scanf("%d%d%d", &vex1, &vex2, &weight); ArcNode *edge = (ArcNode*)malloc(sizeof(ArcNode)); edge->adjvex = vex2; edge->info = weight; edge->next = G->vertices[vex1].first; G->vertices[vex1].first = edge; } } if (choice == 0) { for (int j =1; j <= G->arcnum; j++) { printf("請輸入邊的兩個頂點以及權值:\n"); scanf("%d%d%d", &vex1, &vex2, &weight); ArcNode *edge = (ArcNode*)malloc(sizeof(ArcNode)); edge->adjvex = vex1; edge->info = weight; edge->next = G->vertices[vex2].first; G->vertices[vex2].first = edge; } } } int main() { Graph G; CreateGraph(&G); for(int i = 1; i <= G.vexnum; i++) { printf("|%d|->", G.vertices[i].data); ArcNode *e = G.vertices[i].first; while(e != NULL) { printf("%d->", e->adjvex); e = e->next; } printf("NULL\n"); } return 0; }

執行結果:

  • 鄰接表:

在這裡插入圖片描述

  • 逆鄰接表:
    在這裡插入圖片描述

3. 十字連結串列:

#include <stdio.h>
#include <stdlib.h>

// - - - - - 有向圖的十字連結串列儲存結構 - - - - -
//相當於把有向圖的鄰接表和逆鄰接表結合到一起得到的一種連結串列
#define VERTEX_NUM 100
typedef int VertexType;

//弧結點
typedef struct ArcBox {
	int tailvex, headvex; //弧頭和弧尾的位置
	struct ArcBox *hlink, *tlink; //分別指向下一個弧頭和弧尾相同的鏈域
	int info;  //弧的權值	
}ArcBox;

//頂點結點
typedef struct VexNode {
	VertexType data;
	ArcBox *firstin, *firstout;	
}VexNode;

//圖
typedef struct {
	VexNode xlist[VERTEX_NUM]; //表頭向量
	int vexnum, arcnum; //有向圖的當前頂點數和弧數
}Graph;


void CreateGraph(Graph *G) {
	printf("請輸入頂點數,弧數:\n");
	scanf("%d%d",&G->vexnum, &G->arcnum);
	printf("請輸入頂點:\n");
	for (int i = 1; i <= G->vexnum; i++) {
		scanf("%d",&G->xlist[i].data);
		G->xlist[i].firstin = NULL;
		G->xlist[i].firstout = NULL;
	}
	int vex1, vex2, weight;
	for (int i = 1; i <= G->arcnum; i++) {
		printf("請輸入弧尾,弧頭,弧的權值\n");
		scanf("%d%d%d",&vex1, &vex2, &weight);
		ArcBox *e = (ArcBox*)malloc(sizeof(ArcBox));
		e->tailvex = vex1;
		e->headvex = vex2;
		e->tlink = NULL;
		e->hlink = NULL;
		e->tlink = G->xlist[vex1].firstout;
		G->xlist[vex1].firstout = e;
		e->hlink = G->xlist[vex2].firstin;
		G->xlist[vex2].firstin = e;
	}	
}

4. 鄰接多重表:

// - - - - - 圖的鄰接多重表儲存表示 - - - - -
// - - - - - - 更適用於無向圖 - - - - - -
// - - 相比鄰接表更方便對某一條邊進行操作 - -
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define VERTEX_NUM 100

typedef enum {unvisited, visited} VisitIf;
typedef int VertexType;

typedef struct EBox {
	VisitIf mark; //訪問標記
	int ivex, jvex;
	struct EBox *ilink, *jlink; //分別指向依附這兩個頂點的下一條邊
	int info; //權值
}EBox;

typedef struct VexBox {
	VertexType data;
	EBox *firstedge; //指向第一條依附該頂點的邊
}VexBox;

typedef struct {
	VexBox adjmulist[VERTEX_NUM];
	int vexnum, edgenum; //圖的頂點數和弧數
}Graph;

void CreateGraph(Graph *G) {
	//G = (Graph*)malloc(sizeof(Graph)); 
	printf("請輸入點數,弧數\n");
	scanf("%d%d",&G->vexnum, &G->edgenum);
	printf("請輸入頂點\n");
	for(int i = 1; i <= G->vexnum; i++) {
		scanf("%d",&G->adjmulist[i].data);
		G->adjmulist[i].firstedge = NULL;
	}
	int v1, v2, weight;
	for(int k = 1; k <= G->edgenum; k++) {
		printf("請輸入兩個頂點v1,v2以及弧的權值\n");		
		scanf("%d%d%d",&v1,&v2,&weight);
		EBox *edge = (EBox*)malloc(sizeof(EBox));
		edge->mark = unvisited;
		edge->ivex = v1;
		edge->jvex = v2;
		edge->info = weight;
		edge->ilink = NULL;
		edge->jlink = NULL;
		EBox *p = G->adjmulist[v1].firstedge;
		if (!p) {
			G->adjmulist[v1].firstedge = edge;
		}
		else {
			while((p->ilink != NULL && p->ivex == v1) || (p->jlink != NULL && p->jvex == v1)) {
				if(v1 == p->ivex) 
					p = p->ilink; 
				else 
					p = p->jlink; 
			} 
				if(p->ivex == v1) p->ilink = edge; 
				else p->jlink = edge;  
		}
		p = G->adjmulist[v2].firstedge;
		if (!p) {
			G->adjmulist[v2].firstedge = edge;
		}
		else {
			while((p->ilink != NULL && p->ivex == v2) || (p->jlink != NULL && p->jvex == v2)) {
				if(v2 == p->ivex) 
					p = p->ilink; 
				else
					p = p->jlink; 
			} 
				if(p->ivex == v2) p->ilink = edge; 
				else p->jlink = edge; 

		}
	}
}

void PrintGraph(Graph G)
{
    EBox *p;
    for(int i = 1; i <= G.vexnum; ++i)
    {
        p = G.adjmulist[i].firstedge;
        while(p != NULL)
        {
            if(p->ivex == i)    //判斷相等才能知道連線上的是ivex還是jvex;
            {
                printf("%d--%d\n", G.adjmulist[p->ivex].data, G.adjmulist[p->jvex].data);
                p = p->ilink;
            }
            else
            {
                printf("%d--%d\n", G.adjmulist[p->jvex].data, G.adjmulist[p->ivex].data);
                p = p->jlink;
            }
        }
    }
}

int main () {
	Graph G;
	CreateGraph(&G);
	PrintGraph(G);
	return 0;
}

執行結果:
在這裡插入圖片描述