Ejemplo n.º 1
0
 public CLustring(MaxHeap edgesHeap, List <RGBPixelD> listRGB, Dictionary <int, double>[] AdjListdict)
 {
     adjDictArray   = AdjListdict;                 //⊖(1)
     replacedMatrix = new RGBPixel[256, 256, 256]; //⊖(1)
     size           = listRGB.Count;               //⊖(1)
     red            = green = blue = counter = 0;  //⊖(1)
     this.edgesHeap = edgesHeap;                   //⊖(1)
     this.listRGB   = listRGB;                     //⊖(1)
 }
Ejemplo n.º 2
0
        public void prim()                                       //E Log(D)
        {
            int size = listRGB.Count;                            // Number of Distincit Colors Θ(1)

            bool[] visited = new bool[size];                     //Θ(1)
            Edge[] key     = new Edge[size];                     //Θ(1)
            MSTDictionary = new Dictionary <int, double> [size]; //⊖(1)
            maxHeap       = new MaxHeap(listRGB.Count - 1);      //⊖(1)
            for (int i = 0; i < size; i++)
            {
                key[i] = new Edge(0, double.MaxValue, 0);
            }
            Edge minEdge     = new Edge(0, 0, 0);
            int  edgeCounter = 0;       //Θ(1)

            while (edgeCounter != size) //iteration * body ===> E * O(Body) ==>  E * Log V
            {
                //Edge edgeHolder = edgesHeap.getMinEdge(); //(log E)
                if (visited[minEdge.colorB])                                            //Θ(1)
                {
                    continue;                                                           //Θ(1)
                }
                visited[minEdge.colorB] = true;                                         //Θ(1)
                edgeCounter++;                                                          //Θ(1)
                totalCost += minEdge.weight;                                            //Θ(1)
                if (edgeCounter != 1)                                                   //⊖(1)
                {
                    if (MSTDictionary[minEdge.colorA] == null)                          //⊖(1)
                    {
                        MSTDictionary[minEdge.colorA] = new Dictionary <int, double>(); //⊖(1)
                    }
                    if (MSTDictionary[minEdge.colorB] == null)                          //⊖(1)
                    {
                        MSTDictionary[minEdge.colorB] = new Dictionary <int, double>(); //⊖(1)
                    }
                    MSTDictionary[minEdge.colorA][minEdge.colorB] = minEdge.weight;     //⊖(1)
                    MSTDictionary[minEdge.colorB][minEdge.colorA] = minEdge.weight;     //⊖(1)
                    maxHeap.insertEdge(minEdge);                                        //⊖(log heap size)
                }
                double min = double.MaxValue;
                int    minEdgeIndexHolder = -1;
                for (int i = 0; i < size; i++)                                                  // iteration * body ===> E * O(body) ===> E * log V
                {
                    double weight = calcEuclideanDistance(listRGB[minEdge.colorB], listRGB[i]); //O(1)
                    if (!visited[i] && min > key[i].weight)
                    {
                        min = key[i].weight;
                        minEdgeIndexHolder = i;
                    }
                    if (!visited[i] && key[i].weight > weight)
                    {
                        key[i].weight = weight;
                        key[i].colorA = minEdge.colorB;
                        key[i].colorB = i;
                        //        edgesHeap.insertEdge(new Edge(minEdge.colorB, weight, i)); //O(log E)
                        if (weight < min)
                        {
                            min = weight;
                            minEdgeIndexHolder = i;
                        }
                    }
                }
                if (minEdgeIndexHolder != -1)
                {
                    minEdge = new Edge(key[minEdgeIndexHolder].colorA, key[minEdgeIndexHolder].weight, key[minEdgeIndexHolder].colorB);
                }
            }
            Mean = totalCost / size;
            calcSTDev(maxHeap.getArr(), Mean);
            //   totalCost = Math.Round(Math.Round(sum, 2), 1); //Θ(1)
            Console.WriteLine("Num of Colors: " + size); //Θ(1)

            Console.WriteLine("Sum: " + totalCost);      //Θ(1)
        }