Class ChineseWhispers<V,​E>

  • Type Parameters:
    V - the type of nodes in the graph
    E - the type of edges in the graph
    All Implemented Interfaces:
    Clustering<V>

    public class ChineseWhispers<V,​E>
    extends Object
    implements Clustering<V>
    Implementation of the Chinese Whispers algorithm.
    See Also:
    Biemann (TextGraphs-1)
    • Field Detail

      • ITERATIONS

        public static final int ITERATIONS
        The default number of Chinese Whispers iterations.
        See Also:
        Constant Field Values
      • graph

        protected final org.jgrapht.Graph<V,​E> graph
        The graph.
      • weighting

        protected final NodeWeighting<V,​E> weighting
        The node weighting approach.
      • iterations

        protected final int iterations
        The number of iterations.
      • random

        protected final Random random
        The random number generator.
      • labels

        protected Map<V,​Integer> labels
        The mapping of nodes to labels.
      • steps

        protected int steps
        The number of actual algorithm iterations.
    • Constructor Detail

      • ChineseWhispers

        public ChineseWhispers​(org.jgrapht.Graph<V,​E> graph,
                               NodeWeighting<V,​E> weighting,
                               int iterations,
                               Random random)
        Create an instance of the Chinese Whispers algorithm.
        Parameters:
        graph - the graph
        weighting - the node weighting approach
        iterations - the number of iterations
        random - the random number generator
      • ChineseWhispers

        public ChineseWhispers​(org.jgrapht.Graph<V,​E> graph,
                               NodeWeighting<V,​E> weighting)
        Create an instance of the Chinese Whispers algorithm.
        Parameters:
        graph - the graph
        weighting - the node weighting approach
    • Method Detail

      • provider

        public static <V,​E> Function<org.jgrapht.Graph<V,​E>,​Clustering<V>> provider​(NodeWeighting<V,​E> weighting)
        A factory function that sets up the algorithm for the given graph.
        Type Parameters:
        V - the type of nodes in the graph
        E - the type of edges in the graph
        Parameters:
        weighting - the node weighting approach
        Returns:
        a factory function that sets up the algorithm for the given graph
      • provider

        public static <V,​E> Function<org.jgrapht.Graph<V,​E>,​Clustering<V>> provider​(NodeWeighting<V,​E> weighting,
                                                                                                      int iterations,
                                                                                                      Random random)
        A factory function that sets up the algorithm for the given graph.
        Type Parameters:
        V - the type of nodes in the graph
        E - the type of edges in the graph
        Parameters:
        weighting - the node weighting approach
        iterations - the number of iterations
        random - the random number generator
        Returns:
        a factory function that sets up the algorithm for the given graph
      • fit

        public void fit()
        Description copied from interface: Clustering
        Run the algorithm to induce the parameters of the clusters.
        Specified by:
        fit in interface Clustering<V>
      • step

        protected int step​(List<V> nodes)
        Perform one iteration of the algorithm.
        Parameters:
        nodes - the list of nodes
        Returns:
        whether any label changed or not
      • score

        protected Map<Integer,​Double> score​(org.jgrapht.Graph<V,​E> graph,
                                                  Map<V,​Integer> labels,
                                                  NodeWeighting<V,​E> weighting,
                                                  V node)
        Score the label weights in the given neighborhood graph, which is a subgraph of graph. This method sums the node weights corresponding to each label.
        Parameters:
        graph - the neighborhood graph
        labels - the map of graph nodes to their labels
        weighting - the node weighting approach
        node - the target node
        Returns:
        a mapping of labels to sums of their weights
      • getSteps

        public int getSteps()
        Return the number of iterations actually performed during fit(). Should be no larger than the value of getIterations().
        Returns:
        the number of iterations