仿造<<Java常用算法手册>>里面对的算法,使用C#实现了一遍. 理论知识我就不讲解了,在这本书里面已经写的非常完美!
代码如何下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 图结构 { public class GraphMatrix { public static int MaxNum = 4; //最大顶点 public static int MaxValue = 65535; //最大权值 public char[] Vertex = new char[MaxNum]; //最多的顶点 public int GraphType; //图类型0 无向图,1 有向图 public int VertexNum; //顶点个数 public int EdgeNum; //边的个数 public int[][] EdgeWeight = new int[MaxNum][]; //每条边的权值 int[] isTrav = new int[MaxNum]; //是否已经遍历过该顶点 public GraphMatrix() { for (int i = 0; i < EdgeWeight.Length; i++) { EdgeWeight[i] = new int[MaxNum]; } } //清空图 public void ClearGraph() { for (int i = 0; i < this.VertexNum; i++) { for (int j = 0; j < this.VertexNum; j++) { this.EdgeWeight[i][j] = GraphMatrix.MaxValue; } } } //输出图的关系 public void OutGraph() { Console.WriteLine("\n图顶点的关系"); Console.Write(" "); for (int i = 0; i < VertexNum; i++) { Console.Write(Vertex[i]+" "); } Console.WriteLine(); for (int i = 0; i < VertexNum; i++) { Console.Write(Vertex[i] + " "); for (int j = 0; j < VertexNum; j++) { if (EdgeWeight[i][j] == GraphMatrix.MaxValue) Console.Write("Z "); else Console.Write(EdgeWeight[i][j] + " "); } Console.WriteLine(); } } //遍历图 public void DeepTraGraph() { for (int i = 0; i < VertexNum; i++) { this.isTrav[i] = 0; } Console.Write("深度优先遍历节点: "); for (int i = 0; i < VertexNum; i++) { if (isTrav[i] == 0) { DeepTraOne(i); } } Console.WriteLine(); } //深度优先法 protected void DeepTraOne(int n) { isTrav[n] = 1; Console.Write(Vertex[n]); //遍历该节点是否跟其他节点有关联 for (int i = 0; i < VertexNum; i++) { if(EdgeWeight[n][i] != GraphMatrix.MaxValue && isTrav[n] == 0) { DeepTraOne(i); } } } } }
控制台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 图结构 { class Program { static void Main(string[] args) { GraphMatrix gm = new GraphMatrix(); gm.GraphType = 0; CreateGraphDemo(gm); gm.DeepTraGraph(); gm.OutGraph(); Console.ReadLine(); } static void CreateGraphDemo(GraphMatrix gm) { gm.VertexNum = 4; gm.EdgeNum = 4; gm.Vertex[0] = ‘A‘; gm.Vertex[1] = ‘B‘; gm.Vertex[2] = ‘C‘; gm.Vertex[3] = ‘D‘; SetInfo(gm, ‘A‘, ‘B‘, 5); Console.WriteLine("第一次"); WriterInfo(gm); SetInfo(gm, ‘B‘, ‘C‘, 5); Console.WriteLine("第二次"); WriterInfo(gm); SetInfo(gm, ‘C‘, ‘D‘, 5); Console.WriteLine("第三次"); WriterInfo(gm); SetInfo(gm, ‘D‘, ‘A‘, 5); Console.WriteLine("第四次"); WriterInfo(gm); } static void SetInfo(GraphMatrix gm,int eStartV = 0,int eEndV = 0,int weight = 0) { int len = gm.Vertex.Length; for (int i = 0; i < len; i++) { if (gm.Vertex[i] == eStartV) { for (int j = 0; j < len; j++) { if (gm.Vertex[j] == eEndV) { //赋值权重 gm.EdgeWeight[i][j] = weight; if (gm.GraphType == 0) { gm.EdgeWeight[j][i] = weight; } } } } } } static void WriterInfo(GraphMatrix gm) { for (int i = 0; i < gm.EdgeWeight.Length; i++) { for (int j = 0; j < gm.EdgeWeight[i].Length; j++) { Console.Write(gm.EdgeWeight[i][j] + " "); } Console.WriteLine(); } Console.WriteLine(); } } }
效果图:
时间: 2024-10-20 17:15:15