Divide-and-Conquer

分治通常是用来降低用暴力解法已经能达到多项式时间复杂度的时间复杂度,结合randomization technique是powerful。
– Divide a problem into a number of independent sub-problems
– Conquer the subproblems by solving them recursively;
– Combine the solutions to the subproblems into the solution to the original problem;

复杂度分析

《Divide-and-Conquer》

可分

问题的数据结构为:有相同的结构,但是smaller

  • An array with n elements;
  • A matrix;
  • A set of n elements;
  • A tree;
  • A directed acyclic graph;
  • A general graph;

sort

InsertionSort:Divide n element into a n−1-length array and an element

《Divide-and-Conquer》

MergeSort:Divide into two halves

《Divide-and-Conquer》
《Divide-and-Conquer》

QuickSort:divide according to a randomly-selected pivot

随机选择pivot,会出现最坏情况pivot把number全部分到一个集合里,最好情况是各分一半,大多数情况pivot是nearly-central
《Divide-and-Conquer》
为了使pivot基本分成2半,可以让算法强行选择一个比较合适的pivot,适用于distinct number,比上面的方法更慢。
《Divide-and-Conquer》
由于大多数的pivot都是good,所以randomly选择pivot的时间复杂度是 O(nlog43n)
《Divide-and-Conquer》
Lomuto’s implementation
《Divide-and-Conquer》
Hoare’s implementation [1961]
《Divide-and-Conquer》
前面的方法对于distinc item是有效的,但是当有许多重复元素时,可考虑将元素分成3parts。

Selection:select the k-th smallest items in an array

《Divide-and-Conquer》
pivot的选择会导致不同的时间复杂度,当选择median或者nearly-central时是(n),否则为 O(n2)
《Divide-and-Conquer》
《Divide-and-Conquer》
《Divide-and-Conquer》

CountingInversion: to count inversions in an array of n integers

《Divide-and-Conquer》
可以将CountingInversion问题当成排序问题。

MatrixMultiplication

通过减少子问题个数降低时间复杂度
《Divide-and-Conquer》

ClosestPair problem

如果在一维直线上,通过排序可以得到最近点对。
二维平面上先按照x排序,等分成2部分。
《Divide-and-Conquer》
《Divide-and-Conquer》
《Divide-and-Conquer》

点赞