All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.logicng.graphs.algorithms.BronKerbosch Maven / Gradle / Ivy

///////////////////////////////////////////////////////////////////////////
//                   __                _      _   ________               //
//                  / /   ____  ____ _(_)____/ | / / ____/               //
//                 / /   / __ \/ __ `/ / ___/  |/ / / __                 //
//                / /___/ /_/ / /_/ / / /__/ /|  / /_/ /                 //
//               /_____/\____/\__, /_/\___/_/ |_/\____/                  //
//                           /____/                                      //
//                                                                       //
//               The Next Generation Logic Library                       //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
//                                                                       //
//  Copyright 2015-20xx Christoph Zengler                                //
//                                                                       //
//  Licensed under the Apache License, Version 2.0 (the "License");      //
//  you may not use this file except in compliance with the License.     //
//  You may obtain a copy of the License at                              //
//                                                                       //
//  http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                       //
//  Unless required by applicable law or agreed to in writing, software  //
//  distributed under the License is distributed on an "AS IS" BASIS,    //
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      //
//  implied.  See the License for the specific language governing        //
//  permissions and limitations under the License.                       //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

package org.logicng.graphs.algorithms;

import org.logicng.graphs.datastructures.Graph;
import org.logicng.graphs.datastructures.Node;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * This class implements the Bron-Kerbosch-Algorithm, used to compute all maximal Cliques of a Graph. Requires that the
 * ids of the nodes are comparable.
 * @param  the node type of the graph
 * @version 2.0.0
 * @since 1.2
 */
public final class BronKerbosch> {

    private final Graph g;
    private final Comparator> nodeComparator;
    private final Set>> cliques;

    /**
     * Constructor.
     * @param g the graph whose maximal cliques are to be computed
     */
    public BronKerbosch(final Graph g) {
        this.g = g;
        this.nodeComparator = Comparator.comparing(Node::content);
        this.cliques = new LinkedHashSet<>();
    }

    /**
     * Computes the maximal cliques and returns them as a Set of SortedSets of Nodes.
     * @return the maximal cliques.
     */
    public Set>> compute() {
        this.cliques.clear();
        final SortedSet> p = new TreeSet<>(this.nodeComparator);
        p.addAll(this.g.nodes());
        bk(new TreeSet<>(this.nodeComparator), p, new TreeSet<>(this.nodeComparator));
        return this.cliques;
    }

    private void bk(final SortedSet> r, final SortedSet> p, final SortedSet> x) {
        if (p.isEmpty() && x.isEmpty()) {
            this.cliques.add(r);
            return;
        }
        final SortedSet> pvx = new TreeSet<>(new NodeNeighbourComparator());
        pvx.addAll(p);
        pvx.addAll(x);
        final Node u = pvx.last();
        final SortedSet> pwnu = new TreeSet<>(this.nodeComparator);
        pwnu.addAll(p);
        pwnu.removeAll(u.neighbours());
        for (final Node v : pwnu) {
            final SortedSet> nr = new TreeSet<>(this.nodeComparator);
            nr.addAll(r);
            nr.add(v);
            final SortedSet> np = new TreeSet<>(this.nodeComparator);
            final SortedSet> nx = new TreeSet<>(this.nodeComparator);
            for (final Node neigh : v.neighbours()) {
                if (p.contains(neigh)) {
                    np.add(neigh);
                }
                if (x.contains(neigh)) {
                    nx.add(neigh);
                }
            }
            bk(nr, np, nx);
            p.remove(v);
            x.add(v);
        }
    }

    /**
     * Returns the maximal cliques computed with the last call to compute() as a List of Lists of T.
     * @return the maximal cliques
     */
    public List> getCliquesAsTLists() {
        final List> result = new ArrayList<>();
        for (final Set> clique : this.cliques) {
            final List curList = new ArrayList<>();
            for (final Node node : clique) {
                curList.add(node.content());
            }
            result.add(curList);
        }
        return result;
    }

    /**
     * A comparator between nodes, that compares them by number of neighbours.
     * @version 1.2
     * @since 1.2
     */
    private class NodeNeighbourComparator implements Comparator> {

        @Override
        public int compare(final Node n1, final Node n2) {
            if (n1.neighbours().size() > n2.neighbours().size()) {
                return 1;
            } else if (n1.neighbours().size() < n2.neighbours().size()) {
                return -1;
            } else {
                return BronKerbosch.this.nodeComparator.compare(n1, n2);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy