标签:
/* 问题描述:你必须以图的形式来表示一系列城市和他们之间的距离.编写一个程序来以邻接矩阵的形式表示图; */ using System; using System.Collections.Generic; using System.Text; namespace GraphsUsingAdjMatrixInCSharp { class Graph //图类 { private string[] vertices = new string[10]; //定义一个字符串数组,用来存储各个顶点 private int[,] cost = new int[10, 10]; //定义一个邻接矩阵,来存储相应的边.(即顶点与顶点之间的距离.) private int n; //n为顶点的数量. public Graph() //构造函数 { n = 0; //初始化有0个顶点 for (int i = 0; i < 10; i++) //i:行 for (int j = 0; j < 10; j++) //j:列 { cost[i, j] = 0; } }//设置初始邻接矩阵的内容均为0 private bool edgeExists() //判断边是否存在 { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) if (cost[i, j] != 0) //如果邻接矩阵存储的内容!=0,则返回真. return (true); return (false); //否则,即邻接矩阵的内容为0,返回假 } public void addVertex() //添加有n个城市方法. { string vname; //vname:顶点名称 Console.Write("请输入城市的个数(<10):"); n=Convert.ToInt32(Console.ReadLine()); for(int k=0;k<n;k++) { Console.Write("\n请输入城市: "); vname = Console.ReadLine(); int i = getIndex(vname); //获得顶点的索引 if (i != -1) { Console.WriteLine("\n城市已经存在."); return; } vertices[k] = vname; //将输入的城市名,作为图的顶点放入顶点数组. } } private int getIndex(String vname) //获得顶点索引方法 { for (int i = 0; i < n; i++) if (vertices[i] == vname) return (i); //如果顶点在数组中存在,则返回相应的索引. return (-1); /*如果顶点在数组中不存在,则返回-1;*/ } public void addEdge() //增加边方法;城市之间距离。 { string v1, v2; //v1,v2:始发城市、目标城市 int i1, i2; //i1,i2:获得始发城市、目标城市的索引 if (n == 0) { Console.WriteLine("\n没有城市存在,必须先创建城市."); return; } while (true) { Console.Write("\n请输入始发城市: "); v1 = Console.ReadLine(); i1 = getIndex(v1); if (i1 == -1) //存在,则退出循环. Console.WriteLine("\n出发城市不存在,请重试."); else break; //如果存在则退出循环;否则提示错误信息 } while (true) //同上,判断v2是否存在 { Console.Write("\n请输入目标城市: "); v2 = Console.ReadLine(); i2 = getIndex(v2); if (i2 == -1) Console.WriteLine("\n目标城市不存在,请重试."); else break; } Console.Write("\n请输入距离长度: "); cost[i1, i2] = Convert.ToInt32(Console.ReadLine()); //设置i1,i2:位置为你从v1到v2的距离. } public void display() //显示图的顶点和边的情况. { if (n == 0) { Console.WriteLine("\n图不存在."); return; } Console.WriteLine("\n城市:"); for (int i = 0; i < n; i++) Console.WriteLine(vertices[i]); if (edgeExists()) //边存在,则显示始发城市到目标城市的距离. { Console.WriteLine("\n距离:"); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (cost[i, j] != 0) Console.WriteLine(vertices[i] + "->" + vertices[j] + " -- " + cost[i, j]); } } } class Graph_Program { static void Main(string[] args) { Graph g = new Graph(); char ch; do { Console.WriteLine(); Console.WriteLine("菜单"); Console.WriteLine("1. 添加城市"); Console.WriteLine("2. 添加距离"); Console.WriteLine("3. 显示");; Console.WriteLine("4. 退出"); Console.WriteLine(); Console.Write("请输入您的选择: "); ch = Convert.ToChar(Console.ReadLine()); switch (ch) { case '1': g.addVertex(); break; case '2': g.addEdge(); break; case '3': g.display(); break; case '4': break; default: Console.WriteLine("无效选择."); break; } } while (ch != '4'); } } }
/*扩充活动1,找出给定城市到所有城市之间的最短距离;迪杰斯特拉算法(Dijkstra)*/ using System; using System.Collections.Generic; using System.Text; namespace GraphsUsingAdjMatrixInCSharp { class Graph { private string[] vertices = new string[10]; //定义一个字符串数组,用来存储各个顶点 private int[,] cost = new int[10, 10]; //定义一个邻接矩阵,来存储相应的边.(即顶点与顶点之间的距离.) private int n; //n为顶点的数量. int infinity = 9999999; //infinity:非常大的一个值, public Graph() { n = 0; //n:个顶点 for (int i = 0; i < 10; i++) //行 for (int j = 0; j < 10; j++) //列 { if (i == j) cost[i, j] = 0; else cost[i, j] = infinity; //infinity indicates that there is no edge from vertex i to vertex j } } private bool edgeExists()//判断边是否存在 { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) if ((cost[i,j] != 0) && (cost[i,j] != infinity )) //如果邻接矩阵存储的内容!=0,则返回真. return (true); return (false);//否则,即邻接矩阵的内容为0,返回假 } public void addVertex() //添加有n个城市方法. { string vname; //vname:顶点名称 Console.Write("请输入城市的个数(小于10):"); n=Convert.ToInt32(Console.ReadLine()); for(int k=0;k<n;k++) { Console.Write("请输入城市: "); vname = Console.ReadLine(); int i = getIndex(vname); //获得顶点的索引 if (i != -1) { Console.WriteLine("\n城市已经存在."); return; } vertices[k] = vname; //将输入的城市名,作为图的顶点放入顶点数组. } } private int getIndex(String vname)//获得顶点索引方法 { for (int i = 0; i < n; i++) if (vertices[i] == vname) return (i);//如果顶点在数组中存在,则返回相应的索引. return (-1); /*如果顶点在数组中不存在,则返回-1;*/ } public void addEdge()//增加边方法. { string v1, v2; int i1, i2; if (n == 0) { Console.WriteLine("\n没有城市存在,必须先创建城市."); return; } while (true) { Console.Write("\n请输入始发城市: "); v1 = Console.ReadLine(); i1 = getIndex(v1); if (i1 == -1)//存在,则退出循环. Console.WriteLine("\n出发城市不存在,请重试."); else break; } while (true)//同上,判断v2是否存在 { Console.Write("\n请输入目标城市: "); v2 = Console.ReadLine(); i2 = getIndex(v2); if (i2 == -1) Console.WriteLine("\n目标城市不存在,请重试."); else break; } Console.Write("\n请输入距离长度: "); cost[i1, i2] = Convert.ToInt32(Console.ReadLine());//设置i1,i2:位置为你从v1到v2的距离. } public void display()//显示图的顶点和边的情况. { if (n == 0) { Console.WriteLine("\n图不存在."); return; } Console.WriteLine("\n城市:"); for (int i = 0; i < n; i++) Console.WriteLine(vertices[i]); if (edgeExists())//边存在,则显示始发城市到目标城市的距离. { Console.WriteLine("\n距离:"); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if ((cost[i, j] != 0) && (cost[i, j] != infinity)) Console.WriteLine(vertices[i] + "->" + vertices[j] + " - " + cost[i, j]); } } public void findShortestPath() //寻找最短路径. { int[] Distance = new int[10]; //10个距离的数组 bool[] Final = new bool[10]; //10个顶点. string source; //起始顶点. int src; //起始顶点标志 if (n == 0) { Console.WriteLine("\n图不存在,您必须先插入顶点和边"); return; } while (true) { Console.WriteLine("\n请键入起始顶点: "); source = Console.ReadLine(); src = getIndex(source); if (src == -1) Console.WriteLine("\n起始顶点不存在"); else break; } /*初始化距离数组*/ for (int i = 0; i < n; i++) Distance[i] = cost[src, i]; //初始化:起始顶点到其他顶点的距离 Final[src] = true; //Final[0]=1 for (int i = 1; i < n; i++) //n-1循环 { /*查找最短距离.*/ int v = 0; /* Find a vertex that is not there in Final. Store its index in v */ for (int j = 0; j < n; j++) { if (Final[j] == false) { v = j; break; } } /*在顶点数组中找到距离顶点最小的顶点位置*/ for (int j = 0; j < n; j++) { if ((Final[j] == false) && (Distance[j] < Distance[v])) v = j; } Final[v] = true; /* Compute the distance of all nodes via node with index min */ for (int w = 0; w < n; w++) { if (Final[w] == false) //Final[1]==false { if (Distance[v] + cost[v, w] < Distance[w]) //Distance[3]+cost[3,1]=3+~ <Distance[1]=5 结果不成立. Distance[w] = Distance[v] + cost[v, w]; } } } Console.WriteLine("\n顶点集合中的最小路径为" + source + "是: "); for (int j = 0; j < n; j++) { if (Distance[j] == infinity) Console.WriteLine(source + " -> " + vertices[j] + "=没有路径"); else Console.WriteLine(source + " -> " + vertices[j] + " = " + Distance[j]); } } } class Graph_Program { static void Main(string[] args) { Graph g = new Graph(); char ch; do { Console.WriteLine(); Console.WriteLine("菜单"); Console.WriteLine("1. 添加城市"); Console.WriteLine("2. 添加距离"); Console.WriteLine("3. 显示"); Console.WriteLine("4.从给定的顶点中寻找最短路径"); Console.WriteLine("4. 退出"); Console.WriteLine(); Console.Write("请输入您的选择: "); ch = Convert.ToChar(Console.ReadLine()); switch (ch) { case '1': g.addVertex(); break; case '2': g.addEdge(); break; case '3': g.display(); break; case '4': g.findShortestPath(); break; case '5': break; default: Console.WriteLine("无效选择."); break; } } while (ch != '5'); } } }
标签:
原文地址:http://blog.csdn.net/zhangchen124/article/details/51678312