コード例 #1
0
ファイル: MinPQTest.cs プロジェクト: hong-rong/MyRepository
 public void Initialize()
 {
     _target = new MinPQ<Distance>();
     _target.Insert(new Distance { V = 0, Dist = 3 });
     _target.Insert(new Distance { V = 1, Dist = 1 });
     _target.Insert(new Distance { V = 2, Dist = 2 });
 }
コード例 #2
0
ファイル: SortingTests.cs プロジェクト: rthota50/DSinCS
 public void Min_pq_delete_tests()
 {
     var p = new MinPQ<int>(5);
     p.Insert(5);
     p.Insert(4);
     p.Insert(99);
     p.Insert(14);
     p.Insert(0);
     Assert.AreEqual(0, p.DelMin());
     Assert.AreEqual(4, p.DelMin());
     Assert.AreEqual(5, p.DelMin());
     Assert.AreEqual(14, p.DelMin());
     Assert.AreEqual(99, p.DelMin());
 }
コード例 #3
0
ファイル: KruskalMST.cs プロジェクト: vladdnc/Algorithms-NET
        /// <summary>
        /// Compute a minimum spanning tree (or forest) of an edge-weighted graph.
        /// </summary>
        /// <param name="g">g the edge-weighted graph</param>
        public KruskalMST(EdgeWeightedGraph g)
        {
            // more efficient to build heap by passing array of edges
            var pq = new MinPQ<EdgeW>();
            foreach (var e in g.Edges())
            {
                pq.Insert(e);
            }

            // run greedy algorithm
            var uf = new UF(g.V);
            while (!pq.IsEmpty() && _mst.Size() < g.V - 1)
            {
                var e = pq.DelMin();
                var v = e.Either();
                var w = e.Other(v);
                if (!uf.Connected(v, w))
                { // v-w does not create a cycle
                    uf.Union(v, w);  // merge v and w components
                    _mst.Enqueue(e);  // add edge e to mst
                    _weight += e.Weight;
                }
            }

            // check optimality conditions
            //assert check(G);
        }
コード例 #4
0
ファイル: SortingTests.cs プロジェクト: rthota50/DSinCS
        public void Min_priority_queue()
        {
            var p = new MinPQ<int>(5);
            p.Insert(5);
            Assert.AreEqual("5", string.Join(",", p.CurrentPQ()));

            p.Insert(4);
            Assert.AreEqual("4,5", string.Join(",", p.CurrentPQ()));

            p.Insert(99);
            Assert.AreEqual("4,5,99", string.Join(",", p.CurrentPQ()));

            p.Insert(14);
            Assert.AreEqual("4,5,99,14", string.Join(",", p.CurrentPQ()));

            p.Insert(0);
            Assert.AreEqual("0,4,99,14,5", string.Join(",", p.CurrentPQ()));
        }
コード例 #5
0
ファイル: PriorityQueueTest.cs プロジェクト: beginor/practice
        public void TestMinPQ() {
            var arr = CreateRandomArray(10);
            Insertion<int>.Sort(arr);
            StdOut.WriteLine(arr);

            var pq = new MinPQ<int>();
            foreach (int i in arr) {
                pq.Insert(i);
            }

            while (!pq.IsEmpty) {
                StdOut.WriteLine("delete min: {0}", pq.DelMin());
            }
        }
