当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.Insert方法代码示例

本文整理汇总了C#中PriorityQueue.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.Insert方法的具体用法?C# PriorityQueue.Insert怎么用?C# PriorityQueue.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PriorityQueueSimpleTest

 public void PriorityQueueSimpleTest()
 {
     PriorityQueue<int> pQueue = new PriorityQueue<int>(10);
     pQueue.Insert(4);
     pQueue.Insert(6);
     pQueue.Insert(3);
     pQueue.Insert(8);
     Assert.AreEqual(3, pQueue.DeleteMin());
 }
开发者ID:adilmughal,项目名称:Algorithms-and-Programming-Problems,代码行数:9,代码来源:PriorityQueueTest.cs

示例2: PriorityQueueDoubleDeleteTest

 public void PriorityQueueDoubleDeleteTest()
 {
     PriorityQueue<int> pQueue = new PriorityQueue<int>(10);
     pQueue.Insert(4);
     pQueue.Insert(1);
     pQueue.Insert(3);
     pQueue.Insert(8);
     pQueue.Insert(0);
     Assert.AreEqual(0, pQueue.DeleteMin());
     Assert.AreEqual(1, pQueue.DeleteMin());
 }
开发者ID:adilmughal,项目名称:Algorithms-and-Programming-Problems,代码行数:11,代码来源:PriorityQueueTest.cs

示例3: Compute

        /// <summary>
        /// The dijkstra's shortest path procedure for a directed &quot; non-negative &quot; weighted graphs.
        /// </summary>
        /// <param name="directedGraph">
        /// a directed graph containing a set V of n vertices 
        /// and a set E of m directed edges with nonnegative weights.
        /// </param>
        /// <param name="sourceVertex">a source vertex in V.</param>
        /// <remarks>
        /// Result:
        ///     For each non-source vertex v in V, shortest[v] is the weight sp(s,v) of a shortest path from s to v and pred(v) is the vertex
        ///     preceding v on some shortest path.
        ///     For the source vertex s, shortest(s) = 0 and pred(s) = NULL.
        ///     If there is no path from s to v, then shortest[v] = infinity, and pred(v) = NULL.
        /// </remarks>
        public void Compute(IGraph directedGraph, int sourceVertex)
        {
            Graph.Shortest = new double[directedGraph.N];
            Graph.Predecessors = new int?[directedGraph.N];

            for (int i = 0; i < directedGraph.N; ++i)
            {
                Graph.Shortest[i] = double.PositiveInfinity;
                Graph.Predecessors[i] = null;
            }

            Graph.Shortest[sourceVertex] = 0;

            var queue = new PriorityQueue<int>();

            foreach (var v in directedGraph.Vertices)
                queue.Insert(v);

            while (queue.Count > 0)
            {
                var u = queue.ExtractMin();
                double tempShortest;

                foreach (var v in directedGraph[u])
                {
                    tempShortest = Shortest[v];

                    Graph.Relax(directedGraph, u, v);

                    if (tempShortest != Shortest[v])
                        queue.DecreaseKey(v);
                }
            }
        }
开发者ID:ioab,项目名称:AU,代码行数:49,代码来源:Dijkstra.cs

示例4: TestPriorityQueue

        public static void TestPriorityQueue()
        {
            Random r = new Random();
            int size = 7;
            int range = 100;
            PriorityQueue q = new PriorityQueue(size);

            Console.WriteLine("-----Test INSERT-----");

            for (int i = 0; i < size; i++)
            {
                double val = r.NextDouble() * range;
                if (!q.Insert(i, val))
                {
                    Console.WriteLine("ERROR on index " + i + " with value " + val.ToString("#.##"));
                    return;
                }
                Console.WriteLine("inserted item " + i + " with value " + val.ToString("#.##"));
                Console.WriteLine("queue:\n" + q.ToString());
            }

            Console.WriteLine();
            Console.WriteLine("-----Test REDUCE_VAL-----");

            for (int i = 0; i < size; i++)
            {
                double val = r.NextDouble() * range;
                if (q.ReduceVal(i, val))
                {
                    Console.WriteLine("update item " + i + " with value " + val.ToString("#.##"));
                    Console.WriteLine("queue:\n" + q.ToString());
                }
                else {
                    Console.WriteLine("ERROR updating index " + i + " with value " + val.ToString("#.##"));
                }

            }

            Console.WriteLine();
            Console.WriteLine("-----Test GET_VAL-----");

            for (int i = 0; i < size; i++)
            {
                Console.WriteLine("Node " + i + " has a value of " + q.GetVal(i).ToString("#.##"));
            }

            Console.WriteLine();
            Console.WriteLine("-----Test POP_MIN-----");

            for (int i = 0; i < size; i++)
            {
                int id = q.PopMin();
                Console.WriteLine("popped " + id + " off the queue");
                Console.WriteLine("queue:\n" + q.ToString());
            }

            Console.WriteLine("ALL DONE");
        }
