Ejemplo n.º 1
0
        //public GraphWeightMy()
        //{

        //}

        public override void AddVertex(T item)
        {
            //如果个数超过了数组界限,那么对数组进行扩容
            if (NumVertex == _vertexMys.Length)
            {
                int[,] temp = new int[NumVertex + 1, NumVertex + 1];
                for (int i = 0; i < NumVertex; i++)
                {
                    for (int j = 0; j < NumVertex; j++)
                    {
                        temp[i, j] = _adjMatrix[i, j];
                    }
                }

                _adjMatrix = temp;

                VertexMy <T>[] tempVer = new VertexMy <T> [NumVertex + 1];
                for (int i = 0; i < NumVertex; i++)
                {
                    tempVer[i] = _vertexMys[i];
                }

                _vertexMys = tempVer;
            }

            //将最新的顶点加入进来,同时更新现在的顶点个数
            _vertexMys[NumVertex] = new VertexMy <T>(item);
            NumVertex++;
        }
Ejemplo n.º 2
0
 public GraphWeightMy(int num) : base(num)
 {
     NumVertex  = 0;
     _vertexMys = new VertexMy <T> [num];
     _adjMatrix = new int[num, num];
     for (int i = 0; i < num; i++)
     {
         for (int j = 0; j < num; j++)
         {
             if (i == j)
             {
                 _adjMatrix[i, j] = 0;
             }
             else
             {
                 _adjMatrix[i, j] = int.MaxValue;
             }
         }
     }
 }
Ejemplo n.º 3
0
 private void DisplayPath(int startIndex, int[] distanceMatrix)
 {
     for (int i = 0; i < NumVertex; i++)
     {
         Console.Write("从顶点{0}到达顶点{1}的最短路径是:", _vertexMys[startIndex].Label, _vertexMys[i].Label);
         if (distanceMatrix[i] == int.MaxValue)
         {
             Console.WriteLine("无法到达");
         }
         else
         {
             VertexMy <T> temp = _vertexMys[i];
             Console.Write(_vertexMys[startIndex].Label);
             string tempString = null;
             while (temp.Parent != null)
             {
                 tempString = "--" + temp.Label + tempString;
                 temp       = temp.Parent;
             }
             Console.Write(tempString);
             Console.WriteLine("    最短路径的距离为:{0}", distanceMatrix[i]);
         }
     }
 }