コード例 #1
0
        public void Merge(FibHeap other)
        {
            Debug.Assert(_trees != null);
            Debug.Assert(other._trees != null);
            Debug.Assert(_minNode != null);
            Debug.Assert(other._minNode != null);

            _count += other._count;

            _trees.Merge(other._trees);

            if (other._minNode.Key < _minNode.Key)
            {
                _minNode = other._minNode;
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string line;

            FibHeap heap = null;

            Node[] idmap = null;


            TextReader str;
            int        cmdCount = 0;

            long sum = 0, count = 0;

#if CONSOLE
            str = Console.In;
            if (args.Length != 2)
            {
                Console.WriteLine("Output file name and heap type is required.");
                return;
            }

            if (args[0].Length != 1)
            {
                Console.WriteLine("Heap type can only be 's' or 'n'");
            }

            string outfile = args[1];
            bool   isNaive = args[0][0] == 'n';
#else
            str = new StreamReader("test-b.txt");
            string outfile = "vs-out.txt";
            bool   isNaive = false;
#endif
            Console.WriteLine($"Running naive = {isNaive}");

            long currentSize = 0;

            var reader = new StreamParser(str);

            using (var outGraph = new StreamWriter(outfile)) {
                while (reader.NotFinished)
                {
                    cmdCount++;
#if PRINT_GRAPH
                    heap.PrintDotgraph($"#{cmdCount} Before command {line} ... min {heap.Min()?.Key}");
#endif

                    if (cmdCount % 100000 == 0)
                    {
                        Console.WriteLine($"{cmdCount}");
                    }

                    switch (reader.Command())
                    {
                    case StreamParser.CommandType.NewTest:
                        if (count > 0)
                        {
                            outGraph.WriteLine($"{currentSize};{(float) sum / count}");
                        }

                        heap = new FibHeap(isNaive);

                        int num = reader.Number();

                        currentSize = num;
                        idmap       = new Node[num];


                        sum   = 0;
                        count = 0;
                        break;

                    case StreamParser.CommandType.Ins: {
                        int E = reader.Number();
                        int K = reader.Number();

                        idmap[E] = heap.Insert(K, E);

                        break;
                    }

                    case StreamParser.CommandType.Del: {
                        var min = heap.Min();
                        var res = heap.ExtractMin();

                        idmap[min.Identifier] = null;

                        count++;
                        sum += res;
                        break;
                    }

                    case StreamParser.CommandType.Dec: {
                        int E   = reader.Number();
                        int val = reader.Number();
                        if (idmap[E] != null)
                        {
                            heap.Decrease(idmap[E], val);
                        }
                        break;
                    }
                    }
                }

#if PRINT_GRAPH
                heap.PrintDotgraph($"#{cmdCount} After command {line} ... min {heap.Min()?.Key}");
#endif
            }
        }