public Dictionary <int, double>[] prim(int size, List <RGBPixelD> listRGB)//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) totalCost = 0; 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; 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; STDev = 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) return(MSTDictionary); }
public void Costruct_MST(RGBPixel[,] ImageMatrix) { Find_Distinct_colors(ImageMatrix); int current_Node = 0; int n = Distinct.Count; int[] Prev = new int[n]; int mini; double mind; bool[,,] Visited = new bool[256, 256, 256]; double[] Distance = new double[n]; double minCost = 0; MST = new Edge[Distinct.Count - 1]; int MST_index = 0; for (int k = 0; k < n - 1; k++) { Visited[Distinct[current_Node].red, Distinct[current_Node].green, Distinct[current_Node].blue] = true; mind = 9999999999999999999; mini = -1; for (int i = 0; i < n; i++) { if (k == 0) { Distance[i] = 9999999999999999999; } if (current_Node == i) { continue; } if (Visited[Distinct[i].red, Distinct[i].green, Distinct[i].blue]) { continue; } double Weight = Convert.ToDouble((Distinct[current_Node].red - Distinct[i].red) * (Distinct[current_Node].red - Distinct[i].red) + (Distinct[current_Node].green - Distinct[i].green) * (Distinct[current_Node].green - Distinct[i].green) + (Distinct[current_Node].blue - Distinct[i].blue) * (Distinct[current_Node].blue - Distinct[i].blue)); if (Weight < Distance[i]) { Distance[i] = Weight; Prev[i] = current_Node; } if (Distance[i] < mind) { mind = Distance[i]; mini = i; } } MST[MST_index] = new Edge(Prev[mini], mini, Math.Sqrt(Distance[mini])); MST_index++; minCost += MST[MST_index - 1].weight; current_Node = mini; } // MessageBox.Show(minCost.ToString()); }
private bool WeightEQUALzero(Edge e) { return(e.weight == 0); }
public void Clustering(int k, ref RGBPixel[,] ImageMatrix) { int[,,] Histogram = new int[256, 256, 256]; ArrVisited = new int[Distinct.Count]; componentRoot = new int[Distinct.Count]; subtreeSize = new int[Distinct.Count]; edgeList = new List <int> [Distinct.Count]; AdjList = new List <int> [Distinct.Count]; for (int i = 0; i < Distinct.Count; i++) { Histogram[Distinct[i].red, Distinct[i].green, Distinct[i].blue] = i; AdjList[i] = new List <int>(0); edgeList[i] = new List <int>(0); } for (int i = 0; i < MST.Length; i++) { AdjList[MST[i].from].Add(MST[i].to); AdjList[MST[i].to].Add(MST[i].from); edgeList[MST[i].from].Add(i); edgeList[MST[i].to].Add(i); } { Array.Sort(MST, delegate(Edge e1, Edge e2) { return(e1.CompareTo(e2)); }); Edge[] MST_ = new Edge[MST.Length - k + 1]; deleted = new bool[MST.Length]; for (int i = 0, current = 0; i < k - 1; i++) { while (deleted[current]) { current++; } int next = current; while (deleted[next]) { next++; } if (MST[current].weight > MST[next].weight) { deleted[current] = true; continue; } subtreeSize = new int[Distinct.Count]; componentRoot = new int[Distinct.Count]; for (int node = 0; node < Distinct.Count; node++) { if (subtreeSize[node] == 0) { dfs(node, -1, node); } } int maximumDiff = int.MinValue; int bestEdge = current; for (int edge = current; edge < MST.Length && MST[edge].weight == MST[current].weight; edge++) { if (deleted[edge]) { continue; } int currentDiff = 0; if (subtreeSize[MST[edge].from] < subtreeSize[MST[edge].to]) { currentDiff = Math.Min(subtreeSize[componentRoot[MST[edge].from]] - subtreeSize[MST[edge].from], subtreeSize[MST[edge].from]); } else { currentDiff = Math.Min(subtreeSize[componentRoot[MST[edge].to]] - subtreeSize[MST[edge].to], subtreeSize[MST[edge].to]); } if (currentDiff >= maximumDiff) { bestEdge = edge; maximumDiff = currentDiff; } } deleted[bestEdge] = true; } int j = 0; for (int i = 0; i < MST.Length; i++) { if (deleted[i]) { continue; } MST_[j] = MST[i]; j++; } MST = MST_; } Colors = new RGBPixelD[k + 1]; Colors_count = new int[k + 1]; AdjList = new List <int> [Distinct.Count]; for (int i = 0; i < Distinct.Count; i++) { AdjList[i] = new List <int>(0); } for (int i = 0; i < MST.Length; i++) { AdjList[MST[i].from].Add(MST[i].to); AdjList[MST[i].to].Add(MST[i].from); } MST = new Edge[0]; for (int i = 0; i < Distinct.Count; i++) { if (ArrVisited[i] == 0) { BFS(i); } } for (int i = 1; i < cluster; i++) { Colors[i].red /= Colors_count[i]; Colors[i].green /= Colors_count[i]; Colors[i].blue /= Colors_count[i]; } for (int i = 0; i < ImageMatrix.GetLength(0); i++) { for (int j = 0; j < ImageMatrix.GetLength(1); j++) { RGBPixel NewColor = new RGBPixel(); NewColor.red = Convert.ToByte(Colors[ArrVisited[Histogram[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue]]].red); NewColor.green = Convert.ToByte(Colors[ArrVisited[Histogram[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue]]].green); NewColor.blue = Convert.ToByte(Colors[ArrVisited[Histogram[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue]]].blue); ImageMatrix[i, j] = NewColor; } } // MessageBox.Show(DateTime.Now.ToLongTimeString()); }