Home | History | Annotate | Download | only in ceres
      1 // Ceres Solver - A fast non-linear least squares minimizer
      2 // Copyright 2013 Google Inc. All rights reserved.
      3 // http://code.google.com/p/ceres-solver/
      4 //
      5 // Redistribution and use in source and binary forms, with or without
      6 // modification, are permitted provided that the following conditions are met:
      7 //
      8 // * Redistributions of source code must retain the above copyright notice,
      9 //   this list of conditions and the following disclaimer.
     10 // * Redistributions in binary form must reproduce the above copyright notice,
     11 //   this list of conditions and the following disclaimer in the documentation
     12 //   and/or other materials provided with the distribution.
     13 // * Neither the name of Google Inc. nor the names of its contributors may be
     14 //   used to endorse or promote products derived from this software without
     15 //   specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27 // POSSIBILITY OF SUCH DAMAGE.
     28 //
     29 // Author: sameeragarwal (at) google.com (Sameer Agarwal)
     30 
     31 // This include must come before any #ifndef check on Ceres compile options.
     32 #include "ceres/internal/port.h"
     33 
     34 #ifndef CERES_NO_SUITESPARSE
     35 
     36 #include "ceres/single_linkage_clustering.h"
     37 
     38 #include "ceres/graph.h"
     39 #include "ceres/collections_port.h"
     40 #include "ceres/graph_algorithms.h"
     41 
     42 namespace ceres {
     43 namespace internal {
     44 
     45 int ComputeSingleLinkageClustering(
     46     const SingleLinkageClusteringOptions& options,
     47     const Graph<int>& graph,
     48     HashMap<int, int>* membership) {
     49   CHECK_NOTNULL(membership)->clear();
     50 
     51   // Initially each vertex is in its own cluster.
     52   const HashSet<int>& vertices = graph.vertices();
     53   for (HashSet<int>::const_iterator it = vertices.begin();
     54        it != vertices.end();
     55        ++it) {
     56     (*membership)[*it] = *it;
     57   }
     58 
     59   for (HashSet<int>::const_iterator it1 = vertices.begin();
     60        it1 != vertices.end();
     61        ++it1) {
     62     const int vertex1 = *it1;
     63     const HashSet<int>& neighbors = graph.Neighbors(vertex1);
     64     for (HashSet<int>::const_iterator it2 = neighbors.begin();
     65          it2 != neighbors.end();
     66          ++it2) {
     67       const int vertex2 = *it2;
     68 
     69       // Since the graph is undirected, only pay attention to one side
     70       // of the edge and ignore weak edges.
     71       if ((vertex1 > vertex2) ||
     72           (graph.EdgeWeight(vertex1, vertex2) < options.min_similarity)) {
     73         continue;
     74       }
     75 
     76       // Use a union-find algorithm to keep track of the clusters.
     77       const int c1 = FindConnectedComponent(vertex1, membership);
     78       const int c2 = FindConnectedComponent(vertex2, membership);
     79 
     80       if (c1 == c2) {
     81         continue;
     82       }
     83 
     84       if (c1 < c2) {
     85         (*membership)[c2] = c1;
     86       } else {
     87         (*membership)[c1] = c2;
     88       }
     89     }
     90   }
     91 
     92   // Make sure that every vertex is connected directly to the vertex
     93   // identifying the cluster.
     94   int num_clusters = 0;
     95   for (HashMap<int, int>::iterator it = membership->begin();
     96        it != membership->end();
     97        ++it) {
     98     it->second = FindConnectedComponent(it->first, membership);
     99     if (it->first == it->second) {
    100       ++num_clusters;
    101     }
    102   }
    103 
    104   return num_clusters;
    105 }
    106 
    107 }  // namespace internal
    108 }  // namespace ceres
    109 
    110 #endif  // CERES_NO_SUITESPARSE
    111