Beispiel #1
0
 public void GetResult()
 {
     #region test
     int length = 5;
     heap     = new HeapD(sizeHeap, length);
     result   = new Result(length);
     graph    = new Node[length];
     graph[0] = new Node(1, 10,
                         new Node(2, 30,
                                  new Node(3, 50,
                                           new Node(4, 10, null))));
     graph[1] = null;
     graph[2] = new Node(4, 10, null);
     graph[3] = new Node(1, 40,
                         new Node(2, 20, null));
     graph[4] = new Node(0, 10,
                         new Node(2, 10,
                                  new Node(3, 30, null)));
     #endregion
     DijkstraDHeap(4);
     foreach (int a in result.dist)
     {
         Console.WriteLine(a);
     }
     Console.WriteLine();
     FordBellman(4);
     foreach (int a in result.dist)
     {
         Console.WriteLine(a);
     }
 }
Beispiel #2
0
        public void SecondExperimentForDijkstraDHeap()
        {
            List <int> timeWork = new List <int>();

            graph  = new Node[countNode];
            heap   = new HeapD(sizeHeap, countNode);
            result = new Result(countNode);
            int countIteration = 1000;
            int j = 0;

            for (int i = 0; i < 100; i++)
            {
                while (countIteration != 0)
                {
                    graph[j] = AddEdge(graph[j], 1, countNode - 1);
                    j++;
                    if (j == graph.Length)
                    {
                        j = 0;
                    }
                    countIteration--;
                }
                countIteration = 1000;
                DateTime start = DateTime.Now;
                DijkstraDHeap(0);
                DateTime end = DateTime.Now;
                timeWork.Add((end - start).Milliseconds + (end - start).Seconds * 1000);
            }
        }
Beispiel #3
0
        public static void DeliteMin(this HeapD heap)
        {
            int name0 = heap.names[0];
            int key0  = heap.keys[0];

            heap.names[0] = heap.names[heap.countNotVisit - 1];
            heap.keys[0]  = heap.keys[heap.countNotVisit - 1];
            heap.names[heap.countNotVisit - 1] = name0;
            heap.keys[heap.countNotVisit - 1]  = key0;
            heap.countNotVisit--;
            if (heap.countNotVisit > 0)
            {
                heap.Dive(0);
            }
        }
Beispiel #4
0
        //погружение
        public static void Dive(this HeapD heap, int thisIndex)
        {
            int indexChild = heap.GetIndexMinChild(thisIndex);
            int thisKey    = heap.keys[thisIndex];
            int thisName   = heap.names[thisIndex];
            int i          = thisIndex;

            while (indexChild != i && thisKey > heap.keys[indexChild])
            {
                heap.keys[i]              = heap.keys[indexChild];
                heap.names[i]             = heap.names[indexChild];
                heap.index[heap.names[i]] = i;
                i          = indexChild;
                indexChild = heap.GetIndexMinChild(i);
            }
            heap.keys[i]              = thisKey;
            heap.names[i]             = thisName;
            heap.index[heap.names[i]] = i;
        }
Beispiel #5
0
        //всплытие
        public static void Emersion(this HeapD heap, int thisIndex)
        {
            int indexFather = GetIndexFather(thisIndex, heap.d);
            int thisKey     = heap.keys[thisIndex];
            int thisName    = heap.names[thisIndex];
            int i           = thisIndex;

            while (i != 0 && heap.keys[indexFather] > thisKey)
            {
                heap.keys[i]              = heap.keys[indexFather];
                heap.names[i]             = heap.names[indexFather];
                heap.index[heap.names[i]] = i;
                i           = indexFather;
                indexFather = GetIndexFather(i, heap.d);
            }
            heap.keys[i]              = thisKey;
            heap.names[i]             = thisName;
            heap.index[heap.names[i]] = i;
        }
Beispiel #6
0
        public void FirstExperimentForDijkstraDHeap()
        {
            List <int> timeWork = new List <int>();

            graph  = new Node[countNode];
            heap   = new HeapD(sizeHeap, countNode);
            result = new Result(countNode);
            int countEdge = 10;

            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < graph.Length; j++)
                {
                    graph[j] = AddEdge(graph[j], countEdge, countNode - 1);
                }
                DateTime start = DateTime.Now;
                DijkstraDHeap(1000);
                DateTime end = DateTime.Now;
                timeWork.Add((end - start).Milliseconds + (end - start).Seconds * 1000);
            }
        }
Beispiel #7
0
        public static int GetIndexMinChild(this HeapD heap, int thisIndex)
        {
            int indexFisrtChild = GetIndexFirstChild(heap.countNotVisit, heap.d, thisIndex);
            int result          = indexFisrtChild;

            if (result != -1)
            {
                int indexLastChild = GetIndexLastChild(heap.countNotVisit, heap.d, thisIndex);
                int minKey         = heap.keys[indexFisrtChild];
                for (int i = indexFisrtChild + 1; i <= indexLastChild; i++)
                {
                    if (heap.keys[i] < minKey)
                    {
                        minKey = heap.keys[i];
                        result = i;
                    }
                }
            }
            else
            {
                result = thisIndex;
            }
            return(result);
        }