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

来源:互联网 发布:买个域名多少钱 编辑:程序博客网 时间:2024/05/21 10:47

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

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

#include<stdio.h>#include<stdlib.h>#define MAX 50//maximum number of verticestypedef char VertexType;typedef enum{DG,UDG}GraphType;//0 for directed and 1 undirectedtypedef struct MyGraph{GraphType type;VertexType v[MAX];//verticesint e[MAX][MAX];//edgesint nv;//number of verticesint ne;//number of edges}Graph;int get_index_of_vertex(char v,Graph* p){int i;for(i=0;i<p->nv;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");elseprintf("this is an undirected graph\n");printf("vertex set:\n");for(i=0;i<p->nv;i++)printf("%c ",p->v[i]);printf("\n");printf("adjacent matrix:\n");for(i=0;i<p->nv;i++){for(j=0;j<p->nv;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;i<ptr->nv;i++){printf("no.%d vertex:",i+1);scanf("%c",&ptr->v[i]);getchar();}//initialize the adjacent maxtrixfor(i=0;i<ptr->nv;i++){for(j=0;j<ptr->nv;j++){ptr->e[i][j]=0;}}for(k=0;k<ptr->nv;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 weightelseptr->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;}


邻接表表示法:

#include<stdio.h>#include<stdlib.h>#define MaxVertexNum 10typedef 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 Vnodestruct 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;j<G->ne;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;}