|
| 1 | +/*- |
| 2 | + * Copyright 2023 Barak Ugav |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.jgalgo; |
| 17 | + |
| 18 | +import com.jgalgo.graph.Graph; |
| 19 | +import it.unimi.dsi.fastutil.ints.IntCollection; |
| 20 | + |
| 21 | +/** |
| 22 | + * Cores computing algorithm. |
| 23 | + * <p> |
| 24 | + * Given a graph \(G=(V,E)\), a subgraph \(H\) induced by a subset of vertices \(W\) is a \(k\)-core or a core of order |
| 25 | + * \(k\) if \(\forall v \in W : deg_H(v) \geq k\) and \(H\) is a maximum subgraph with this property. The core number of |
| 26 | + * vertex is the highest order of a core that contains this vertex. The degree \(deg(v)\) can be: in-degree, out-degree, |
| 27 | + * in-degree + out-degree, determining different types of cores. |
| 28 | + * |
| 29 | + * @see CoreAlgo.DegreeType |
| 30 | + * @author Barak Ugav |
| 31 | + */ |
| 32 | +public interface CoreAlgo { |
| 33 | + |
| 34 | + /** |
| 35 | + * Compute the cores of the graph with respect to both in and out degree of the vertices. |
| 36 | + * <p> |
| 37 | + * For a detail description of the cores definition, see the interface documentation {@link CoreAlgo}. |
| 38 | + * |
| 39 | + * @param g a graph |
| 40 | + * @return the cores of the graph |
| 41 | + */ |
| 42 | + default CoreAlgo.Result computeCores(Graph g) { |
| 43 | + return computeCores(g, DegreeType.OutAndInDegree); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Compute the cores of the graph with respect the given degree type. |
| 48 | + * <p> |
| 49 | + * Cores are defined with respect to either the out edges, in edges, or both. For undirected graphs the degree type |
| 50 | + * is ignored. |
| 51 | + * <p> |
| 52 | + * For a detail description of the cores definition, see the interface documentation {@link CoreAlgo}. |
| 53 | + * |
| 54 | + * @param g a graph |
| 55 | + * @param degreeType the degree type the cores are computed with respect to |
| 56 | + * @return the cores of the graph |
| 57 | + */ |
| 58 | + CoreAlgo.Result computeCores(Graph g, DegreeType degreeType); |
| 59 | + |
| 60 | + /** |
| 61 | + * The degree type the cores are defined with respect to. |
| 62 | + * <p> |
| 63 | + * A \(k\)-core is a maximal set of vertices such that the graph induced by the set has minimum degree \(k\). |
| 64 | + * Different types of degrees can be considered, yielding different types of cores. For undirected graphs the degree |
| 65 | + * type has no effect. |
| 66 | + * |
| 67 | + * @see CoreAlgo |
| 68 | + * @see CoreAlgo#computeCores(Graph, DegreeType) |
| 69 | + * @author Barak Ugav |
| 70 | + */ |
| 71 | + static enum DegreeType { |
| 72 | + /** |
| 73 | + * Cores will be computed with respect to the out-degree of the vertices, namely the number of outgoing edges. |
| 74 | + */ |
| 75 | + OutDegree, |
| 76 | + |
| 77 | + /** |
| 78 | + * Cores will be computed with respect to the in-degree of the vertices, namely the number of incoming edges. |
| 79 | + */ |
| 80 | + InDegree, |
| 81 | + |
| 82 | + /** |
| 83 | + * Cores will be computed with respect to the sum of the in and out degrees of the vertices. |
| 84 | + */ |
| 85 | + OutAndInDegree |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * The result of the cores computation. |
| 90 | + * |
| 91 | + * @author Barak Ugav |
| 92 | + */ |
| 93 | + static interface Result { |
| 94 | + |
| 95 | + /** |
| 96 | + * The core number of the given vertex. |
| 97 | + * <p> |
| 98 | + * The core number of a vertex is the highest order of a core that contains this vertex. |
| 99 | + * |
| 100 | + * @param v a vertex in the graph |
| 101 | + * @return the core number of the vertex |
| 102 | + */ |
| 103 | + int vertexCoreNum(int v); |
| 104 | + |
| 105 | + /** |
| 106 | + * The maximum core number of the graph. |
| 107 | + * |
| 108 | + * @return the maximum core number of the graph |
| 109 | + */ |
| 110 | + int maxCore(); |
| 111 | + |
| 112 | + /** |
| 113 | + * The vertices of the given core. |
| 114 | + * |
| 115 | + * @param core the core number (order) |
| 116 | + * @return the vertices of the core |
| 117 | + */ |
| 118 | + IntCollection coreVertices(int core); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Create a new builder for core algorithms. |
| 124 | + * |
| 125 | + * @return a new builder for core algorithms |
| 126 | + */ |
| 127 | + static CoreAlgo.Builder newBuilder() { |
| 128 | + return CoreAlgoImpl::new; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * A builder for {@link CoreAlgo} objects. |
| 133 | + * |
| 134 | + * @see CoreAlgo#newBuilder() |
| 135 | + * @author Barak Ugav |
| 136 | + */ |
| 137 | + static interface Builder { |
| 138 | + |
| 139 | + /** |
| 140 | + * Build a new {@link CoreAlgo} object. |
| 141 | + * |
| 142 | + * @return a new {@link CoreAlgo} object |
| 143 | + */ |
| 144 | + CoreAlgo build(); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments