algorithms-ch2-divide and conquer(分治法)

The divide-and-conquer strategy solves a problem by:

  1. Breaking it into subproblems that are themselves smaller instances of the same type of
    problem
  2. Recursively solving these subproblems
  3. Appropriately combining their answers

2.1 Multiplication

only three multiplications

bc+ad = (a+b)(c+d)−ac−bd

For instance, if x = 101101102 (the subscript 2 means “binary”) then xL = 10112, xR = 01102,
The product of x and y can then be rewritten as

xy = (2^(n/2)xL + xR)(2^(n/2)yL + yR) = 2^n xLyL +2^(n/2) (xLyR + xRyL) + xRyR.

Time complexity analysis:

  • additions take linear time,
  • The significant operations are the four n/2-bit multiplications, xLyL, xLyR, xRyL, xRyR;
    so: we get the recurrence relation

T(n) = 4T(n/2)+O(n).

three times multiplications(改进版):

T(n) = 3T(n/2)+O(n).

A divide-and-conquer algorithm for integer multiplication (3次乘法)

function multiply(x,y)
Input: Positive integers x and y, in binary
Output: Their product
/
n = max(size of x, size of y)
if n = 1: return xy
/
xL, xR = leftmost ⌈n/2⌉, rightmost ⌊n/2⌋ bits of x
yL, yR = leftmost ⌈n/2⌉, rightmost ⌊n/2⌋ bits of y
P1 =multiply(xL,yL)
P2 =multiply(xR,yR)
P3 =multiply(xL +xR,yL +yR)
return P1 ×2^n +(P3 −P1 −P2)×2^(n/2) +P2

2.2 Master theorem

master theorem

2.3 Mergesort

归并排序:T(n) = 2T(n/2) + O(n) ==> O(nlogn)
二分搜索:T(n) = T(n/2) + O(1) ==> O(logn)

function mergesort(a[1,...n])
Input: An array of numbers a[1 . . . n]
Output: A sorted version of this array
/
if n>1:
  return merge(mergesort[1.... ⌊n/2⌋]),mergesort(a[⌊n/2⌋ + 1 . . . n]))
else
  return a

function merge(x[1 . . . k], y[1 . . . l])
if k = 0: return y[1...l]
if l = 0: return x[1...k]
if x[1] ≤ y[1]:
  return x[1]◦merge(x[2...k],y[1...l])
else:
  return y[1]◦merge(x[1...k],y[2...l])
//◦ denotes concatenation连接操作 . 
//each merge operation takes k+l operations:O(k+l)

all the real work is done in merg-
ing, which doesn’t start until the recursion gets down to singleton arrays

function iterative-mergesort(a[1 . . . n])
Input: elements a1,a2,...,an to be sorted
/
 Q = [ ] (empty queue)
for i=1 to n:
  inject(Q, [ai ])
//initialize the queue, put all elements in this queue

while |Q| > 1:
  inject(Q, merge(eject(Q), eject(Q)))
//merge 前两个从queue中取出的单元素,再把merge结果插入queue后面,
//注意:queue不是一个存i个元素的数组,在第一次while之后queue中已经存在Q1[ai,aj]这样的东西了,所以最后得到一个完整的Q时,Q = 1,退出循环

return eject(Q)
//完成sort, 按顺序eject queue中元素

排序的下限:涉及两两比较的方法来对一组数进行排序的最优复杂度是O(nlogn),所以归并排序是最优排序。
Here is the argument: Consider any such tree that sorts an array of n elements. Each of its leaves is labeled by a permutation of {1, 2, . . . , n}. In fact, every permutation must appear as the label of a leaf. The reason is simple: if a particular permutation is missing, when we feed the algorithm an input ordered according to this same permutation, the algorithm's correctness will be uninsured. And since there are n! permutations of n elements, it follows that the tree has at least n! leaves.
We are almost done: This is a binary tree, and we argued that it has at least n! leaves.
Recall now that a binary tree of depth d has at most 2^d leaves (proof: an easy induction on d). So, the depth of our tree—and the complexity of our algorithm—must be at least log(n!).

depth d, last level 2^d leaves;
last level n!, depth log(n!)==complexity: log(n!)=nlogn

大概思路就是:

  1. 每个最下面的leaf 表示这n个元素的一种大小排列可能
  2. 共会有n!种排列可能,所以最下面一级有n!个leaves
  3. 从depth= d ==> last level at most 2^d leaves 推出last level n! ==>depth log(n!)
  4. 算法复杂度=log(n!)=nlogn

注意:存在线性排序(O(n)),此法不涉及两两比较。例:对1,2,3,4,5,8,9,19,20排序,设一个bool型数组a[1..20],初值全为false,顺序扫一遍输入,扫到n,则把a[n]设为true,然后扫一遍a,把值为true的输出,即已排好序。但不适合对大数字排序。空间换时间

2.4 medians

find the medians:

  1. sort the elements, nlogn
  2. SELECTION
    Input: A list of numbers S; an integer k
    Output: The kth smallest element of S

For any number v, imagine splitting list S into three categories

  • elements smaller than v, SL,
  • those equal to v (there might be duplicates), Sv
  • and those greater than v. SR
SL,Sv, and SR can be computed from S in linear time
  • if we choose v , such that |SL|,|SR| ≈ 1|S|.
    running time : T (n) = T (n/2) + O(n),
  • if we choose v = max/min number of s.
    running time : T(n)=Θ(n2)
  • if we choose v = median
    running time : T(n)=O(n),
  • we will call v good if it lies withinthe 25th to 75th percentile of the array that it is chosen from, We like these choices of vbecause they ensure that the sublists SL and SR have size at most three-fourths that of S

2.5matrix multiplication

The product of two n×n matrices X and Y is a third n×n matrix Z=XY, with (i,j)th entry

(i,j)th entry

T(n) = 8T(n/2) + O(n2).

可以分解成7个n/2规模的子矩阵乘法,

T(n) = 7T(n/2) + O(n^2), 解得O(n^2.81)

exercise
Introduction to Algorithms, Third Edition
(CLRS)
4.1-5
Use the following ideas to develop a nonrecursive, linear-time algorithm for the maximum-subarray problem. Start at the left end of the array, and progress toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum subarray of A[1...j] , extend the answer to find a maximum subarray ending at index j+1 by using the following observation: a maximum subarray of A[1...j+1] is either a maximum subarray of A[1...j] or a subarray A[i...j+1] , for some 1<=i<= j+1 Determine a maximum subarray of the form A[i...j+1] in constant time based on knowing a maximum subarray ending at index j .
//题目要求看起来有点像动态规划

find-maximum-subarray(A, low, high)

array.left = 0
array.right = 0
sum = A[low]
tempSum = 0
for i = low to high
  tempSum = Max(A[i],tempSum+A[i])
  if tempSum>sum
    sum = tempSum
    right = i
  if tempSum == A[i]
    left = i
return (left,right,sum)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,951评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,606评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,601评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,478评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,565评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,587评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,590评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,337评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,785评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,096评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,273评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,935评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,578评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,199评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,440评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,163评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,133评论 2 352

推荐阅读更多精彩内容