N久都没做过关于C#的WinForm程序了,一直都是在研究asp.net的程序。
今天有个朋友问到深度遍历图这样的问题,开始都不知道如何下手,就问了问baidu 和 google,看到有人用C++写的这样的例子,顺便就学习了一下,发现自己都忘得差不多了(包括:数据结构),只能联想到刚开始学vs2003的时候,学习的第一个Hello Worl的例子,要创建一个控制台应用程序。
接着就打开VS2005,新建——>项目——>控制台应用程序
代码如下:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string args)
- {
- Graph graph = new Graph();
- graph.addVertex('A');
- graph.addVertex('B');
- graph.addVertex('C');
- graph.addVertex('D');
- graph.addVertex('E');
- graph.addVertex('F');
- graph.addVertex('G');
- graph.addVertex('H');
- graph.addEdge('A', 'B');
- graph.addEdge('B', 'D');
- graph.addEdge('B', 'E');
- graph.addEdge('E', 'H');
- graph.addEdge('D', 'H');
- graph.addEdge('A', 'C');
- graph.addEdge('C', 'F');
- graph.addEdge('F', 'G');
- graph.addEdge('C', 'G');
- Console.Write("深度遍历结果:");
- graph.dfs();
- Console.WriteLine();
- }
- }
- class Vertex
- {
- public Vertex(char label)
- {
- _label = label;
- wasVisited = false;
- }
- public char _label;
- public bool wasVisited;
- }
- class Graph
- {
- private static int flag = 1;
- private int max_vertexs = 20;//最大顶点数
- private Vertex vertexList;
- private int[,] adjMat;
- private int countVertexs;
- private Stack thestack;
- public Graph()
- {
- vertexList = new Vertex[max_vertexs];
- adjMat = new int[max_vertexs, max_vertexs];
- countVertexs = 0;
- for (int j = 0; j < max_vertexs; j++)
- for (int k = 0; k < max_vertexs; k++)
- adjMat[j, k] = 0;
- thestack = new Stack(max_vertexs);
- }
- //初始添加点数
- public void addVertex(char label)
- {
- vertexList[countVertexs++] = new Vertex(label);
- }
- public void addEdge(int start, int end)
- {
- adjMat[start, end] = 1;
- adjMat[end, start] = 1;
- }
- public void addEdge(char startV, char endV)
- {
- int start = -1, end = -1;
- for (int i = 0; i < countVertexs; i++)
- {
- if (startV == vertexList[i]._label) start = i;
- if (endV == vertexList[i]._label) end = i;
- }
- if (start == -1) Console.WriteLine("顶点{0}不存在", startV);
- if (end == -1) Console.WriteLine("顶点{0}不存在", endV);
- //权值默认为1
- adjMat[start, end] = 1;
- adjMat[end, start] = 1;
- }
- //显示字符
- public void displayVertex(int v)
- {
- if (flag == 1)
- {
- Console.Write(vertexList[v]._label);
- }
- else
- {
- Console.Write("," + vertexList[v]._label);
- }
- flag++;
- }
- //深度优先遍历
- public void dfs()
- {
- vertexList[0].wasVisited = true;
- displayVertex(0);
- thestack.Push(0);
- //遍历结点
- while (thestack.Count!=0)
- {
- //从第v个顶点出发递归地深度优先遍历图 (读取栈里的第一个元素,但是不出栈)
- int v = getAdjUnvisitedVertex(Int32.Parse((thestack.Peek().ToString())));
- if (v == -1)
- //元素出栈
- thestack.Pop();
- else
- {
- vertexList[v].wasVisited = true;
- displayVertex(v);
- //元素进栈
- thestack.Push(v);
- }
- }
- //初始化所有的顶点状态为未被访问
- for (int j = 0; j < countVertexs; j++)
- vertexList[j].wasVisited = false;
- }
- public int getAdjUnvisitedVertex(int v)
- {
- for (int j = 0; j < countVertexs; j++)
- if (adjMat[v, j] == 1 && vertexList[j].wasVisited == false)
- return j;
- return -1;
- }
- }
- }
结果如图: