예제 #1
0
        public double MstPrim(List <RgbPixel> distinctColors)                                                // Θ(V^2)
        {
            var mstCost = 0d;                                                                                // Θ(1)
            int startNode = 0, nextNode = 0;                                                                 // Θ(1)

            _key[0]    = 0;                                                                                  // Θ(1)
            _parent[0] = 0;                                                                                  // Θ(1)
            for (var i = 0; i < _VertixSize - 1; i++)                                                        // Θ(v * body)
            {
                _visted[startNode] = true;                                                                   // Θ(1)
                double miniCost = Int32.MaxValue;                                                            // Θ(1)
                for (var j = 0; j < _VertixSize; j++)                                                        // Θ(v * body)
                {
                    if (_visted[j] == false)                                                                 // Θ(1)
                    {
                        var cost = RgbPixel.EuclideanDistance(distinctColors[startNode], distinctColors[j]); // Θ(1)
                        if (cost < _key[j])                                                                  // Θ(1)
                        {
                            _parent[j] = startNode;                                                          // Θ(1)
                            _key[j]    = cost;                                                               // Θ(1)
                        }
                        if (_key[j] < miniCost)                                                              // Θ(1)
                        {
                            nextNode = j;                                                                    // Θ(1)
                            miniCost = _key[j];                                                              // Θ(1)
                        }
                    }
                }
                startNode = nextNode;                                                // Θ(1)
                mstCost  += _key[startNode];                                         // Θ(1)
                edges.Add(new Edge(startNode, _parent[startNode], _key[startNode])); // Θ(1)
            }

            return(Math.Round(mstCost, 2));  // Θ(1)
        }
예제 #2
0
        public double MstPrim(List <RgbPixel> distinctColors)
        {
            int u = 0, curntNode = 0;
            var mstCost = 0d;

            _key[0]    = 0;
            _parent[0] = -1;
            for (int i = 0; i < _VertixSize; i++)
            {
                _visted[u] = true;
                int miniCost = Int32.MaxValue;
                for (int j = 0; j < _VertixSize; j++)
                {
                    if (_visted[j] == false)
                    {
                        var cost = RgbPixel.EuclideanDistance(distinctColors[u], distinctColors[j]);
                        if (cost < _key[j])
                        {
                            _parent[j] = u;
                            _key[j]    = cost;
                        }
                        if (_key[j] < miniCost)
                        {
                            curntNode = j;
                            miniCost  = _key[j];
                        }
                    }
                }
                u        = curntNode;
                mstCost += miniCost;
            }

            return(Math.Round(mstCost, 1));
        }