|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Amaya. |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 17 | + * MA 02110-1301 USA |
| 18 | + */ |
| 19 | +package cu.edu.cujae.graphy.algorithms; |
| 20 | + |
| 21 | +import cu.edu.cujae.graphy.core.Graph; |
| 22 | +import cu.edu.cujae.graphy.core.iterators.GraphIterator; |
| 23 | +import cu.edu.cujae.graphy.utils.HashTuple; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.TreeSet; |
| 28 | + |
| 29 | +/** |
| 30 | + * The <b>Bron-Kerbosch algorithm</b> is an enumeration algorithm for finding all |
| 31 | + * maximal cliques in an undirected graph. That is, it lists all the subsets of |
| 32 | + * vertices with the properties that each pair of vertices in one of the listed |
| 33 | + * subsets is connected by an edge, and no listed subset can have any vertices |
| 34 | + * added to it while preserving its complete connectivity. An isolated vertex is |
| 35 | + * not considered a maximal clique. |
| 36 | + * |
| 37 | + * The variant of the algorithm implemented is the one that involves a "pivot element" |
| 38 | + * for efficiency purposes. |
| 39 | + * |
| 40 | + * @author Amaya D. Fuentes |
| 41 | + * @param <T> |
| 42 | + */ |
| 43 | +public class BronKerboschAlgorithm<T> extends AbstractAlgorithm<Collection<Collection<Integer>>> |
| 44 | +{ |
| 45 | + private Set<Integer> vertices; |
| 46 | + private HashMap<Integer, Collection<Integer>> neighbors; |
| 47 | + |
| 48 | + public BronKerboschAlgorithm(Graph<T> graph) { |
| 49 | + |
| 50 | + super(new HashTuple<Collection<Integer>>()); |
| 51 | + |
| 52 | + if(graph.isDirected()) { |
| 53 | + throw new IllegalArgumentException("The graph is directed"); |
| 54 | + } |
| 55 | + |
| 56 | + if(graph.size() == 0) { |
| 57 | + throw new IllegalArgumentException("The graph is empty."); |
| 58 | + } |
| 59 | + |
| 60 | + //initialize class fields |
| 61 | + this.vertices = new TreeSet<>(); |
| 62 | + this.neighbors = new HashMap<>(); |
| 63 | + |
| 64 | + //get the vertices and their neighbors, excluding isolated vertices |
| 65 | + GraphIterator<T> dfsIterator = (GraphIterator<T>) graph.depthFirstSearchIterator(true); |
| 66 | + while(dfsIterator.hasNext()) { |
| 67 | + dfsIterator.next(); |
| 68 | + if(!dfsIterator.getAllAdjacentVertices().isEmpty()) { |
| 69 | + vertices.add(dfsIterator.getLabel()); |
| 70 | + neighbors.put(dfsIterator.getLabel(),dfsIterator.getAllAdjacentVertices()); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Algorithm<Collection<Collection<Integer>>> apply() { |
| 77 | + |
| 78 | + Set<Integer> P = vertices; |
| 79 | + Set<Integer> R = new TreeSet<>(); |
| 80 | + Set<Integer> X = new TreeSet<>(); |
| 81 | + |
| 82 | + maximalCliques(R, P, X); |
| 83 | + |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Recursive method for finding all maximal cliques (Bron-Kerbosch algorithm |
| 89 | + * with pivot). |
| 90 | + * |
| 91 | + * @param R - The set to fill with the vertices that form a maximal clique |
| 92 | + * @param P - The set of candidate vertices to be added to R |
| 93 | + * @param X - The set of processed vertices or belonging to a maximal clique |
| 94 | + */ |
| 95 | + private void maximalCliques(Set<Integer> R, Set<Integer> P, Set<Integer> X) { |
| 96 | + |
| 97 | + if(P.isEmpty() && X.isEmpty()) { |
| 98 | + Collection<Collection<Integer>> result = getResult(); |
| 99 | + result.add(R); |
| 100 | + } |
| 101 | + else { |
| 102 | + Set<Integer> P1 = new TreeSet<>(P); |
| 103 | + Integer u = findPivot(unite(P1, X)); |
| 104 | + for(Integer v: remove(P, u)) { |
| 105 | + maximalCliques(unite(R, v), intersect(P1, v), intersect(X, v)); |
| 106 | + P1.remove(v); |
| 107 | + X.add(v); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * Method for finding the element of a set with the higher degree in the |
| 115 | + * DFS search tree (the most neighbors) |
| 116 | + * |
| 117 | + * @param set - The set where the pivot will be found |
| 118 | + * @return The element of the set with the highest degree |
| 119 | + */ |
| 120 | + private Integer findPivot(Set<Integer> set) { |
| 121 | + Integer pivot = null; |
| 122 | + int maxDegree = 0; |
| 123 | + for(Integer vertex: set) { |
| 124 | + int degree = neighbors.get(vertex).size(); |
| 125 | + if(degree >= maxDegree) { |
| 126 | + maxDegree = degree; |
| 127 | + pivot = vertex; |
| 128 | + } |
| 129 | + } |
| 130 | + return pivot; |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + /** |
| 136 | + * Method to get the intersection of a set with the neighbors of a vertex |
| 137 | + * without altering the original set. |
| 138 | + * |
| 139 | + * @param set |
| 140 | + * @param vertex |
| 141 | + * @return The set corresponding to the intersection |
| 142 | + */ |
| 143 | + private Set<Integer> intersect(Set<Integer> set, Integer vertex) { |
| 144 | + Set<Integer> intersection = new TreeSet<>(set); |
| 145 | + intersection.retainAll(neighbors.get(vertex)); |
| 146 | + return intersection; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Method to get the union of two sets without altering them |
| 151 | + * |
| 152 | + * @param set1 |
| 153 | + * @param set2 |
| 154 | + * @return The set corresponding to the union of set1 and set2 |
| 155 | + */ |
| 156 | + private Set<Integer> unite(Set<Integer> set1, Set<Integer> set2) { |
| 157 | + Set<Integer> union = new TreeSet<>(set1); |
| 158 | + union.addAll(set2); |
| 159 | + return union; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Method to get the union of a set and the neighbors of a vertex without |
| 164 | + * altering the original set |
| 165 | + * |
| 166 | + * @param set |
| 167 | + * @param vertex |
| 168 | + * @return The set corresponding to the union |
| 169 | + */ |
| 170 | + private Set<Integer> unite(Set<Integer> set, Integer vertex) { |
| 171 | + Set<Integer> union = new TreeSet<>(set); |
| 172 | + union.add(vertex); |
| 173 | + return union; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Method to get the difference of a set with the neighbors of a vertex |
| 178 | + * without altering the original set |
| 179 | + * |
| 180 | + * @param set |
| 181 | + * @param vertex |
| 182 | + * @return The set corresponding to the difference |
| 183 | + */ |
| 184 | + private Set<Integer> remove(Set<Integer> set, Integer vertex) { |
| 185 | + Set<Integer> removal = new TreeSet<>(set); |
| 186 | + removal.removeAll(neighbors.get(vertex)); |
| 187 | + return removal; |
| 188 | + } |
| 189 | +} |
0 commit comments