Divide and Conquer
2016-02-25 15:55
477 查看
Divide and Conquer | Set 1 (Introduction)
Like Greedy and DynamicProgramming, Divide and Conquer is an algorithmic paradigm. 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
Following are some standard algorithms that are Divide and Conquer algorithms.
1) Binary
Search is a searching algorithm. In each step, the algorithm compares the input element x with the value of the middle element in array. If the values match, return the index of middle. Otherwise, if x is less than the middle element, then the algorithm
recurs for left side of middle element, else recurs for right side of middle element.
2) 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.
3) 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.
4) 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.
7) Karatsuba
algorithm for fast multiplication it does multiplication of two n-digit numbers in at most
single-digit
multiplications in general (and exactly
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.
We will publishing above algorithms in separate posts.
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, Binary Search 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 (See this for
details).
References
Algorithms
by Sanjoy Dasgupta, Christos Papadimitriou, Umesh Vazirani
Introduction
to Algorithms by Clifford Stein, Thomas H. Cormen, Charles E. Leiserson, Ronald L.
http://en.wikipedia.org/wiki/Karatsuba_algorithm
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
相关文章推荐
- 1101 Quick Sort
- UIImageView用法
- 浅谈 easyui tabs 的href和content属性
- java request获取各种数据
- incredibuild 命令行 error D8037
- 用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
- Ultra-QuickSort(树状数组求逆序对数)
- IOS之UITabBarController
- 基于pt-query-digest的慢查询日志持续追踪 推荐
- 从源代码安装Guidance
- ./configure: error: the GeoIP module requires the GeoIP library.
- 《iOS Human Interface Guidelines》——The Status Bar
- UIResponder
- Formal System-相继式演算(Sequenz)
- iOS开发系列--UITableView全面解析
- queue
- execute、executeupdate、executequery三者的区别
- Google推出iOS功能性UI测试框架EarlGrey
- Leetcode 225:Implement Stack using Queues
- iOS的UDID废用以及UUID配合keychain的替换方案实现