11周 项目3

来源:互联网 发布:外贸邦海关数据 编辑:程序博客网 时间:2024/05/27 03:29
问题及描述:
/*   烟台大学计算机学院      文件名称:aa.cpp      作者:李金朴   完成日期:2017年11月12日      问题描述:实现图遍历算法,分别输出如下图结构的深度优先(DFS)遍历序列和广度优先遍历(BFS)序列。      输入描述:无     输出描述:输出广度与深度的遍历结果    用到了算法库graph.h     */     BFS:  [cpp] view plain copy  #include <stdio.h>    #include <malloc.h>    #include "../graph.h"    int visited[MAXV]={};    void DFS(ALGraph *G, int v)    {        ArcNode *p;        int w;        visited[v]=1;        printf("%d ", v);        p=G->adjlist[v].firstarc;        while (p!=NULL)        {            w=p->adjvex;            if (visited[w]==0)                DFS(G,w);            p=p->nextarc;        }    }        int main()    {        int i;        ALGraph *G;        int A[6][6]=        {            {0,1,1,1,0,0},            {1,0,1,0,1,0},            {1,1,0,0,0,1},            {1,0,0,0,0,1},            {0,1,0,0,0,1},            {0,0,1,1,1,0}        };        ArrayToList(A[0], 6, G);                printf(" 由2开始深度遍历:");        DFS(G, 2);        printf("\n");            for(i=0; i<MAXV; i++) visited[i]=0;        printf(" 由0开始深度遍历:");        DFS(G, 0);        printf("\n");        return 0;    }    DFS:  [cpp] view plain copy  #include <stdio.h>    #include <malloc.h>    #include "../graph.h"        void BFS(ALGraph *G, int v)    {        ArcNode *p;        int w,i;        int queue[MAXV],front=0,rear=0; //定义循环队列        int visited[MAXV];     //定义存放节点的访问标志的数组        for (i=0; i<G->n; i++) visited[i]=0; //访问标志数组初始化        printf("%2d",v);            //输出被访问顶点的编号        visited[v]=1;                       //置已访问标记        rear=(rear+1)%MAXV;        queue[rear]=v;              //v进队        while (front!=rear)         //若队列不空时循环        {            front=(front+1)%MAXV;            w=queue[front];             //出队并赋给w            p=G->adjlist[w].firstarc;   //找w的第一个的邻接点            while (p!=NULL)            {                if (visited[p->adjvex]==0)                {                    printf("%2d",p->adjvex); //访问之                    visited[p->adjvex]=1;                    rear=(rear+1)%MAXV; //该顶点进队                    queue[rear]=p->adjvex;                }                p=p->nextarc;       //找下一个邻接顶点            }        }        printf("\n");    }            int main()    {        ALGraph *G;      int A[6][6]=        {            {0,1,1,1,0,0},            {1,0,1,0,1,0},            {1,1,0,0,0,1},            {1,0,0,0,0,1},            {0,1,0,0,0,1},            {0,0,1,1,1,0}        };        ArrayToList(A[0], 6, G);            printf(" 由2开始广度遍历:");        BFS(G, 2);            printf(" 由0开始广度遍历:");        BFS(G, 0);        return 0;    } 
运行结果:
原创粉丝点击