コード例 #1
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="vertex">Вершина</param>
 public GraphVertexInfo(GraphVertex vertex)
 {
     Vertex         = vertex;
     IsUnvisited    = true;
     EdgesWeightSum = int.MaxValue;
     PreviousVertex = null;
 }
コード例 #2
0
        /// <summary>
        /// Генерація шляху
        /// </summary>
        /// <param name="startVertex">Початкова вершина</param>
        /// <param name="endVertex">Кінцева вершина</param>
        /// <returns>Шлях</returns>
        string GetPath(GraphVertex startVertex, GraphVertex endVertex)
        {
            var path = endVertex.ToString();

            while (startVertex != endVertex)
            {
                endVertex = GetVertexInfo(endVertex).PreviousVertex;
                path      = endVertex.ToString() + path;
            }

            return(path);
        }
コード例 #3
0
        /// <summary>
        /// Отримання інформації про вершину
        /// </summary>
        /// <param name="v">Вершина</param>
        /// <returns>Інформаія про вершину</returns>
        GraphVertexInfo GetVertexInfo(GraphVertex v)
        {
            foreach (var i in infos)
            {
                if (i.Vertex.Equals(v))
                {
                    return(i);
                }
            }

            return(null);
        }
コード例 #4
0
        /// <summary>
        /// Пошук найкоротшого шляху по вершинах
        /// </summary>
        /// <param name="startVertex">Стартова вершина</param>
        /// <param name="finishVertex">Фінішна вершина</param>
        /// <returns>Кратчайший путь</returns>
        public string FindShortestPath(GraphVertex startVertex, GraphVertex finishVertex)
        {
            InitInfo();
            var first = GetVertexInfo(startVertex);

            first.EdgesWeightSum = 0;
            while (true)
            {
                var current = FindUnvisitedVertexWithMinSum();
                if (current == null)
                {
                    break;
                }

                SetSumToNextVertex(current);
            }

            return(GetPath(startVertex, finishVertex));
        }
コード例 #5
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="connectedVertex">Звязана вершина</param>
 /// <param name="weight">Вага ребра</param>
 public GraphEdge(GraphVertex connectedVertex, int weight)
 {
     ConnectedVertex = connectedVertex;
     EdgeWeight      = weight;
 }
コード例 #6
0
 /// <summary>
 /// Додати ребро
 /// </summary>
 /// <param name="vertex">Вершина</param>
 /// <param name="edgeWeight">Вага</param>
 public void AddEdge(GraphVertex vertex, int edgeWeight)
 {
     AddEdge(new GraphEdge(vertex, edgeWeight));
 }