分治法是最广泛使用的算法设计方法之一,其基本思想:把大问题分解成一些较小的问题,然后由小问题的解方便地构造出大问题的解。

  分治法说穿了就是把问题放小,如果被分的问题还是比较大,那么久继续分下去。为了能清晰地反映采用分治策略设计算法的基本步骤,下面用一个称之为抽象化控制的过程来非形式的描述算法的控制流向,下面笔者举例来说明这个问题。

void div(p,q) {
int n,A[n]; //定义成全程变量
int m,p,q; //1≤p≤q≤n
if(small(p,q)) return(answer(p,q));
else
{
m = divide(p,q); //p≤m<q
return (combine(div(p,m),div(m+,q)));
};
}//div

  在这个算法中,small(p,q)是一个布尔值函数,它用以判断输入为A(p:q)的问题是否小到无需进一步细分就能算出其答案的程度。若是,则调用能直接计算此规模下的子问题解的函数answer(p,q);若否,则调用分割函数divide(p,q),返回一个新的分割点m(整数)。于是,原问题被分成输入为A(p:m)和A(m+1:q)的两个子问题。对这两个子问题分别递归调用div得到各自的解x和y,再用一个合并函数combine(x,y)将这两个子问题的解合成原问题(输入为 A(p,q))的解。倘若所分成的两个子问题的输入规模大致相等,则div总的计算时间可用下面的递归关系式来表示:
            g(n) 当n足够小,                                                    

T(n)=                                             

    2T(n/2)+f(n) 否则
其中,T(n)是输入规模为n的div的运行时间,g(n)是输入规模足够小以至于能直接求解时的运行时间,f(n)是combine的时间

显然用递归过程描述以分治法为基础的算法是理所当然的,但为了提高效率,往往需要将这一递归形式转换成迭代形式。例如下面这个算法:

void div1(p,q) {
//div的迭代模型,定义了一个适当大小的工作栈
int s,t;
intiStack(sqStack); //定义工作栈sqStack
L1:while(!small(p,q)) {
m = divied(p,q); //确定如何分割输入
push(sqStack,(p,q,m,,)); //处理第一次递归调用
q = m;
};//while
t = answer(p,q);
while(!StackEmpty( sqStack )) {
pop(sqStack,(p, q, m, s, ret)); //退栈,ret为返回地址
if(ret==) {
push(sqStack,(p, q, m, t, )); //处理第二次递归调用
p = m + ;
go to L1;}
else {
t = combine(s,t); //将两个子问题的解合并成一个解
};//if
};//while
return t;
}//div1   当然,这个算法还可以简化

Divide and conquer method的更多相关文章

  1. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  2. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  3. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  4. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  5. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  6. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  7. The Divide and Conquer Approach - 归并排序

    The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...

  8. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  9. [算法]分治算法(Divide and Conquer)

    转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...

随机推荐

  1. 168. Excel Sheet Column Title

    Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...

  2. 在MFC框架中使用OpenGL的简单实例

    引言 我们知道,在MFC框架中,用于绘图的接口是GDI.但GDI只能绘制简单的2D图形,要想制作精美的3D图形,一个可行的办法是使用OpenGL或者Direct3D等第三方库. 由于最近在给导师的一个 ...

  3. cocos2d中两种移动的算法

    在对cocos2d的sprite处理移动的过程中,通常用到的两种移动的算法: 假设这个CCNode是直接放在CCLayer上的 距离差法: CGPoint curTouchPosUI = [touch ...

  4. 关于ANSI 和 Unicode

    关于ANSI和Unicode 1.ANSI American National Standards Institute(美国国家标准学会),ANSI编码不是一种具体的编码方式,而是一种指定在某些环境下 ...

  5. leetcode@ [87] Scramble String (Dynamic Programming)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. kvm usb

  7. 创建本地CM 离线服务器

    一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...

  8. python 定制类

    看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方法我们也知道是为了能让cla ...

  9. light oj 1078 - Integer Divisibility

    1078 - Integer Divisibility   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  10. define定义方法

    define('a',function(){ ; return x; }); define('b',['a'],function(a){ ; return y+a; }); M.use(['b'],f ...