コード例 #1
0
        public void computeMST(GraphAdjList adjList,
                               int start)
        {
            initialize(adjList);

            Q.updatePriority(new HeapClass()
            {
                data     = start,
                priority = 0
            });

            while (Q.count() != 0)
            {
                HeapClass priorityElement = Q.getMax();
                foreach (var v in adjList.getAdj(priorityElement.data))
                {
                    if (Q.contains(v.Item1) && -1 * v.Item2 > Q.getPriority(v.Item1))
                    {
                        Q.updatePriority(new HeapClass()
                        {
                            data     = v.Item1,
                            priority = -1 * v.Item2
                        });

                        parents[v.Item1] = priorityElement.data;
                    }
                }
            }

            Console.WriteLine("\nvertex\tparent");
            for (int i = 0; i < V; i++)
            {
                Console.WriteLine("\n" + i + "\t\t" + parents[i]);
            }
        }
コード例 #2
0
ファイル: PriorityQueue.cs プロジェクト: abhisek1291/graphs
        void swap(int a, int b)
        {
            HeapClass temp = arr[a];

            arr[a] = arr[b];
            arr[b] = temp;
        }
コード例 #3
0
ファイル: Dijkstra.cs プロジェクト: abhisek1291/graphs
        public void computeShortestPaths(GraphAdjList adjList,
                                         int source)
        {
            initializeSingleSource(source);
            extractEdges(adjList);

            for (int i = 0; i < V; i++)
            {
                Q.insertHeap(new HeapClass()
                {
                    data = i, priority = distances[i]
                });
            }

            while (Q.count() != 0)
            {
                HeapClass elem = Q.getMax();
                foreach (var v in adjList.getAdj(elem.data))
                {
                    relax(elem.data, v.Item1, v.Item2);
                }
            }

            Console.WriteLine("\nvertex\tparent\tdistance");
            for (int i = 0; i < V; i++)
            {
                Console.WriteLine(i + "\t" + parents[i] + "\t" + (-1 * distances[i]).ToString());
            }
        }
コード例 #4
0
ファイル: PriorityQueue.cs プロジェクト: abhisek1291/graphs
        public void updatePriority(HeapClass elem)
        {
            int elemIndex = Array.IndexOf(arr, arr.Where(k => k.data == elem.data).FirstOrDefault());

            arr[elemIndex].priority = elem.priority;
            while (elemIndex != 0 && arr[parent(elemIndex)].priority < arr[elemIndex].priority)
            {
                swap(elemIndex, parent(elemIndex));
                elemIndex = parent(elemIndex);
            }
        }
コード例 #5
0
ファイル: PriorityQueue.cs プロジェクト: abhisek1291/graphs
        public void insertHeap(HeapClass data)
        {
            if (heapCurrentFillLevel == heapSize)
            {
                throw new Exception("heap size exceeded");
            }

            heapCurrentFillLevel++;
            int currentIndex = heapCurrentFillLevel - 1;

            arr[currentIndex] = data;

            while (currentIndex != 0 && arr[parent(currentIndex)].priority < arr[currentIndex].priority)
            {
                swap(currentIndex, parent(currentIndex));
                currentIndex = parent(currentIndex);
            }
        }
コード例 #6
0
ファイル: PriorityQueue.cs プロジェクト: abhisek1291/graphs
        public HeapClass getMax()
        {
            if (heapCurrentFillLevel <= 0)
            {
                throw new Exception("heapsize less than 0");
            }

            if (heapCurrentFillLevel == 1)
            {
                heapCurrentFillLevel -= 1;
                return(arr[heapCurrentFillLevel]);
            }

            //Console.Write(heapCurrentFillLevel);
            HeapClass element = arr[0];

            arr[0] = arr[heapCurrentFillLevel - 1];
            heapCurrentFillLevel -= 1;

            heapify(0);
            return(element);
        }