Blame internal/ceres/graph_algorithms.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
//
Packit ea1746
// Various algorithms that operate on undirected graphs.
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_GRAPH_ALGORITHMS_H_
Packit ea1746
#define CERES_INTERNAL_GRAPH_ALGORITHMS_H_
Packit ea1746
Packit ea1746
#include <algorithm>
Packit ea1746
#include <vector>
Packit ea1746
#include <utility>
Packit ea1746
#include "ceres/collections_port.h"
Packit ea1746
#include "ceres/graph.h"
Packit ea1746
#include "ceres/wall_time.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
// Compare two vertices of a graph by their degrees, if the degrees
Packit ea1746
// are equal then order them by their ids.
Packit ea1746
template <typename Vertex>
Packit ea1746
class VertexTotalOrdering {
Packit ea1746
 public:
Packit ea1746
  explicit VertexTotalOrdering(const Graph<Vertex>& graph)
Packit ea1746
      : graph_(graph) {}
Packit ea1746
Packit ea1746
  bool operator()(const Vertex& lhs, const Vertex& rhs) const {
Packit ea1746
    if (graph_.Neighbors(lhs).size() == graph_.Neighbors(rhs).size()) {
Packit ea1746
      return lhs < rhs;
Packit ea1746
    }
Packit ea1746
    return graph_.Neighbors(lhs).size() < graph_.Neighbors(rhs).size();
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const Graph<Vertex>& graph_;
Packit ea1746
};
Packit ea1746
Packit ea1746
template <typename Vertex>
Packit ea1746
class VertexDegreeLessThan {
Packit ea1746
 public:
Packit ea1746
  explicit VertexDegreeLessThan(const Graph<Vertex>& graph)
Packit ea1746
      : graph_(graph) {}
Packit ea1746
Packit ea1746
  bool operator()(const Vertex& lhs, const Vertex& rhs) const {
Packit ea1746
    return graph_.Neighbors(lhs).size() < graph_.Neighbors(rhs).size();
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const Graph<Vertex>& graph_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Order the vertices of a graph using its (approximately) largest
Packit ea1746
// independent set, where an independent set of a graph is a set of
Packit ea1746
// vertices that have no edges connecting them. The maximum
Packit ea1746
// independent set problem is NP-Hard, but there are effective
Packit ea1746
// approximation algorithms available. The implementation here uses a
Packit ea1746
// breadth first search that explores the vertices in order of
Packit ea1746
// increasing degree. The same idea is used by Saad & Li in "MIQR: A
Packit ea1746
// multilevel incomplete QR preconditioner for large sparse
Packit ea1746
// least-squares problems", SIMAX, 2007.
Packit ea1746
//
Packit ea1746
// Given a undirected graph G(V,E), the algorithm is a greedy BFS
Packit ea1746
// search where the vertices are explored in increasing order of their
Packit ea1746
// degree. The output vector ordering contains elements of S in
Packit ea1746
// increasing order of their degree, followed by elements of V - S in
Packit ea1746
// increasing order of degree. The return value of the function is the
Packit ea1746
// cardinality of S.
Packit ea1746
template <typename Vertex>
Packit ea1746
int IndependentSetOrdering(const Graph<Vertex>& graph,
Packit ea1746
                           std::vector<Vertex>* ordering) {
Packit ea1746
  const HashSet<Vertex>& vertices = graph.vertices();
Packit ea1746
  const int num_vertices = vertices.size();
Packit ea1746
Packit ea1746
  CHECK_NOTNULL(ordering);
Packit ea1746
  ordering->clear();
Packit ea1746
  ordering->reserve(num_vertices);
Packit ea1746
Packit ea1746
  // Colors for labeling the graph during the BFS.
Packit ea1746
  const char kWhite = 0;
Packit ea1746
  const char kGrey = 1;
Packit ea1746
  const char kBlack = 2;
Packit ea1746
Packit ea1746
  // Mark all vertices white.
Packit ea1746
  HashMap<Vertex, char> vertex_color;
Packit ea1746
  std::vector<Vertex> vertex_queue;
Packit ea1746
  for (typename HashSet<Vertex>::const_iterator it = vertices.begin();
Packit ea1746
       it != vertices.end();
Packit ea1746
       ++it) {
Packit ea1746
    vertex_color[*it] = kWhite;
Packit ea1746
    vertex_queue.push_back(*it);
Packit ea1746
  }
Packit ea1746
Packit ea1746
Packit ea1746
  std::sort(vertex_queue.begin(), vertex_queue.end(),
Packit ea1746
            VertexTotalOrdering<Vertex>(graph));
Packit ea1746
Packit ea1746
  // Iterate over vertex_queue. Pick the first white vertex, add it
Packit ea1746
  // to the independent set. Mark it black and its neighbors grey.
Packit ea1746
  for (int i = 0; i < vertex_queue.size(); ++i) {
Packit ea1746
    const Vertex& vertex = vertex_queue[i];
Packit ea1746
    if (vertex_color[vertex] != kWhite) {
Packit ea1746
      continue;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    ordering->push_back(vertex);
Packit ea1746
    vertex_color[vertex] = kBlack;
Packit ea1746
    const HashSet<Vertex>& neighbors = graph.Neighbors(vertex);
Packit ea1746
    for (typename HashSet<Vertex>::const_iterator it = neighbors.begin();
Packit ea1746
         it != neighbors.end();
Packit ea1746
         ++it) {
Packit ea1746
      vertex_color[*it] = kGrey;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  int independent_set_size = ordering->size();
Packit ea1746
Packit ea1746
  // Iterate over the vertices and add all the grey vertices to the
Packit ea1746
  // ordering. At this stage there should only be black or grey
Packit ea1746
  // vertices in the graph.
Packit ea1746
  for (typename std::vector<Vertex>::const_iterator it = vertex_queue.begin();
Packit ea1746
       it != vertex_queue.end();
Packit ea1746
       ++it) {
Packit ea1746
    const Vertex vertex = *it;
Packit ea1746
    DCHECK(vertex_color[vertex] != kWhite);
Packit ea1746
    if (vertex_color[vertex] != kBlack) {
Packit ea1746
      ordering->push_back(vertex);
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  CHECK_EQ(ordering->size(), num_vertices);
Packit ea1746
  return independent_set_size;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Same as above with one important difference. The ordering parameter
Packit ea1746
// is an input/output parameter which carries an initial ordering of
Packit ea1746
// the vertices of the graph. The greedy independent set algorithm
Packit ea1746
// starts by sorting the vertices in increasing order of their
Packit ea1746
// degree. The input ordering is used to stabilize this sort, i.e., if
Packit ea1746
// two vertices have the same degree then they are ordered in the same
Packit ea1746
// order in which they occur in "ordering".
Packit ea1746
//
Packit ea1746
// This is useful in eliminating non-determinism from the Schur
Packit ea1746
// ordering algorithm over all.
Packit ea1746
template <typename Vertex>
Packit ea1746
int StableIndependentSetOrdering(const Graph<Vertex>& graph,
Packit ea1746
                                 std::vector<Vertex>* ordering) {
Packit ea1746
  CHECK_NOTNULL(ordering);
Packit ea1746
  const HashSet<Vertex>& vertices = graph.vertices();
Packit ea1746
  const int num_vertices = vertices.size();
Packit ea1746
  CHECK_EQ(vertices.size(), ordering->size());
Packit ea1746
Packit ea1746
  // Colors for labeling the graph during the BFS.
Packit ea1746
  const char kWhite = 0;
Packit ea1746
  const char kGrey = 1;
Packit ea1746
  const char kBlack = 2;
Packit ea1746
Packit ea1746
  std::vector<Vertex> vertex_queue(*ordering);
Packit ea1746
Packit ea1746
  std::stable_sort(vertex_queue.begin(), vertex_queue.end(),
Packit ea1746
                  VertexDegreeLessThan<Vertex>(graph));
Packit ea1746
Packit ea1746
  // Mark all vertices white.
Packit ea1746
  HashMap<Vertex, char> vertex_color;
Packit ea1746
  for (typename HashSet<Vertex>::const_iterator it = vertices.begin();
Packit ea1746
       it != vertices.end();
Packit ea1746
       ++it) {
Packit ea1746
    vertex_color[*it] = kWhite;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  ordering->clear();
Packit ea1746
  ordering->reserve(num_vertices);
Packit ea1746
  // Iterate over vertex_queue. Pick the first white vertex, add it
Packit ea1746
  // to the independent set. Mark it black and its neighbors grey.
Packit ea1746
  for (int i = 0; i < vertex_queue.size(); ++i) {
Packit ea1746
    const Vertex& vertex = vertex_queue[i];
Packit ea1746
    if (vertex_color[vertex] != kWhite) {
Packit ea1746
      continue;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    ordering->push_back(vertex);
Packit ea1746
    vertex_color[vertex] = kBlack;
Packit ea1746
    const HashSet<Vertex>& neighbors = graph.Neighbors(vertex);
Packit ea1746
    for (typename HashSet<Vertex>::const_iterator it = neighbors.begin();
Packit ea1746
         it != neighbors.end();
Packit ea1746
         ++it) {
Packit ea1746
      vertex_color[*it] = kGrey;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  int independent_set_size = ordering->size();
Packit ea1746
Packit ea1746
  // Iterate over the vertices and add all the grey vertices to the
Packit ea1746
  // ordering. At this stage there should only be black or grey
Packit ea1746
  // vertices in the graph.
Packit ea1746
  for (typename std::vector<Vertex>::const_iterator it = vertex_queue.begin();
Packit ea1746
       it != vertex_queue.end();
Packit ea1746
       ++it) {
Packit ea1746
    const Vertex vertex = *it;
Packit ea1746
    DCHECK(vertex_color[vertex] != kWhite);
Packit ea1746
    if (vertex_color[vertex] != kBlack) {
Packit ea1746
      ordering->push_back(vertex);
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  CHECK_EQ(ordering->size(), num_vertices);
Packit ea1746
  return independent_set_size;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Find the connected component for a vertex implemented using the
Packit ea1746
// find and update operation for disjoint-set. Recursively traverse
Packit ea1746
// the disjoint set structure till you reach a vertex whose connected
Packit ea1746
// component has the same id as the vertex itself. Along the way
Packit ea1746
// update the connected components of all the vertices. This updating
Packit ea1746
// is what gives this data structure its efficiency.
Packit ea1746
template <typename Vertex>
Packit ea1746
Vertex FindConnectedComponent(const Vertex& vertex,
Packit ea1746
                              HashMap<Vertex, Vertex>* union_find) {
Packit ea1746
  typename HashMap<Vertex, Vertex>::iterator it = union_find->find(vertex);
Packit ea1746
  DCHECK(it != union_find->end());
Packit ea1746
  if (it->second != vertex) {
Packit ea1746
    it->second = FindConnectedComponent(it->second, union_find);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  return it->second;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Compute a degree two constrained Maximum Spanning Tree/forest of
Packit ea1746
// the input graph. Caller owns the result.
Packit ea1746
//
Packit ea1746
// Finding degree 2 spanning tree of a graph is not always
Packit ea1746
// possible. For example a star graph, i.e. a graph with n-nodes
Packit ea1746
// where one node is connected to the other n-1 nodes does not have
Packit ea1746
// a any spanning trees of degree less than n-1.Even if such a tree
Packit ea1746
// exists, finding such a tree is NP-Hard.
Packit ea1746
Packit ea1746
// We get around both of these problems by using a greedy, degree
Packit ea1746
// constrained variant of Kruskal's algorithm. We start with a graph
Packit ea1746
// G_T with the same vertex set V as the input graph G(V,E) but an
Packit ea1746
// empty edge set. We then iterate over the edges of G in decreasing
Packit ea1746
// order of weight, adding them to G_T if doing so does not create a
Packit ea1746
// cycle in G_T} and the degree of all the vertices in G_T remains
Packit ea1746
// bounded by two. This O(|E|) algorithm results in a degree-2
Packit ea1746
// spanning forest, or a collection of linear paths that span the
Packit ea1746
// graph G.
Packit ea1746
template <typename Vertex>
Packit ea1746
WeightedGraph<Vertex>*
Packit ea1746
Degree2MaximumSpanningForest(const WeightedGraph<Vertex>& graph) {
Packit ea1746
  // Array of edges sorted in decreasing order of their weights.
Packit ea1746
  std::vector<std::pair<double, std::pair<Vertex, Vertex> > > weighted_edges;
Packit ea1746
  WeightedGraph<Vertex>* forest = new WeightedGraph<Vertex>();
Packit ea1746
Packit ea1746
  // Disjoint-set to keep track of the connected components in the
Packit ea1746
  // maximum spanning tree.
Packit ea1746
  HashMap<Vertex, Vertex> disjoint_set;
Packit ea1746
Packit ea1746
  // Sort of the edges in the graph in decreasing order of their
Packit ea1746
  // weight. Also add the vertices of the graph to the Maximum
Packit ea1746
  // Spanning Tree graph and set each vertex to be its own connected
Packit ea1746
  // component in the disjoint_set structure.
Packit ea1746
  const HashSet<Vertex>& vertices = graph.vertices();
Packit ea1746
  for (typename HashSet<Vertex>::const_iterator it = vertices.begin();
Packit ea1746
       it != vertices.end();
Packit ea1746
       ++it) {
Packit ea1746
    const Vertex vertex1 = *it;
Packit ea1746
    forest->AddVertex(vertex1, graph.VertexWeight(vertex1));
Packit ea1746
    disjoint_set[vertex1] = vertex1;
Packit ea1746
Packit ea1746
    const HashSet<Vertex>& neighbors = graph.Neighbors(vertex1);
Packit ea1746
    for (typename HashSet<Vertex>::const_iterator it2 = neighbors.begin();
Packit ea1746
         it2 != neighbors.end();
Packit ea1746
         ++it2) {
Packit ea1746
      const Vertex vertex2 = *it2;
Packit ea1746
      if (vertex1 >= vertex2) {
Packit ea1746
        continue;
Packit ea1746
      }
Packit ea1746
      const double weight = graph.EdgeWeight(vertex1, vertex2);
Packit ea1746
      weighted_edges.push_back(
Packit ea1746
          std::make_pair(weight, std::make_pair(vertex1, vertex2)));
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // The elements of this vector, are pairs
Packit ea1746
  // edge>. Sorting it using the reverse iterators gives us the edges
Packit ea1746
  // in decreasing order of edges.
Packit ea1746
  std::sort(weighted_edges.rbegin(), weighted_edges.rend());
Packit ea1746
Packit ea1746
  // Greedily add edges to the spanning tree/forest as long as they do
Packit ea1746
  // not violate the degree/cycle constraint.
Packit ea1746
  for (int i =0; i < weighted_edges.size(); ++i) {
Packit ea1746
    const std::pair<Vertex, Vertex>& edge = weighted_edges[i].second;
Packit ea1746
    const Vertex vertex1 = edge.first;
Packit ea1746
    const Vertex vertex2 = edge.second;
Packit ea1746
Packit ea1746
    // Check if either of the vertices are of degree 2 already, in
Packit ea1746
    // which case adding this edge will violate the degree 2
Packit ea1746
    // constraint.
Packit ea1746
    if ((forest->Neighbors(vertex1).size() == 2) ||
Packit ea1746
        (forest->Neighbors(vertex2).size() == 2)) {
Packit ea1746
      continue;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    // Find the id of the connected component to which the two
Packit ea1746
    // vertices belong to. If the id is the same, it means that the
Packit ea1746
    // two of them are already connected to each other via some other
Packit ea1746
    // vertex, and adding this edge will create a cycle.
Packit ea1746
    Vertex root1 = FindConnectedComponent(vertex1, &disjoint_set);
Packit ea1746
    Vertex root2 = FindConnectedComponent(vertex2, &disjoint_set);
Packit ea1746
Packit ea1746
    if (root1 == root2) {
Packit ea1746
      continue;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    // This edge can be added, add an edge in either direction with
Packit ea1746
    // the same weight as the original graph.
Packit ea1746
    const double edge_weight = graph.EdgeWeight(vertex1, vertex2);
Packit ea1746
    forest->AddEdge(vertex1, vertex2, edge_weight);
Packit ea1746
    forest->AddEdge(vertex2, vertex1, edge_weight);
Packit ea1746
Packit ea1746
    // Connected the two connected components by updating the
Packit ea1746
    // disjoint_set structure. Always connect the connected component
Packit ea1746
    // with the greater index with the connected component with the
Packit ea1746
    // smaller index. This should ensure shallower trees, for quicker
Packit ea1746
    // lookup.
Packit ea1746
    if (root2 < root1) {
Packit ea1746
      std::swap(root1, root2);
Packit ea1746
    }
Packit ea1746
Packit ea1746
    disjoint_set[root2] = root1;
Packit ea1746
  }
Packit ea1746
  return forest;
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_GRAPH_ALGORITHMS_H_