1. 程式人生 > >Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of efficiently finding largest entries in matrices with certain structural properties. Many concrete problems can be modelled as matrix search problems, and for some of them we provide explicit solutions that allow you to solve them without knowing about the matrix search technique. Examples are, the computation of all furthest neighbors for the vertices of a convex polygon, maximal k

-gons inscribed into a planar point set, and computing rectangular p-centers. 

monotone_matrix_search() 和 sorted_matrix_search()處理的問題是在一定結構屬性的矩陣中高效尋找最大入口。很多具體的問題可以建模為矩陣搜尋問題,有些問題我們可以在你不知道矩陣搜尋技術的情況下提供顯式解決問題的方法。這方面的例子有:計算一個凸多邊形的頂點集的所有最遠鄰居,一個平面點集的內接最大k邊形(k-gon)和求一個矩形p心。

Example

本例我們建立一個隨機的向量 a=(ai)i=1,,

(由0-99中均勻分佈產生)並建立一個笛卡爾矩陣M,矩陣中包含所有ai+aj,i,j∈{1,,5},如果a排序,則M也排序。則我們可以使用 sorted_matrix_search() 來計算M中a的最大入口的上界。

In the following program we build a random vector a=(ai)i=1,,5 (elements drawn uniformly from {0,,99}) and construct a Cartesian matrix M containing as elements all sums ai+aj,i,j{1,,5}

. If a is sorted, M is sorted as well. So we can apply sorted_matrix_search() to compute the upper bound for the maximal entry of a in M.


File Matrix_search/sorted_matrix_search.cpp

#include <CGAL/Random.h> #include <CGAL/Cartesian_matrix.h> #include <CGAL/sorted_matrix_search.h> #include <vector> #include <algorithm> #include <iterator> #include <boost/functional.hpp>   typedef int Value; typedef std::vector<Value> Vector; typedef Vector::iterator Value_iterator; typedef std::vector<Vector> Vector_cont; typedef CGAL::Cartesian_matrix<std::plus<int>, Value_iterator, Value_iterator> Matrix;   int main() { // set of vectors the matrices are build from: Vector_cont vectors;   // generate a random vector and sort it: Vector a; const int n = 5; for (int i = 0; i < n; ++i) a.push_back(CGAL::get_default_random()(100)); std::sort(a.begin(), a.end()); std::cout << "a = ( "; std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << ")\n";   // build a Cartesian matrix from a: Matrix M(a.begin(), a.end(), a.begin(), a.end());   // search for an upper bound for max(a): Value bound = a[n-1]; Value upper_bound = CGAL::sorted_matrix_search( &M, &M + 1, CGAL::sorted_matrix_search_traits_adaptor( boost::bind2nd(std::greater_equal<Value>(), bound), M)); std::cout << "Upper bound for " << bound << " is " << upper_bound << "." << std::endl;   return 0; }