예제 #1
0
파일: GraphUtil.cs 프로젝트: zimhe/Graph
        /// <summary>
        ///
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="vertexDistances"></param>
        /// <param name="startVertex"></param>
        /// <returns></returns>
        public static IEnumerable <int> WalkToMin(EdgeGraph graph, float[] vertexDistances, int startVertex)
        {
            int   v0 = startVertex;
            float d0 = vertexDistances[v0];

            while (true)
            {
                // edge to lowest neighbour
                int   eMin = -1;
                float dMin = float.MaxValue;

                // find edge to neighbour with smallest distance
                foreach (var ei in graph.GetIncidentEdges(v0))
                {
                    var v1 = graph.GetOppositeVertex(ei, v0);
                    var d1 = vertexDistances[v1];

                    if (d1 < dMin)
                    {
                        eMin = ei;
                        dMin = d1;
                    }
                }

                // if less than current distance, take a step
                if (dMin < d0)
                {
                    yield return(eMin);

                    // update current vertex and distance
                    v0 = graph.GetOppositeVertex(eMin, v0);
                    d0 = dMin;
                }
                else
                {
                    yield break;
                }
            }
        }