コード例 #6
0
ファイル: MinPQWorker.cs プロジェクト: vladdnc/Algorithms-NET
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - tinyPQ.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            var fieName = string.Empty;
            switch (fileNumber)
            {
                case "1":
                    fieName = "tinyPQ.txt";
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In(string.Format("Files\\Sorting\\{0}", fieName));
            var words = @in.ReadAllStrings();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            var listStrings = words.ToList();

            var pq = new MinPQ<string>(new StringComparer());
            //Fill Priority Queue
            foreach (var word in listStrings)
            {
                pq.Insert(word);
            }
            // print results
            foreach (var item in pq)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
コード例 #7
0
ファイル: KruskalMST.cs プロジェクト: qizl/Algorithms
        public KruskalMST(EdgeWeightedGraph g)
        {
            MinPQ<Edge> pq = new MinPQ<Edge>();
            foreach (Edge e in g.Edges())
                pq.Insert(e);

            Part1.QuickUnion uf = new Part1.QuickUnion(g.V());
            while (!pq.IsEmpty() && this._mst.Count < g.V() - 1)
            {
                Edge e = pq.DelMin();
                int v = e.Either();
                int w = e.Other(v);
                if (!uf.IsConnected(v, w))
                {
                    uf.Union(v, w);
                    this._mst.Enqueue(e);
                    this.Weight += e.Weight();
                }
            }
        }
コード例 #8
0
ファイル: KruskalMST.cs プロジェクト: darkeclipz/graphs
        public KruskalMST(EdgeWeightedGraph g)
        {
            _mst = new Queue <Edge>();
            var pq = new MinPQ <Edge>(g.E);

            foreach (var e in g.Edges())
            {
                pq.Insert(e);
            }
            var uf = new UF(g.V);

            while (!pq.IsEmpty && _mst.Count < g.V - 1)
            {
                var e = (Edge)pq.DeleteMin();
                int v = e.Either(), w = e.Other(v);
                if (uf.Connected(v, w))
                {
                    continue;
                }
                uf.Union(v, w);
                _mst.Enqueue(e);
            }
        }
コード例 #9
0
        public KruskalMST(IEdgeWeightGraph G)
        {
            mst = new Chapter1.Queue <IEdge>();
            MinPQ <IEdge> pq = new MinPQ <IEdge>();

            foreach (var e in G.Edges())
            {
                pq.Insert(e);
            }
            UF uf = new UF(G.V);

            while (!pq.IsEmpty && mst.Size < G.V - 1)
            {
                IEdge e = pq.Delete();  //找到权重最小的边
                int   v = e.Either, w = e.Other(v);
                if (uf.Connected(v, w)) //忽略失效的边
                {
                    continue;
                }
                uf.Union(v, w);
                mst.Enqueue(e);
            }
        }
コード例 #10
0
        public void MinPQTest()
        {
            MinPQ <Edge> minQ;
            int          edgeNum, vNum;

            //Build minQueue
            using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyEWG.txt"))
            {
                string line = null;
                line = sr.ReadLine(); // read V;
                string[] pair = line.Split();
                vNum    = Int32.Parse(line);
                line    = sr.ReadLine();
                edgeNum = Int32.Parse(line);

                minQ = new MinPQ <Edge>(edgeNum);

                //while((line=sr.ReadLine())!=null)   //each line is an edge
                for (int i = 0; i < edgeNum; i++)
                {
                    line = sr.ReadLine();
                    pair = line.Split();
                    int    v      = Int32.Parse(pair[0]);
                    int    w      = Int32.Parse(pair[1]);
                    double weight = Double.Parse(pair[2]);
                    Edge   e      = new Edge(v, w, weight);

                    minQ.Insert(e);
                }
            }

            for (int i = 0; i < edgeNum; i++)
            {
                Edge e = minQ.DelMin();
                Console.WriteLine(e.ToString());
            }
        }
コード例 #11
0
        /// <summary>
        /// Returns a uniformly random tree on <tt>V</tt> vertices.
        /// This algorithm uses a Prufer sequence and takes time proportional to <em>V log V</em>.
        /// http://www.proofwiki.org/wiki/Labeled_Tree_from_Prüfer_Sequence
        /// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.36.6484&rep=rep1&type=pdf
        /// </summary>
        /// <param name="v">V the number of vertices in the tree</param>
        /// <returns>a uniformly random tree on <tt>V</tt> vertices</returns>
        public static Graph Tree(int v)
        {
            var g = new Graph(v);

            // special case
            if (v == 1) return g;

            // Cayley's theorem: there are V^(V-2) labeled trees on V vertices
            // Prufer sequence: sequence of V-2 values between 0 and V-1
            // Prufer's proof of Cayley's theorem: Prufer sequences are in 1-1
            // with labeled trees on V vertices
            var prufer = new int[v - 2];
            for (var i = 0; i < v - 2; i++)
                prufer[i] = StdRandom.Uniform(v);

            // degree of vertex v = 1 + number of times it appers in Prufer sequence
            var degree = new int[v];
            for (var vi = 0; vi < v; vi++)
                degree[vi] = 1;
            for (var i = 0; i < v - 2; i++)
                degree[prufer[i]]++;

            // pq contains all vertices of degree 1
            var pq = new MinPQ<Integer>();
            for (var vi = 0; vi < v; vi++)
                if (degree[vi] == 1) pq.Insert(vi);

            // repeatedly delMin() degree 1 vertex that has the minimum index
            for (var i = 0; i < v - 2; i++)
            {
                int vmin = pq.DelMin();
                g.AddEdge(vmin, prufer[i]);
                degree[vmin]--;
                degree[prufer[i]]--;
                if (degree[prufer[i]] == 1) pq.Insert(prufer[i]);
            }
            g.AddEdge(pq.DelMin(), pq.DelMin());
            return g;
        }