Ejemplo n.º 1
0
 public void FibLink(FibHeap H, NodeTree y, NodeTree x)
 {
     H.Roots.Remove(y);
     x.Childrens.AddLast(y);
     y.Parent = x;
     x.Degree++;
     y.Mark = false;
 }
Ejemplo n.º 2
0
        public void Consolidate(FibHeap H)
        {
            var maxDegree = Convert.ToInt32(Math.Log10(NodesCount)) + 2;
            var A         = new NodeTree[maxDegree];

            for (int i = 0; i < maxDegree; i++)
            {
                A[i] = null;
            }

            var rootsCount = Roots.Count;
            var tmpRoots   = new List <NodeTree>(this.Roots);

            for (int i = 0; i < tmpRoots.Count; i++)
            {
                var x = tmpRoots[i];
                var d = x.Degree;
                while (A[d] != null)
                {
                    var y = A[d];
                    if (x.NodeData > y.NodeData)
                    {
                        var tmp = x;
                        x = y;
                        y = tmp;
                    }
                    FibLink(H, y, x);
                    A[d] = null;
                    d++;
                }
                A[d] = x;
            }

            H.Min = null;

            for (int i = 0; i < maxDegree; i++)
            {
                if (A[i] != null)
                {
                    if (H.Min == null)
                    {
                        H.Min = A[i];
                    }
                    else
                    {
                        if (A[i].NodeData < H.Min.NodeData)
                        {
                            H.Min = A[i];
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Сливает две кучи в одну.
        /// </summary>
        public FibHeap Union(FibHeap fib)
        {
            var unionFib = new FibHeap {
                Min = this.Min
            };

            this.Roots.AddRange(fib.Roots);
            unionFib.Roots = new List <NodeTree>(this.Roots);
            if ((this.Min == null) || (fib.Min != null && fib.Min.NodeData < this.Min.NodeData))
            {
                unionFib.Min = fib.Min;
            }
            unionFib.NodesCount = this.NodesCount + fib.NodesCount;
            return(unionFib);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var a = new FibHeap();

            a.Insert(5);
            a.Insert(6);
            a.Insert(7);
            a.Insert(3);
            a.Insert(10);

            a.DecreaseKey(a.Min, 2);
            Console.WriteLine(a.ExtractMin().NodeData);
            Console.WriteLine(a.ExtractMin().NodeData);
            Console.WriteLine(a.ExtractMin().NodeData);
            Console.WriteLine(a.ExtractMin().NodeData);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            // create objects
            FileReader fr = new FileReader();

            // read list of commands
            List<List<int>> commands = fr.getCommands("input.txt");

            // prepare list of results
            List<int[]> results = new List<int[]>();

            // prepare list of couriers
            List<FibHeap> couriers = new List<FibHeap>();

            /*
             * loop thru commands
             *
             * commands are "documented" in FileReader.cs
             *
             */
            foreach (List<int> command in commands)
            {
                if (command[0] == 1)
                {
                    Console.WriteLine("Adding courier " + command[1]);
                    connections.Add(command[1]);
                    couriers.Add(new FibHeap());
                }
                if (command[0] == 2)
                {
                    Console.WriteLine("Adding to courier " + command[1] + " package id: " + command[2] + " prio: " + command[3]);
                    couriers[findSlot(command[1])].Insert(command[3]);
                }
                if (command[0] == 3)
                {
                    Console.WriteLine("Showing courier " + command[1] + " first package");

                        int package = couriers[findSlot(command[1])].ExtractMax();

                        int[] result = new int[2];
                        result[0] = command[1];
                        result[1] = package;

                        results.Add(result);
                }
                if (command[0] == 4)
                {
                    Console.WriteLine("Move packages from couirer " + command[1] + " to courier " + command[2]);
                    couriers[findSlot(command[1])].Union(couriers[findSlot(command[2])]);
                    couriers[findSlot(command[2])] = new FibHeap();
                }
            }

            // print results
            fr.printResults("output.txt", results);

            //Console.ReadKey();
        }
Ejemplo n.º 6
0
        // Union (actually adding H2 to H1(this))
        public void Union(FibHeap heap)
        {
            int frozen = n;

            // copy all vertex with changing all id
            foreach (FibHeapVertex x in heap.vertexes)
            {
                vertexes.Add(x);
                x.id += frozen;
                n++;
                last_id++;

                // parent
                if (x.parent != -1) x.parent += frozen;

                // child
                if (x.child != -1) x.child += frozen;

                // left
                x.left += frozen;

                // right
                x.right += frozen;
            }

            // copy roots
            foreach (int id in heap.roots)
            {
                AddRoot(vertexes[id+frozen]);
            }

            // max
            if (heap.max > max)
            {
                max = heap.max;
                max_id = heap.max_id+frozen;
            }
        }