Beispiel #1
0
 /// <summary>
 /// Vertex info constructor
 /// </summary>
 /// <param name="vertex">Vertex</param>
 public GraphVertexInfo(GraphVertex vertex)
 {
     Vertex         = vertex;
     IsUnvisited    = true;
     EdgesWeightSum = int.MaxValue;
     PreviousVertex = null;
 }
Beispiel #2
0
 /// <summary>
 /// Add vertex to path
 /// </summary>
 /// <param name="vertex">Vertex</param>
 public void AddVertex(GraphVertex vertex)
 {
     if (vertex != null && !Path.Contains(vertex))
     {
         Path.Add(vertex);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Find all possible paths with stated length from stated vertex
        /// </summary>
        /// <param name="firstVertex">Start vertex</param>
        /// <param name="visitedVertices">Just create new empty list</param>
        /// <param name="paths">Create new empty list before calling method</param>
        /// <param name="pathWeight">Path weight</param>
        private void DepthFirstSearch(GraphVertex firstVertex, List <GraphVertex> visitedVertices, List <GraphPath> paths, int pathWeight)
        {
            if (firstVertex == null)
            {
                return;
            }

            var tempList = visitedVertices.GetRange(0, visitedVertices.Count);

            tempList.Add(firstVertex);

            if (pathWeight == 1)
            {
                tempList.Add(firstVertex);
                paths.Add(new GraphPath(tempList));
                tempList.Remove(firstVertex);
            }

            foreach (var vertex in firstVertex.ConnectedVertices)
            {
                if (!tempList.Contains(vertex))
                {
                    DepthFirstSearch(vertex, tempList, paths, pathWeight - 1);
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Change connected vertex
 /// </summary>
 /// <param name="vertex">New vertex</param>
 public void SetVertex(GraphVertex vertex)
 {
     if (vertex != null)
     {
         ConnectedVertex = vertex;
     }
 }
Beispiel #5
0
        /// <summary>
        /// Get vertex info
        /// </summary>
        /// <param name="vertex">Vertex</param>
        /// <returns>Vertex info</returns>
        private GraphVertexInfo GetVertexInfo(GraphVertex vertex)
        {
            foreach (var item in InfoList)
            {
                if (item.Vertex.Equals(vertex))
                {
                    return(item);
                }
            }

            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// Path formation
        /// </summary>
        /// <param name="endVertex">End vertex</param>
        /// <returns>Path</returns>
        private GraphPath GetPath(GraphVertex endVertex)
        {
            var tempEndVertex = endVertex;
            var Path          = new List <GraphVertex>();

            while (tempEndVertex != null)
            {
                Path.Insert(0, tempEndVertex);
                tempEndVertex = GetVertexInfo(tempEndVertex).PreviousVertex;
            }

            return(new GraphPath(Path, GetVertexInfo(endVertex).EdgesWeightSum));
        }
Beispiel #7
0
        /// <summary>
        /// Search a shortest path between two vertices
        /// </summary>
        /// <param name="firstVertex">First vertex</param>
        /// <param name="secondVertex">Second vertex</param>
        /// <param name="searchingField">Field of edge to search the shortest path by</param>
        /// <returns>Кратчайший путь</returns>
        public GraphPath FindShortestPath(GraphVertex firstVertex, GraphVertex secondVertex, PathSearchingField searchingField = PathSearchingField.Weight)
        {
            InitInfo();
            var first = GetVertexInfo(firstVertex);

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

            return(GetPath(secondVertex));
        }
Beispiel #8
0
 /// <summary>
 /// Edge constructor
 /// </summary>
 /// <param name="connectedVertex">Vertex which new edge will be connected to</param>
 /// <param name="weight">Edge weight</param>
 public GraphEdge(GraphVertex connectedVertex, int weight, int length)
 {
     ConnectedVertex = connectedVertex;
     EdgeWeight      = weight;
     EdgeLength      = length;
 }
Beispiel #9
0
 /// <summary>
 /// Add new edge
 /// </summary>
 /// <param name="vertex">Vertex which new edge will be connected to</param>
 /// <param name="edgeWeight">Edge weight</param>
 /// <param name="edgeLength">Edge length</param>
 public void AddEdge(GraphVertex vertex, int edgeWeight, int edgeLength)
 {
     AddEdge(new GraphEdge(vertex, edgeWeight, edgeLength));
 }