实现教材算法7.2利用邻接矩阵构造无向图的算法,在此基础上进行深度优先遍历和广度优先遍历。

本文介绍了一个关于图的遍历实验,包括使用邻接矩阵构造无向图,并实现了深度优先遍历和广度优先遍历算法。通过实验,学生能够理解和掌握图的基本存储结构和遍历方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

软件学院实验报告

姓名:              学号:              专业:               年级:

课程名称

数据结构

实验名称

实验9、图的遍历

实验的准备阶段

实验内容

(1)实验目的

通过该实验,使学生掌握图的几种存储结构,理解图的深度优先和广度优先遍历算法的思想和实现办法,

(2)实验内容

实现教材算法7.2利用邻接矩阵构造无向图的算法,在此基础上进行深度优先遍历和广度优先遍历。

(3)参考界面

(4)验收/测试用例

  1. 创建所示无向图

屏幕输出邻接矩阵

     0  1  0  0  0  1

1

2

3

4

5

6

     1  0  1  1  0  0

     0  1  0  1  1  0

     0  1  1  0  1  0

     0  0  1  1  0  1

     1  0  0  0  1  0

  1. 深度优先遍历

屏幕输出: 1 2 3 4 5 6

  1. 广度优先遍历

屏幕输出:1 2 6 3 4 5

  1. 另外:自己画出一个无向图,再测试一遍。

实验类型

验证性

实验的重点、难点

重点: 图遍历

难点: 图的深度优先遍历

实验环境

VC++6.0

实验的实施阶段

实验步骤及完成任务情况

一、设计思想

二、主要源代码

#include<iostream>

#include<stdlib.h>

#define MaxVertexNum 100

#define OK 1

#define ERROR 0

using namespace std;

typedef int status;

typedef int vertex;

typedef int weightType;

struct QNode;

typedef QNode* PtrtoQNode;

struct _Queue;

typedef struct _Queue* Queue;

struct QNode{

    vertex v;

    PtrtoQNode next;

};

struct _Queue{

    PtrtoQNode front;

    PtrtoQNode rear;

};

struct eNode;

typedef struct eNode* edge;

struct GNode;

typedef struct GNode* PtrToGNode;

typedef struct GNode* MGraph;

struct eNode{

    vertex v;

    vertex w;

    weightType weight;

}; 

struct GNode{

    int vn,en;

    weightType G[MaxVertexNum][MaxVertexNum];

};

bool visited[MaxVertexNum];

MGraph create(){

    MGraph G=(MGraph)malloc(sizeof(struct GNode));

    return G;  

}

status insert(edge e,MGraph G){

    G->G[e->v][e->w]=e->weight;

    G->G[e->w][e->v]=e->weight;

    return OK;

}

MGraph build(MGraph G){

    cout<<"输入vertexNum"<<endl;

    cout<<"输入edgeNum"<<endl;

    int vn;

    cin>>G->vn;

    cin>>G->en;

    cout<<"输入edge"<<endl;

    edge e=(edge)malloc(sizeof(struct eNode));

    for(int i=0;i<G->en;i++){

        cin>>e->v>>e->w>>e->weight;

        insert(e,G);

    }

    return G;

}

status show(MGraph G){

    for(int i=1;i<=G->vn;i++){

        for(int j=1;j<=G->vn;j++){

           cout<<G->G[i][j]<<' ';

        }

        cout<<endl;

    }

    return OK;

}

status DFS(MGraph G,vertex v){

        visited[v]=true;

        cout<<v<<' ';

        for(int i=1;i<=G->vn;i++){

           if(G->G[v][i]&&!visited[i])

           DFS(G,i);

        }

        return OK;

}

Queue InitQueue(){

    Queue Q=(Queue)malloc(sizeof(struct _Queue));

    Q->front=Q->rear=(PtrtoQNode)malloc(sizeof(struct QNode));

    Q->front->next=NULL;

    return Q;

}

status EnQueue(vertex v,Queue Q){

    PtrtoQNode p=(PtrtoQNode)malloc(sizeof(struct QNode));

    p->v=v;

    p->next=NULL;

    Q->rear->next=p;

    Q->rear=p;

    return OK;

}

status DeQueue(Queue Q,vertex* v){

    *v=Q->front->next->v;

    PtrtoQNode p=Q->front->next;

    if(p==Q->rear){

        free(p);

        Q->rear=Q->front;

        return OK;

    }

    else{

    Q->front->next=p->next;

    free(p);

    return OK;

    }

}

status BFS(MGraph G,vertex v){

    Queue Q=InitQueue();

    EnQueue(v,Q);

    visited[v]=true;

    vertex w;

    while(Q->front!=Q->rear){

        DeQueue(Q,&w);

        cout<<w<<' ';

        for(int i=1;i<=G->vn;i++){

           if(G->G[w][i]&&!visited[i]){   

           visited[i]=true;

           EnQueue(i,Q);

        }

    }

    }

}

void set(bool visited[]){

    for(int i=0;i<MaxVertexNum;i++)

{

    visited[i]=false;

}

}

int main(){

    set(visited);

    MGraph G=create();

    int i;

    do{

        cout<<"***************\n"

           <<"***无向网图****\n"

           <<"1.构建网图\n"

           <<"2.输出邻接矩阵\n"

           <<"3.深度优先遍历\n"

           <<"4.广度优先遍历\n"

           <<"5.退出\n"

           << "***************\n"

           << "***************\n"

           <<endl;

           cin>>i;

           system("cls");

           switch(i){

               case 1:

                   G=build(G);

                   cout<<"OK"<<endl;

                   break;

               case 2:

                   show(G);

                   cout<<"OK"<<endl;

                   break;

               case 3:

                   if(DFS(G,1))

                   cout<<endl<<"OK"<<endl;

                   set(visited);

                   break;

               case 4:

                   if(BFS(G,1))

                   cout<<endl<<"OK"<<endl;

                   set(visited);

                   break;

               default:

                   break;

           }

           system("pause");

           system("cls");

    }while(i!=5);

    return 0;

}

实验结果的处理阶段

实验结果

(注意:请同学们看完要求,把本段红色文字删除!!!)粘贴运行结果的截图,并适当用文字描述运行结果。

实验结果总结

(注意:请同学们看完要求,把本段红色文字删除!!!)

写自己遇到的问题,以及解决的方法。至少写两个。

或者写遇到的一个问题,写一段自己的收获,自己学会了什么。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值