图---邻接表(建立,深度遍历,广度遍历)
来源:互联网 发布:雅马哈授权网络经销商 编辑:程序博客网 时间:2024/06/06 03:28
图的邻接表表示法类似于树的孩子链表表示法。对于图G中的每个顶点vi ,该方法把所有邻接于vi 的顶点vj 链成一个带头结点的单链表,这个单链表就称为顶点vi 的邻接表(Adjacency List)。
以下代码测试过,为图的邻接表表示方式。
-
-
-
- #include <stdio.h>
- #define MaxVertexNum 100
- #define QueueSize 30
- typedef enum{FALSE,TRUE}Boolean;
- Boolean visited[MaxVertexNum];
- typedef char VertexType;
- typedef int EdgeType;
- typedef struct node
- {
- int adjvex;
- struct node *next;
-
- }EdgeNode;
- typedef struct vnode
- {
- VertexType vertex;
- EdgeNode *firstedge;
- }VertexNode;
- typedef VertexNode AdjList[MaxVertexNum];
- typedef struct
- {
- AdjList adjlist;
- int n,e;
- }ALGraph;
-
-
-
- void CreateGraphAL (ALGraph *G)
- {
- int i,j,k;
- EdgeNode * s;
- printf("请输入顶点数和边数(输入格式为:顶点数,边数):/n");
- scanf("%d,%d",&(G->n),&(G->e));
- printf("请输入顶点信息(输入格式为:顶点号<CR>)每个顶点以回车作为结束:/n");
- for (i=0;i<G->n;i++)
- {
- scanf("/n%c",&(G->adjlist[i].vertex));
- G->adjlist[i].firstedge=NULL;
- }
- printf("请输入边的信息(输入格式为:i,j):/n");
- for (k=0;k<G->e;k++)
- {
- scanf("/n%d,%d",&i,&j);
- s=new EdgeNode;
- s->adjvex=j;
- s->next=G->adjlist[i].firstedge;
- G->adjlist[i].firstedge=s;
- s=new EdgeNode;
- s->adjvex=i;
- s->next=G->adjlist[j].firstedge;
- G->adjlist[j].firstedge=s;
- }
- }
-
-
-
- void DFS(ALGraph *G,int i)
- {
-
- EdgeNode *p;
- printf("visit vertex:%c/n",G->adjlist[i].vertex);
- visited[i]=TRUE;
- p=G->adjlist[i].firstedge;
- while(p)
- {
- if (!visited[p->adjvex])
- DFS(G,p->adjvex);
- p=p->next;
- }
- }
- void DFSTraverseM(ALGraph *G)
- {
- int i;
- for(i=0;i<G->n;i++)
- visited[i]=FALSE;
- for(i=0;i<G->n;i++)
- if(!visited[i])
- DFS(G,i);
- }
-
-
-
- typedef struct
- {
- int front;
- int rear;
- int count;
- int data[QueueSize];
- }CirQueue;
- void InitQueue(CirQueue *Q)
- {
- Q->front=Q->rear=0;
- Q->count=0;
- }
- int QueueEmpty(CirQueue *Q)
- {
- return Q->count=QueueSize;
- }
- int QueueFull(CirQueue *Q)
- {
- return Q->count==QueueSize;
- }
- void EnQueue(CirQueue *Q,int x)
- {
- if (QueueFull(Q))
- printf("Queue overflow");
- else
- {
- Q->count++;
- Q->data[Q->rear]=x;
- Q->rear=(Q->rear+1)%QueueSize;
- }
- }
- int DeQueue(CirQueue *Q)
- {
- int temp;
- if(QueueEmpty(Q))
- {
- printf("Queue underflow");
- return NULL;
- }
- else
- {
- temp=Q->data[Q->front];
- Q->count--;
- Q->front=(Q->front+1)%QueueSize;
- return temp;
- }
- }
- void BFS(ALGraph*G,int k)
- {
- int i;
- CirQueue Q;
- EdgeNode *p;
- InitQueue(&Q);
- printf("visit vertex:%c/n",G->adjlist[k].vertex);
- visited[k]=TRUE;
- EnQueue(&Q,k);
- while(!QueueEmpty(&Q))
- {
- i=DeQueue(&Q);
- p=G->adjlist[i].firstedge;
- while(p)
- {
- if(!visited[p->adjvex])
- {
- printf("visit vertex:%c",G->adjlist[p->adjvex].vertex);
- visited[p->adjvex]=TRUE;
- EnQueue(&Q,p->adjvex);
- }
- p=p->next;
- }
- }
- }
- void BFSTraverseM(ALGraph *G)
- {
- int i;
- for (i=0;i<G->n;i++)
- visited[i]=FALSE;
- for (i=0;i<G->n;i++)
- if (!visited[i])
- BFS(G,i);
- }
-
-
-
- int main()
- {
- ALGraph G;
- CreateGraphAL(&G);
- printf("深度优先遍历:/n");
- DFSTraverseM(&G);
- printf("广度优先遍历:/n");
- BFSTraverseM(&G);
- return 0;
- }
测试结果如下:
如果测试有误,若是代码有错,请指出,共同学习,呵呵。
0 0