コード例 #1
0
            public virtual void primMST(int[] parent, double[] min_weights, int vertices, RGBPixel[] Nodes)
            {
                bool[]   inHeap = new bool[vertices];
                double[] key    = new double[vertices];

                //create heapNode for all the vertices

                HeapNode[] heapNodes = new HeapNode[vertices];

                for (int i = 0; i < vertices; i++)
                {
                    heapNodes[i]        = new HeapNode(); //heapnode contains(vertex,key)
                    heapNodes[i].vertex = i;
                    heapNodes[i].key    = int.MaxValue;
                    parent[i]           = int.MaxValue;
                    min_weights[i]      = double.MaxValue;
                    inHeap[i]           = true;
                    key[i] = int.MaxValue;
                }
                //decrease the key for the first index

                heapNodes[0].key = 0;
                //add all the vertices to the MinHeap

                MinHeap minHeap = new MinHeap(vertices);

                //add all the vertices to priority queue

                for (int i = 0; i < vertices; i++)
                {
                    minHeap.insert(heapNodes[i]);
                }

                //while minHeap is not empty
                parent[0]      = -1;
                min_weights[0] = 0;
                while (!minHeap.Empty)
                {
                    //extract the min

                    HeapNode extractedNode = minHeap.extractMin();

                    //extracted vertex

                    int extractedVertex = extractedNode.vertex;

                    inHeap[extractedVertex] = false;  //false because it has been extracted.

                    //iterate through all the adjacent vertices

                    //LinkedList<Edge> list = adjacencylist[extractedVertex];


                    double weght;


                    for (int l = 0; l < vertices; l++)
                    {
                        weght = ((Nodes[extractedVertex].red - Nodes[l].red) * (Nodes[extractedVertex].red - Nodes[l].red)) + ((Nodes[extractedVertex].blue - Nodes[l].blue) * (Nodes[extractedVertex].blue - Nodes[l].blue)) + ((Nodes[extractedVertex].green - Nodes[l].green) * (Nodes[extractedVertex].green - Nodes[l].green));

                        //graph.addEdge(j, l, weght);
                        if (inHeap[l])
                        {
                            int destination = l;

                            double newKey = weght;

                            //check if updated key < existing key, if yes, update if

                            if (key[destination] > newKey)
                            {
                                decreaseKey(minHeap, newKey, destination);

                                //update the parent node for destination

                                parent[destination] = extractedVertex;

                                min_weights[destination] = Math.Sqrt(newKey);

                                key[destination] = newKey; //destination vertex carries the weight between it and its source u in old code
                            }
                        }
                    }
                    //Edge edge = i;

                    //only if edge destination is present in heap
                }

                Globals.sum = 0;
                for (int j = 0; j < vertices; j++)      //calculates the sum of the MST
                {
                    Globals.sum += min_weights[j];
                }
                Globals.sum = Math.Round(Globals.sum, 2);
            }
コード例 #2
0
        public static void primMST(RGBPixel[,] imagematrix)
        {
            RGBPixel[] Graph = constructGraph(imagematrix);
            int        size  = Graph.Length;

            bool[]      inHeap    = new bool[size];
            ResultSet[] resultSet = new ResultSet[size];
            double[]    key       = new double[size];
            HeapNode[]  heapNodes = new HeapNode[size];

            for (int i = 0; i < size; i++)
            {
                heapNodes[i]        = new HeapNode();
                heapNodes[i].vertex = i;
                heapNodes[i].key    = double.MaxValue;
                resultSet[i]        = new ResultSet();
                inHeap[i]           = true;
                key[i] = double.MaxValue;
            }
            heapNodes[0].key = 0;

            MinHeap minHeap = new MinHeap(Graph);

            for (int i = 0; i < size; i++)
            {
                minHeap.insert(heapNodes[i]);
            }

            while (!minHeap.isEmpty())
            {
                HeapNode extractedNode = minHeap.extractMin();

                int extractedVertex = extractedNode.vertex;
                inHeap[extractedVertex] = false;

                for (int i = 0; i < Graph.Length; i++)
                {
                    if (inHeap[i])
                    {
                        double newKey = GetEgdeWeight(Graph[extractedVertex], Graph[i]);
                        if (key[i] > newKey)
                        {
                            decreaseKey(minHeap, newKey, i);
                            resultSet[i].parent  = extractedVertex;
                            resultSet[i].weight  = newKey;
                            resultSet[i].current = i;
                            key[i] = newKey;
                        }
                    }
                }
            }

            double W = 0;

            Array.Sort(resultSet, (x, y) => y.weight.CompareTo(x.weight));
            for (int i = 0; i < Graph.Length; i++)
            {
                W += resultSet[i].weight;
            }
            Console.Write("Sum of MST is: ");
            Console.WriteLine(W);
            clustering(resultSet, Graph);
            //Clustering
        }