コード例 #1
0
        private void SetSumToNextVertex(VertexInfo vertexInfo)
        {
            vertexInfo.IsVisited = true;

            for (int i = 0; i < vertexInfo.Vertex.Nodes.Count; ++i)
            {
                long sum = vertexInfo.SumWeightEdges + vertexInfo.Vertex.Nodes[i].Weight;

                VertexInfo nextVertexInfo = GetVertexInfo(vertexInfo.Vertex.Nodes[i].Connectable);

                if (sum < nextVertexInfo.SumWeightEdges)
                {
                    nextVertexInfo.SumWeightEdges = Convert.ToInt32(sum);

                    nextVertexInfo.PrevVertex = vertexInfo.Vertex;
                }
            }
        }
コード例 #2
0
        private VertexInfo FindMinUnvisitedVertex()
        {
            int minSum = Int32.MaxValue;

            VertexInfo minVertexInfo = null;

            foreach (var vertex in vertexInfos)
            {
                if (!vertex.IsVisited && vertex.SumWeightEdges < minSum)
                {
                    minVertexInfo = vertex;

                    minSum = vertex.SumWeightEdges;
                }
            }

            return(minVertexInfo);
        }
コード例 #3
0
        public List <int> FindShortestPath(int startVertexId, int endVertexId)
        {
            VertexInfo startVertexInfo = GetVertexInfo(startVertexId);

            startVertexInfo.SumWeightEdges = 0;

            VertexInfo currentVertexInfo = null;

            while (true)
            {
                currentVertexInfo = FindMinUnvisitedVertex();

                if (currentVertexInfo == null)
                {
                    break;
                }

                SetSumToNextVertex(currentVertexInfo);
            }

            return(GetPath(startVertexId, endVertexId));
        }