C# 图结构操作
2016-02-15 17:12
555 查看
仿造<<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 = 1; Console.Write(Vertex ); //遍历该节点是否跟其他节点有关联 for (int i = 0; i < VertexNum; i++) { if(EdgeWeight [i] != GraphMatrix.MaxValue && isTrav == 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(); } } }
效果图:
相关文章推荐
- 从0自学C#05--窗体之间的相互访问
- C#图片添加水印的实现代码
- c#中new关键字的用处
- c#委托
- C# WebService (二)发布与IIS配置
- c# - Cache Code
- C# 使用属性实现位状态结构体
- 系统时间自动同步服务导致C#定时器失效的问题
- C#实现知乎登录、点赞、关注、退出、评论(一)登录
- C# datagridview 从excel导入数据后时间的显示
- C#实现自己的Ctr+V
- c# 创建只接收消息的窗口
- C# .ToString() 格式化
- 单例模式2
- C#成神之路<20> C#多态继承
- C# WINFORM ListView用法详解(转)
- C# WInform 界面左导航菜单
- C#调用系统注册程序打开pdf文件
- VS2013 自动添加头部注释 -C#开发
- C# 线程同步示例