Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="writer"></param>
        public static void EquivalenceClasses(TextReader reader, TextWriter writer)
        {
            string     str       = reader.ReadLine();
            IPartition partition = new PartitionAsForest(Convert.ToInt32(str));

            while ((str = reader.ReadLine()) != null)
            {
                string[] strs = str.Split(null);
                int      j    = Convert.ToInt32(strs[0]);
                int      k    = Convert.ToInt32(strs[1]);
                ISet     set1 = partition.Find(j);
                ISet     set2 = partition.Find(k);
                if (set1 != set2)
                {
                    partition.Join(set1, set2);
                }
                else
                {
                    writer.WriteLine("redundant pair:{0},{1}", j, k);
                }
            }
            writer.WriteLine(partition);
        }
Пример #2
0
        /// <summary>
        /// Kruskal's algorithm is an algorithm that finds a minimum spanning tree for a connected weighted graph.
        /// This means it finds a subset of the edges that forms a tree that includes every vertex,
        /// where the total weight of all the edges in the tree is minimized.
        /// If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component).
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public static IGraph KruskalsAlgorithm(IGraph g)
        {
            Console.WriteLine("Starting...");
            int    i1     = g.NumberOfVertices;
            IGraph graph1 = new GraphAsLists(i1);

            for (int j1 = 0; j1 < i1; j1++)
            {
                graph1.AddVertex(j1);
            }
            IPriorityQueue priorityQueue = new BinaryHeap(g.NumberOfEdges);
            IEnumerator    iEnumerator   = g.Edges.GetEnumerator();

            Console.WriteLine("got the edge enumerator...");
            try
            {
                while (iEnumerator.MoveNext())
                {
                    IEdge edge1 = (IEdge)iEnumerator.Current;
                    int   k;
                    //the casting depends on the datatype of the weight, here you are on your own
                    //we'll assume that an int will do as an example
                    if (edge1.Weight == null)
                    {
                        k = 0;
                    }
                    else
                    {
                        try
                        {
                            k = (int)edge1.Weight;
                        }
                        catch
                        {
                            k = 0;
                        }
                    }

                    priorityQueue.Enqueue(new Association(k, edge1));
                }
            }
            finally
            {
                IDisposable iDisposable = iEnumerator as IDisposable;
                if (iDisposable != null)
                {
                    iDisposable.Dispose();
                }
            }
            Console.WriteLine("after the edge enumerator...");
            Console.WriteLine(priorityQueue.ToString());
            IPartition partition = new PartitionAsForest(i1);

            Console.WriteLine("The partition: " + partition.Count);
            while (!priorityQueue.IsEmpty && (partition.Count > 1))
            {
                IEdge edge2 = (IEdge)((Association)priorityQueue.DequeueMin()).Value;
                Console.WriteLine(edge2.ToString());
                int i2 = edge2.V0.Number;
                int j2 = edge2.V1.Number;
                Console.WriteLine("got vertices (" + i2 + "," + j2 + ")");
                ISet set1 = partition.Find(i2);
                ISet set2 = partition.Find(j2);

                if (set1 != set2)
                {
                    partition.Join(set1, set2);
                    graph1.AddConnection(i2, j2);
                }
            }
            return(graph1);
        }