数据结构与算法分析
深度优先搜索和广度优先搜索算法实现
四川大学软件学院 学生实验报告
实验名称:数据结构与算法分析
深度优先搜索和广度优先搜索算法实现
实验报告
班级 __ 姓名 学号
一、实验号题目:深度优先搜索和广度优先搜索算法实现 二、实验的目的和要求: 1.采用C++实现; 2.熟练掌握图的应用;
3.熟练掌握图的邻接表存储结构以及拓扑排序的基本思想。 4.上机调试程序,掌握查错、排错使程序能正确运行。 三、实验的环境: 1.硬件环境: 2.软件环境:
(1)操作系统windowsXP SP2。 (2)编译系统Mingw32 2.95
C-Free开发工具: Borland C++ Builder 6.0 C-Free中使用的编译系统: Mingw32 2.95 C-Free中使用的调试系统: GDB 5.2.1 C-Free中使用的VCL组件: SynEdit1.1
(3)编辑软件特点
使用c-Free自带的编辑软件,C-Free的智能输入功能能够大大提高你的代码编写速度,它能够
记住你已经输入的所有标识符、关键字,下一次输入标识符时,你不需要输入全部的标识符名称,输入一到二个字母,编辑窗口中会出现你需要的标识符。
四、算法描述:
深度优先搜索
深度优先搜索类似于树的先根遍历。假设给定图G的初态是所有顶点均未访问过,在G中任选一顶点v为初始出发点,则深度优先搜索可定义如下:首先,访问出发点v,并将其标记为已访问过,然后依次从v的未被访问过的邻接点出发深度优先遍历图,直到图中所有和v有路径相通的顶点都被访问到;若此时图中还有顶点未被访问,则另选图中一个未曾被访问的顶点作为起始点,重复上述过程,直到图中所有顶点都被访问到为止。
深度优先搜索和广度优先搜索算法实现
广度优先搜索
广度优先搜索类似于树的按层次遍历的过程。从图中的某个顶点v0出发,在访问v0之后依次访问v0的所有未被访问过的邻接点w1,w2,w3,…wk,然后按这些邻接点被访问的先后次序依次从w1,w2,w3,…wk出发访问它们的邻接点,如此下去,直至图中所有已被访问的顶点的邻接点都被访问到。若此时图中尚有顶点未被访问,则另选图中一个未曾被访问的顶点作起始点,重复上述过程,直至图中所有顶点都被访问到为止。
五、源程序清单:
#include <iostream.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> #include <ctype.h>
// Used by the mark array #define UNVISITED 0 #define VISITED 1
// Graph abstract class
class Graph { public:
// Return the number of vertices
深度优先搜索和广度优先搜索算法实现
virtual int n() =0;
// Return the current number of edges virtual int e() =0;
// Return the index of the first neigbor of given vertex virtual int first(int) =0;
// Return the index of the next neigbor of given vertex virtual int next(int, int) =0;
// Store an edge defined by two vertices and weight virtual void setEdge(int, int, int) =0; // Delete edge defined by two vertices
virtual void delEdge(int, int) =0;
// Return weight of edge connecting two vertices // Return 0 if no such edge exists virtual int weight(int, int) =0; // Get mark value for a vertex virtual int getMark(int) =0; // Set mark value for a vertex virtual void setMark(int, int) =0; };
class Edge{
public:
int vectex,weight;
Edge() {vectex = -1;weight = -1; }
Edge(int v,int w) {vectex =v; weight =w;} };
class Graphm : public Graph { // Implement adjacency matrix private:
int numVertex, numEdge; // Store number of vertices, edges int **matrix; // Pointer to adjacency matrix int *mark; // Pointer to mark array
public:
Graphm(int numVert) { // Make graph w/ numVert vertices int i, j;
numVertex = numVert; numEdge = 0;
mark = new int[numVert]; // Initialize mark array for (i=0; i<numVertex; i++) mark[i] = UNVISITED;
matrix = (int**) new int*[numVertex]; // Make matrix for (i=0; i<numVertex; i++)
matrix[i] = new int[numVertex];
for (i=0; i< numVertex; i++) // Edges start w/ 0 weight
深度优先搜索和广度优先搜索算法实现
for (int j=0; j<numVertex; j++) matrix[i][j] = 0; }
~Graphm() { // Destructor
delete [] mark; // Return dynamically allocated memory for (int i=0; i<numVertex; i++) delete [] matrix[i]; delete [] matrix; }
int n() { return numVertex; } // Number of vertices int e() { return numEdge; } // Number of edges
int first(int v) { // Return v's first neighbor int i;
for (i=0; i<numVertex; i++) if (matrix[v][i] != 0) return i; return i; // Return n if none }
int next(int v1, int v2) { // Get v1's neighbor after v2 int i;
for(i=v2+1; i<numVertex; i++) if (matrix[v1][i] != 0) return i; return i; }
// Set edge (v1, v2) to wgt
void setEdge(int v1, int v2, int wgt) { Assert(wgt>0, "Illegal weight value"); if (matrix[v1][v2] == 0) numEdge++; matrix[v1][v2] = wgt; }
void delEdge(int v1, int v2) { // Delete edge (v1, v2) if (matrix[v1][v2] != 0) numEdge--; matrix[v1][v2] = 0; }
int weight(int v1, int v2) { return matrix[v1][v2]; } int getMark(int v) { return mark[v]; } void setMark(int v, int val) { mark[v] = val; } };
深度优先搜索和广度优先搜索算法实现
// Abstract queue class
template <class Elem> class Queue { public:
// Reinitialize the queue. The user is responsible for // reclaiming the storage used by the stack elements. virtual void clear() = 0;
// Place an element at the rear of the queue. Return // true if successful, false if not (if queue is full). virtual bool enqueue(const Elem&) = 0;
// Remove the element at the front of the queue. Return // true if succesful, false if queue is empty.
// The element removed is returned in the first parameter. virtual bool dequeue(Elem&) = 0; // Remove Elem from front // Return in first parameter a copy of the front element. // Return true if succesful, false if queue is empty. virtual bool frontValue(Elem&) const = 0; // Return the number of elements in the queue. virtual int length() const …… 此处隐藏:4367字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [教育文库]夜场KTV服务员的岗位职责及工作流程[1]
- [教育文库]企划、网络、市场绩效考核方案
- [教育文库]学党史、知党情、强党性--“党的基本理
- [教育文库]2016年高考物理大一轮总复习(江苏专版
- [教育文库]干部廉洁自律自查自纠的报告
- [教育文库]2010年北京大学心理学系拟录取硕士研究
- [教育文库]资金时间价值练习题及答案
- [教育文库]保护环境的心得体会
- [教育文库]英语角内容:英语趣味小知识
- [教育文库]档案收集与管理工作通知
- [教育文库]劳动规章制度范本范本
- [教育文库]高考物理一轮复习课后限时作业1运动的
- [教育文库]机械工艺夹具毕业设计195推动架设计说
- [教育文库]通用技术教学比赛说课稿2
- [教育文库]2018年四年级英语下册 Module 7 Unit 2
- [教育文库]第2章 宽带IP网络的体系结构
- [教育文库]九年级化学第五单元课题3《根据化学方
- [教育文库]小学英语六年级情态动词用法归纳
- [教育文库]甲级单位编制窑井盖项目可行性报告(立
- [教育文库]2016-2021年中国城市规划行业全景调研
- 高考英语听力十大场景词汇总结
- 全省领导班子思想政治建设座谈会会议精
- 人教版新课标高一英语提优竞赛试题 下
- 江西省2014年生物中考试题
- 长沙镇食品药品安全事故应急预案
- 《金刚石、石墨和C60》片段教学设计
- 福州教育学院(王旭东)
- 基于EDA音乐播放器的设计
- 9、古诗两首《夜书所见》《九月九日忆
- 小学语文课外阅读有效策略探讨
- 贵州文化产业发展成支柱产业的问卷调查
- 膀胱类癌的诊治体会(附3例报告)
- 发动机积碳产生的原因
- Configuring Code Composer Studio for
- 学生良好的心理素质如何培养点滴谈
- 46 电沉积法制备锂离子电池用硅-锂薄膜
- 美舍雅阁公司管理中各部门职责
- 去壳剥皮的小妙招
- 六自由度运动平台的仿真研究
- Pride and Prejudice(傲慢与偏见)