开发者ID:kevinjreece,项目名称:cs312-network-routing,代码行数:58,代码来源:PriorityQueue.cs

示例5: TryTakeTopItemTest

        public void TryTakeTopItemTest()
        {
            var queue = new PriorityQueue<int>();
            queue.Insert(1);

            int topItem;
            Assert.IsTrue(queue.TryTakeTopItem(out topItem));
            Assert.AreEqual(topItem, 1);
            Assert.IsFalse(queue.TryTakeTopItem(out topItem));
        }
开发者ID:deva666,项目名称:PriorityProducerConsumerQueue,代码行数:10,代码来源:PriorityQueueTests.cs

示例6: Run

        public static void Run(Vertex vertex)
        {
            vertex.Depth = 0;
            PriorityQueue<Vertex> queue = new PriorityQueue<Vertex>((x, y) => y.Depth.CompareTo(x.Depth));

            queue.Insert(vertex);
            while(!queue.IsEmpty)
            {
                Vertex current = queue.Pop();
                foreach(Edge edge in current.Edges)
                {
                    if(edge.From.Depth + edge.Weight < edge.To.Depth)
                    {
                        edge.To.Depth = edge.From.Depth + edge.Weight;
                        edge.To.Parent = edge.From;
                        queue.Insert(edge.To);
                    }
                }
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:20,代码来源:Djikstra.cs

示例7: InsertTest

        public void InsertTest()
        {
            var range = new int[ARRAY_SIZE];
            var random = new Random();
            var queue = new PriorityQueue<int>();
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                var number = random.Next(int.MaxValue);
                range[i] = number;
                queue.Insert(number);
            }

            TestSort(range, queue);
        }
开发者ID:deva666,项目名称:PriorityProducerConsumerQueue,代码行数:14,代码来源:PriorityQueueTests.cs

示例8: AllPathDijkstra

        public double AllPathDijkstra(Stopwatch s)
        {
            int n_nodes = points.Count();
            PriorityQueue q = new PriorityQueue(n_nodes);
            double[] src_dist = new double[n_nodes];
            int[] prev = new int[n_nodes];
            int i;

            s.Start();

            for (i = 0; i < n_nodes; i++)
            {
                src_dist[i] = double.MaxValue;
                prev[i] = -1;
                q.Insert(i, src_dist[i]);
            }

            src_dist[src] = 0;
            q.ReduceVal(src, src_dist[src]);

            while (!q.IsEmpty())
            {
                int id = q.PopMin();
                HashSet<int> adj_nodes = adjacencyList[id];
                for (i = 0; i < adj_nodes.Count(); i++)
                {
                    int temp_id = adj_nodes.ElementAt(i);
                    double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]);
                    //Console.WriteLine("Distance from " + id + " to " + temp_id + " is " + temp_dist);

                    if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance
                    {
                        src_dist[temp_id] = temp_dist;
                        prev[temp_id] = id;

                        if (!q.ReduceVal(temp_id, temp_dist))
                        {
                            Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id);
                            return 1;
                        }
                        //Console.WriteLine("queue:\n" + q.ToString());
                    }
                }
            }

            s.Stop();

            return src_dist[dst];
        }
开发者ID:kevinjreece,项目名称:cs312-network-routing,代码行数:49,代码来源:Form1.cs

示例9: Main

        static void Main()
        {
            var test = new PriorityQueue<int>();
            test.Insert(1);
            test.Insert(2);
            test.Insert(3);
            test.Insert(4);
            test.Insert(0);
            test.Insert(20);
            test.Insert(7);
            test.Insert(3000);

            var count = test.Size;
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(test.Pop());
            }
        }
开发者ID:Ivorankov,项目名称:DSA,代码行数:18,代码来源:EntryPoint.cs

