Пример #1
0
        private bool CalculateClusterMembership(List <INode> clusters, List <INode> classes)
        {
            bool changed = false;

            for (int i = 0; i < classes.Count; i++)
            {
                int    newClusterId = -1;
                double min_distance = -1;
                double distance     = -1;
                for (int k = 0; k < ClusterNumber; k++)
                {
                    // Distance - it is similarity of entities
                    // Max similarity means that distance between entities is minimal
                    distance = NodeSimilarity.JaccardCoefficient(clusters[k], classes[i]);
                    if (newClusterId == -1 || distance > min_distance)
                    {
                        newClusterId = k;
                        min_distance = distance;
                    }
                }

                // Mark class to move into another cluster
                if (newClusterId != classes[i].ClusterId)
                {
                    classes[i].ClusterId = newClusterId;
                    changed = true;
                }
            }
            return(changed);
        }
Пример #2
0
        private bool ClusteringStep(List <INode> data, ref int clusterCounter)
        {
            double[,] distances = new double[data.Count, data.Count];

            double minimal_distance      = -1;
            int    firstNodeToJoinIndex  = -1;
            int    secondNodeToJoinIndex = -1;

            for (int i = 0; i < data.Count; i++)
            {
                for (int j = i; j < data.Count; j++)
                {
                    if (i != j)
                    {
                        double distance = NodeSimilarity.JaccardCoefficient(data[i], data[j]);
                        distances[i, j] = distance;

                        // Max distance means that object are most similar
                        if (minimal_distance == -1 || distance > minimal_distance)
                        {
                            minimal_distance      = distance;
                            firstNodeToJoinIndex  = i;
                            secondNodeToJoinIndex = j;
                        }
                    }
                }
            }

            // No joined clusters
            if (minimal_distance == -1)
            {
                return(false);
            }

            INode  firstNode   = data[firstNodeToJoinIndex];
            INode  secondNode  = data[secondNodeToJoinIndex];
            string clusterName = "Cluster #" + clusterCounter++;

            // Join clusters and remove them from list
            INode joinedNode = new Node(clusterName, firstNode, secondNode);

            data.RemoveAt(firstNodeToJoinIndex);
            if (firstNodeToJoinIndex < secondNodeToJoinIndex)
            {
                secondNodeToJoinIndex--;
            }
            data.RemoveAt(secondNodeToJoinIndex);
            data.Add(joinedNode);

            return(true);
        }