算法分析与设计 第二版 英文版 (潘彦 著) 清华大学出版社 课后答案--solu4
算法分析与设计重点课后习题答案

习题13.设计算法求数组中相差最小的两个元素(称为最接近数)的差。
要求分别给出伪代码和C++描述。
//采用分治法//对数组先进行快速排序//在依次比较相邻的差#include <iostream>using namespace std;int partions(int b[],int low,int high){int prvotkey=b[low];b[0]=b[low];while (low<high){while (low<high&&b[high]>=prvotkey)--high;b[low]=b[high];while (low<high&&b[low]<=prvotkey)++low;b[high]=b[low];}b[low]=b[0];return low;}void qsort(int l[],int low,int high){int prvotloc;if(low<high){prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴qsort(l,low,prvotloc-1); //递归调用排序由low 到prvotloc-1qsort(l,prvotloc+1,high); //递归调用排序由 prvotloc+1到 high}}void quicksort(int l[],int n){qsort(l,1,n); //第一个作为枢轴,从第一个排到第n个}int main(){int a[11]={0,2,32,43,23,45,36,57,14,27,39};int value=0;//将最小差的值赋值给valuefor (int b=1;b<11;b++)cout<<a[b]<<' ';cout<<endl;quicksort(a,11);for(int i=0;i!=9;++i){if( (a[i+1]-a[i])<=(a[i+2]-a[i+1]) )value=a[i+1]-a[i];elsevalue=a[i+2]-a[i+1];}cout<<value<<endl;return 0;}4.设数组a[n]中的元素均不相等,设计算法找出a[n]中一个既不是最大也不是最小的元素,并说明最坏情况下的比较次数。
Algorithms Chapter 1 绪论

怎么处理?
30
The Design and Analysis of Algorithms
Chapter 1 Introduction to Algorithms
What’s an Algorithm?
算法是一系列解决问题的清晰指令,也就是说,能够对 一定规范的输入,在有限时间内获得所要求的输出。
16
The Design and Analysis of Algorithms
算法可以解决哪些问题
找出人类DNA中所有100000种基因,确定构成人类DNA的30亿种化学基 17 对的各种序列。
The Design and Analysis of Algorithms
算法可以解决哪些问题
快速访问和检索互联网数据
The Design and Analysis of Algorithms
例子
• “贝格尔”编排法(Beiger Arrangement) 把参赛队数分一半(参赛队为单数时,最后以“0” 表示形成双数),前一半由1号开始,自上而下写在 左边;后一半的数自下而上写在右边,然后用横线 把相对的号数连接起来。这即是第一轮的比赛。 第二轮将第一轮右上角的编号(“0”或最大的一个代 号数)移到左角上,第三轮又移到右角上,以此类推。 即单数轮次时“0”或最大的一个代号在右上角,双 数轮次时则在左上角。
//使用欧几里得算法计算gcd(m,n) //输入:两个不全为0的非负整数m,n //输出:m,n的最大公约数
28
The Design and Analysis of Algorithms
例子
• “贝格尔”编排法(Beiger Arrangement)
这种编排方法是否完美?
算法设计与分析(第2版)-王红梅-胡明-习题答案(1)

算法设计与分析(第2版)-王红梅-胡明-习题答案习题11. 图论诞生于七桥问题。
出生于瑞士的伟大数学家欧拉(Leonhard Euler ,1707—1783)提出并解决了该问题。
七桥问题是这样描述的:一个人是否能在一次步行中穿越哥尼斯堡(现在叫加里宁格勒,在波罗的海南岸)城中全部的七座桥后回到起点,且每座桥只经过一次,图 1.7是这条河以及河上的两个岛和七座桥的草图。
请将该问题的数据模型抽象出来,并判断此问题是否有解。
七桥问题属于一笔画问题。
输入:一个起点输出:相同的点1, 一次步行2, 经过七座桥,且每次只经历过一次3, 回到起点该问题无解:能一笔画的图形只有两类:一类是所有的点都是偶点。
另一类是只有二个奇点的图形。
2.在欧几里德提出的欧几里德算法中(即最初的欧几里德算法)用的不是除法而是减法。
请用伪代码描述这个版本的欧几里德算法1.r=m-n2.循环直到r=02.1 m=n图1.7 七桥问题2.2 n=r2.3 r=m-n3 输出m3.设计算法求数组中相差最小的两个元素(称为最接近数)的差。
要求分别给出伪代码和C++描述。
//采用分治法//对数组先进行快速排序//在依次比较相邻的差#include <iostream>using namespace std;int partions(int b[],int low,int high){int prvotkey=b[low];b[0]=b[low];while (low<high){while (low<high&&b[high]>=prvotkey)--high;b[low]=b[high];while (low<high&&b[low]<=prvotkey)++low;b[high]=b[low];}b[low]=b[0];return low;}void qsort(int l[],int low,int high){int prvotloc;if(low<high){prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴qsort(l,low,prvotloc-1); //递归调用排序由low 到prvotloc-1qsort(l,prvotloc+1,high); //递归调用排序由 prvotloc+1到 high}}void quicksort(int l[],int n){qsort(l,1,n); //第一个作为枢轴,从第一个排到第n个}int main(){int a[11]={0,2,32,43,23,45,36,57,14,27,39};int value=0;//将最小差的值赋值给valuefor (int b=1;b<11;b++)cout<<a[b]<<' ';cout<<endl;quicksort(a,11);for(int i=0;i!=9;++i){if( (a[i+1]-a[i])<=(a[i+2]-a[i+1]) )value=a[i+1]-a[i];elsevalue=a[i+2]-a[i+1];}cout<<value<<endl;return 0;}4.设数组a[n]中的元素均不相等,设计算法找出a[n]中一个既不是最大也不是最小的元素,并说明最坏情况下的比较次数。
算法设计与分析-课后习题集答案

