示例#1
0
        static void Main(string[] args)
        {
            AdjacencyList <string> a = new AdjacencyList <string>();
            //添加顶点
            string A = "A";
            string B = "B";
            string C = "C";
            string D = "D";

            a.AddVertex(A);
            a.AddVertex(B);
            a.AddVertex(C);
            a.AddVertex(D);
            //添加边
            a.AddEdge(A, B, 0.25, true);
            a.AddEdge(A, C, 0.50, true);
            a.AddEdge(A, D, 0.75, true);
            a.AddEdge(B, D, 1.00, false);
            Console.WriteLine(a.ToString());
            Console.ReadKey();

            //dijkstra1.AdjacencyList<string>.Dijkstra.dijkstra(a.items, 10);//.items.get(1), B);
        }
        static void Main(string[] args)
        {
            //获取配置文件顶点数
            int VertexNum = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["VertexNum"]);
            //Vertex配置文件保存顶点信息,格式为 "(顶点名,顶点id)|(顶点名,顶点id)|...|"
            string VertexDataString = System.Configuration.ConfigurationManager.AppSettings["VertexData"];

            AdjacencyList a = new AdjacencyList(VertexNum);

            string[] strArray = VertexDataString.Split('|');
            foreach (string k in strArray)
            {
                string x = k.Trim('(').Trim(')'); //去掉左右小括号
                //分割顶点的名称和id
                int pos = x.IndexOf(',');
                if (pos < 0)
                {
                    break;
                }
                AdjacencyList.VertexData v = new AdjacencyList.VertexData(x.Substring(0, pos), Convert.ToInt32(x.Substring(pos + 1)));

                //Console.WriteLine("{0}{1}",v.GetVertexName(),v.GetVertexID());
                //添加顶点
                a.AddVertex(v);
            }

            //由配置文件获取加权有向图边的关系,格式暂时为 "(顶点名1,顶点名2,权值,是否是单向)|(顶点名1,顶点名2,权值,是否是单向)|...|"
            string EdgeString = System.Configuration.ConfigurationManager.AppSettings["Edge"];

            strArray = EdgeString.Split('|');
            foreach (string k in strArray)
            {
                string x   = k.Trim('(').Trim(')');
                int    pos = 0;

                pos = x.IndexOf(',');
                if (pos < 0)
                {
                    break;
                }
                AdjacencyList.VertexData VertexData1 = a.Find(x.Substring(0, pos)).data;
                x = x.Remove(0, pos + 1);

                pos = x.IndexOf(',');
                AdjacencyList.VertexData VertexData2 = a.Find(x.Substring(0, pos)).data;
                x = x.Remove(0, pos + 1);

                pos = x.IndexOf(',');
                double weight = Convert.ToDouble(x.Substring(0, pos));
                x = x.Remove(0, pos + 1);

                bool isDirected;
                if (x == "T")
                {
                    isDirected = true;
                }
                else if (x == "F")
                {
                    isDirected = false;
                }
                else
                {
                    throw new ArgumentException("无效的方向标识!");
                }

                //添加边
                a.AddEdge(VertexData1, VertexData2, weight, isDirected);
            }

            //Console.WriteLine(ConString);

            //添加顶点
            //AdjacencyList.VertexData A = new AdjacencyList.VertexData("A", 1);
            //AdjacencyList.VertexData B = new AdjacencyList.VertexData("B", 2);
            //AdjacencyList.VertexData C = new AdjacencyList.VertexData("C", 3);
            //AdjacencyList.VertexData D = new AdjacencyList.VertexData("D", 4);
            //AdjacencyList.VertexData E = new AdjacencyList.VertexData("E", 5);
            //AdjacencyList.VertexData F = new AdjacencyList.VertexData("F", 6);

            //a.AddVertex(A);
            //a.AddVertex(B);
            //a.AddVertex(C);
            //a.AddVertex(D);
            //a.AddVertex(E);
            //a.AddVertex(F);
            //添加边
            //a.AddEdge(A, B, 3, true);
            //a.AddEdge(A, C, 10, true);
            //a.AddEdge(C, F, 5, true);
            //a.AddEdge(B, D, 2, true);
            //a.AddEdge(D, E, 4, true);
            //a.AddEdge(E, C, 2, true);
            //a.AddEdge(E, F, 7, true);

            Console.WriteLine(a.ToString());
            Console.ReadKey();

            string StartVertexName = System.Configuration.ConfigurationManager.AppSettings["StartVertexName"];
            string EndVertexName   = System.Configuration.ConfigurationManager.AppSettings["EndVertexName"];

            RouteDijkstra.DijkstraShortestPath(a, VertexNum, a.Find(StartVertexName).data, a.Find(EndVertexName).data);
        }