示例10: Main

        static void Main(string[] args)
        {
            PriorityQueue<int> pq = new PriorityQueue<int>();

            for (int i = 0; i < 13; i++)
            {
                pq.Insert(i);
            }

            foreach (var item in pq)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("*********************");

            //while (!pq.IsEmpty)
            //{
            //    Console.WriteLine(pq.RemoveMin());
            //}

            Console.ReadLine();
        }
开发者ID:sametdumankaya,项目名称:DataStructures,代码行数:23,代码来源:Program.cs

示例11: Main

        static void Main()
        {
            //var testHeap = new BinaryHeap<int>();
            //testHeap.Add(5);
            //testHeap.Add(6);
            //testHeap.Add(7);
            //testHeap.Add(8);
            //testHeap.Add(9);
            //testHeap.Add(1);
            //testHeap.Add(2);
            //testHeap.Add(3);
            //testHeap.Add(31);
            //testHeap.Add(4);

            var testPriorityQueue = new PriorityQueue<string>();
            testPriorityQueue.Insert("Pesho", 1);
            testPriorityQueue.Insert("Gosho", 3);
            testPriorityQueue.Insert("Ganka", 2);
            testPriorityQueue.Insert("Tyanko", 7);
            testPriorityQueue.Insert("Minka", 5);
            testPriorityQueue.Insert("Zhelyo", 6);
        }
开发者ID:AsenTahchiyski,项目名称:SoftUni,代码行数:22,代码来源:Program.cs

示例12: OnePathDijkstra

        public double OnePathDijkstra(Stopwatch s)
        {
            int n_nodes = points.Count();
            PriorityQueue q = new PriorityQueue(n_nodes);
            double[] src_dist = new double[n_nodes];
            int[] prev = new int[n_nodes];
            int i;

            s.Start();

            for (i = 0; i < n_nodes; i++)
            {
                src_dist[i] = double.MaxValue;
                prev[i] = -1;
            }

            src_dist[src] = 0;
            prev[src] = -1;

            q.Insert(src, src_dist[src]);

            while (!q.IsEmpty())
            {
                int id = q.PopMin();
                if (id == dst) { break; }
                HashSet<int> adj_nodes = adjacencyList[id];
                for (i = 0; i < adj_nodes.Count(); i++)
                {
                    int temp_id = adj_nodes.ElementAt(i);
                    double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]);

                    if (src_dist[temp_id] == double.MaxValue) // If the node has NOT been visited
                    {
                        src_dist[temp_id] = temp_dist;
                        prev[temp_id] = id;

                        if (!q.Insert(temp_id, src_dist[temp_id]))
                        {
                            Console.WriteLine("ERROR inserting id " + temp_id + " connected to id " + id);
                            return 1;
                        }
                    }
                    else if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance
                    {
                        src_dist[temp_id] = temp_dist;
                        prev[temp_id] = id;

                        if (!q.ReduceVal(temp_id, temp_dist))
                        {
                            Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id);
                            return 1;
                        }
                    }
                }
            }

            s.Stop();

            if (src_dist[dst] != double.MaxValue)
            {
                Pen pen = new Pen(Color.Black);
                Font font = new Font("Arial", 8);
                SolidBrush brush = new SolidBrush(Color.Black);
                int temp = dst;
                while (prev[temp] != -1)
                {
                    PointF p1 = points[temp];
                    PointF p2 = points[prev[temp]];
                    graphics.DrawLine(pen, p1, p2);
                    graphics.DrawString(GetDist(p1, p2).ToString("#.##"), font, brush, GetMidPoint(p1, p2));
                    temp = prev[temp];
                }
                pictureBox.Refresh();
            }
            else
            {
                Console.WriteLine("Destination is unreachable");
            }

            return src_dist[dst];
        }
开发者ID:kevinjreece,项目名称:cs312-network-routing,代码行数:81,代码来源:Form1.cs

