Exemple #1
0
        /// <summary>
        /// Aplica algoritmo de Prim al grafo para hallar el MSP.
        /// El Indentificador de los nodes tienen que ser de tipo int
        /// </summary>
        /// <param name="graph">Grafo</param>
        /// <returns>El MSP</returns>
        public static IEnumerable <IEdge> Run(IGraph graph)
        {
            int[]     distancia = new int[graph.VertexList.Count];
            IVertex[] phi       = new IVertex[graph.VertexList.Count];

            foreach (IVertex vertex in graph.VertexList)
            {
                distancia[(int)vertex.Identifier] = int.MaxValue;
                phi[(int)vertex.Identifier]       = null;
            }

            Heap <IVertex> p_Queue = Heap <int> .Build_Heap(graph.VertexList, x => (int)x.Identifier, HeapType.MinHeap);

            while (p_Queue.Count > 0)
            {
                var current = p_Queue.DeQueue();
                foreach (IEdge edge in current.Out)
                {
                    if (p_Queue.Contains(edge.Target) && edge.Cost < distancia[(int)edge.Target.Identifier])
                    {
                        distancia[(int)edge.Target.Identifier] = (int)edge.Cost;
                        phi[(int)edge.Target.Identifier]       = edge.Source;
                    }
                }
            }

            for (int i = 0; i < graph.VertexList.Count; i++)
            {
                yield return(new EdgeResult(graph.VertexList[i], phi[i], distancia[i], false));
            }
        }