数据结构实验报告:图的基本操作及应用

版权声明:转载请留言 https://blog.csdn.net/qq_40744093 https://blog.csdn.net/qq_40744093/article/details/86709594

                                            图的遍历

一、实验目的

1、使学生可以巩固所学的有关图的基本知识。

2、熟练掌握图的存储结构。

3、熟练掌握图的两种遍历算法。

4、掌握如何应用图解决各种实际问题。

二、实验内容

题目一: 图的遍历(* 必做题

题目:最小生成树问题(**

题目:拓扑排序的应用(***

题目:最短路径问题(***

、实验题目

题目一: 图的遍历(* 必做题

[问题描述]

对给定图,实现图的深度优先遍历和广度优先遍历。

[基本要求]

以邻接表为存储结构,实现连通无向图的深度优先和广度优先遍历。以用户指定的结点为起点,分别输出每种遍历下的结点访问序列。

[测试数据]
  由学生依据软件工程的测试技术自己确定。

四、实验过程

【源代码及注释】

#include<iostream>
#include<cstring>
#define MaxV 100                    //宏定义最大顶点数
using namespace std;
int visited[MaxV];                  //设置标记数组
typedef struct ArcNode
{
     int adjvex;              		//邻接点域,存储该邻点顶点对应的下标
     struct ArcNode *nextarc;		//邻节点
}ArcNode;
typedef struct VNode
{
     char data;	   			        //顶点对应的数据
     ArcNode  *firstarc;            //边表头指针指向邻顶点
}VNode,AdjList[MaxV];
typedef struct
{
     AdjList  vertices;             //MaxV个顶点结点
     int vexnum,arcnum;			    //顶点数,边数
}ALGraph;
typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct {
    int    data[MaxV];
    int    front,rear;
}Queue;                             //队列结构的定义
void CreateALGraph(ALGraph *G);     //无向图的创建
void DFS(ALGraph G,int i);          //深度优先搜索遍历
void InitQueue(Queue &Q);           //初始化队列
void EnQueue(Queue &Q,int e);       //入队
bool QueueEmpty(Queue &Q);          //判断队列是否为空
void DeQueue(Queue &Q,int &e);      //出队
void BFS(ALGraph &G);               //广度优先搜索遍历
void CreateALGraph(ALGraph *G)
{
    cout<<"分别输入总顶点与总边的数目:"<<endl;;
    cin>>G->vexnum>>G->arcnum;
    cout<<"依次输入"<<G->vexnum<<"个顶点值:"<<endl;;
    for(int k=1;k<=G->vexnum;k++)
    {
        cin>>G->vertices[k].data;
        G->vertices[k].firstarc=NULL;
    }
    cout<<"依次输入"<<G->arcnum<<"条边起始点与结束点对应的下标:"<<endl;
    for(int k=1;k<=G->arcnum;k++)
    {
        int i,j;                    //i表示这条边起始点对应的下标
                                    //j表示这条边结束点对应的下标
        cin>>i>>j;
        ArcNode *p=new ArcNode;
        p->adjvex=j;
        p->nextarc=G->vertices[i].firstarc;
        G->vertices[i].firstarc=p;
        ArcNode *q=new ArcNode;
        q->adjvex=i;
        q->nextarc=G->vertices[j].firstarc;
        G->vertices[j].firstarc=q;
    }
}
void DFS(ALGraph G,int i)
{
    visited[i]=1;                    //将要访问过的置为1
    cout<<G.vertices[i].data<<" ";   //输出顶点
    ArcNode *p=G.vertices[i].firstarc;
    while(p)
    {
        int j=p->adjvex;
        if(!visited[j])
            DFS(G,j);
        p=p->nextarc;
    }
}
void InitQueue(Queue &Q)
{
    Q.front=Q.rear=0;
}
void EnQueue(Queue &Q,int e)
{
    if ((Q.rear+1)%MaxV == Q.front)
        return;
    Q.data[Q.rear]=e;
    Q.rear=(Q.rear+1)%MaxV;
}
bool QueueEmpty(Queue &Q)
{
    if (Q.front==Q.rear)
        return true;
    else
        return false;
}
void DeQueue(Queue &Q,int &e)
{
    if (Q.front==Q.rear)
        return;
    e=Q.data[Q.front];
    Q.front=(Q.front+1)%MaxV;
}
void BFS(ALGraph &G)
{
    ArcNode *p;
   	Queue Q;
   	InitQueue(Q);
    for(int j = 1; j<=G.vexnum; j++)
    {
        if(!visited[j])
        {
            cout<<G.vertices[j].data<<" ";  //打印顶点
			 visited[j] = 1;
            EnQueue(Q,j);
            while(!QueueEmpty(Q))
            {
                int m;
                DeQueue(Q,m);      		 //访问点出队列
                p=G.vertices[m].firstarc;//找到当前顶点边表链表头指针
                while(p)
                {
                    if(!visited[p->adjvex])
                    {
                        cout<<G.vertices[p->adjvex].data<<" ";
                        visited[p->adjvex] = 1;
                        EnQueue(Q,p->adjvex);
                    }
                    p=p->nextarc;
                }
            }
        }
    }
}
int main()
{
    ALGraph G;                         //定义无向图G
    CreateALGraph(&G);                 //创建邻接表
    memset(visited,0,sizeof(visited)); //将标记数组置为0
    cout<<"深度优先搜索遍历:";
    DFS(G,1);                          //从第一个顶点进行深度优先遍历
    cout<<endl;
    memset(visited,0,sizeof(visited)); //将标记数组置为0
    cout<<"广度优先搜索遍历:";
    BFS(G);                            //广度优先搜索遍历
    return 0;
}

【运行结果】

              

猜你喜欢

转载自blog.csdn.net/qq_40744093/article/details/86709594