Divide and Conquer

本文深入探讨了Divide and Conquer算法的概念、工作原理及在快速排序、归并排序、最近两点距离计算等算法中的应用,强调了算法选择的关键因素,并通过实例展示了算法的时间复杂度优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Divide and Conquer is an algorithmic paradigm based on multi-branched recursion.
A typical Divide and Conquer algorithm solves a problem using following three steps:

1. Divide: Break the given problem into subproblems of same type.
2. Conquer: Recursively solve these subproblems
3. Combine: Appropriately combine the answers


The name "divide and conquer" is sometimes applied also to algorithms that reduce each problem to only one subproblem, such as the binary search algorithm.
Because it uses tail recursion, so it can be converted into simple loops. Under this broad definition, however, every algorithm that uses recursion or loops could be regarded as a "divide and conquer algorithm". Therefore, some authors (I prefer this viewpoint too) consider that the name "divide and conquer" should be used only when each problem may generate two or more subproblems.The name decrease and conquer has been proposed instead for the single-subproblem class.

Divide and Conquer (D & C) vs Dynamic Programming (DP)
Both paradigms (D & C and DP) divide the given problem into subproblems and solve subproblems. How to choose one of them for a given problem? Divide and Conquer should be used when same subproblems are not evaluated many times. Otherwise Dynamic Programming or Memoization should be used. For example, Quicksort is a Divide and Conquer algorithm, we never evaluate the same subproblems again. On the other hand, for calculating nth Fibonacci number, Dynamic Programming should be preferred.

Following are some standard algorithms that are Divide and Conquer algorithms.


1) Quicksort is a sorting algorithm. The algorithm picks a pivot element, rearranges the array elements in such a way that all elements smaller than the picked pivot element move to left side of pivot, and all greater elements move to right side. Finally, the algorithm recursively sorts the subarrays on left and right of pivot element.


2) Merge Sort is also a sorting algorithm. The algorithm divides the array in two halves, recursively sorts them and finally merges the two sorted halves.


3) Closest Pair of Points The problem is to find the closest pair of points in a set of points in x-y plane. The problem can be solved in O(n^2) time by calculating distances of every pair of points and comparing the distances to find the minimum. The Divide and Conquer algorithm solves the problem in O(nLogn) time.


5) Strassen’s Algorithm is an efficient algorithm to multiply two matrices. A simple method to multiply two matrices need 3 nested loops and is O(n^3). Strassen’s algorithm multiplies two matrices in O(n^2.8974) time.


6) Cooley–Tukey Fast Fourier Transform (FFT) algorithm is the most common algorithm for FFT. It is a divide and conquer algorithm which works in O(nlogn) time.


5) Karatsuba algorithm for fast multiplication it does multiplication of two n-digit numbers in at most 3 n^{\log_23}\approx 3 n^{1.585} single-digit multiplications in general (and exactly n^{\log_23} when n is a power of 2). It is therefore faster than the classical algorithm, which requires n2 single-digit products. If n = 210 = 1024, in particular, the exact counts are 310 = 59,049 and (210)2 = 1,048,576, respectively.



References

http://www.geeksforgeeks.org/fundamentals-of-algorithms/

http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm

### 分治算法的时间复杂度 分治算法是一种重要的算法设计技术,其核心在于将一个问题分解成多个较小规模的相同问题,分别求解后再合并各个子问题的结果得到原始问题的答案。对于这类算法而言,时间复杂度主要取决于三个因素: - **分解操作的成本**:即将原问题划分为更小子问题所需的工作量。 - **解决各子问题所需的总成本**:这通常是递归调用本身的时间开销。 - **组合子问题解决方案的成本**:即如何有效地把所有子问题的解答汇总起来形成最终答案。 #### 计算方法 为了计算分治算法的时间复杂度,可以利用主定理(Master Theorem),它提供了一种简便的方法来估计形如 \(T(n)=aT(\frac{n}{b})+f(n)\) 的递推关系式的渐近界[^3]。这里, - \(n\) 表示输入大小; - \(a \geq 1\) 和 \(b > 1\) 是常数; - \(f(n)\) 描述了除递归外其他部分工作量的增长速度。 当应用到具体的例子时,比如快速排序,如果每次都能均匀分割,则有 \(a=2\),\(b=2\),而额外处理(比较和交换元素)大致线性增长,因此 \(f(n)=O(n)\),从而得出平均情况下快速排序的时间复杂度为 \(O(n\log n)\)[^4]。 #### 示例 考虑经典的归并排序作为实例展示分治算法及其时间复杂度分析过程: ```java public class MergeSort { public static void merge(int[] array, int left, int mid, int right){ // 合并两个已排序序列... } public static void sort(int[] array, int left, int right){ if (left < right){ int mid = (left + right)/2; // 对左半边进行递归排序 sort(array, left, mid); // 对右半边进行递归排序 sort(array, mid+1, right); // 将两部分有序数组合并在一起 merge(array, left, mid, right); } } } ``` 在这个实现中,`sort()` 函数负责递归地拆分子数组直到不能再分为止,之后再由 `merge()` 来完成实际的数据整理任务。由于每层递归都会使待处理区间减半,并且每一层都需要遍历整个列表来进行合并操作,故此算法的整体时间复杂度同样遵循 \(O(n\log n)\) 的规律。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值