深度优先搜索.docx_第1页
深度优先搜索.docx_第2页
深度优先搜索.docx_第3页
深度优先搜索.docx_第4页
全文预览已结束

下载本文档

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

文档简介

深度优先搜索private void DFS(int i, ref string DFSResult) /深?度优?先搜?索递Y归函数y vertexListi.Visited = true; DFSResult += vertexListi.VertexName + n; EdgeNode p = vertexListi.FirstNode; while (p != null) if (vertexListp.Index.Visited = true) p = p.Next; else DFS(p.Index, ref DFSResult); / / 得?到?深?度优?先搜?索序列D / / 进?行D深?度优?先搜?索的?起e始?点?名?称? / 深?度优?先搜?索序列D public string DFSTraversal(string StartVertexName) string DFSResult = string.Empty; int i = GetIndex(StartVertexName); if (i != -1) for (int j = 0; j vertexCount; j+) vertexListj.Visited = false; DFS(i, ref DFSResult); return DFSResult; 广度优先搜索 public string BFSTraversal(string StartNodeName) string BFSResult = string.Empty; int i = GetIndex(StartNodeName); if (i != -1) for (int j = 0; j vertexCount; j+) vertexListj.Visited = false; vertexListi.Visited = true; BFSResult += vertexListi.VertexName + n; LinkQueue LQ = new LinkQueue(); LQ.EnQueue(i); while(LQ.IsEmpty()=false) int j = LQ.QueueFront; LQ.DeQueue(); EdgeNode p = vertexListj.FirstNode; while (p != null) if (vertexListp.Index.Visited = false) vertexListp.Index.Visited = true; BFSResult += vertexListp.Index.VertexName + n; p = p.Next; return BFSResult; / / 得?到?连?通网?的?最?小?生成树 / / 树根结点? / 连?通网?的?最?小?生成树 public SpanTreeNode MiniSpanTree(string vName) SpanTreeNode SpanTree = null; int i = GetIndex(vName); if (i != -1) SpanTree = new SpanTreeNodevertexCount; int count = 0; SpanTreecount = new SpanTreeNode(vertexListi.VertexName, NULL, 0.0); count+; Aid aid = new AidvertexCount; for (int j = 0; j vertexCount; j+) aidj.LowCost = double.MaxValue; aidj.VertexIndex = i; EdgeNode p1 = vertexListi.FirstNode; while (p1 != null) aidp1.Index.LowCost = p1.Weight; p1 = p1.Next; aidi.VertexIndex = -1; while (count vertexCount) double min = double.MaxValue; int v = i; for (int k = 0; k vertexCount; k+) if (aidk.VertexIndex != -1 & aidk.LowCost min) min = aidk.LowCost; v = k; SpanTreecount = new SpanTreeNode(vertexListv.VertexName, vertexListaidv.VertexIndex.VertexName, min); aidv.VertexIndex = -1; count+; EdgeNode p2 = vertexListv.FirstNode; while (p2 != null) if (aidp2.Index.VertexIndex != -1 & p2.Weight aidp2.Index.LowCost) aidp2.Index.LowCost = p2.Weight; aidp2.Index.VertexIndex = v; p2 = p2.Next; return SpanTree; / / 单源最?短路径? / / 寻找最?短路径?的?源点? / 源点?到?各个?顶点?的?最?短路径? public string ShortestPath(string vName) string Result = string.Empty; int v = GetIndex(vName); if (v != -1) double Dist = new doublevertexCount; string Path = new stringvertexCount; /初?始?化 for (int i = 0; i vertexCount; i+) vertexListi.Visited = false; Disti = double.MaxValue; Pathi = vertexListv.VertexName; Distv = 0.0; vertexListv.Visited = true; for (int i = 0; i vertexCount - 1; i+) EdgeNode p = vertexListv.FirstNode; while (p != null) if (vertexListp.Index.Visited = false & Distv + p.Weight + vertexListp.Index.VertexName; p = p.Next; double min = double.MaxValue; for (int j = 0; j vertexCount; j+) if (vertexListj.Visited = false & Distj m

温馨提示

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

评论

0/150

提交评论