Exemple #1
0
        public void IndexMinPQTest1()
        {
            const int    MaxSize  = 8;
            const double MinValue = 3.9;
            const double MaxValue = MinValue * MaxSize + 32;
            int          index;

            // MinValue index == 3, MaxValue index == 4
            double[] items = { MinValue * 2, MinValue * 3, MinValue * 4, MinValue, MaxValue, MinValue * 5, MinValue * 6, MinValue * 7 };
            StdRandom.Seed = 101;

            IndexMinPQ <double> pq = new IndexMinPQ <double>(MaxSize);

            index = StdRandom.Uniform(items.Length);
            Assert.IsFalse(pq.Contains(index));
            Assert.IsTrue(pq.IsEmpty);
            Assert.AreEqual(0, pq.Count);

            try
            {
                index = pq.DelMin();
                Assert.Fail("Failed to catch exception");
            }
            catch (InvalidOperationException) { }

            for (int i = 0; i < items.Length; i++)
            {
                pq.Insert(i, items[i]);
            }
            Assert.AreEqual(items.Length, pq.Count);
            Assert.AreEqual(MinValue, pq.MinKey);
            Assert.AreEqual(3, pq.MinIndex);
            Assert.AreEqual(MaxValue, pq.KeyOf(4));

            index = StdRandom.Uniform(items.Length);
            Assert.AreEqual(items[index], pq.KeyOf(index));

            pq.ChangeKey(1, pq.MinKey * 0.9); // make it the smallest item
            Assert.AreEqual(1, pq.MinIndex);

            pq.DecreaseKey(3, pq.MinKey * 0.87);
            Assert.AreEqual(3, pq.MinIndex);

            pq.Delete(3);
            Assert.AreNotEqual(3, pq.MinIndex);

            Assert.AreEqual(1, pq.DelMin());
        }
Exemple #2
0
        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 = "tinyIndexPQ.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Sorting\\{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 IndexMinPQ <string>(listStrings.Count);

            //Fill Priority Queue
            for (var i = 0; i < listStrings.Count; i++)
            {
                pq.Insert(i, listStrings[i]);
            }
            // print results
            foreach (var item in pq)
            {
                Console.WriteLine(pq.KeyOf(item));
            }


            Console.ReadLine();
        }
Exemple #3
0
    // Returns shortest path from src to dst (not including src)
    public List <TileNode> GetShortestPath(Vector2Int src, Vector2Int dst, UnitType unitType)
    {
        float[,] distance  = new float[width, height];
        Vector2Int[,] prev = new Vector2Int[width, height];
        IndexMinPQ <float> minPQ = new IndexMinPQ <float>(width * height);

        distance[src.x, src.y] = 0.0f;
        prev[src.x, src.y]     = src;
        minPQ.Insert(ToTileIndex1D(src), 0.0f);
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (i == src.x && j == src.y)
                {
                    continue;
                }

                distance[i, j] = float.PositiveInfinity;
                prev[i, j]     = new Vector2Int(-1, -1);
                minPQ.Insert(ToTileIndex1D(i, j), float.PositiveInfinity);
            }
        }

        while (!minPQ.IsEmpty)
        {
            float        dist      = minPQ.MinKey;
            Vector2Int   min       = ToTileIndex2D(minPQ.DelMin());
            Vector2Int[] neighbors = GetNeighbors(min);
            foreach (Vector2Int neighbor in neighbors)
            {
                int   neighborInd = ToTileIndex1D(neighbor);
                float edgeDist    = GetTileMoveWeight(neighbor, unitType);
                if (minPQ.Contains(neighborInd) && edgeDist != 0.0f)
                {
                    float currentDist = minPQ.KeyOf(neighborInd);
                    if (dist + edgeDist < currentDist)
                    {
                        distance[neighbor.x, neighbor.y] = dist + edgeDist;
                        prev[neighbor.x, neighbor.y]     = min;
                        minPQ.DecreaseKey(neighborInd, dist + edgeDist);
                    }
                }
            }
        }

        if (distance[dst.x, dst.y] == float.PositiveInfinity)
        {
            return(null);
        }
        List <TileNode> path = new List <TileNode>();
        TileNode        dstNode;

        dstNode.coords = dst;
        dstNode.dist   = distance[dst.x, dst.y];
        path.Add(dstNode);
        Vector2Int tile = dst;

        while (prev[tile.x, tile.y] != src)
        {
            tile = prev[tile.x, tile.y];
            TileNode node;
            node.coords = tile;
            node.dist   = distance[tile.x, tile.y];
            path.Add(node);
        }
        path.Reverse();
        return(path);
    }