(2)当 时, ,所以,可选 , 。对于 , ,所以, 。
(3)由(1)、(2)可知,取 , , ,当 时,有 ,所以 。
11. (1)当 时, ,所以 , 。可选 , 。对于 , ,即 。
(2)当 时, ,所以 , 。可选 , 。对于 , ,即 。
(3)因为 , 。当 时, , 。所以,可选 , ,对于 , ,即 。
第二章
2-17.证明:设 ,则 。
当 时, 。所以, 。
第五章
5-4.SolutionType DandC1(int left,int right)
{while(!Small(left,right)&&left<right)
{int m=Divide(left,right);
所以n-1<=m<=n (n-1)/2;
O(n)<=m<=O(n2);
克鲁斯卡尔对边数较少的带权图有较高的效率,而 ,此图边数较多,接近完全图,故选用普里姆算法。
10.
T仍是新图的最小代价生成树。
证明:假设T不是新图的最小代价生成树,T’是新图的最小代价生成树,那么cost(T’)<cost(T)。有cost(T’)-c(n-1)<cost(t)-c(n-1),即在原图中存在一颗生成树,其代价小于T的代价,这与题设中T是原图的最小代价生成树矛盾。所以假设不成立。证毕。
13.template <class T>
select (T&x,int k)
{
if(m>n) swap(m,n);
if(m+n<k||k<=0) {cout<<"Out Of Bounds"; return false;}
算法分析与设计 第二版 英文版 (潘彦 著) 清华大学出版社 课后答案--solu8

9. Shortest path counting A chess rook can move horizontally or vertically to any square in the same row or in the same column of a chessboard. Find the number of shortest paths by which a rook can move from one corner of a chessboard to the diagonally opposite corner [Gar78], p.10 (a) by a dynamic programming algorithm.
10. a. In the situation where teams A and B need i and j games, respectively, to win the series, consider the result of team A winning the game and the result of team A losing the game. b. Set up a table with five rows (0 ≤ i ≤ 4) and five columns (0 ≤ j ≤ 4) and fill it by using the recurrence derived in part (a). c. A pseudocode should be guided by the recurrence set up in part (a). The efficiency answers follow immediately from the table’s size and the time spent on computing each of its entries.
清华大学第二版算法分析与设计课件第二章pdf

Average case: A(n) – “average” over inputs of size n
• Number of times the basic operation will be executed on typical input • NOT the average of worst and best case • Expected number of basic operations repetitions considered as a random variable under some assumption about the probability distribution of all possible inputs of size n
Design and Analysis of Algorithms - Chapter 2 5
Example: Sequential search
Problem: Given a list of n elements and a search key K, find an element equal to K, if any. Algorithm: Scan the list and compare its successive elements with K until either a matching element is found (successful search) of the list is exhausted (unsuccessful search) Worst case Best case Average case
See table 2.1
Design and Analysis of Algorithms - Chapter 2
算法分析与设计习题答案

算法分析与设计习题答案《算法分析与设计》期末复习题及答案⼀、简要回答下列问题:1.算法重要特性是什么?2.算法分析的⽬的是什么?3.算法的时间复杂性与问题的什么因素相关?4.算法的渐进时间复杂性的含义?5.最坏情况下的时间复杂性和平均时间复杂性有什么不同?6.简述⼆分检索(折半查找)算法的基本过程。
7.背包问题的⽬标函数和贪⼼算法最优化量度相同吗?8.采⽤回溯法求解的问题,其解如何表⽰?有什么规定?9.回溯法的搜索特点是什么?10.n皇后问题回溯算法的判别函数place的基本流程是什么?11.为什么⽤分治法设计的算法⼀般有递归调⽤?12.为什么要分析最坏情况下的算法时间复杂性?13.简述渐进时间复杂性上界的定义。
14.⼆分检索算法最多的⽐较次数?15.快速排序算法最坏情况下需要多少次⽐较运算?16.贪⼼算法的基本思想?17.回溯法的解(x1,x2,……x n)的隐约束⼀般指什么?18.阐述归并排序的分治思路。
19.快速排序的基本思想是什么。
20.什么是直接递归和间接递归?消除递归⼀般要⽤到什么数据结构?21.什么是哈密顿环问题?22.⽤回溯法求解哈密顿环,如何定义判定函数?23.请写出prim算法的基本思想。
参考答案:1. 确定性、可实现性、输⼊、输出、有穷性2. 分析算法占⽤计算机资源的情况,对算法做出⽐较和评价,设计出额更好的算法。
3. 算法的时间复杂性与问题的规模相关,是问题⼤⼩n的函数。
4.当问题的规模n趋向⽆穷⼤时,影响算法效率的重要因素是T(n)的数量级,⽽其他因素仅是使时间复杂度相差常数倍,因此可以⽤T(n)的数量级(阶)评价算法。
时间复杂度T(n)的数量级(阶)称为渐进时间复杂性。
5. 最坏情况下的时间复杂性和平均时间复杂性考察的是n固定时,不同输⼊实例下的算法所耗时间。
最坏情况下的时间复杂性取的输⼊实例中最⼤的时间复杂度:W(n) = max{ T(n,I) } , I∈Dn平均时间复杂性是所有输⼊实例的处理时间与各⾃概率的乘积和:A(n) =∑P(I)T(n,I) I∈Dn6. 设输⼊是⼀个按⾮降次序排列的元素表A[i:j] 和x,选取A[(i+j)/2]与x⽐较,如果A[(i+j)/2]=x,则返回(i+j)/2,如果A[(i+j)/2]回溯法的搜索特点是什么7. 不相同。
算法设计与分析(第2版)习题答案

习题11. 图论诞生于七桥问题。
出生于瑞士的伟大数学家欧拉(Leonhard Euler ,1707—1783)提出并解决了该问题。
七桥问题是这样描述的:一个人是否能在一次步行中穿越哥尼斯堡(现在叫加里宁格勒,在波罗的海南岸)城中全部的七座桥后回到起点,且每座桥只经过一次,图 1.7是这条河以及河上的两个岛和七座桥的草图。
请将该问题的数据模型抽象出来,并判断此问题是否有解。
七桥问题属于一笔画问题。
输入:一个起点输出:相同的点1, 一次步行2, 经过七座桥,且每次只经历过一次3, 回到起点该问题无解:能一笔画的图形只有两类:一类是所有的点都是偶点。
另一类是只有二个奇点的图形。
2.在欧几里德提出的欧几里德算法中(即最初的欧几里德算法)用的不是除法而是减法。
请用伪代码描述这个版本的欧几里德算法1.r=m-n2.循环直到r=02.1 m=n2.2 n=r2.3 r=m-n3 输出m3.设计算法求数组中相差最小的两个元素(称为最接近数)的差。
要求分别给出伪代码和C ++描述。
//采用分治法//对数组先进行快速排序//在依次比较相邻的差#include <iostream>using namespace std;int partions(int b[],int low,int high) {图1.7 七桥问题int prvotkey=b[low];b[0]=b[low];while (low<high){while (low<high&&b[high]>=prvotkey)--high;b[low]=b[high];while (low<high&&b[low]<=prvotkey)++low;b[high]=b[low];}b[low]=b[0];return low;}void qsort(int l[],int low,int high){int prvotloc;if(low<high){prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴 qsort(l,low,prvotloc-1); //递归调用排序由low 到prvotloc-1qsort(l,prvotloc+1,high); //递归调用排序由 prvotloc+1到 high}}void quicksort(int l[],int n){qsort(l,1,n); //第一个作为枢轴,从第一个排到第n个}int main(){int a[11]={0,2,32,43,23,45,36,57,14,27,39};int value=0;//将最小差的值赋值给valuefor (int b=1;b<11;b++)cout<<a[b]<<' ';cout<<endl;quicksort(a,11);for(int i=0;i!=9;++i){if( (a[i+1]-a[i])<=(a[i+2]-a[i+1]) )value=a[i+1]-a[i];elsevalue=a[i+2]-a[i+1];}cout<<value<<endl;return 0;}4.设数组a[n]中的元素均不相等,设计算法找出a[n]中一个既不是最大也不是最小的元素,并说明最坏情况下的比较次数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ww
9. Let A[0..n − 1] be an array of n distinct real numbers. A pair (A[i], A[j ]) is said to be an inversion if these numbers are out of order, i.e., i < j but A[i] > A[j ]. Design an O(n log n) algorithm for counting the number of inversions.
b. T (n) = 4T (n/2) + n2 , T (1) = 1 c. T (n) = 4T (n/2) + n3 , T (1) = 1 6. Apply mergesort to sort the list E, X, A, M, P, L, E in alphabetical order. 7. Is mergesort a stable sorting algorithm? 8. a. Solve the recurrence relation for the number of key comparisons made by mergesort in the worst case. (You may assume that n = 2k .)
da
w.
b. Set up a recurrence relation for the number of key comparisons made by mergesort on best-case inputs and solve it for n = 2k .
co
m
Hints to Exercises 4.1
课
后
b. Set up and solve a recurrence relation for the number of multiplications made by this algorithm. c. How does this algorithm compare with the brute-force algorithm for this problem?
Exercises 4.1
b. What will be your algorithm’s output for arrays with several elements of the largest value? c. Set up and solve a recurrence relation for the number of key comparisons made by your algorithm.
Solving it by backward substitutions for n = 2k yields the following: C (2k ) = = = = = = = 2C (2k−1 ) + 1 2[2C (2k−2 ) + 1] + 1 = 22 C (2k−2 ) + 2 + 1 22 [2C (2k−3 ) + 1] + 2 + 1 = 23 C (2k−3 ) + 22 + 2 + 1 ... 2i C (2k−i ) + 2i−1 + 2i−2 + ... + 1 ... 2k C (2k−k ) + 2k−1 + 2k−2 + ... + 1 = 2k − 1 = n − 1.
6. Trace the algorithm as it was done for another input in the section. 7. How can mergesort reverse a relative ordering of two elements?
c. Do not forget to include key moves made both before the split and during the merging. 9. Modify mergesort to solve the problem.
b. Set up and solve (for n = 2k ) a recurrence relation for the number of key comparisons made by your algorithm.
3. a. Write a pseudocode for a divide-and-conquer algorithm for the exponentiation problem of computing an where a > 0 and n is a positive integer.
w.
3
kh
8. a. Use backward substitutions, as usual.
da
w.
co
m
Solutions to Exercises 4.1
1. a. Call Algorithm MaxIndex (A[0..n − 1]) where Algorithm MaxIndex (A[l..r]) //Input: A portion of array A[0..n − 1] between indices l and r (l ≤ r) //Output: The index of the largest element in A[l..r] if l = r return l else temp1 ← MaxIndex (A[l.. (l + r)/2 ]) temp2 ← MaxIndex (A[ (l + r)/2 + 1..r]) if A[temp1] ≥ A[temp2] return temp1 else return temp2 b. This algorithm returns the index of thቤተ መጻሕፍቲ ባይዱ leftmost largest element. c. The recurrence for the number of element comparisons is
4. As mentioned in Chapter 2, logarithm bases are irrelevant in most contexts arising in analyzing an algorithm’s efficiency class. Is it true for both assertions of the Master Theorem that include logarithms? 5. Find the order of growth for solutions of the following recurrences. a. T (n) = 4T (n/2) + n, T (1) = 1
课
后
Design a divide-and-conquer algorithm for this problem.
答 案
11. Tromino puzzle A tromino is an L-shaped tile formed by adjacent 1by-1 squares. The problem is to cover any 2n -by-2n chessboard with one missing square (anywhere on the board) with trominoes. Trominoes should cover all the squares of the board except the missing one with no overlaps.
11. A divide-and-conquer algorithm works by reducing a problem’s instance to several smaller instances of the same problem.
课
后
答 案
10. n/a
网
ww
b. What inputs minimize the number of key comparisons made by mergesort? How many comparisons are made by mergesort on such inputs during the merging stage?
1. In more than one respect, this question is similar to the divide-and-conquer computation of the sum of n numbers. Also, you were asked to analyze an almost identical algorithm in Exercises 2.4. 2. Unlike Problem 1, a divide-and-conquer algorithm for this problem can be more efficient by a constant factor than the brute-force algorithm. 3. How would you compute a8 by solving two exponentiation problems of size 4? How about a9 ? 4. Look at the notations used in the theorem’s statement. 5. Apply the Master Theorem.
kh
d. How does this algorithm compare with the brute-force algorithm for this problem?