13-14-01ADA09(减治法-减常因子算法I)

合集下载

5-第五章 减治法

5-第五章 减治法

2.比较v和 A[x]
确定查找范围
5.6.3 二叉查找树的查找和插入
• 二叉查找树:左子树的值小于根顶点,右 子树大于根顶点。
小结
5.4 生成组台对象的算法
• 组合问题 1、计数 2、结构
组合问题
5.4.1 生成排列
用减一思想生成{1,2,…,n}所有排列。
Johnson-trotter算法
• 字典序---增序排队
5.4.2 生成子集
1、挤压序: 所 2、是否存在—种生成比特串的最小变化算法,使得每
一个比特串和它的直接前趋之间仅仅相差一 个比特 位。
n / 2 ,它要求找出这样一个元素,该元素比列表中的—半元素大,又比另—半元素
小。这个中间的值被称为中值,它在数理统计中是—个非常重要的量。
• 类似快速排序的分区做法
• 例
15 15
效率分析: 1)平均效率 2)最差效率
5.6.2 插值查找
插值查找用于有序数组,“插值”代替了折 半查找中的中间值 1.计算
• 最坏输入是一个严格递减的数组,这种输 入的比较次数是
• 最好的情况下(升序),在外部循环的每 次送代中,比较操作只执行一次
• 平均
5.2深度优先查找和广度忧先查找
• 什么叫图的遍历 从图的任意点出发沿着一些边访问图中的 所有顶点,且使每个顶点仅被访问一次,这就 叫图的遍历. • 我们来看一下图的遍历的两种方法: 1.深度优先搜索 2.广度优先搜索
第一种算法是深度优先查找的一个简单应用:执行一次DFS遍 历,并记住顶点变成死端(即退出遍历栈)的顺序。将该次序反过来 就得到了拓扑排序的一个解。
第二种算法基于减(减一)治技术的一个直 接实现:不断地做这样—件事,在余下的有向 图中求出一个源,它是一个没有输入边的顶点, 然后把它和所有从它出发的边都删除。

减治法

减治法
原问题 的规模是n
子问题 的规模是n/2 子问题的解
原问题的解
图5.1 减治法的典型情况(减半技术)
对于给定的整数a和非负整数n,计算an的值。
利用减治法,如果n=1,可以简单地返回a的值,如果n是偶数并且n>1,可 以把该问题的规模减半,即计算an/2的值,而且规模为n的解an和规模减半 的解an/2之间具有明显的对应关系:an=(an/2)2,如果n是奇数并且n>1,可 以先用偶指数的规则计算a(n-1),再把结果乘以a。所以,应用减治技术得到 如下计算方法:
排序问题中的减治法
堆排序
选择问题
堆排序

28 25 36 18 32 28 36 25 18 16 32 25 16 36 25 18 16 36 28 32
28
18 16
32
堆排序是利用堆(假设利用大根堆)的特性进 行排序的方法,其基本思想是:首先将待排序 的记录序列构造成一个堆,此时,选出了堆中 所有记录的最大者即堆顶记录,然后将它从堆 中移走(通常将堆顶记录和堆中最后一个记录 交换),并将剩余的记录再调整成堆,这样又 找出了次大的记录,以此类推,直到堆中只有 一个记录为止。
n 1 n 1
查找问题中的减治法
折半查找
二叉查找树
折半查找
在有序表{ 7, 14, 18, 21, 23, 29, 31, 35, 38, 42, 46, 49, 52 }中查找值为14的 记录的过程如图所示。
0 1
7
2
14
3
18
4
21
5
23
6
29
7
31
8
35
9
38
10
42

第 五 章 减治法

