summaryrefslogtreecommitdiff
path: root/elki/src/main/java/de/lmu/ifi/dbs/elki/algorithm/clustering/subspace/clique/CLIQUEUnit.java
blob: cf96670eeed9bf971107a6bac238d4061088a1e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package de.lmu.ifi.dbs.elki.algorithm.clustering.subspace.clique;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2015
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Iterator;

import de.lmu.ifi.dbs.elki.data.NumberVector;
import de.lmu.ifi.dbs.elki.database.ids.DBIDRef;
import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil;
import de.lmu.ifi.dbs.elki.database.ids.DBIDs;
import de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs;
import de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs;

/**
 * Represents a unit in the CLIQUE algorithm.
 * 
 * @author Elke Achtert
 * 
 * @apiviz.composedOf CLIQUEInterval
 * @apiviz.composedOf ModifiableDBIDs
 * 
 * @param <V> the type of NumberVector this unit contains
 */
public class CLIQUEUnit<V extends NumberVector> {
  /**
   * The one-dimensional intervals of which this unit is build.
   */
  private ArrayList<CLIQUEInterval> intervals;

  /**
   * The ids of the feature vectors this unit contains.
   */
  private ModifiableDBIDs ids;

  /**
   * Flag that indicates if this unit is already assigned to a cluster.
   */
  private boolean assigned;

  /**
   * Creates a new k-dimensional unit for the given intervals.
   * 
   * @param intervals the intervals belonging to this unit
   * @param ids the ids of the feature vectors belonging to this unit
   */
  private CLIQUEUnit(ArrayList<CLIQUEInterval> intervals, ModifiableDBIDs ids) {
    this.intervals = intervals;
    this.ids = ids;
    assigned = false;
  }

  /**
   * Creates a new one-dimensional unit for the given interval.
   * 
   * @param interval the interval belonging to this unit
   */
  public CLIQUEUnit(CLIQUEInterval interval) {
    intervals = new ArrayList<>();
    intervals.add(interval);
    ids = DBIDUtil.newHashSet();
    assigned = false;
  }

  /**
   * Returns true, if the intervals of this unit contain the specified feature
   * vector.
   * 
   * @param vector the feature vector to be tested for containment
   * @return true, if the intervals of this unit contain the specified feature
   *         vector, false otherwise
   */
  public boolean contains(V vector) {
    for(CLIQUEInterval interval : intervals) {
      final double value = vector.doubleValue(interval.getDimension());
      if(interval.getMin() > value || value >= interval.getMax()) {
        return false;
      }
    }
    return true;
  }

  /**
   * Adds the id of the specified feature vector to this unit, if this unit
   * contains the feature vector.
   * 
   * @param id Vector id
   * @param vector the feature vector to be added
   * @return true, if this unit contains the specified feature vector, false
   *         otherwise
   */
  public boolean addFeatureVector(DBIDRef id, V vector) {
    if(contains(vector)) {
      ids.add(id);
      return true;
    }
    return false;
  }

  /**
   * Returns the number of feature vectors this unit contains.
   * 
   * @return the number of feature vectors this unit contains
   */
  public int numberOfFeatureVectors() {
    return ids.size();
  }

  /**
   * Returns the selectivity of this unit, which is defined as the fraction of
   * total feature vectors contained in this unit.
   * 
   * @param total the total number of feature vectors
   * @return the selectivity of this unit
   */
  public double selectivity(double total) {
    return ids.size() / total;
  }

  /**
   * Returns a sorted set of the intervals of which this unit is build.
   * 
   * @return a sorted set of the intervals of which this unit is build
   */
  public ArrayList<CLIQUEInterval> getIntervals() {
    return intervals;
  }

  /**
   * Returns the interval of the specified dimension.
   * 
   * @param dimension the dimension of the interval to be returned
   * @return the interval of the specified dimension
   */
  public CLIQUEInterval getInterval(int dimension) {
    // TODO: use binary search instead?
    for(CLIQUEInterval i : intervals) {
      if(i.getDimension() == dimension) {
        return i;
      }
    }
    return null;
  }

  /**
   * Returns true if this unit contains the left neighbor of the specified
   * interval.
   * 
   * @param i the interval
   * @return true if this unit contains the left neighbor of the specified
   *         interval, false otherwise
   */
  public boolean containsLeftNeighbor(CLIQUEInterval i) {
    CLIQUEInterval interval = getInterval(i.getDimension());
    return (interval != null) && (interval.getMax() == i.getMin());
  }

  /**
   * Returns true if this unit contains the right neighbor of the specified
   * interval.
   * 
   * @param i the interval
   * @return true if this unit contains the right neighbor of the specified
   *         interval, false otherwise
   */
  public boolean containsRightNeighbor(CLIQUEInterval i) {
    CLIQUEInterval interval = getInterval(i.getDimension());
    return (interval != null) && (interval.getMin() == i.getMax());
  }

  /**
   * Returns true if this unit is already assigned to a cluster.
   * 
   * @return true if this unit is already assigned to a cluster, false
   *         otherwise.
   */
  public boolean isAssigned() {
    return assigned;
  }

  /**
   * Marks this unit as assigned to a cluster.
   */
  public void markAsAssigned() {
    this.assigned = true;
  }

  /**
   * Returns the ids of the feature vectors this unit contains.
   * 
   * @return the ids of the feature vectors this unit contains
   */
  public DBIDs getIds() {
    return ids;
  }

  /**
   * Joins this unit with the specified unit.
   * 
   * @param other the unit to be joined
   * @param all the overall number of feature vectors
   * @param tau the density threshold for the selectivity of a unit
   * @return the joined unit if the selectivity of the join result is equal or
   *         greater than tau, null otherwise
   */
  public CLIQUEUnit<V> join(CLIQUEUnit<V> other, double all, double tau) {
    CLIQUEInterval i1 = this.intervals.get(this.intervals.size() - 1);
    CLIQUEInterval i2 = other.intervals.get(other.intervals.size() - 1);
    if(i1.getDimension() >= i2.getDimension()) {
      return null;
    }

    Iterator<CLIQUEInterval> it1 = this.intervals.iterator();
    Iterator<CLIQUEInterval> it2 = other.intervals.iterator();
    ArrayList<CLIQUEInterval> resultIntervals = new ArrayList<>();
    for(int i = 0; i < this.intervals.size() - 1; i++) {
      i1 = it1.next();
      i2 = it2.next();
      if(!i1.equals(i2)) {
        return null;
      }
      resultIntervals.add(i1);
    }
    resultIntervals.add(this.intervals.get(this.intervals.size() - 1));
    resultIntervals.add(other.intervals.get(other.intervals.size() - 1));

    HashSetModifiableDBIDs resultIDs = DBIDUtil.newHashSet(this.ids);
    resultIDs.retainAll(other.ids);

    if(resultIDs.size() / all >= tau) {
      return new CLIQUEUnit<>(resultIntervals, resultIDs);
    }

    return null;
  }

  /**
   * Returns a string representation of this unit that contains the intervals of
   * this unit.
   * 
   * @return a string representation of this unit
   */
  @Override
  public String toString() {
    StringBuilder result = new StringBuilder();
    for(CLIQUEInterval interval : intervals) {
      result.append(interval).append(' ');
    }

    return result.toString();
  }
}