C#深度优先遍历结构算法

来源:互联网 发布:淘宝国产手机 编辑:程序博客网 时间:2024/06/02 06:43
今天有个朋友问到深度遍历图这样的问题,开始都不知道如何下手,就问了问baidu 和 google,看到有人用C++写的这样的例子,顺便就学习了一下,发现自己都忘得差不多了(包括:数据结构),只能联想到刚开始学vs2003的时候,学习的第一个Hello Worl的例子,要创建一个控制台应用程序。(文/zhangfeng)

    接着就打开VS2005,新建——>项目——>控制台应用程序

  代码如下:
  1.   1 using System;
  2.   2 using System.Collections.Generic;
  3.   3 using System.Text;
  4.   4 using System.Collections;
  5.   5 
  6.   6 namespace ConsoleApplication2
  7.   7 {
  8.   8    class Program
  9.   9    {
  10. 10        static void Main(string[] args)
  11. 11        {
  12. 12            Graph graph = new Graph();
  13. 13            graph.addVertex('A');
  14. 14            graph.addVertex('B');
  15. 15            graph.addVertex('C');
  16. 16            graph.addVertex('D');
  17. 17            graph.addVertex('E');
  18. 18            graph.addVertex('F');
  19. 19            graph.addVertex('G');
  20. 20            graph.addVertex('H');
  21. 21            graph.addEdge('A', 'B');
  22. 22            graph.addEdge('B', 'D');
  23. 23            graph.addEdge('B', 'E');
  24. 24            graph.addEdge('E', 'H');
  25. 25            graph.addEdge('D', 'H');
  26. 26            graph.addEdge('A', 'C');
  27. 27            graph.addEdge('C', 'F');
  28. 28            graph.addEdge('F', 'G');
  29. 29            graph.addEdge('C', 'G');
  30. 30            Console.Write("深度遍历结果:");
  31. 31            graph.dfs();
  32. 32            Console.WriteLine();
  33. 33        }
  34. 34    }
  35. 35 
  36. 36    class Vertex
  37. 37    {
  38. 38        public Vertex(char label)
  39. 39        {
  40. 40            _label = label;
  41. 41            wasVisited = false;
  42. 42        }
  43. 43 
  44. 44 
  45. 45        public char _label;
  46. 46        public bool wasVisited;
  47. 47    }
  48. 48 
  49. 49    class Graph
  50. 50    {
  51. 51        private static int flag = 1;
  52. 52        private int max_vertexs = 20;//最大顶点数
  53. 53        private Vertex[] vertexList;
  54. 54        private int[,] adjMat;
  55. 55        private int countVertexs;
  56. 56        private Stack thestack;
  57. 57        public Graph()
  58. 58        {
  59. 59            vertexList = new Vertex[max_vertexs];
  60. 60            adjMat = new int[max_vertexs, max_vertexs];
  61. 61            countVertexs = 0;
  62. 62            for (int j = 0; j < max_vertexs; j++)
  63. 63                for (int k = 0; k < max_vertexs; k++)
  64. 64                    adjMat[j, k] = 0;
  65. 65            thestack = new Stack(max_vertexs);
  66. 66        }
  67. 67        //初始添加点数
  68. 68        public void addVertex(char label)
  69. 69        {
  70. 70            vertexList[countVertexs++] = new Vertex(label);
  71. 71        }
  72. 72 
  73. 73        public void addEdge(int start, int end)
  74. 74        {
  75. 75            adjMat[start, end] = 1;
  76. 76            adjMat[end, start] = 1;
  77. 77        }
  78. 78 
  79. 79        public void addEdge(char startV, char endV)
  80. 80        {
  81. 81            int start = -1, end = -1;
  82. 82            for (int i = 0; i < countVertexs; i++)
  83. 83            {
  84. 84                if (startV == vertexList._label) start = i;
  85. 85                if (endV == vertexList._label) end = i;
  86. 86            }
  87. 87            if (start == -1) Console.WriteLine("顶点{0}不存在", startV);
  88. 88            if (end == -1) Console.WriteLine("顶点{0}不存在", endV);
  89. 89            //权值默认为1 
  90. 90            adjMat[start, end] = 1;
  91. 91            adjMat[end, start] = 1;
  92. 92        }
  93. 93 
  94. 94        //显示字符
  95. 95        public void displayVertex(int v)
  96. 96        {
  97. 97            if (flag == 1)
  98. 98            {
  99. 99                Console.Write(vertexList[v]._label);
  100. 100            }
  101. 101            else
  102. 102            {
  103. 103                Console.Write("," + vertexList[v]._label);
  104. 104            }
  105. 105            flag++;
  106. 106        }
  107. 107        //深度优先遍历
  108. 108        public void dfs()
  109. 109        {
  110. 110            vertexList[0].wasVisited = true;
  111. 111            displayVertex(0);
  112. 112            thestack.Push(0);
  113. 113            //遍历结点
  114. 114            while (thestack.Count!=0)
  115. 115            {
  116. 116                //从第v个顶点出发递归地深度优先遍历图  (读取栈里的第一个元素,但是不出栈)
  117. 117                int v = getAdjUnvisitedVertex(Int32.Parse((thestack.Peek().ToString())));
  118. 118                if (v == -1)
  119. 119                    //元素出栈
  120. 120                    thestack.Pop();
  121. 121                else
  122. 122                {
  123. 123                    vertexList[v].wasVisited = true;
  124. 124                    displayVertex(v);
  125. 125                    //元素进栈
  126. 126                    thestack.Push(v);
  127. 127                }
  128. 128            }
  129. 129            //初始化所有的顶点状态为未被访问
  130. 130            for (int j = 0; j < countVertexs; j++)
  131. 131                vertexList[j].wasVisited = false;
  132. 132        }
  133. 133 
  134. 134        public int getAdjUnvisitedVertex(int v)
  135. 135        {
  136. 136            for (int j = 0; j < countVertexs; j++)
  137. 137                if (adjMat[v, j] == 1 && vertexList[j].wasVisited == false)
  138. 138                    return j;
  139. 139            return -1;
  140. 140        }
  141. 141    }
  142. 142 }
  143. 143 
复制代码
结果如图:

原创粉丝点击