Exemplo n.º 1
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="vertex">Вершина</param>
 public GraphVertexInfo(GraphVertex vertex)
 {
     Vertex         = vertex;
     IsUnvisited    = true;
     EdgesWeightSum = int.MaxValue;
     PreviousVertex = null;
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Формирование пути
        /// </summary>
        /// <param name="startVertex">Начальная вершина</param>
        /// <param name="endVertex">Конечная вершина</param>
        /// <returns>Путь</returns>
        int GetPath(GraphVertex startVertex, GraphVertex endVertex)
        {
            if (startVertex == null || endVertex == null)
            {
                return(-1);
            }

            var path = 0;

            while (startVertex != endVertex)
            {
                endVertex = GetVertexInfo(endVertex).PreviousVertex;

                path++;
            }

            return(path);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Поиск кратчайшего пути по вершинам
        /// </summary>
        /// <param name="startVertex">Стартовая вершина</param>
        /// <param name="finishVertex">Финишная вершина</param>
        /// <returns>Кратчайший путь</returns>
        public int 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));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="connectedVertex">Связанная вершина</param>
 /// <param name="weight">Вес ребра</param>
 public GraphEdge(GraphVertex connectedVertex, int weight)
 {
     ConnectedVertex = connectedVertex;
     EdgeWeight      = weight;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Добавить ребро
 /// </summary>
 /// <param name="vertex">Вершина</param>
 /// <param name="edgeWeight">Вес</param>
 public void AddEdge(GraphVertex vertex, int edgeWeight)
 {
     AddEdge(new GraphEdge(vertex, edgeWeight));
 }