图的表示(邻接矩阵和邻接表)

图的表示有邻接矩阵、关联矩阵、邻接表等方式。

下面用代码实现邻接矩阵和邻接表。

#include
#include
#define MAX 50//maximum number of vertices
typedef char VertexType;

typedef enum{DG,UDG}GraphType;//0 for directed and 1 undirected

typedef struct MyGraph{
	GraphType type;
	VertexType v[MAX];//vertices
	int e[MAX][MAX];//edges
	int nv;//number of vertices
	int ne;//number of edges
}Graph;

int get_index_of_vertex(char v,Graph* p){
	int i;
	for(i=0;inv;i++){
		if(p->v[i]==v)
			return i;
	}
	return 0;
}

void print(Graph* p){
	int i,j;
	if(p->type==0)
		printf("this is a directed graph\n");
	else
		printf("this is an undirected graph\n");
	printf("vertex set:\n");
	for(i=0;inv;i++)
		printf("%c ",p->v[i]);
	printf("\n");
	printf("adjacent matrix:\n");
	for(i=0;inv;i++){
		for(j=0;jnv;j++){
			printf("%d ",p->e[i][j]);
		}
		printf("\n");
	}
}

void create_graph(Graph* ptr){
	int i,j,k;
	int type;
	char e1,e2;
	int index1,index2;
	//determine type:
	printf("Input graph type:");
	scanf("%d",&type);
	if(type==0)
		ptr->type=DG;
	else if(type==1)
		ptr->type=UDG;
	else{
		printf("no such type,0 for directed graph and 1 for undirected graph\n");
		return;
	}
	//
	printf("Input number of vertices:");
	scanf("%d",&ptr->nv);
	printf("Input number of edges:");
	scanf("%d",&ptr->ne);
	getchar();
	//
	for(i=0;inv;i++){
		printf("no.%d vertex:",i+1);
		scanf("%c",&ptr->v[i]);
		getchar();
	}

	//initialize the adjacent maxtrix
	for(i=0;inv;i++){
		for(j=0;jnv;j++){
			ptr->e[i][j]=0;
		}
	}

	for(k=0;knv;k++){
		printf("input no.%d edge:",k+1);
		scanf("%c",&e1);
		scanf("%c",&e2);
		fflush(stdin);
		index1=get_index_of_vertex(e1,ptr);
		index2=get_index_of_vertex(e2,ptr);
		if(ptr->type==1)
			ptr->e[index1][index2]=ptr->e[index2][index1]=1;//1 can be replaced by weight
		else
			ptr->e[index1][index2]=1;//1 can be replaced by weight
		//getchar();
	}
}

int main(){
	Graph* p=(Graph*)malloc(sizeof(struct MyGraph));
	create_graph(p);
	print(p);
	return EXIT_SUCCESS;
}
图的表示(邻接矩阵和邻接表)_第1张图片

邻接表表示法:

#include
#include
#define MaxVertexNum 10

typedef struct Node *EdgeNode;
typedef struct Vnode *VertexNode;
typedef struct GraphNode *Graph;

struct Node{//边表的结构
	int adjvex;//节点编号
	EdgeNode next;//next指针
};

typedef struct Vnode{
	int vertex;//顶点域
	EdgeNode firstedge;//管理边表的指针
}AdjList[MaxVertexNum];//数组的每个元素都是struct Vnode

struct GraphNode{
	int nv;//顶点数
	int ne;//边数
	AdjList adjlist;
};

Graph ReadG(){
	Graph G=(Graph)malloc(sizeof(struct GraphNode));
	int i,j;
	int v;
	int e1,e2;
	EdgeNode eptr;
	printf("input number of vertices:");
	scanf("%d",&G->nv);
	printf("inpur number of edges:");
	scanf("%d",&G->ne);
	//
	printf("input vertices(starting from 1):\n");
	for(i=1;i<=G->nv;i++){
		scanf("%d",&v);
		G->adjlist[i].vertex=v;//读入顶点的编号
		G->adjlist[i].firstedge=NULL;//初始化指针
	}
	printf("input edges:\n");
	for(j=0;jne;j++){
		scanf("%d%d",&e1,&e2);
		eptr=(EdgeNode)malloc(sizeof(struct Vnode));
		eptr->adjvex=e2;
		eptr->next=G->adjlist[e1].firstedge;//注意这行
		G->adjlist[e1].firstedge=eptr;//注意这行
		/*
		无向图:
		eptr=(EdgeNode)malloc(sizeof(struct Vnode));
		eptr->adjvex=e1;
		eptr->next=G->adjlist[e2].firstedge;
		G->adjlist[e2].firstedge=eptr;
		*/
	}
	return G;
}

void print(Graph G){
	int i,j;
	for(i=1;i<=G->nv;i++){
		while(G->adjlist[i].firstedge){
			printf("%d->",G->adjlist[i].vertex);
			printf("%d\n",G->adjlist[i].firstedge->adjvex);
			G->adjlist[i].firstedge=G->adjlist[i].firstedge->next;
		}
	}
}

int main(){
	Graph G=ReadG();
	print(G);
	return EXIT_SUCCESS;
}

图的表示(邻接矩阵和邻接表)_第2张图片


你可能感兴趣的:(数据结构与算法分析)