图的深度和广度遍历-实验报告.doc_第1页
图的深度和广度遍历-实验报告.doc_第2页
图的深度和广度遍历-实验报告.doc_第3页
图的深度和广度遍历-实验报告.doc_第4页
图的深度和广度遍历-实验报告.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验报告一、 实验目的和内容1. 实验目的掌握图的邻接矩阵的存储结构;实现图的两种遍历:深度优先遍历和广度优先遍历。2. 实验内容1图的初始化;2图的遍历:深度优先遍历和广度优先遍历。二、实验方案程序主要代码:/ / 邻接矩阵的节点数据/ public struct ArcCellpublic int Type;/顶点的关系类型,对无权图,用1或0表示相邻;/对带权图,则为权值类型。public object Data;/该弧相关信息public ArcCell(int type,object data)Type = type;Data = data;/ / 图的类型/ public enum GKind DG,DN,UDG,UDN;/有向图,有向网,无向图,无向网/ / 图类/ public class Graphpublic static int Max_Vertex_Num = 20;/最大顶点数private object Vexs; /顶点数据数组private ArcCell , Arcs;/邻接矩阵private GKind Kind;/图的种类private int VexNum,ArcNum;/当前顶点数和弧数/ / 图的初始化方法/ / 顶点数/ 弧数/ 图的类型public Graph(int vexnum,int arcnum,GKind k)VexNum = vexnum;ArcNum = arcnum;Kind = k;Vexs = new objectMax_Vertex_Num;Arcs = new ArcCellMax_Vertex_Num,Max_Vertex_Num;/ / 设置v1,v2之间的弧的权值,顶点的关系类型,对无权图,用1或0表示相邻;/ 对带权图,则为权值类型。/ / 顶点1/ 顶点2/ 权/ 成功返回真,否则返回假public bool SetArcInfo(int v1,int v2,int adj,object data)if(v1VexNum & v2VexNum)Arcsv1,v2.Type = adj;Arcsv1,v2.Data = data;switch(Kind)case GKind.DG:break;case GKind.UDG:Arcsv2,v1.Type = adj;Arcsv2,v1.Data = data;break;case GKind.DN:break;case GKind.UDN:break;return true;elsereturn false;/ / 设置指定顶点的信息/ / 顶点号/ 要设置的信息/ 成功返回真,否则返回假public bool SetVexInfo(int v,object info)if(vVexNum)Vexsv = info;return true;elsereturn false;/ / 返回v的第一个邻接顶点,若没有则返回-1/ public int FirstAdjVex(int v)for(int j=0;j0)&(this.Arcsv,j.Typeint.MaxValue)return j;return -1;/指定节点vex的(相对于Fvex)下一个邻接顶点,若没有则返回-1public int NextAdjVex(int vex,int Fvex)for(int j=0;j0)&(this.Arcsvex,j.TypeFvex)return j;return -1;public static bool visited;/访问标志数组/ / 深度遍历,递归算法/ public string DFSTraverse()visited = new boolthis.VexNum; /初始化访问标志数组string str =;for(int v=0;vthis.VexNum;v+)visitedv = false;for(int v=0;vthis.VexNum;v+)if(!visitedv)str +=DFS(v);return str;/ / 从第v个顶点出发递归地深度优先遍历/ public string DFS(int v)string str =;visitedv = true;str += + this.Vexsv;for(int i=FirstAdjVex(v);i=0;i=NextAdjVex(v,i)if(!visitedi)str +=DFS(i);return str;/ / 深度优先遍历,非递归算法/ public string DFSTrav()visited = new boolthis.VexNum;/初始化访问标志数组string str =;for(int v=0;vthis.VexNum;v+)visitedv = false;System.Collections.Stack st = new Stack();/初始化辅助栈for(int v=0;v0)int u = (int)st.Pop();for(int w=FirstAdjVex(u);w=0;w=NextAdjVex(u,w)if(!visitedw)visitedw = true;str += +this.Vexsw;st.Push(w);break;return str;/ / 广度优先遍历,非递归算法/ public string BFSTraverse()visited = new boolthis.VexNum;/初始化访问标志数组string str =;for(int v=0;vthis.VexNum;v+)visitedv = false;System.Collections.Queue Q = new Queue();/初始化辅助队列for(int v=0;v0)int u = (int)Q.Dequeue();for(int w=FirstAdjVex(u);w=0;w=NextAdjVex(u,w)if(!visitedw)visitedw = true;str += +this.Vexsw;Q.Enqueue(w);return str;/ / 显示邻接矩阵/ public string Display()string graph = ;for(int i=0;ithis.VexNum;i+)for(int j=0;jthis.ArcNum;j+)graph += + this.Arcsi,j.Type;graph +=n;return graph;/ / 应用程序的主入口点。/ STAThreadstatic void Main(string args)string a=;while(true)Graph g = new Graph(8,9,GKind.UDG);g.SetArcInfo(0,1,1,0);g.SetArcInfo(0,2,1,0);g.SetArcInfo(1,3,1,0);g.SetArcInfo(1,4,1,0);g.SetArcInfo(2,5,1,0);g.SetArcInfo(2,6,1,0);g.SetArcInfo(3,7,1,0);g.SetArcInfo(4,7,1,0);g.SetArcInfo(5,6,1,0);g.SetVexInfo(0,V1);g.SetVexInfo(1,V2);g.SetVexInfo(2,V3);g.SetVexInfo(3,V4);g.SetVexInfo(4,V5);g.SetVexInfo(5,V6);g.SetVexInfo(6,V7);g.SetVexInfo(7,V8);System.Console.WriteLine( 8顶点,9弧无向图的邻接矩阵:n);System.Console.Write(g.Display();System.Console.WriteLine(n 深度优先遍历(递归算法):n);System.Console.WriteLine(g.DFSTraverse();System.Console.WriteLine(n 深度优先遍历(非递归算法):n);System.Console.WriteLine(g.DFSTrav();System.Console.WriteLine(n 广度优先遍历(非递归算法):n);System.Console.WriteLine(g.BFSTraverse();System.Console.WriteLine(n 输入:exit ,退出程序);a = System.Console.ReadLine();if(a =exit)break;if(a.Trim().Length =0 )continue;System.Console.WriteLine( -n);三、 实验数据、结果分析程序运行结果:图如下:V1V2V4V5V8V3V4V7理论结果如下:深度优先遍历:V1 V

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论