示例13: FindPath

    public bool FindPath()
    {
        //Check if indexes are valid
        if (!IsIndexValid (First) || !IsIndexValid (Last))
            return false;

        //Check if starting and ending position are same
        if (First == Last) {
            ConstructPath (new Dictionary<Point, Point> (), Last);
            return true;
        }

        var profiler = new Stopwatch ();
        profiler.Start ();

        //Create open/close sets and cameFrom
        var closed = new HashSet<Point> ();
        var open = new PriorityQueue<Point> (true);
        var cameFrom = new Dictionary<Point, Point> ();

        //Create f/g score dicts
        var gScore = new Dictionary<Point, float> ();
        var fScore = new Dictionary<Point, float> ();

        //Prepare the first node
        var current = First;
        gScore [current] = 0f;
        fScore [current] = gScore [current] + CostEstimate (current, Last);
        open.Insert (current, fScore [current]);

        //Main loop
        while (!open.IsEmpty) {
            //Take the first in the priority queue
            current = open.Pop ();
            closed.Add (current);

            //If goal reached
            if (current == Last) {
                ConstructPath (cameFrom, current);
                profiler.Stop ();
                ComputationTime = profiler.Elapsed;
                return true;
            }

            //Iterate neighbours
            var neighbours = GetNeighbours (current);
            foreach (var neighbourPair in neighbours) {
                var neighbour = neighbourPair.Key;
                var cost = neighbourPair.Value;
                //Don't search closed nodes
                if (closed.Contains (neighbour))
                    continue;

                //Check if it's worth it
                var tentativeGScore = gScore [current] + cost;
                if (open.Contains (neighbour) && tentativeGScore >= gScore [neighbour])
                    continue;

                //Set up neighbour
                cameFrom [neighbour] = current;
                gScore [neighbour] = tentativeGScore;
                fScore [neighbour] = gScore [neighbour] + CostEstimate (neighbour, Last);

                //Change priority or add it to the priority queue
                if (open.ChangePriority (neighbour, fScore [neighbour]))
                    continue;

                open.Insert (neighbour, fScore [neighbour]);
            }
        }

        profiler.Stop ();
        ComputationTime = profiler.Elapsed;
        return false;
    }
开发者ID:Botyto,项目名称:UnityTown,代码行数:75,代码来源:IsoPathfinder.cs

示例14: DoSearch

        private void DoSearch(ulong q, int τ, PriorityQueue W, List<Node> node)
        {
            if (!node.Any()) return;

            // Leaf node : test each element in this node's bucket.
            if (node.Count > 1)
            {
                var n = node.Count;
                for (var i = 0; i < n; i++)
                {
                    comparisons++;
                    var elementID = node[i].i;
                    var element = S[elementID];
                    var elementDist = distance(q, element);
                    if (elementDist < τ)
                    {
                        τ = W.Insert(elementID, elementDist) ?? τ;
                    }
                }
                return;
            }

            // Non-leaf node
            var id = node[0].i;
            var p = S[id];
            var dist = distance(q, p);

            comparisons++;

            // This vantage-point is close enough to q.
            if (dist < τ)
            {
                τ = W.Insert(id, dist) ?? τ;
            }

            // The order of exploration is determined by comparison with μ.
            // The sooner we find elements close to q, the smaller τ and the more nodes we skip.
            // P. Yianilos uses the middle of left/right bounds instead of μ.
            var μ = node[0].μ;
            var L = node[0].L;
            var R = node[0].R;
            //if (μ == null)
             //   return;
            if (dist < μ)
            {
                if (L.Any() && node[0].m - τ < dist)
                {
                    DoSearch(q, τ, W, L);
                }
                if (R.Any() && μ - τ < dist && dist < node[0].M + τ)
                {
                    DoSearch(q, τ, W, R);
                }
            }
            else
            {
                if (R.Any() && dist < node[0].M + τ)
                {
                    DoSearch(q, τ, W, R);
                }
                if (L.Any() && node[0].m - τ < dist && dist < μ + τ)
                {
                    DoSearch(q, τ, W, L);
                }
            }
        }
开发者ID:sgnatonski,项目名称:slavapp,代码行数:66,代码来源:VPTree.cs

示例15: PriorityQueueDuplicateElementsTest

        public void PriorityQueueDuplicateElementsTest()
        {
            var values = new[] { 3, 3, 3, 5, 3, 3, 3, 3 };
            var maxpq = new PriorityQueue<int>(ExtremeType.Maximum);

            Assert.IsTrue(maxpq.IsEmpty);

            for (int i = 0; i < values.Length; i++)
            {
                maxpq.Insert(values[i]);
            }

            Assert.AreEqual(5, maxpq.DeleteExtreme());


            while (!maxpq.IsEmpty)
            {
                int curr = maxpq.DeleteExtreme();

                Assert.AreEqual(3, curr);
            }
        }
开发者ID:destinyclown,项目名称:VBF,代码行数:22,代码来源:CommonTests.cs


注:本文中的PriorityQueue.Insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。