Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="vertex"></param>
        /// <returns></returns>
        private GraphVertexInfo GetGraphVertexInfo(GraphVertex vertex)
        {
            foreach (var item in infos)
            {
                if (item.Vertex.Equals(vertex))
                {
                    return(item);
                }
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="startVertex"></param>
        /// <param name="finishVertex"></param>
        /// <returns></returns>
        public string FindShortestPath(GraphVertex startVertex, GraphVertex finishVertex)
        {
            InitInfo();

            var first = GetGraphVertexInfo(startVertex);

            first.EdgesWeightSum = 0;

            var current = FindUnvisitedVertexWithSum();

            while (current != null)
            {
                SetSumToNextVertex(current);
                current = FindUnvisitedVertexWithSum();
            }

            return(GetPath(startVertex, finishVertex));
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="startVertex"></param>
        /// <param name="endVertex"></param>
        /// <returns></returns>
        private string GetPath(GraphVertex startVertex, GraphVertex endVertex)
        {
            try
            {
                var    path = endVertex.ToString();
                double sum  = 0;

                while (startVertex != endVertex)
                {
                    double weight     = 0;
                    string nameOfPrev = endVertex.Name;
                    endVertex = GetGraphVertexInfo(endVertex).PreviousVertex;

                    for (int i = 0; i < endVertex.Edges.Count; ++i)
                    {
                        if (nameOfPrev == endVertex.Edges[i].ConnectedVertex.Name)
                        {
                            weight = endVertex.Edges[i].EdgeWeight;
                            sum   += weight;
                            break;
                        }
                    }

                    path = $"{endVertex.ToString()} ({weight}) -> {path}";
                }

                return(path + $"\n\nВес общего пути: {sum}");
            }
            catch (Exception ex)
            {
                NotFoundPathExeption exeption = new NotFoundPathExeption
                {
                    StartVertex = startVertex.Name,
                    EndVertex   = endVertex.Name
                };

                throw exeption;
            }
        }
Beispiel #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="vertex"></param>
 /// <param name="weight"></param>
 public void AddEdge(GraphVertex vertex, double weight)
 {
     AddEdge(new GraphEdge(vertex, weight));
 }