第 五 章 减治法
仅仅通过一次重量的比较,就可以判断伪币是否存在。
算 分 析 与 设 计
西南科技大学
金块问题
有一个老板有一袋金块。每个月将有两 名雇员会因其优异的表现分别被奖励一 个金块。按规矩,排名第一的雇员将得 到袋中最重的金块,排名最后的雇员将 得到袋中最轻的金块。如果每个月都有 新的金块周期性的加入袋中,则每个月 都必须找出最轻和最重的金块。假设有 一台比较重量的仪器,我们希望用最少 的比较次数找出最轻和最重的金块。
算 分 析 与 设 计
西南科技大学
直接插入排序实现方法
减一技术下,该方法遵循的思路是:假设对较 小数组 A[0..n-2]排序问题已经解决了,得到一 个大小为n-1的有序数组。然后将要排序的第n 个元素,插入到数组的适合位置上,得到大小 为n的有序数组 A[0..n-1]。伪代码如下: void InsertionSort(a[]) {for(i=1;i<n-1;i++) //从第二个记录起进行插入 for (j=i-1; j>=0;j--) if a[j+1]-(a[j]) < 0 Swap(a[j+1], a[j]); }
算 分 析 与 设 计
西南科技大学
俄式乘法☺ 俄式乘法☺
算法思想:两个A和B数相乘,把数A每 次除以2,直到为0为止,另一个数B则不 断加倍,若第数A未除尽时,则数B应加 上自己。 7×8的计算步骤: 7 8 3 16+ 8 1 32+ 16 + 8
算 分 析 与 设 计
西南科技大学
约瑟夫斯问题( 约瑟夫斯问题(一)
算 分 析 与 设 计
西南科技大学
减常数因子减治法
减常数因子减治法的一个 典型算法就是折半查找 (Bin_Search)。它搜索 一个排序好的数组,将查 找目标与数组的中间位置 的元素相比,比它大则递 归查找数组的左边,反之 亦然。这个每次迭代都将 问题减小为原来的1/2。 折半查找每次都消去一个 常数因子2,因此其时间 效率为O(logn)。

C++减治法查找范围整数

C++减治法查找范围整数

while(x<k1-1||y>k2-1) { int temp; int f1,f2;//存储最小和最大数的下标 f1=x; f2=y; for(int i=x; i<=y; i++) { if(a[f1].data>a[i].data) f1=i; if(a[f2].data<a[i].data) f2=i; } if(x<k1-1) { temp=a[x].data; a[x].data=a[f1].data; a[f1].data=temp; a[x].flag=0; x++; } if(y>k2-1) { temp=a[y].data; a[y].data=a[f2].data; a[f2].data=temp; a[y].flag=0; y--; } } } void Show(Mat &a,int n,int k1,int k2)
四、实验结果:
该算法的时间复杂度取决于n的大小和输入的 K1、K2的情况,最好情况是K1、K2恰好在输入的 无序列表的两端,此时不做运算,直接输出,时间 复杂度为O(0)。最坏情况是K1=K2=n/2时,此时做 n/2次运算,时间复杂度为O(n/2)。
{ cout<<"第"<<k1<<"小到"<<k2<<"小之间的所有整数有:"; for(int i=0; i<n; i++) { if(a[i].flag) cout<<a[i].data<<" "; } cout<<endl; } void main() { int choice; cout<<" 1: 执行程序! 2: 退出程序!"<<endl; do { cout<<"请选择你的操作:"; cin>>choice; switch(choice) { case 1: { int n; int k1,k2; cout<<"请输入无序列表n的大小:"; cin>>n; cout<<"请输入无序列表中的所有整数:"; for(int i=0; i<n; i++) { a[i].flag=1; cin>>a[i].data; } cout<<"请输入k1,k2的值:"; cin>>k1>>k2;

第7章 减治法(《算法设计与分析(第3版)》C++版 王红梅 清华大学出版社)

第7章 减治法(《算法设计与分析(第3版)》C++版 王红梅 清华大学出版社)

比较对象,若 k 与中间元素相等,则查找成功;若 k 小于中间元素,则在中间元
算 法 设

素的左半区继续查找;若 k 大于中间记录,则在中间元素的右半区继续查找。不
与 分

断重复上述过程,直到查找成功,或查找区间为空,查找失败。
( 第
版 )
k
清 华


[ r1 … … … rmid-1 ] rmid [ rmid+1 … … … rn ] (mid=(1+n)/2)
Page 4
3
7.1.2 一个简单的例子——俄式乘法
【问题】俄式乘法(russian multiplication)用来计算两个正整数 n 和 m 的乘积
,运算规则:如果 n 是偶数,计算 n/2×2m;如果 n 是奇数,计算(n-1)/2×2m+
m;当 n 等于 1 时,返回 m 的值。


俄式乘法的优点?
与 分 析
2. 测试查找区间[low,high]是否存在,若不存在,则查找失败,返回 0;
( 第
3. 取中间点 mid = (low+high)/2; 比较 k 与 rmid,有以下三种情况:
版 )
3.1 若 k < rmid,则 high = mid - 1;查找在左半区进行,转步骤2;
清 华
3.2 若 k > rmid,则 low = mid + 1;查找在右半区进行,转步骤2;
Page 12
7.2.2 选择问题
【想法】假定轴值的最终位置是 s,则: (1)若 k=s,则 rs 就是第 k 小元素; (2)若 k<s,则第 k 小元素一定在序列 r1 ~ rs-1 中; (3)若 k>s,则第 k 小元素一定在序列 rs+1 ~ rn 中。

减治算法

减治算法

俄式乘法
n n * m= — * 2 * m 2
• 两个大整数m和n乘法
n为偶数
n -1 n * m= —— * 2 * m + m 2
n为奇数
an=(a(n-1)/2)2 *a n是奇数
an =a
子问题的解
n=1
原始问题的解Leabharlann 7减常因子法 减治法的第 2 种变化。折半查找 —— 常因子 = 2 , 注意与分治法区别 这类算法效率常常是对数型,速度非常快,不要指望 有很多此类算法,常因子 ≠ 2 的情况更是少之又少。 常因子 = 3,每次规模减 2 / 3 . 假币问题 有n 枚外观相同的硬币,其中有一枚假币。设假币较 轻,用一架天平将这枚假币检测出来 减常因子策略 (多种方法,这里仅用减治策略) 把 n 枚硬币等分为两堆 —— n 为奇数:留下一枚硬币,把两堆放天平上。若两 堆硬币重量相同,留下这枚即为假币 n 为偶数:较轻的一堆含假币,继续分解它,丢弃 较重的那一堆 —— 常因子 = 2 ,减半法
解此递推式,得 本算法并非最高效的算法!更高效的策略是: 每次把 n 个硬币分为 3 堆,常因子 = 3,每次减去问题 规模的2/3 . 称重次数约为 log3n, 比 log2n 更小。
减常因子法算例 —— 俄式乘法(俄国农夫法) 问题:两个正整数相乘。十九世纪,俄国农 民广泛采用该算法,不需使用九九乘法表。 算法策略 n 和 m 为两个正整数,计算它们的乘积。 问题规模选择 n ,采用减治 策略,可得递推式:(规模减半,常因子 = 2) n为偶数: n为奇数: 算法停止: n = 1 时停止:1×m = m 算法实现:递归法、非递归法
分治法和减治法区别
分治法是对分解的子问题分别求解,需要 对子问题的解进行合并,而减治法只对一个子问 题求解,并且不需要进行解的合并。应用减治法 (例如减半法)得到的算法通常具有如下递推式:

数据结构与算法 减治法讲解

数据结构与算法 减治法讲解

3. 重复2,直到栈空为止,DFS 过程结束
DFS 算法构造出一棵 DFS 树(或森林)
DFS 算法过程图示(以树为例)
根据访问顺序,用连续整数标记
西 各个顶点,如图。
1
华 大
红色箭头表示回溯过程。
学 数
2
13
学 与
3
14

算 机
4
11 16


5
9
12



6
10
7
8
18
22
15 21
17
西a c b



数 学
d
e
f

计 算
acd
(2)
机 学


b

c

f
院头

襄顶


c d

a a

b e

e
点ecd f
(2)
(3) 边
(2)


(3)
f b e
(2)
图的邻接链表表示
访问顶点数与 n 的关系:
1. 每个表头顶点需要访问,以找到 该顶点开始的邻接顶点链
学 数
减常量法:常量通常为1(减 1 法)
学 减常因子法:常因子通常为 2(减半法)
与 计
减可变规模法:每次减去的规模不同
原问题(规模n)




子问题(规模n-1)

原问题 (规模n)


子问题(规模n/2)
子问题解

算法设计与分析-第5章-减治法

算法设计与分析-第5章-减治法

0 T (n) T (n / 2) 1
n 1 n 1
所以,通常来说,应用减治法处理问题的效率是很高的, 一般是O(log2n)数量级。
5.2 查找问题中的减治法
5.2.1 折半查找 5.2.2 二叉查找树 5.2.3 选择问题
5.2.1
一、问题描述:
折半查找
应用折半查找方法在一个有序序列中查找值为k的记 录。若查找成功,返回记录k在序列中的位置,若查找失 败,返回失败信息。
low=1
பைடு நூலகம்
18>14
mid=3
mid=7 31>14 high=6
high=13
high=2
mid=1
7<14
low=2
mid=2
14=14
5.2.1 折半查找
三、算法设计
算法5.1——折半查找
1. low=1;high=n; //设置初始查找区间 2. 测试查找区间[low,high]是否存在,若不存在,则查找失败; 否则 3. 取中间点mid=(low+high)/2; 比较k与r[mid],有以下三种情况: 3.1 若k<r[mid],则high=mid-1;查找在左半区进行,转2; 3.2 若k>r[mid],则low=mid+1;查找在右半区进行,转2; 3.3 若k=r[mid],则查找成功,返回记录在表中位置mid;
{ int data; //结点的值,假设查找集合的元素为整型
BiNode *lchild, *rchild; //指向左、右子树的指针 };
算法5.2——二叉排序树的查找
BiNode * SearchBST(BiNode *root, int k) { if (root= =NULL) return NULL; else if (root->data==k) return root; else if (k<root->data) return SearchBST(root->lchild, k); else return SearchBST(root->rchild, k); }
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

2013-2014-02 《Design and Analysis of Algorithm》
SCUN
2013-12-28
11
Worst-Case Analysis

In the worst case, we will either find the element on the last pass, or not find the element at all In each pass, only need 1 comparison. Hence, if we knew how many passes it has done, worst case is trivial If n = 2k-1, then there must be k = log(n+1) passes So, informally, we get: Tworst(n) = O(logn) If n is an arbitrary positive integer n, since after one pass, the algorithm faces the same situation but for an array half the size, we get the following recurrence relation for Tworst(n): Tworst(n) = Tworst ( n/2 ) + 1 n>1 Tworst(1) = 1 So, formally: Tworst(n) = logn + 1 = log(n+1) = O(logn)

Combinatorial problem
• • Fake-coin problem (S5.5) Josephus problem (S5.5)
Binary Search (basic idea)

Used with a sorted list (the list is in increasing order)
2013-2014-02 《Design and Analysis of Algorithm》 SCUN
2013-12-28 13
Average-Case Analysis (cont.)
• So we can represent binary search as a binary tree:
2013-2014-02 《Design and Analysis of Algorithm》
l 0; r n-1 while l r do m (l+r)/2 if K = A[m] return m else if K < A[m] r m-1 else l m+1 return -1
Hale Waihona Puke Thinking: How to rewrite this algorithm in recursive format?
Problem of size n
subproblem of size n/2
solution to the subproblem solution to the original problem
2013-2014-02 《Design and Analysis of Algorithm》
SCUN
2013-12-28

a a = n -1 a a
n =1
n >1
a n =1 n2 2 Decrease by constant n a = (a ) n > 1 and is even factor: ( n - 1) 2 2 ´ ) a n> 1 and is odd ( a
2013-2014-02 《Design and Analysis of Algorithm》 SCUN

2013-2014-02 《Design and Analysis of Algorithm》 SCUN
2013-12-28 8
Binary search example:Find the key value 14
0 1 2 3 4 5 6 7 8 9 10 11 12
3 14 27 31 39 42 55 70 74 81 85 93 98
2013-12-28 6
The Application of Decrease by a constant factor technique

Search problem • The binary search (S4.3, P135-139)

Numerical problem
• Russian peasant multiplication (S5.5)

Decrease by a constant factor (usually by half)
• • • • Binary search Fake-coin problem Russian peasant multiplication Josephus problem

Variable-size decrease
Goals of the Lecture

At the end of this lecture, you should
• Master the basic idea and all kinds of variations of decrease and conquer technique • Master the binary search algorithm and its best-case, worstcase and average-case analysis • Understand the fake coin problem and its solution • Be familiar with the Russian peasant multiplication method • Understand the josephus problem and its solution
2013-2014-02 《Design and Analysis of Algorithm》 SCUN
2013-12-28 12


Average-Case Analysis

Successful Search • If the search is always successful, there are n places (number of possible keys) the key could be found • We will consider each of these to be equivalent and so will give each a probability of 1/n • There is one place we check on the first pass, two places we could check on the second pass, and four places we could check on the third pass, and so on.
2013-2014-02 《Design and Analysis of Algorithm》
SCUN
2013-12-28
3
Decrease-and-Conquer (basic idea)
1.
2. 3.
Reduce problem instance to smaller instance of the same problem Solve smaller instance Extend solution of smaller instance to obtain solution to original instance
2013-2014-02 《Design and Analysis of Algorithm》
SCUN
2013-12-28
10
Best-case Analysis
The input instances in which the target matches the middle element will lead to the best case. This means only one comparison is done. So we get: Tbest (n) = Ω(1)
4
3 Types of Decrease and Conquer

Decrease by a constant (usually by 1)
• Insertion sort • Graph traversal algorithms (DFS and BFS) • Algorithms for generating permutations, subsets
n Brute Force: a n = a a a
Consider the problem of exponentiation: an
a a = n 2 n 2 a a
n

Divide and conquer:
Decrease by one:
n
n =1 n >1
Chapter 5
Decrease and Conquer (I)
Introduction to Decrease-and-Conquer Decrease-by-a-Constant-Factor Algorithms The Binary Search Fake-coin Problem Russian Peasant Multiplication Josephus Problem
low=0
27>14
相关文档
最新文档