算法分析第二章答案
- 格式:pptx
- 大小:496.45 KB
- 文档页数:35
算法设计与分析C语⾔描述(陈慧南版)课后答案第⼀章15P1-3. 最⼤公约数为1。
快1414倍。
主要考虑循环次数,程序1-2的while 循环体做了10次,程序1-3的while 循环体做了14141次(14142-2循环)若考虑其他语句,则没有这么多,可能就601倍。
第⼆章32P2-8.(1)画线语句的执⾏次数为log n 。
(log )n O 。
划线语句的执⾏次数应该理解为⼀格整体。
(2)画线语句的执⾏次数为111(1)(2)16jnii j k n n n ===++=∑∑∑。
3()n O 。
(3)画线语句的执⾏次数为。
O 。
(4)当n 为奇数时画线语句的执⾏次数为(1)(3)4n n ++,当n 为偶数时画线语句的执⾏次数为 2(2)4n +。
2()n O 。
2-10.(1)当 1n ≥ 时,225825n n n -+≤,所以,可选 5c =,01n =。
对于0n n ≥,22()5825f n n n n =-+≤,所以,22582()n n n -+=O 。
(2)当 8n ≥ 时,2222582524n n n n n -+≥-+≥,所以,可选 4c =,08n =。
对于0n n ≥,22()5824f n n n n =-+≥,所以,22582()n n n -+=Ω。
(3)由(1)、(2)可知,取14c =,25c =,08n =,当0n n ≥时,有22212582c n n n c n ≤-+≤,所以22582()n n n -+=Θ。
2-11. (1) 当3n ≥时,3log log n n n <<,所以()20log 21f n n n n =+<,3()log 2g n n n n =+>。
可选 212c =,03n =。
对于0n n ≥,()()f n cg n ≤,即()(())f n g n =O 。
注意:是f (n )和g (n )的关系。
算法设计与分析知到章节测试答案智慧树2023年最新天津大学第一章测试1.下列关于效率的说法正确的是()。
参考答案:提高程序效率的根本途径在于选择良好的设计方法,数据结构与算法;效率主要指处理机时间和存储器容量两个方面;效率是一个性能要求,其目标应该在需求分析时给出2.算法的时间复杂度取决于()。
参考答案:问题的规模;待处理数据的初态3.计算机算法指的是()。
参考答案:解决问题的有限运算序列4.归并排序法的时间复杂度和空间复杂度分别是()。
参考答案:O(nlog2n);O(n)5.将长度分别为m,n的两个单链表合并为一个单链表的时间复杂度为O(m+n)。
()参考答案:错6.用渐进表示法分析算法复杂度的增长趋势。
()参考答案:对7.算法分析的两个主要方面是时间复杂度和空间复杂度的分析。
()参考答案:对8.某算法所需时间由以下方程表示,求出该算法时间复杂度()。
参考答案:O(nlog2n)9.下列代码的时间复杂度是()。
参考答案:O(log2N)10.下列算法为在数组A[0,...,n-1]中找出最大值和最小值的元素,其平均比较次数为()。
参考答案:3n/2-3/2第二章测试1.可用Master方法求解的递归方程的形式为()。
参考答案:T(n)=aT(n/b)+f(n) , a≥1, b>1, 为整数, f(n)>0.2.参考答案:对3.假定,, 递归方程的解是. ( )参考答案:对4.假设数组A包含n个不同的元素,需要从数组A中找出n/2个元素,要求所找的n/2个元素的中点元素也是数组A的中点元素。
针对该问题的任何算法需要的时间复杂度的下限必为。
( )参考答案:错5.使用Master方法求解递归方程的解为().参考答案:6.考虑包含n个二维坐标点的集合S,其中n为偶数,且所有坐标点中的均不相同。
一条竖直的直线若能把S集合分成左右两部分坐标点个数相同的子集合,则称直线L为集合S的一条分界线。
若给定集合S,则可在时间内找到这条分界线L。
绪论单元测试1.山东师范大学的管教授在哪个问题上给出了比较好的解决方法。
A:邮递员问题B:背包问题C:装载问题D:最大团问题答案:A第一章测试1.算法具备的四个基本性质是()A:输入B:有限性C:确定性D:输出答案:ABCD2.算法就是程序A:错B:对答案:A3.描述渐进上界的符号是()A:ΩB:ωC:OD:θ答案:C4.f(n)=3n2+n+1,下面不正确的是()A:f(n)=O(n3)B:f(n)=O(n2)C:f(n)=O(2n)D:f(n)=O(3n2)答案:C5.在算法分析中,我们希望找到更加高阶的上界函数A:错B:对答案:A第二章测试1.Strassen 矩阵乘法是利用()实现的算法。
A:贪心法B:分治策略C:动态规划法D:回溯法答案:B2.使用分治法求解不需要满足的条件是()A:子问题不能够重复B:子问题的解可以合并C:子问题必须是一样的D:原问题和子问题使用相同的方法解答案:C3.实现棋盘覆盖算法利用的算法是()。
A:分治法B:回溯法C:动态规划法D:贪心法答案:A4.实现循环赛日程表利用的算法是()。
A:贪心法B:回溯法C:分治策略D:动态规划法答案:C5.从分治法的一般设计模式可以看出,用它设计出的程序一般是递归算法A:对B:错答案:A第三章测试1.动态规划算法一般分成()三个阶段。
A:求解B:分析C:分段D:汇总答案:ABC2.动态规划的基本要素有()?A:备忘录方法B:最优子结构C:子问题的重叠性质答案:ABC3.用动态规划法求解的问题都可以分解为相互重叠的子问题。
A:对B:错答案:A4.动态规划法利用递推关系式()计算,实现动态规划过程。
A:循环B:递归C:自底向上D:自顶向下答案:C5.最优子结构是问题可以用动态规划法求解的前提。
A:错B:对答案:B第四章测试1.贪心算法中每次做出的贪心选择都是全局最优选择。
A:对B:错答案:B2.下面问题不能使用贪心法解决的是A:N皇后问题B:最小花费生成树问题C:背包问题D:单源最短路径问题答案:A3.背包问题的贪心算法所需的计算时间为A:O(n2n)B:O(n)C:O(nlogn)D:O(2n)答案:C4.哈夫曼编码是自底向上构造的A:错B:对答案:B5.Kruskal算法的时间复杂度是A:O(eloge)B:O(n)C:O(nlogn)D:O(2n)答案:A第五章测试1.回溯法就是穷举法A:错B:对答案:A2.回溯法使用的是广度优先遍历A:对B:错答案:B3.回溯法必须寻找一个限界函数A:对B:错答案:B4.使用回溯法时可以考虑以下哪些方面()A:约束函数B:解空间结构C:解的向量形式D:解的最优子结构性质答案:ABC5.回溯法在处理n皇后问题时,必须把解空间组织成子集树。
作业一学号:_____ 姓名:_____说明:1、正文用宋体小四号,1.5倍行距。
2、报告中的图片、表格中的文字均用宋体五号,单倍行距。
3、图片、表格均需要有图片编号和标题,均用宋体五号加粗。
4、参考文献用宋体、五号、单倍行距,请参照参考文献格式国家标准(GB/T 7714-2005)。
5、公式请使用公式编辑器。
P144.用伪代码写一个算法来求方程ax2+bx+c=0的实根,a,b,c 是任意实系数。
(可以假设sqrt(x)是求平方根的函数。
)算法:Equate(a,b,c)//实现二元一次方程求解实数根//输入:任意系数a,b,c//输出:方程的实数根x1,x2或无解If a≠0p←b2−4acIf p>0x1←−b+sqrt(p)2ax2←−b−sqrt(p)2areturn x1,x2else if p=0return −b2aelsereturn “no real roots”elseif b≠0return −cbelseif c≠0return “no real numbers”elsereturn “no real roots”5.写出将十进制正整数转换为二进制整数的标准算法。
a.用文字描述。
b.用伪代码描述。
a.解:输入:一个正整数n输出:正整数n相应的二进制数第一步:用n 除以2,余数赋给K[i](i=0,1,2...),商赋给n第二步:如果n=0 ,则到第三步,否则重复第一步第三步:将K[i]按照i从高到低的顺序输出b.解:算法:DecToBin(n)//实现正整数十进制转二进制//输入:一个正整数n//输出:正整数n对应的二进制数组K[0..i]i ←1while n≠0 doK[i]←n%2n←(int)n/2i ++while i≠0doprint K[i]i - -p462.请用O,Ω 和θ的非正式定义来判断下列断言是真还是假。
a. n(n+1)/2∈O(n3)b. n(n+1)/2∈O(n2)c. n(n+1)/2∈θ(n3)d. n(n+1)/2∈Ω(n)解:断言为真:a,b,d断言为假:cP535.考虑下面的算法。
算法设计与分析基础课后练习答案习题1.14.设计一个计算的算法,n是任意正整数。
除了赋值和比较运算,该算法只能用到基本的四则运算操作。
算法求//输入:一个正整数n2//输出:。
step1:a=1;step2:若a*a<n 转step3,否则输出a;step3:a=a+1转step 2;5. a.用欧几里德算法求gcd(31415,14142)。
b. 用欧几里德算法求gcd(31415,14142),比检查min{m,n}和gcd(m,n)间连续整数的算法快多少倍?请估算一下。
a. gcd(31415,14142) = gcd(14142,3131) = gcd(3131, 1618) =gcd(1618, 1513) = gcd(1513, 105) = gcd(1513, 105) = gcd(105, 43) =gcd(43, 19) = gcd(19, 5) = gcd(5, 4) = gcd(4, 1) = gcd(1,0) = 1.b.有a可知计算g cd(31415,14142)欧几里德算法做了11次除法。
连续整数检测算法在14142每次迭代过程中或者做了一次除法,或者两次除法,因此这个算法做除法的次数鉴于1·14142和2·14142之间,所以欧几里德算法比此算法快1·14142/11 ≈1300 与2·14142/11 ≈2600 倍之间。
6.证明等式gc d(m,n)=gcd(n,m mod n)对每一对正整数m,n都成立.Hint:根据除法的定义不难证明:●如果d整除u和v, 那么d一定能整除u±v;●如果d整除u,那么d也能够整除u的任何整数倍k u.对于任意一对正整数m,n,若d能整除m和n,那么d一定能整除n和r=m mod n=m-qn;显然,若d能整除n和r,也一定能整除m=r+qn和n。
第一章测试1.解决一个问题通常有多种方法。
若说一个算法“有效”是指( )A:这个算法能在人的反应时间内将问题解决B:(这个算法能在一定的时间和空间资源限制内将问题解决)和(这个算法比其他已知算法都更快地将问题解决)C:这个算法能在一定的时间和空间资源限制内将问题解决D:这个算法比其他已知算法都更快地将问题解决答案:B2.农夫带着狼、羊、白菜从河的左岸到河的右岸,农夫每次只能带一样东西过河,而且,没有农夫看管,狼会吃羊,羊会吃白菜。
请问农夫能不能过去?()A:不一定B:不能过去C:能过去答案:C3.下述()不是是算法的描述方式。
A:自然语言B:程序设计语言C:E-R图D:伪代码答案:C4.有一个国家只有6元和7元两种纸币,如果你是央行行长,你会设置()为自动取款机的取款最低限额。
A:40B:42C:29D:30答案:D5.算法是一系列解决问题的明确指令。
()A:对B:错答案:A6.程序=数据结构+算法()A:错B:对答案:B7.同一个问题可以用不同的算法解决,同一个算法也可以解决不同的问题。
()A:错答案:B8.算法中的每一条指令不需有确切的含义,对于相同的输入不一定得到相同的输出。
( )A:错B:对答案:A9.可以用同样的方法证明算法的正确性与错误性 ( )A:对B:错答案:B10.求解2个数的最大公约数至少有3种方法。
( )A:错B:对答案:A11.没有好的算法,就编不出好的程序。
()A:对B:错答案:A12.算法与程序没有关系。
( )A:错B:对答案:A13.我将来不进行软件开发,所以学习算法没什么用。
( )A:对B:错答案:B14.gcd(m,n)=gcd(n,m m od n)并不是对每一对正整数(m,n)都成立。
( )A:错B:对答案:A15.既然程序设计语言可以描述算法,所以算法就是程序。
( )A:错B:对答案:A第二章测试1.并不是所有的算法,规模更大的输入需要更长的运行时间。
( )A:对答案:B2.算法效率分析框架主要关心一个算法的基本操作次数的增长次数,并把它作为算法效率的主要指标。
《数据结构与算法分析》(C++第二版)【美】Clifford A.Shaffer著课后习题答案二5Binary Trees5.1 Consider a non-full binary tree. By definition, this tree must have some internalnode X with only one non-empty child. If we modify the tree to removeX, replacing it with its child, the modified tree will have a higher fraction ofnon-empty nodes since one non-empty node and one empty node have been removed.5.2 Use as the base case the tree of one leaf node. The number of degree-2 nodesis 0, and the number of leaves is 1. Thus, the theorem holds.For the induction hypothesis, assume the theorem is true for any tree withn − 1 nodes.For the induction step, consider a tree T with n nodes. Remove from the treeany leaf node, and call the resulting tree T. By the induction hypothesis, Thas one more leaf node than it has nodes of degree 2.Now, restore the leaf node that was removed to form T. There are twopossible cases.(1) If this leaf node is the only child of its parent in T, then the number ofnodes of degree 2 has not changed, nor has the number of leaf nodes. Thus,the theorem holds.(2) If this leaf node is the child of a node in T with degree 2, then that nodehas degree 1 in T. Thus, by restoring the leaf node we are adding one newleaf node and one new node of degree 2. Thus, the theorem holds.By mathematical induction, the theorem is correct.32335.3 Base Case: For the tree of one leaf node, I = 0, E = 0, n = 0, so thetheorem holds.Induction Hypothesis: The theorem holds for the full binary tree containingn internal nodes.Induction Step: Take an arbitrary tree (call it T) of n internal nodes. Selectsome internal node x from T that has two leaves, and remove those twoleaves. Call the resulting tree T’. Tree T’ is full and has n−1 internal nodes,so by the Induction Hypothesis E = I + 2(n − 1).Call the depth of node x as d. Restore the two children of x, each at leveld+1. We have nowadded d to I since x is now once again an internal node.We have now added 2(d + 1) − d = d + 2 to E since we added the two leafnodes, but lost the contribution of x to E. Thus, if before the addition we had E = I + 2(n − 1) (by the induction hypothesis), then after the addition we have E + d = I + d + 2 + 2(n − 1) or E = I + 2n which is correct. Thus,by the principle of mathematical induction, the theorem is correct.5.4 (a) template <class Elem>void inorder(BinNode<Elem>* subroot) {if (subroot == NULL) return; // Empty, do nothingpreorder(subroot->left());visit(subroot); // Perform desired actionpreorder(subroot->right());}(b) template <class Elem>void postorder(BinNode<Elem>* subroot) {if (subroot == NULL) return; // Empty, do nothingpreorder(subroot->left());preorder(subroot->right());visit(subroot); // Perform desired action}5.5 The key is to search both subtrees, as necessary.template <class Key, class Elem, class KEComp>bool search(BinNode<Elem>* subroot, Key K);if (subroot == NULL) return false;if (subroot->value() == K) return true;if (search(subroot->right())) return true;return search(subroot->left());}34 Chap. 5 Binary Trees5.6 The key is to use a queue to store subtrees to be processed.template <class Elem>void level(BinNode<Elem>* subroot) {AQueue<BinNode<Elem>*> Q;Q.enqueue(subroot);while(!Q.isEmpty()) {BinNode<Elem>* temp;Q.dequeue(temp);if(temp != NULL) {Print(temp);Q.enqueue(temp->left());Q.enqueue(temp->right());}}}5.7 template <class Elem>int height(BinNode<Elem>* subroot) {if (subroot == NULL) return 0; // Empty subtreereturn 1 + max(height(subroot->left()),height(subroot->right()));}5.8 template <class Elem>int count(BinNode<Elem>* subroot) {if (subroot == NULL) return 0; // Empty subtreeif (subroot->isLeaf()) return 1; // A leafreturn 1 + count(subroot->left()) +count(subroot->right());}5.9 (a) Since every node stores 4 bytes of data and 12 bytes of pointers, the overhead fraction is 12/16 = 75%.(b) Since every node stores 16 bytes of data and 8 bytes of pointers, the overhead fraction is 8/24 ≈ 33%.(c) Leaf nodes store 8 bytes of data and 4 bytes of pointers; internal nodesstore 8 bytes of data and 12 bytes of pointers. Since the nodes havedifferent sizes, the total space needed for internal nodes is not the sameas for leaf nodes. Students must be careful to do the calculation correctly,taking the weighting into account. The correct formula looks asfollows, given that there are x internal nodes and x leaf nodes.4x + 12x12x + 20x= 16/32 = 50%.(d) Leaf nodes store 4 bytes of data; internal nodes store 4 bytes of pointers. The formula looks as follows, given that there are x internal nodes and35x leaf nodes:4x4x + 4x= 4/8 = 50%.5.10 If equal valued nodes were allowed to appear in either subtree, then during a search for all nodes of a given value, whenever we encounter a node of that value the search would be required to search in both directions.5.11 This tree is identical to the tree of Figure 5.20(a), except that a node with value 5 will be added as the right child of the node with value 2.5.12 This tree is identical to the tree of Figure 5.20(b), except that the value 24 replaces the value 7, and the leaf node that originally contained 24 is removed from the tree.5.13 template <class Key, class Elem, class KEComp>int smallcount(BinNode<Elem>* root, Key K);if (root == NULL) return 0;if (KEComp.gt(root->value(), K))return smallcount(root->leftchild(), K);elsereturn smallcount(root->leftchild(), K) +smallcount(root->rightchild(), K) + 1;5.14 template <class Key, class Elem, class KEComp>void printRange(BinNode<Elem>* root, int low,int high) {if (root == NULL) return;if (KEComp.lt(high, root->val()) // all to leftprintRange(root->left(), low, high);else if (KEComp.gt(low, root->val())) // all to rightprintRange(root->right(), low, high);else { // Must process both childrenprintRange(root->left(), low, high);PRINT(root->value());printRange(root->right(), low, high);}}5.15 The minimum number of elements is contained in the heap with a single node at depth h − 1, for a total of 2h−1 nodes.The maximum number of elements is contained in the heap that has completely filled up level h − 1, for a total of 2h − 1 nodes.5.16 The largest element could be at any leaf node.5.17 The corresponding array will be in the following order (equivalent to level order for the heap):12 9 10 5 4 1 8 7 3 236 Chap. 5 Binary Trees5.18 (a) The array will take on the following order:6 5 3 4 2 1The value 7 will be at the end of the array.(b) The array will take on the following order:7 4 6 3 2 1The value 5 will be at the end of the array.5.19 // Min-heap classtemplate <class Elem, class Comp> class minheap {private:Elem* Heap; // Pointer to the heap arrayint size; // Maximum size of the heapint n; // # of elements now in the heapvoid siftdown(int); // Put element in correct placepublic:minheap(Elem* h, int num, int max) // Constructor{ Heap = h; n = num; size = max; buildHeap(); }int heapsize() const // Return current size{ return n; }bool isLeaf(int pos) const // TRUE if pos a leaf{ return (pos >= n/2) && (pos < n); }int leftchild(int pos) const{ return 2*pos + 1; } // Return leftchild posint rightchild(int pos) const{ return 2*pos + 2; } // Return rightchild posint parent(int pos) const // Return parent position { return (pos-1)/2; }bool insert(const Elem&); // Insert value into heap bool removemin(Elem&); // Remove maximum value bool remove(int, Elem&); // Remove from given pos void buildHeap() // Heapify contents{ for (int i=n/2-1; i>=0; i--) siftdown(i); }};template <class Elem, class Comp>void minheap<Elem, Comp>::siftdown(int pos) { while (!isLeaf(pos)) { // Stop if pos is a leafint j = leftchild(pos); int rc = rightchild(pos);if ((rc < n) && Comp::gt(Heap[j], Heap[rc]))j = rc; // Set j to lesser child’s valueif (!Comp::gt(Heap[pos], Heap[j])) return; // Done37swap(Heap, pos, j);pos = j; // Move down}}template <class Elem, class Comp>bool minheap<Elem, Comp>::insert(const Elem& val) { if (n >= size) return false; // Heap is fullint curr = n++;Heap[curr] = val; // Start at end of heap// Now sift up until curr’s parent < currwhile ((curr!=0) &&(Comp::lt(Heap[curr], Heap[parent(curr)]))) {swap(Heap, curr, parent(curr));curr = parent(curr);}return true;}template <class Elem, class Comp>bool minheap<Elem, Comp>::removemin(Elem& it) { if (n == 0) return false; // Heap is emptyswap(Heap, 0, --n); // Swap max with last valueif (n != 0) siftdown(0); // Siftdown new root valit = Heap[n]; // Return deleted valuereturn true;}38 Chap. 5 Binary Trees// Remove value at specified positiontemplate <class Elem, class Comp>bool minheap<Elem, Comp>::remove(int pos, Elem& it) {if ((pos < 0) || (pos >= n)) return false; // Bad posswap(Heap, pos, --n); // Swap with last valuewhile ((pos != 0) &&(Comp::lt(Heap[pos], Heap[parent(pos)])))swap(Heap, pos, parent(pos)); // Push up if largesiftdown(pos); // Push down if small keyit = Heap[n];return true;}5.20 Note that this summation is similar to Equation 2.5. To solve the summation requires the shifting technique from Chapter 14, so this problem may be too advanced for many students at this time. Note that 2f(n) − f(n) = f(n),but also that:2f(n) − f(n) = n(24+48+616+ ··· +2(log n − 1)n) −n(14+28+316+ ··· +log n − 1n)logn−1i=112i− log n − 1n)= n(1 − 1n− log n − 1n)= n − log n.5.21 Here are the final codes, rather than a picture.l 00h 010i 011e 1000f 1001j 101d 11000a 1100100b 1100101c 110011g 1101k 11139The average code length is 3.234455.22 The set of sixteen characters with equal weight will create a Huffman coding tree that is complete with 16 leaf nodes all at depth 4. Thus, the average code length will be 4 bits. This is identical to the fixed length code. Thus, in this situation, the Huffman coding tree saves no space (and costs no space).5.23 (a) By the prefix property, there can be no character with codes 0, 00, or 001x where “x” stands for any binary string.(b) There must be at least one code with each form 1x, 01x, 000x where“x” could be any binary string (including the empty string).5.24 (a) Q and Z are at level 5, so any string of length n containing only Q’s and Z’s requires 5n bits.(b) O and E are at level 2, so any string of length n containing only O’s and E’s requires 2n bits.(c) The weighted average is5 ∗ 5 + 10 ∗ 4 + 35 ∗ 3 + 50 ∗ 2100bits per character5.25 This is a straightforward modification.// Build a Huffman tree from minheap h1template <class Elem>HuffTree<Elem>*buildHuff(minheap<HuffTree<Elem>*,HHCompare<Elem> >* hl) {HuffTree<Elem> *temp1, *temp2, *temp3;while(h1->heapsize() > 1) { // While at least 2 itemshl->removemin(temp1); // Pull first two treeshl->removemin(temp2); // off the heaptemp3 = new HuffTree<Elem>(temp1, temp2);hl->insert(temp3); // Put the new tree back on listdelete temp1; // Must delete the remnantsdelete temp2; // of the trees we created}return temp3;}6General Trees6.1 The following algorithm is linear on the size of the two trees. // Return TRUE iff t1 and t2 are roots of identical// general treestemplate <class Elem>bool Compare(GTNode<Elem>* t1, GTNode<Elem>* t2) { GTNode<Elem> *c1, *c2;if (((t1 == NULL) && (t2 != NULL)) ||((t2 == NULL) && (t1 != NULL)))return false;if ((t1 == NULL) && (t2 == NULL)) return true;if (t1->val() != t2->val()) return false;c1 = t1->leftmost_child();c2 = t2->leftmost_child();while(!((c1 == NULL) && (c2 == NULL))) {if (!Compare(c1, c2)) return false;if (c1 != NULL) c1 = c1->right_sibling();if (c2 != NULL) c2 = c2->right_sibling();}}6.2 The following algorithm is Θ(n2).// Return true iff t1 and t2 are roots of identical// binary treestemplate <class Elem>bool Compare2(BinNode<Elem>* t1, BinNode<Elem* t2) { BinNode<Elem> *c1, *c2;if (((t1 == NULL) && (t2 != NULL)) ||((t2 == NULL) && (t1 != NULL)))return false;if ((t1 == NULL) && (t2 == NULL)) return true;4041if (t1->val() != t2->val()) return false;if (Compare2(t1->leftchild(), t2->leftchild())if (Compare2(t1->rightchild(), t2->rightchild())return true;if (Compare2(t1->leftchild(), t2->rightchild())if (Compare2(t1->rightchild(), t2->leftchild))return true;return false;}6.3 template <class Elem> // Print, postorder traversalvoid postprint(GTNode<Elem>* subroot) {for (GTNode<Elem>* temp = subroot->leftmost_child();temp != NULL; temp = temp->right_sibling())postprint(temp);if (subroot->isLeaf()) cout << "Leaf: ";else cout << "Internal: ";cout << subroot->value() << "\n";}6.4 template <class Elem> // Count the number of nodesint gencount(GTNode<Elem>* subroot) {if (subroot == NULL) return 0int count = 1;GTNode<Elem>* temp = rt->leftmost_child();while (temp != NULL) {count += gencount(temp);temp = temp->right_sibling();}return count;}6.5 The Weighted Union Rule requires that when two parent-pointer trees are merged, the smaller one’s root becomes a child of the larger one’s root. Thus, we need to keep track of the number of nodes in a tree. To do so, modify the node array to store an integer value with each node. Initially, each node isin its own tree, so the weights for each node begin as 1. Whenever we wishto merge two trees, check the weights of the roots to determine which has more nodes. Then, add to the weight of the final root the weight of the new subtree.6.60 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15-1 0 0 0 0 0 0 6 0 0 0 9 0 0 12 06.7 The resulting tree should have the following structure:42 Chap. 6 General TreesNode 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Parent 4 4 4 4 -1 4 4 0 0 4 9 9 9 12 9 -16.8 For eight nodes labeled 0 through 7, use the following series of equivalences: (0, 1) (2, 3) (4, 5) (6, 7) (4 6) (0, 2) (4 0)This requires checking fourteen parent pointers (two for each equivalence),but none are actually followed since these are all roots. It is possible todouble the number of parent pointers checked by choosing direct children ofroots in each case.6.9 For the “lists of Children” representation, every node stores a data value and a pointer to its list of children. Further, every child (every node except the root)has a record associated with it containing an index and a pointer. Indicatingthe size of the data value as D, the size of a pointer as P and the size of anindex as I, the overhead fraction is3P + ID + 3P + I.For the “Left Child/Right Sibling” representation, every node stores three pointers and a data value, for an overhead fraction of3PD + 3P.The first linked representation of Section 6.3.3 stores with each node a datavalue and a size field (denoted by S). Each child (every node except the root)also has a pointer pointing to it. The overhead fraction is thusS + PD + S + Pmaking it quite efficient.The second linked representation of Section 6.3.3 stores with each node adata value and a pointer to the list of children. Each child (every node exceptthe root) has two additional pointers associated with it to indicate its placeon the parent’s linked list. Thus, the overhead fraction is3PD + 3P.6.10 template <class Elem>BinNode<Elem>* convert(GTNode<Elem>* genroot) {if (genroot == NULL) return NULL;43GTNode<Elem>* gtemp = genroot->leftmost_child();btemp = new BinNode(genroot->val(), convert(gtemp),convert(genroot->right_sibling()));}6.11 • Parent(r) = (r − 1)/k if 0 < r < n.• Ith child(r) = kr + I if kr +I < n.• Left sibling(r) = r − 1 if r mod k = 1 0 < r < n.• Right sibling(r) = r + 1 if r mod k = 0 and r + 1 < n.6.12 (a) The overhead fraction is4(k + 1)4 + 4(k + 1).(b) The overhead fraction is4k16 + 4k.(c) The overhead fraction is4(k + 2)16 + 4(k + 2).(d) The overhead fraction is2k2k + 4.6.13 Base Case: The number of leaves in a non-empty tree of 0 internal nodes is (K − 1)0 + 1 = 1. Thus, the theorem is correct in the base case.Induction Hypothesis: Assume that the theorem is correct for any full Karytree containing n internal nodes.Induction Step: Add K children to an arbitrary leaf node of the tree withn internal nodes. This new tree now has 1 more internal node, and K − 1more leaf nodes, so theorem still holds. Thus, the theorem is correct, by the principle of Mathematical Induction.6.14 (a) CA/BG///FEDD///H/I//(b) CA/BG/FED/H/I6.15 X|P-----| | |C Q R---| |V M44 Chap. 6 General Trees6.16 (a) // Use a helper function with a pass-by-reference// variable to indicate current position in the// node list.template <class Elem>BinNode<Elem>* convert(char* inlist) {int curr = 0;return converthelp(inlist, curr);}// As converthelp processes the node list, curr is// incremented appropriately.template <class Elem>BinNode<Elem>* converthelp(char* inlist,int& curr) {if (inlist[curr] == ’/’) {curr++;return NULL;}BinNode<Elem>* temp = new BinNode(inlist[curr++], NULL, NULL);temp->left = converthelp(inlist, curr);temp->right = converthelp(inlist, curr);return temp;}(b) // Use a helper function with a pass-by-reference // variable to indicate current position in the// node list.template <class Elem>BinNode<Elem>* convert(char* inlist) {int curr = 0;return converthelp(inlist, curr);}// As converthelp processes the node list, curr is// incremented appropriately.template <class Elem>BinNode<Elem>* converthelp(char* inlist,int& curr) {if (inlist[curr] == ’/’) {curr++;return NULL;}BinNode<Elem>* temp =new BinNode<Elem>(inlist[curr++], NULL, NULL);if (inlist[curr] == ’\’’) return temp;45curr++ // Eat the internal node mark.temp->left = converthelp(inlist, curr);temp->right = converthelp(inlist, curr);return temp;}(c) // Use a helper function with a pass-by-reference// variable to indicate current position in the// node list.template <class Elem>GTNode<Elem>* convert(char* inlist) {int curr = 0;return converthelp(inlist, curr);}// As converthelp processes the node list, curr is// incremented appropriately.template <class Elem>GTNode<Elem>* converthelp(char* inlist,int& curr) {if (inlist[curr] == ’)’) {curr++;return NULL;}GTNode<Elem>* temp =new GTNode<Elem>(inlist[curr++]);if (curr == ’)’) {temp->insert_first(NULL);return temp;}temp->insert_first(converthelp(inlist, curr));while (curr != ’)’)temp->insert_next(converthelp(inlist, curr));curr++;return temp;}6.17 The Huffman tree is a full binary tree. To decode, we do not need to know the weights of nodes, only the letter values stored in the leaf nodes. Thus, we can use a coding much like that of Equation 6.2, storing only a bit mark for internal nodes, and a bit mark and letter value for leaf nodes.7Internal Sorting7.1 Base Case: For the list of one element, the double loop is not executed and the list is not processed. Thus, the list of one element remains unaltered and is sorted.Induction Hypothesis: Assume that the list of n elements is sorted correctlyby Insertion Sort.Induction Step: The list of n + 1 elements is processed by first sorting thetop n elements. By the induction hypothesis, this is done correctly. The final pass of the outer for loop will process the last element (call it X). This isdone by the inner for loop, which moves X up the list until a value smallerthan that of X is encountered. At this point, X has been properly insertedinto the sorted list, leaving the entire collection of n + 1 elements correctly sorted. Thus, by the principle of Mathematical Induction, the theorem is correct.7.2 void StackSort(AStack<int>& IN) {AStack<int> Temp1, Temp2;while (!IN.isEmpty()) // Transfer to another stackTemp1.push(IN.pop());IN.push(Temp1.pop()); // Put back one elementwhile (!Temp1.isEmpty()) { // Process rest of elemswhile (IN.top() > Temp1.top()) // Find elem’s placeTemp2.push(IN.pop());IN.push(Temp1.pop()); // Put the element inwhile (!Temp2.isEmpty()) // Put the rest backIN.push(Temp2.pop());}}46477.3 The revised algorithm will work correctly, and its asymptotic complexity will remain Θ(n2). However, it will do about twice as many comparisons, since it will compare adjacent elements within the portion of the list already knownto be sorted. These additional comparisons are unproductive.7.4 While binary search will find the proper place to locate the next element, it will still be necessary to move the intervening elements down one position in the array. This requires the same number of operations as a sequential search. However, it does reduce the number of element/element comparisons, and may be somewhat faster by a constant factor since shifting several elements may be more efficient than an equal number of swap operations.7.5 (a) template <class Elem, class Comp>void selsort(Elem A[], int n) { // Selection Sortfor (int i=0; i<n-1; i++) { // Select i’th recordint lowindex = i; // Remember its indexfor (int j=n-1; j>i; j--) // Find least valueif (Comp::lt(A[j], A[lowindex]))lowindex = j; // Put it in placeif (i != lowindex) // Add check for exerciseswap(A, i, lowindex);}}(b) There is unlikely to be much improvement; more likely the algorithmwill slow down. This is because the time spent checking (n times) isunlikely to save enough swaps to make up.(c) Try it and see!7.6 • Insertion Sort is stable. A swap is done only if the lower element’svalue is LESS.• Bubble Sort is stable. A swap is done only if the lower element’s valueis LESS.• Selection Sort is NOT stable. The new low value is set only if it isactually less than the previous one, but the direction of the search isfrom the bottom of the array. The algorithm will be stable if “less than”in the check becomes “less than or equal to” for selecting the low key position.• Shell Sort is NOT stable. The sublist sorts are done independently, andit is quite possible to swap an element in one sublist ahead of its equalvalue in another sublist. Once they are in the same sublist, they willretain this (incorrect) relationship.• Quick-sort is NOT stable. After selecting the pivot, it is swapped withthe last element. This action can easily put equal records out of place.48 Chap. 7 Internal Sorting• Conceptually (in particular, the linked list version) Mergesort is stable.The array implementations are NOT stable, since, given that the sublistsare stable, the merge operation will pick the element from the lower listbefore the upper list if they are equal. This is easily modified to replace“less than” with “less than or equal to.”• Heapsort is NOT stable. Elements in separate sides of the heap are processed independently, and could easily become out of relative order.• Binsort is stable. Equal values that come later are appended to the list.• Radix Sort is stable. While the processing is from bottom to top, thebins are also filled from bottom to top, preserving relative order.7.7 In the worst case, the stack can store n records. This can be cut to log n in the worst case by putting the larger partition on FIRST, followed by the smaller. Thus, the smaller will be processed first, cutting the size of the next stacked partition by at least half.7.8 Here is how I derived a permutation that will give the desired (worst-case) behavior:a b c 0 d e f g First, put 0 in pivot index (0+7/2),assign labels to the other positionsa b c g d e f 0 First swap0 b c g d e f a End of first partition pass0 b c g 1 e f a Set d = 1, it is in pivot index (1+7/2)0 b c g a e f 1 First swap0 1 c g a e f b End of partition pass0 1 c g 2 e f b Set a = 2, it is in pivot index (2+7/2)0 1 c g b e f 2 First swap0 1 2 g b e f c End of partition pass0 1 2 g b 3 f c Set e = 3, it is in pivot index (3+7/2)0 1 2 g b c f 3 First swap0 1 2 3 b c f g End of partition pass0 1 2 3 b 4 f g Set c = 4, it is in pivot index (4+7/2)0 1 2 3 b g f 4 First swap0 1 2 3 4 g f b End of partition pass0 1 2 3 4 g 5 b Set f = 5, it is in pivot index (5+7/2)0 1 2 3 4 g b 5 First swap0 1 2 3 4 5 b g End of partition pass0 1 2 3 4 5 6 g Set b = 6, it is in pivot index (6+7/2)0 1 2 3 4 5 g 6 First swap0 1 2 3 4 5 6 g End of parition pass0 1 2 3 4 5 6 7 Set g = 7.Plugging the variable assignments into the original permutation yields:492 6 4 0 13 5 77.9 (a) Each call to qsort costs Θ(i log i). Thus, the total cost isni=1i log i = Θ(n2 log n).(b) Each call to qsort costs Θ(n log n) for length(L) = n, so the totalcost is Θ(n2 log n).7.10 All that we need to do is redefine the comparison test to use strcmp. The quicksort algorithm itself need not change. This is the advantage of paramerizing the comparator.7.11 For n = 1000, n2 = 1, 000, 000, n1.5 = 1000 ∗√1000 ≈ 32, 000, andn log n ≈ 10, 000. So, the constant factor for Shellsort can be anything less than about 32 times that of Insertion Sort for Shellsort to be faster. The constant factor for Shellsort can be anything less than about 100 times thatof Insertion Sort for Quicksort to be faster.7.12 (a) The worst case occurs when all of the sublists are of size 1, except for one list of size i − k + 1. If this happens on each call to SPLITk, thenthe total cost of the algorithm will be Θ(n2).(b) In the average case, the lists are split into k sublists of roughly equal length. Thus, the total cost is Θ(n logk n).7.13 (This question comes from Rawlins.) Assume that all nuts and all bolts havea partner. We use two arrays N[1..n] and B[1..n] to represent nuts and bolts. Algorithm 1Using merge-sort to solve this problem.First, split the input into n/2 sub-lists such that each sub-list contains twonuts and two bolts. Then sort each sub-lists. We could well come up with apair of nuts that are both smaller than either of a pair of bolts. In that case,all you can know is something like:N1, N2。
数据结构与算法分析课后习题答案【篇一:《数据结构与算法》课后习题答案】>2.3.2 判断题2.顺序存储的线性表可以按序号随机存取。
(√)4.线性表中的元素可以是各种各样的,但同一线性表中的数据元素具有相同的特性,因此属于同一数据对象。
(√)6.在线性表的链式存储结构中,逻辑上相邻的元素在物理位置上不一定相邻。
(√)8.在线性表的顺序存储结构中,插入和删除时移动元素的个数与该元素的位置有关。
(√)9.线性表的链式存储结构是用一组任意的存储单元来存储线性表中数据元素的。
(√)2.3.3 算法设计题1.设线性表存放在向量a[arrsize]的前elenum个分量中,且递增有序。
试写一算法,将x 插入到线性表的适当位置上,以保持线性表的有序性,并且分析算法的时间复杂度。
【提示】直接用题目中所给定的数据结构(顺序存储的思想是用物理上的相邻表示逻辑上的相邻,不一定将向量和表示线性表长度的变量封装成一个结构体),因为是顺序存储,分配的存储空间是固定大小的,所以首先确定是否还有存储空间,若有,则根据原线性表中元素的有序性,来确定插入元素的插入位置,后面的元素为它让出位置,(也可以从高下标端开始一边比较,一边移位)然后插入x ,最后修改表示表长的变量。
int insert (datatype a[],int *elenum,datatype x) /*设elenum为表的最大下标*/ {if (*elenum==arrsize-1) return 0; /*表已满,无法插入*/else {i=*elenum;while (i=0 a[i]x)/*边找位置边移动*/{a[i+1]=a[i];i--;}a[i+1]=x;/*找到的位置是插入位的下一位*/ (*elenum)++;return 1;/*插入成功*/}}时间复杂度为o(n)。
2.已知一顺序表a,其元素值非递减有序排列,编写一个算法删除顺序表中多余的值相同的元素。
2-2下面的7个算法……..请给出正确性证明public class TestbinarySearch {public static int binarySearch1(int a[], int x, int n) { int left = 0;int right = n - 1;while (left <= right) {int middle = (left + right) / 2;System.out.println("left=" + left + " right" + right + " middle="+ middle);if (x == a[middle])return middle;if (x > a[middle])left = middle;elseright = middle;}return -1;}public static int binarySearch2(int a[], int x, int n) { int left = 0;int right = n - 1;while (left < (right - 1)) {int middle = (left + right) / 2;System.out.println("left=" + left + " right" + right + " middle="+ middle);if (x < a[middle])right = middle;elseright = middle;}if (x == a[left]) {return left;}return -1;}public static int binarySearch3(int a[], int x, int n) { int left = 0;int right = n - 1;int middle = (left + right) / 2;System.out.println("left=" + left + " right" + right + " middle="+ middle);if (x >= a[middle]) {left = middle;} else {right = middle;}}if (x == a[left]) {return left;}return -1;}public static int binarySearch4(int a[], int x, int n) { if (n > 0 && x >= a[0]) {int left = 0;int right = n - 1;while (left < right) {int middle = (left + right) / 2;System.out.println("left=" + left + " right" + right+ " middle=" + middle);if (x < a[middle]) {right = middle - 1;} else {left = middle;}}if (x == a[left]) {return left;}}return -1;}public static int binarySearch5(int a[], int x, int n) { if (n > 0 && x >= a[0]) {int left = 0;int right = n - 1;int middle = (left + right + 1) / 2;System.out.println("left=" + left + " right" + right+ " middle=" + middle);if (x < a[middle]) {right = middle - 1;} else {left = middle;}}if (x == a[left]) {return left;}}return -1;}public static int binarySearch6(int a[], int x, int n) { if (n > 0 && x >= a[0]) {int left = 0;int right = n - 1;while (left < right) {int middle = (left + right) / 2;System.out.println("left=" + left + " right" + right+ " middle=" + middle);if (x < a[middle]) {right = middle - 1;} else {left = middle + 1;}}if (x == a[left]) {return left;}}return -1;}public static int binarySearch7(int a[], int x, int n) { if (n > 0 && x >= a[0]) {int left = 0;int right = n - 1;while (left < right) {int middle = (left + right) / 2;System.out.println("left=" + left + " right" + right+ " middle=" + middle);if (x < a[middle]) {right = middle;} else {left = middle;}}if (x == a[left]) {return left;}}return -1;}/** 测试方法一方法一的while的循环条件出错,因为middle=(left+right)/2 而left=middle或者right=middle* 所以当left+1=right时,left<=righ一直成立所以无法返回x=5这个值* left=0,right=4.middle=2,x>a[middle],left=2;* left=2,right=4.middle=3,x>a[middle],left=3;* left=3,right=4.middle=3,x>a[middle],left=3;* left=3,right=4.middle=3,x>a[middle],left=3;出现死循环*/public static void Test1() {int a[] = new int[5];for (int i = 1; i < 6; i++) {a[i - 1] = i;}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "");}System.out.println("");for (int i = 1; i < a.length+1; i++)System.out.println("在下标为"+ binarySearch1(a, i, a.length) + "的位置");;// 当x=5无法查找到边界问题没有出处理好}/** 方法二和方法一差不多但是由于下条件left<rigth-1使下标的判断题条件出错了left=0 right4 middle=2 left=0* right2 middle=1 在下标为0的位置 left=0 right4 middle=2 left=0 right2 middle=1* 在下标为-1的位置 left=0 right4 middle=2 left=0 right2 middle=1 在下标为-1的位置 left=0* right4 middle=2 left=0 right2 middle=1 在下标为-1的位置*/public static void Test2() {int a[] = new int[5];for (int i = 1; i < 6; i++) {a[i - 1] = i;}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "");}System.out.println("");for (int i = 1; i < a.length + 1; i++)System.out.println("在下标为"+ binarySearch2(a, i, a.length) + "的位置");// 当x=5无法查找到边界问题没有出处理好}/** 方法三也是会出现边界的问题当x5时会出现死循环其它正常 left=0 right4 middle=2 left=0 right2 middle=1* 在下标为0的位置 left=0 right4 middle=2 left=0 right2 middle=1 在下标为1的位置 left=0* right4 middle=2 left=2 right4 middle=3 在下标为2的位置 left=0 right4 middle=2* left=2 right4 middle=3 在下标为3的位置当x=5时会有以下死循环 left=3 right4 middle=3* left=3 right4 middle=3 left=3 right4 middle=3 left=3 right4 middle=3 * left=3 right4 middle=3*/public static void Test3() {int a[] = new int[5];for (int i = 1; i < 6; i++) {a[i - 1] = i;}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "");}System.out.println("");for (int i = 1; i < a.length + 1; i++)System.out.println("在下标为"+ binarySearch3(a, i, a.length) + "的位置");}/** 方法四也是因为下标出现问题所以出现死循环 left=0 right1 middle=0left=0 right1 middle=0* left=0 right1 middle=0 left=0 right1 middle=0 left=0 right1 middle=0 */public static void Test4() {int a[] = new int[5];for (int i = 1; i < 6; i++) {a[i - 1] = i;}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "");}System.out.println("");for (int i = 1; i < a.length + 1; i++)System.out.println("在下标为"+ binarySearch4(a, i, a.length) + "的位置");}/** 方法五是正确的结果如下 left=0 right4 middle=2 left=0 right1 middle=1 在下标为0的位置* left=0 right4 middle=2 left=0 right1 middle=1 在下标为1的位置 left=0 right4* middle=2 left=2 right4 middle=3 在下标为2的位置 left=0 right4 middle=2 left=2* right4 middle=3 left=3 right4 middle=4 在下标为3的位置 left=0 right4 middle=2* left=2 right4 middle=3 left=3 right4 middle=4 在下标为4的位置*/public static void Test5() {int a[] = new int[5];for (int i = 1; i < 6; i++) {a[i - 1] = i;}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "");}System.out.println("");for (int i = 1; i < a.length + 1; i++)System.out.println("在下标为"+ binarySearch5(a, i, a.length) + "的位置");}/** 方法六是错误的由于下标混乱导致除了最后一个元素可以搜索到,其他元素都不可以搜索到结果如下 left=0 right4 middle=2* left=0 right1 middle=0 在下标为-1的位置 left=0 right4 middle=2 left=0 right1* middle=0 在下标为1的位置 left=0 right4 middle=2 left=3 right4 middle=3 在下标为-1的位置* left=0 right4 middle=2 left=3 right4 middle=3 在下标为-1的位置 left=0 right4* middle=2 left=3 right4 middle=3 在下标为4的位置*/public static void Test6() {int a[] = new int[5];for (int i = 1; i < 6; i++) {a[i - 1] = i;}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "");}System.out.println("");for (int i = 1; i < a.length + 1; i++)System.out.println("在下标为"+ binarySearch6(a, i, a.length) + "的位置");}/** 方法七是错误的,原因是左边的边界没有处理好导致x=1时会出现死循环所以算法是错误的结果如下 left=0 right1 middle=0* left=0 right1 middle=0 left=0 right1 middle=0 left=0 right1 middle=0 * left=0 right1 middle=0*/public static void Test7() {int a[] = new int[5];for (int i = 1; i < 6; i++) {a[i - 1] = i;}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "");}System.out.println("");for (int i = 1; i < a.length; i++)System.out.println("在下标为"+ binarySearch7(a, i, a.length) + "的位置");}public static void main(String[] args) {Test1();//死循环Test2();//只能找到左边界Test3();//只能有边界Test4();//死循环Test5();//正确Test6();//只能找到右边界Test7();//左边死循环}}。
第一章测试1.算法就是一组有穷的规则,它们规定了解决某一特定类型问题的一系列运算。
()A:对B:错答案:A2.计算机的资源最重要的是内存和运算资源。
因而,算法的复杂性有时间和空间之分。
()A:对B:错答案:A3.时间复杂度是指算法最坏情况下的运行时间。
()A:对B:错答案:B4.下面关于算法的说法中正确的是。
(1)求解某一问题的算法是唯一的。
(2)算法中每条指令的执行次数是有限的,执行每条指令的时间也是有限的。
(3)算法的每一条指令是清晰无歧义的。
(4)算法可以用某种程序设计语言具体实现,所以算法和程序是等价的。
()A:(2)(3)B:(1)(3)C:(1)(2)D:(2)(4)答案:A5.描述算法的基本方法有。
(1)自然语言(2)流程图(3)伪代码(4)程序设计语言()A:(1)(2)(3)B:(1)(3)(4)C:(1)(2)(3)(4)D:(2)(3)(4)答案:C6.算法分析是()A:将算法用某种程序设计语言恰当地表示出来B:证明算法对所有可能的合法出入都能算出正确的答案C:对算法需要多少计算时间和存储空间作定量分析D:在抽象数据数据集合上执行程序,以确定是否产生错误结果答案:C7.算法是由若干条指令组成的有穷序列,而且满足以下叙述中的性质。
(1)输入:有0个或多个输入(2)输出:至少有一个输出(3)确定性:指令清晰、无歧义(4)有限性:指令执行次数有限,而且执行时间有限()A:(1)(2)(3)B:(1)(2)(4)C:(1)(2)(3)(4)D:(1)(3)(4)答案:C8.下面函数中增长率最低的是()A:n2B:log2nC:nD:2n答案:B9.下面属于算法的特性有( )。
A:有限性:算法中每条指令的执行次数是有限的,执行每条指令的时间也是有限的。
B:输入:有0个或多个外部量作为算法的输入。
C:确定性:组成算法的每条指令是清晰,无歧义的。
D:输出:算法产生至少一个量作为输出。
答案:ABCD10.当m为24,n为60时,使用欧几里得算法求m和n的最大公约数,需要进行()次除法运算。
参考答案2.1要证明))(()(n g n f Θ=当且仅当存在正常数021,,n c c ,使得当0n n ≥时, )()()(21n g c n f n g c ≤≤。
取1,5/321==c c 代入不等式,由左不等式可得n>=21/13,由右不等式可得n>=0。
所以取13/210=n 。
所以当0n n ≥,1,5/321==c c 时,))(()(n g n f Θ=2.2))(),(max())()((21))(),(max(2)()())(),(max()())(),(max()(n g n f n g n f n g n f n g n f n g n f n g n g n f n f ≤+≤+≤≤即所以有,因为又因为)()())(),(max(n g n f n g n f 或者= )()())(),(max(n g n f n g n f +≤所以可得取对于,1,21,210==≥c c n n ))()(())(),(max())()((21n g n f c n g n f n g n f c +≤≤+ 故所证成立。
2.3要证 )()(n a n b b θ=+ 当且仅当存在正常数c c a 210,,,使得n c a n n c b b b 21)(≤≤+ 先设c 1=)21(b ,则由)()(1a n n c b b +≤)()21(a n n b b+≤⇔ a n a n a n n 22121−≥⇔≤−⇔+≤⇔ 又因为0≥n ,所以可取|2|0a n =,使得对于任何的a ,当0n n ≥时,有 )()21(a n n b b +≤而当|2|0a n =时,)2(|)|()(n n a n a n b b b +++≤≤n n n b b b b2)2()23(=≤≤所以 c 2可取2b 所以当|2|,,0212)21(a n c c b b===时,可证)()(n a n bb θ=+//根据极限来证也可以。
算法分析与设计习题第一章算法引论一、填空题:1、算法运行所需要的计算机资源的量,称为算法复杂性,主要包括时间复杂度和空间复杂度。
2、多项式10()m m A n a n a n a =+++的上界为O(n m )。
3、算法的基本特征:输入、输出、确定性、有限性。
4、如何从两个方面评价一个算法的优劣:时间复杂度、空间复杂度。
5、计算下面算法的时间复杂度记为: O(n 3) 。
for(i=1;i<=n;i++)for(j=1;j<=n;j++){c[i][j]=0;for(k=1;k<=n;k++)c[i][j]= c[i][j]+a[i][k]*b[k][j];}6、描述算法常用的方法:自然语言、伪代码、程序设计语言、流程图、盒图、PAD 图。
7、算法设计的基本要求:正确性 和 可读性。
8、计算下面算法的时间复杂度记为: O(n 2) 。
for (i =1;i<n; i++){ y=y+1;for (j =0;j <=2n ;j++ )x ++;}9、计算机求解问题的步骤:问题分析、数学模型建立、算法设计与选择、算法表示、算法分析、算法实现、程序调试、结果整理文档编制。
10、算法是指解决问题的 方法或过程 。
二、简答题:1、按照时间复杂度从低到高排列:O( 4n 2)、O( logn)、O( 3n )、O( 20n)、O( 2)、O( n 2/3),O( n!)应该排在哪一位?答:O( 2),O( logn),O( n 2/3),O( 20n),O( 4n 2),O( 3n ),O( n!)2、什么是算法?算法的特征有哪些?答:1)算法:指在解决问题时,按照某种机械步骤一定可以得到问题结果的处理过程。
通俗讲,算法:就是解决问题的方法或过程。
2)特征:1)算法有零个或多个输入;2)算法有一个或多个输出; 3)确定性 ; 4)有穷性3、给出算法的定义?何谓算法的复杂性?计算下例在最坏情况下的时间复杂性?for(j=1;j<=n;j++) (1)for(i=1;i<=n;i++) (2) {c[i][j]=0; (3)for(k=1;k<=n;k++) (4)c[i][j]= c[i][j]+a[i][k]*b[k][j]; } (5)答:1)定义:指在解决问题时,按照某种机械步骤一定可以得到问题结果的处理过程。
算法设计与分析基础课后练习答案习题1.14.设计一个计算的算法,n是任意正整数。
除了赋值和比较运算,该算法只能用到基本的四则运算操作。
算法求//输入:一个正整数n 2//输出:。
step1:a=1;step2:若a*a<n 转step 3,否则输出a;step3:a=a+1转step 2;5. a.用欧几里德算法求gcd(31415,14142)。
b. 用欧几里德算法求gcd(31415,14142),比检查min{m,n}和gcd(m,n)间连续整数的算法快多少倍?请估算一下。
a. gcd(31415, 14142) = gcd(14142, 3131) = gcd(3131, 1618) =gcd(1618, 1513) = gcd(1513, 105) = gcd(1513, 105) = gcd(105, 43) =gcd(43, 19) = gcd(19, 5) = gcd(5, 4) = gcd(4, 1) = gcd(1,0) = 1.b.有a可知计算gcd(31415,14142)欧几里德算法做了11次除法。
连续整数检测算法在14142每次迭代过程中或者做了一次除法,或者两次除法,因此这个算法做除法的次数鉴于1·14142 和2·14142之间,所以欧几里德算法比此算法快1·14142/11 ≈1300 与2·14142/11 ≈2600 倍之间。
6.证明等式gcd(m,n)=gcd(n,m mod n)对每一对正整数m,n都成立.Hint:根据除法的定义不难证明:●如果d整除u和v, 那么d一定能整除u±v;●如果d整除u,那么d也能够整除u的任何整数倍ku.对于任意一对正整数m,n,若d能整除m和n,那么d一定能整除n和r=m mod n=m-qn;显然,若d能整除n和r,也一定能整除m=r+qn和n。
数对(m,n)和(n,r)具有相同的公约数的有限非空集,其中也包括了最大公约数。
习题11.图论诞生于七桥问题。
出生于瑞士的伟大数学家欧拉(Leonhard Euler ,1707—1783)提出并解决了该问题。
七桥问题是这样描述的:一个人是否能在一次步行中穿越哥尼斯堡(现在叫加里宁格勒,在波罗的海南岸)城中全部的七座桥后回到起点,且每座桥只经过一次,图1.7是这条河以及河上的两个岛和七座桥的草图。
请将该问题的数据模型抽象出来,并判断此问题是否有解。
七桥问题属于一笔画问题。
输入:一个起点 输出:相同的点 1, 一次步行2, 经过七座桥,且每次只经历过一次 3, 回到起点该问题无解:能一笔画的图形只有两类:一类是所有的点都是偶点。
另一类是只有二个奇点的图形。
2.在欧几里德提出的欧几里德算法中(即最初的欧几里德算法)用的不是除法而是减法。
请用伪代码描述这个版本的欧几里德算法 1.r=m-n2.循环直到r=0 2.1 m=n 2.2 n=r 2.3 r=m-n 3 输出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]中一个既不是最大也不是最小的元素,并说明最坏情况下的比较次数。
习题11. 图论诞生于七桥问题。
出生于瑞士的伟大数学家欧拉(LeonhardEuler ,1707—1783)提出并解决了该问题。
七桥问题是这样描述的:一个人是否能在一次步行中穿越哥尼斯堡(现在叫加里宁格勒,在波罗的海南岸)城中全部的七座桥后回到起点,且每座桥只经过一次,图是这条河以及河上的两个岛和七座桥的草图。
请将该问题的数据模型抽象出来,并判断此问题是否有解。
七桥问题属于一笔画问题。
输入:一个起点输出:相同的点1, 一次步行2, 经过七座桥,且每次只经历过一次3, 回到起点该问题无解:能一笔画的图形只有两类:一类是所有的点都是偶点。
另一类是只有二个奇点的图形。
图 七桥问题南2.在欧几里德提出的欧几里德算法中(即最初的欧几里德算法)用的不是除法而是减法。
请用伪代码描述这个版本的欧几里德算法=m-n2.循环直到r=0m=nn=rr=m-n3 输出m3.设计算法求数组中相差最小的两个元素(称为最接近数)的差。
要求分别给出伪代码和C++描述。
编写程序,求n至少为多大时,n个“1”组成的整数能被2013整除。
#include<iostream>using namespace std;int main(){double value=0;for(int n=1;n<=10000 ;++n){value=value*10+1;if(value%2013==0){cout<<"n至少为:"<<n<<endl;break;}}计算π值的问题能精确求解吗编写程序,求解满足给定精度要求的π值#include <iostream>using namespace std;int main (){double a,b;double arctan(double x);圣经上说:神6天创造天地万有,第7日安歇。
为什么是6天呢任何一个自然数的因数中都有1和它本身,所有小于它本身的因数称为这个数的真因数,如果一个自然数的真因数之和等于它本身,这个自然数称为完美数。
智慧树知到《算法分析与设计》章节测试答案第一章1、给定一个实例,如果一个算法能得到正确解答,称这个算法解答了该问题。
A:对B:错答案: 错2、一个问题的同一实例可以有不同的表示形式A:对B:错答案: 对3、同一数学模型使用不同的数据结构会有不同的算法,有效性有很大差别。
A:对B:错答案: 对4、问题的两个要素是输入和实例。
A:对B:错答案: 错5、算法与程序的区别是()A:输入B:输出C:确定性D:有穷性答案: 有穷性6、解决问题的基本步骤是()。
(1)算法设计(2)算法实现(3)数学建模(4)算法分析(5)正确性证明A:(3)(1)(4)(5)(2)B:(3)(4)(1)(5)(2)C:(3)(1)(5)(4)(2)D:(1)(2)(3)(4)(5)答案: (3)(1)(5)(4)(2)7、下面说法关于算法与问题的说法错误的是()。
A:如果一个算法能应用于问题的任意实例,并保证得到正确解答,称这个算法解答了该问题。
B:算法是一种计算方法,对问题的每个实例计算都能得到正确答案。
C:同一问题可能有几种不同的算法,解题思路和解题速度也会显著不同。
D:证明算法不正确,需要证明对任意实例算法都不能正确处理。
答案: 证明算法不正确,需要证明对任意实例算法都不能正确处理。
8、下面关于程序和算法的说法正确的是()。
A:算法的每一步骤必须要有确切的含义,必须是清楚的、无二义的。
B:程序是算法用某种程序设计语言的具体实现。
C:程序总是在有穷步的运算后终止。
D:算法是一个过程,计算机每次求解是针对问题的一个实例求解。
答案: 算法的每一步骤必须要有确切的含义,必须是清楚的、无二义的。
,程序是算法用某种程序设计语言的具体实现。
,算法是一个过程,计算机每次求解是针对问题的一个实例求解。
9、最大独立集问题和()问题等价。
A: 最大团B:最小顶点覆盖C:区间调度问题D:稳定匹配问题答案:最大团,最小顶点覆盖10、给定两张喜欢列表,稳定匹配问题的输出是()。
第一章测试1.算法的重要特性( )。
A:能行性B:输出C:有穷性D:确定性E:输入答案:ABCDE2.语句 return sum(x,y);执行频度为1 ( )A:对B:错答案:B3.的上界函数是 ( )A:对B:错答案:A4.算法时间复杂度为O(1)说明算法执行时间是单位时间( )A:对B:错答案:B5.集合的位向量表示法,合并集合操作的时间复杂度为( )A:B:C:D:答案:A6.带加权规则的Union算法中,Parent(1)=-8,Parent(2)=-4,1、2代表的集合合并后,集合的根是1,Parent(1)=-12,Parent(2)=1( )A:对B:错答案:A7.写一个算法交换两个变量x、y的值不使用第三个变量。
答案:8.求下列函数的渐进表达式:; ; ;答案:9.的渐进表达式=____答案:10.按照渐进阶从低到高的顺序排列以下表达式:,,, ,,,。
答案:第二章测试1.递归程序每一次递归执行的语句都完全相同( )A:对B:错答案:B2.对数组ary[0:n-1]求和,采用如下递归方式:arysum(n)=ary[n-1]+arysum(n-1),递归方式是( )A:线性递归B:非线性递归答案:A3.问题规模为的全排列问题,可以看作个规模为的全排列问题,因此时间复杂度为: ( )A:错B:对答案:B4.递归程序简洁明了,因此比非递归程序执行效率高( )A:错B:对答案:A5.Master Method适应于求解形式如T(n)=aT(n/b)+f(n)的递归关系式。
其中,a表示子问题个数, n/b子问题规模,f(n)表示划分子问题或整合子问题解的时间。
( )A:对B:错答案:A6.递归关系式:F(n)=F(n-1)+F(n-2)+1是二阶齐次常系数线性递归式。
( )A:错B:对答案:A7.解形式为( )(p均为待定系数):A:B:C:D:答案:C8.求解非线性变系数递归关系式一个原则是“变换”,经过变换将其转换为线性常系数等常规可求的递归式。