USC算法导论作业2
算法导论02

Algorithm : Design & Analysis [2]
In the last class…
Goal of the Course Algorithm: the Concept Algorithm Analysis: the Contents Average and Worst-Case Analysis Lower Bounds and the Complexity of Problems
Searching a Sequence
For a given K, there are two possibilities
K in E (say, with probability q), then K may be any one of E[i] (say, with equal probability, that is 1/n) K not in E (with probability 1-q), then K may be located in any one of gap(i) (say, with equal probability, that is 1/(n+1))
Input:
an array E containing n entries of numeric type sorted in non-decreasing order a value K
Output:
index for which K=E[index], if K is in E, or, -1, if K is not in E
Asymptotic Behavior
Asymptotic growth rate The Sets Ο, Ω and Θ Complexity Class An Example: Maximum Subsequence Sum
算法导论 答案 (2)

算法导论答案算法导论概述《算法导论》是一本经典的计算机科学教材,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein合著。
这本书详细介绍了算法的设计、分析和实现,并涵盖了算法导论领域的许多重要概念和技术。
本文将为你提供一些关于《算法导论》中一些常见问题的答案。
1. 什么是算法?算法是一系列明确定义的步骤,用于解决特定问题或完成特定任务。
它可以是一个计算过程、一个程序或一个有限的操作序列。
算法通常用于计算和数据处理领域,是计算机科学的核心概念。
2. 为什么学习算法很重要?学习算法的重要性体现在以下几个方面:•提高问题解决能力:算法是解决问题的有效工具。
学习算法可以帮助我们思考和理解问题,并设计出相应的解决方案。
•优化计算性能:算法的设计和分析可以帮助我们提高计算的效率和性能。
合适的算法可以在短时间内处理大规模数据集和复杂计算任务。
•促进技术创新:算法是许多技术和应用的基石,包括人工智能、机器学习、数据挖掘等。
学习算法可以为我们打开更多的研究和创新机会。
3. 《算法导论》提供了哪些内容?《算法导论》这本书详细介绍了算法的基本概念和设计技巧,并提供了许多典型算法的实现和分析。
以下是该书的一些主要内容:•算法分析:对算法进行时间复杂度和空间复杂度的理论分析,帮助我们评估算法的效率和性能。
•排序和查找算法:介绍了各种排序算法(如插入排序、归并排序、快速排序)和查找算法(如二分查找、哈希表)。
•图算法:讨论了图的表示方法和图搜索算法(如深度优先搜索、广度优先搜索)以及最短路径算法(如Dijkstra算法)等。
•动态规划和贪心算法:介绍了动态规划和贪心算法的原理和应用,用于解决具有最优子结构性质的问题。
•分治算法和递归思想:讲解了分治算法的基本原理,并提供了许多使用递归思想解决问题的例子。
•NP完全问题:探讨了NP完全问题的性质和求解方法,引导了读者进入计算复杂性理论的领域。
USC算法导论作业2

Problem 1 (4.3-2): The recurrence T (n) = 7T (n/2) + n2 describes the running time of an algorithm A. A competing algorithm A has a running time of T (n) = aT (n/4) + n2 . What is the largest integer value for a such that A is asymptotically faster than A? Solution 1: By the master theorem, the worst-case asymptotic complexity of A is Θ(nlg 7 ) ≈ Θ(n2.80735 ), and the worst-case asymptotic complexity of A is Θ(nlog4 a ), if a > 16. For A to be asymptotically faster than A, log4 a < lg 7 = log4 49. Therefore, the largest integer value for a such that A is asymptotically faster than A is 48. Problem 2 (Derived from 4-1 and 4-4): Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Make your bounds as tight as possible, and justify your answers. a. T (n) = 2T (n/2) + n3 . b. T (n) = 16T (n/4) + n2 . c. T (n) = 7T (n/3) + n2 . d. T (n) = 7T (n/2) + n2 . √ e. T (n) = 2T (n/4) + n. f. T (n) = 3T (n/2) + n lg n. √ g. T (n) = 4T (n/2) + n2 n. h. T (n) = T (9n/10) + n. Solution 2: Use the master method to solve these recurrences. a. Case 3 of master theorem. T (n) = Θ(n3 ). b. Case 2 of master theorem. T (n) = Θ(n2 lg n). c. Case 3 of master theorem. T (n) = Θ(n2 ). d. Case 1 of master theorem. T (n) = Θ(nlg 7 ). √ e. Case 2 of master theorem. T (n) = Θ( n lg n). f. Case 1 of master theorem. T (n) = Θ(nlg 3 ). √ g. Case 3 of master theorem. T (n) = Θ(n2 n). h. Case 3 of master theorem. T (n) = Θ(n). Problem 3 (Derived from 4-1 and 4-4): Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Make your bounds as tight as possible, and justify your answers. You may assume that T (1) is a constant. a. T (n) = T (n − 1) + n.
《算法导论》习题答案

Chapter2 Getting Start2.1 Insertion sort2.1.2 将Insertion-Sort 重写为按非递减顺序排序2.1.3 计算两个n 位的二进制数组之和2.2 Analyzing algorithms2.2.1将函数32/10001001003n n n --+用符号Θ表示2.2.2写出选择排序算法selection-sort当前n-1个元素排好序后,第n 个元素已经是最大的元素了.最好时间和最坏时间均为2()n Θ2.3 Designing algorithms2.3.3 计算递归方程的解22()2(/2)2,1k if n T n T n n if n for k =⎧=⎨+ = >⎩ (1) 当1k =时,2n =,显然有()lg T n n n =(2) 假设当k i =时公式成立,即()lg 2lg22i i i T n n n i ===⋅,则当1k i =+,即12i n +=时,111111()(2)2(2)222(1)22lg(2)lg i i i i i i i i T n T T i i n n ++++++==+=⋅+=+== ()lg T n n n ∴ =2.3.4 给出insertion sort 的递归版本的递归式(1)1()(1)()1if n T n T n n if n Θ =⎧=⎨-+Θ >⎩2.3-6 使用二分查找来替代insertion-sort 中while 循环内的线性扫描,是否可以将算法的时间提高到(lg )n n Θ?虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2()n Θ2.3-7 给出一个算法,使得其能在(lg )n n Θ的时间内找出在一个n 元素的整数数组内,是否存在两个元素之和为x首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找: Search(A,n,x)QuickSort(A,n);i←1; j←n;while A[i]+A[j]≠x and i<jif A[i]+A[j]<xi←i+1elsej←j -1if A[i]+A[j]=xreturn trueelsereturn false时间复杂度为)(n Θ。
《算法导论(第二版)》(中文版)课后答案

5
《算法导论(第二版) 》参考答案 do z←y 调用之前保存结果 y←INTERVAL-SEARCH-SUBTREE(y, i) 如果循环是由于y没有左子树,那我们返回y 否则我们返回z,这时意味着没有在z的左子树找到重叠区间 7 if y≠ nil[T] and i overlap int[y] 8 then return y 9 else return z 5 6 15.1-5 由 FASTEST-WAY 算法知:
15
lg n
2 lg n1 1 2cn 2 cn (n 2 ) 2 1
4.3-1 a) n2 b) n2lgn c) n3 4.3-4
2
《算法导论(第二版) 》参考答案 n2lg2n 7.1-2 (1)使用 P146 的 PARTION 函数可以得到 q=r 注意每循环一次 i 加 1,i 的初始值为 p 1 ,循环总共运行 (r 1) p 1次,最 终返回的 i 1 p 1 (r 1) p 1 1 r (2)由题目要求 q=(p+r)/2 可知,PARTITION 函数中的 i,j 变量应该在循环中同 时变化。 Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; 7.3-2 (1)由 QuickSort 算法最坏情况分析得知:n 个元素每次都划 n-1 和 1 个,因 为是 p<r 的时候才调用,所以为Θ (n) (2)最好情况是每次都在最中间的位置分,所以递推式是: N(n)= 1+ 2*N(n/2) 不难得到:N(n) =Θ (n) 7.4-2 T(n)=2*T(n/2)+ Θ (n) 可以得到 T(n) =Θ (n lgn) 由 P46 Theorem3.1 可得:Ω (n lgn)
中科大算法导论第一,二次和第四次作业答案

2.2-3 再次考虑线性查找问题 (见练习2.1-3)。在平均情况 下,需要检查输入序列中的多 少个元素?假定待查找的元素 是数组中任何一个元素的可能 性是相等的。在最坏情况下有 怎样呢?用Θ形式表示的话,线 性查找的平均情况和最坏情况 运行时间怎样?对你的答案加 以说明。 • 线性查找问题 • 输入:一列数A=<a1,a2,…,an>和一 个值v。 • 输出:下标i,使得v=A[i],或者当 v不在A中出现时为NIL。 • 平均情况下需要查找 (1+2+…+n)/n=(n+1)/2 • 最坏情况下即最后一个元素为待 查找元素,需要查找n个。 • 故平均情况和最坏情况的运行时 间都为Θ(n)。
• 2.3-2改写MERGE过程,使之不使 用哨兵元素,而是在一旦数组L或R 中的所有元素都被复制回数组A后, 就立即停止,再将另一个数组中 余下的元素复制回数组A中。 • MERGE(A,p,q,r) 1. n1←q-p+1 2. n2 ←r-q 3. create arrays L[1..n1] and R[1..n2] 4. for i ←1 to n1 5. do L*i+ ←A*p+i-1] 6. for j ←1 to n2 7. do R*j+ ←A*q+j+ 8. i ←1 9. j ←1
10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
k ←p while((i<=n1) and (j<=n2)) do if L[i]<=R[j] do A[k]=L[i] i++ else do A[k]=R[j] j++ k++ while(i<=n1) do A[k++]=L[i++] while(j<=n2) do A[k++]=R[j++]
USC算法导论作业3

CSCI303Homework3Problem1(9-1):Given a set A of n numbers,we wish tofind the k largest in sorted order using a comparison-based algorithm.Find the algorithm that implements each of the following methods with the best asymptotic worst-case running time,and analyze the running time of the algorithms in terms of n and k.a.Sort the numbers,and list the k largest.b.Build a max-priority queue from the numbers,and call Extract-Max k times.e an order-statistic algorithm tofind the i th largest number,partition around that num-ber,and sort the k largest numbers.Solution1:a.Merge-Sort(A,1,n)return A[n−k...k]This algorithm takes only as long as it takes to Merge-Sort a list of n numbers,so its running time isΘ(n lg n).b.Build-Max-Heap(A)for i←1to kB[i]←Heap-Extract-Max(A)return BThis algorithmfirst calls Build-Max-Heap on A,which has worst-case asymptotic com-plexity O(n).Then it calls Heap-Extract-Max k times,each of which has worst-case asymptotic complexity O(lg n).So the worst-case asymptotic complexity for this algorithm is O(n+k lg n).c.i←Select(A,k)A[n]↔A[i]Partition(A)Merge-Sort(A,n−k,n)return A[n−k...k]This algorithmfirst calls Select,which has worst-case asymptotic complexity O(n), then calls Partition,which also has worst-case asymptotic complexity O(n),then calls Merge-Sort to sort just the last k elements,which has worst-case asymptotic complexity O(k lg k).So the worst-case asymptotic complexity for this algorithm is O(n+k lg k).Problem2(9.3-5):Suppose that you have a“black-box”worst-case linear-time median subroutine.Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic.Solution2:Simple-Select(A,p,r,i)if p=rreturn A[p]A[r]↔A[Index-Of-Median(A,p,r)] Partition(A,p,r)k← r−p+12if i=kreturn A[k]else if i<kreturn Simple-Select(A,1,k−1,i)elsereturn Simple-Select(A,k+1,r,i−k)Problem3(9.3-8):Let X[1,...,n]and Y[1,...,n]be two arrays,each containing n numbers already in sorted order. Give an O(lg n)-time algorithm tofind the median of all2n elements in arrays X and Y. Solution3:Two-List-Median(X,p,q,Y,s,t)mx←Index-Of-Median(X,p,q)my←Index-Of-Median(Y,s,t)X[mx]↔X[q]Y[my]↔Y[t]Partition(X,p,q)Partition(Y,s,t)if X[mx]=Y[my]return X[mx]else if X[mx]>Y[my]return Two-List-Median(X,p,mx−1,Y,my+1,t)elsereturn Two-List-Median(X,mx+1,q,Y,s,my−1)Problem4(8.1-1):What is the smallest possible depth of a leaf in a decision tree for a comparison sort?Solution4:Given an array A that contains n elements,the smallest possible depth of a leaf in a decision tree to sort A using a comparison-type sort is n−1.To verify that A is in sorted order takes n−1 comparisons,and if fewer comparisons are used then at least one element was not compared to any of the others,so that element might not be in the correct position.Problem 5(Derived from 8.1-4):You are given a sequence of n elements to sort and a number k such that k divides n .The input sequence consists of n/k subsequences,each containing k elements.The elements in a given subsequence are all smaller than the elements in the succeeding subsequence and larger than the elements in the preceding subsequence.Thus,all that is needed to sort the whole sequence of length n is to sort the k elements in each of the n/k subsequences.Show an Ω(n lg k )lower bound on the number of comparisons needed to solve this varient of the sorting problem using a comparison-type sorting algorithm.(Hint:It is not rigorous to simply combine the lower bounds for the individual subsequences.)Solution 5:We will construct a decision tree for this varient of the sorting problem and show that it has height at least n lg k .Each leaf of the decision tree corresponds to a permutation of the original sequence.How many permutations are there?Each subsequence has k !permutations,and there are n/k subsequences,so there are (k !)n/k permutations of the whole sequence.Thus the decision tree has (k !)n/k leaves.A binary tree with (k !)n/k leaves has height at least lg (k !)n/k .lg (k !)n/k =n/k lg(k !)=Θ(n/k ·k lg k )=Θ(n lg k )Therefore the lower bound on the number of comparisons needed to solve this varient of the sorting problem using a comparison-type sorting algorithm is Ω(n lg k ).。
USC算法导论作业

CSCI 303Homework 1Problem 1(2.1-1):Using Figure 2.2as a model,illustrate the operation of Insertion-Sort on the array A = 31,41,59,26,41,58 .Solution 1:265841594131Problem 2(2.2-1):Express the function n 3/1000−100n 2−100n +3in terms of Θ-notation.Solution 2:n 3/1000−100n 2−100n +3=Θ(n 3)Problem 3(Derived from 2.2-2):Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A [1].Then find the second smallest element of A ,and exchange it with A [2].Continue in this manner for the first n −1elements of A .Write pseudocode for this algorithm,which is known as Selection-Sort .Give the worst-case running times of selection sort in Θ-notation.Solution 3:Selection-Sort (A )for i ←1to length [A ]do min-value ←A [i ]min-index =ifor j =i +1to length [A ]do if A [j ]≤min-valuemin-value =A [j ]min-index =jA [i ]↔A [min-index ]The worst-case running time of Selection-Sort is Θ(n 2).Using Figure2.4as a model,illustrate the operation of merge sort on the array A= 3,41,52,26,38,57,9,49 . Solution4:Sorted SequenceInitial SequenceProblem5(Derived from1.2-2):Suppose we are comparing two sorting algorithms.Suppose that for all inputs of size n,thefirst algorithm runs in8n2seconds,while the second algorithm runs in64n lg n seconds.For whichvalues of n does thefirst algorithm beat the second algorithm?Solution5:Thefirst algorithm beats the second algorithm if8n2<64n lg n.For this to happen,n<8lg n.This is true for2≤n≤43.Problem6(1.2-3):What is the smallest value of n such that an algorithm whose running time is100n2runs fasterthan an algorithm whose running time is2n?Solution6:n=15Assume that a new Intel processor can execute1015operations per second.You have two algorithms that test whether a number is prime or not.Thefirst algorithm uses100n2operations for a number with n decimal digits.The second uses2n operations for a number with n decimal ing thefirst algorithm,how many seconds would the Intel processor take to determine whether a1000 decimal digit number is prime?Using the second algorithm,how many seconds would the Intel processor take to determine whether a1000decimal digit number is prime?Solution7:Thefirst algorithm would take10−7seconds(100nanoseconds).The second algorithm would take 10286seconds.For comparison,the universe is about4.33×1017seconds old,so it would take approximately2.5×10268times the age of the universe to solve the problem using the second algorithm.Problem8(1-1Comparison of running times):For each function f(n)and time t in the following table,determine the largest size n of a prob-lem that can be solved in time t,assuming that the algorithm to solve the problem takes f(n) microseconds.1second1minute1hour1day1month1year1centurylg n√nnn lg nn2n32nn!Solution8:1second1minute1hour1day1month1year1centurylg n210626×10723.6×10928.64×101022.63×101223.16×101323.16×1015√n10123.6×10151.3×10197.46×10216.92×10249.96×10249.96×1030 n1066×1073.6×1098.64×10102.63×10123.16×10133.16×1015 n lg n6274628014171.33×1082.76×1097.29×10107.98×10116.87×1013 n210007745600002939381621643561753856175382 n3100391153244201380231600146677 2n19253136414451n!9111213151617Problem9(Derived from3.1-2):Show that for all numbers a and b,(n+a)b=Θ(n b). Solution9:(n+a)b=bk=0bkn b−k a k(The binomial formula)=n b+bn b−1k+···+bnk b−1+k b=Θ(n b)Problem10(3.1-4):Is2n+1=O(2n)?Is22n=O(2n)?Solution10:2n+1=2×2n so2n+1=O(2n)22n=2n×2n so22n=O(2n)Problem11(Not in book):In the table below,write O if f(n)=O(g(n)),Ωif f(n)=Ω(g(n)),andΘif f(n)=Θ(g(n)).g(n)↓f(n)→5n+33.14×1088n2lg4n+n32n5n3+4n2lg n+3n12n+lg8n 5n+33.14×1088n2lg4n+n32n5n3+4n2lg n+3n12n+lg8nSolution11:g(n)↓f(n)→5n+33.14×1088n2lg4n+n32n5n3+4n2lg n+3n12n+lg8n 5n+3ΘOΩΩΩΘ3.14×108ΩΘΩΩΩΩ8n2lg4n+n3O OΘΩΩO 2n O O OΘO O5n3+4n2lg n+3n O O OΩΘO 12n+lg8nΘOΩΩΩΘ。
算法导论参考答案

算法导论参考答案算法导论参考答案算法导论是计算机科学领域中一本经典的教材,被广泛应用于计算机科学和工程的教学和研究中。
它涵盖了算法设计和分析的基本概念,以及各种常见算法的实现和应用。
本文将为读者提供一些算法导论中常见问题的参考答案,以帮助读者更好地理解和掌握这门课程。
1. 什么是算法?算法是一系列解决问题的步骤和规则。
它描述了如何将输入转换为输出,并在有限的时间内完成。
算法应具备正确性、可读性、健壮性和高效性等特点。
2. 如何分析算法的效率?算法的效率可以通过时间复杂度和空间复杂度来衡量。
时间复杂度表示算法执行所需的时间量级,常用的时间复杂度有O(1)、O(n)、O(logn)、O(nlogn)和O(n^2)等。
空间复杂度表示算法执行所需的额外空间量级,通常以字节为单位。
3. 什么是渐进符号?渐进符号用于表示算法的时间复杂度或空间复杂度的增长趋势。
常见的渐进符号有大O符号、Ω符号和Θ符号。
大O符号表示算法的上界,Ω符号表示算法的下界,Θ符号表示算法的平均情况。
4. 什么是分治法?分治法是一种算法设计策略,将问题分解为若干个子问题,并对子问题进行独立求解,最后将子问题的解合并得到原问题的解。
典型的分治算法有归并排序和快速排序。
5. 什么是动态规划?动态规划是一种通过将问题分解为相互重叠的子问题来求解的方法。
它通常用于求解具有重叠子问题和最优子结构性质的问题。
典型的动态规划算法有背包问题和最短路径问题。
6. 什么是贪心算法?贪心算法是一种通过每一步选择局部最优解来求解整体最优解的方法。
贪心算法通常不能保证得到全局最优解,但在某些问题上能够得到近似最优解。
典型的贪心算法有霍夫曼编码和最小生成树算法。
7. 什么是图算法?图算法是一类用于解决图结构相关问题的算法。
图由节点和边组成,节点表示对象,边表示对象之间的关系。
图算法包括图的遍历、最短路径、最小生成树和网络流等问题的求解。
8. 什么是NP完全问题?NP完全问题是一类在多项式时间内无法求解的问题。
算法导论(第二版)习题答案(英文版)

Last update: December 9, 2002
1.2 − 2 Insertion sort beats merge sort when 8n2 < 64n lg n, ⇒ n < 8 lg n, ⇒ 2n/8 < n. This is true for 2 n 43 (found by using a calculator). Rewrite merge sort to use insertion sort for input of size 43 or less in order to improve the running time. 1−1 We assume that all months are 30 days and all years are 365.
n
Θ
i=1
i
= Θ(n2 )
This holds for both the best- and worst-case running time. 2.2 − 3 Given that each element is equally likely to be the one searched for and the element searched for is present in the array, a linear search will on the average have to search through half the elements. This is because half the time the wanted element will be in the first half and half the time it will be in the second half. Both the worst-case and average-case of L INEAR -S EARCH is Θ(n). 3
算法导论参考答案

第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。
给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。
插入排序算法伪代码INSERTION-SORT(A)1 for j ←2 to length[A]2 do key ←A[j]3 Insert A[j] into the sorted sequence A[1..j-1]4 i ←j-15 while i > 0 and A[i] > key6 do A[i+1]←A[i]7 i ←i − 18 A[i+1]←keyC#对揑入排序算法的实现:public static void InsertionSort<T>(T[] Input) where T:IComparable<T>{T key;int i;for (int j = 1; j < Input.Length; j++){key = Input[j];i = j - 1;for (; i >= 0 && Input[i].CompareTo(key)>0;i-- )Input[i + 1] = Input[i];Input[i+1]=key;}}揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j]这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1.如果按照大部分计算机编程语言的思路,修改为:INSERTION-SORT(A)1 for j ← 1 to length[A]2 do key ←A[j]3 i ←j-14 while i ≥ 0 and A[i] > key5 do A[i+1]←A[i]6 i ←i − 17 A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。
算法导论作业2答案

Introduction to Algorithm s Day 10 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Handout12Exercise2-1.Do Exercise5.3-1on page104of CLRS.Solution:R ANDOMIZE-I N-P LACEExchangefor todo ExchangeFor our base case,we have initialized to2.Therefore we must show that for each possible1-permutation,the subarray A[1]contains this1-permutation with probability. Clearly this is the case,as each element has a chance of of being in thefirst position.Exercise2-2.Do Exercise6.1-2on page129of CLRS.Solution:By definition,a element heap has a height of.Therefore where is and our base case is correct.Now we use induction,and assume that all trees with nodes or fewer has a height of. Next we consider a tree with nodes.Looking at the node,we know its height is one greater than its parent(and since we’re not in the base case,all nodes have a parent).The parent of the th node in the tree is also the th node in the tree.Therefore its height is.Then the th node in the tree has a height of. Therefore by induction we have shown that the height of an node tree is.Exercise2-3.Do Exercise6.4-3on page136of CLRS.Solution:The running time of H EAPSORT on an array of length that is already sorted in increasing order is because even though it is already sorted,it will be transformed back into a heap and sorted.The running time of H EAPSORT on an array of length that is sorted in decreasing order will be.This occurs because even though the heap will be built in linear time,every time the element is removed and the H EAPIFY is called it will cover the full height of the tree. Exercise2-4.Do Exercise7.2-2on page153of CLRS.Solution:The running time will be because every time partition is called,all of the elements will be put into the subarray of elements smaller than the partition.The recurrence will bewhich is clearlyExercise2-5.Do Problem7-3on page161of CLRS.Solution:(a)This sort is intuitively correct because the largest rd of the elements will eventually be sorted amung their peers.If they are in thefirst third of the array to begin with,they will be sorted into the middle third.If they are in the middle or last third,then they will obviously be sorted into their proper position.Similarly any element which belongs in the each of the thirds will be sorted into position by the three sub-sorts.(b)Which solves to(c)S TOOGE S ORT is slower than all of the other algorithms we have studied.I NSERTION, M ERGE S ORT,H EAPSORT,and Q UICKSORT.Therefore all other sorts are faster and these professors do not deserve tenure for this work!Problem2-1.Average-case performance of quicksortWe have shown that the expected time of randomized quicksort is,but we have not yet analyzed the average-case performance of ordinary quicksort.We shall prove that,under the assumption that all input permutations are equally likely,not only is the running time of ordinary quicksort,but it performs essentially the same comparisons and exchanges between input elements as randomized quicksort.Consider the implementation of P ARTITION given in lecture on a subarray:P ARTITION123for to4do if5then6exchange7exchange8returnLet be a set of distinct elements which are provided in random order(all orders equally likely) as the input array to P ARTITION,where is the size of the array.Let denote the initial value of.(a)Argue that is a random permutation of,that is,that all permuta-tions of the input subarray are equally likely.Solution:Given that is random(all orders are equally likely),there are possiblepermutations of the elements.Each element has a probabilityof being chosen as the pivot,therefore the number of permutations of the remainingelements is.Consequently the permutations are equallylikely.Define as follows:ififif(b)Consider two input arrays and consisting of the elements of suchthat for all.Suppose that we run P ARTITIONon and and trace the two executions to record the branches taken,indices calculated,and exchanges performed—but not the actual array values ma-nipulated.Argue briefly that the two execution traces are identical.Argue further thatP ARTITION performs the same permutation on both inputs.Solution:P ARTITION takes different branches based only on the comparisons made in thefunction.This is clear by observing line4of the function,as it is the only place wherethe sequence of instructions may differ.As the arrays have identical functionvalues,they must take the same branches,calculate the same indicies and perform thesame exchanges.Consequently P ARTITION will perform the same partition on botharrays,which follows directly as the exchanges performed are identical.Define a sequence to be an input pattern if,for ,and.Define a sequence to be an output pattern ififififWe say that a permutation of satisfies a pattern iffor all.(c)How many input patterns are there?How many output patterns are there?Solution:There are input patterns because we can choose positions out of possible positions to have.There is one()output pattern because the pattern must be exactly negative ones followed by a0,followed byones.(d)How many permutations of satisfy a particular input pattern?How manypermutations of satisfy a particular output pattern?Solution:permutations are possible of to satisfy a particular input pattern.This is the total number of ways to rearrange the elements which have a value ofamongst themselves,and rearrange those with a value of1amongst themselves.Thereare also permutations possible to satisfy a particular output patternfor the same reason.Let be an input pattern,and let be an output pattern.Define to be the set of permutations of that satisfy,and likewise define to be the set of permutations of that satisfy.(e)Argue that P ARTITION implements a bijection from to.(Hint:Use the factfrom group theory that composing afixed permutation with each of the possiblepermutations yields the set of all permutations.)Solution:All members of satisfy and so they all have the same result when the functionis applied to its elements.Therefore by part(b)when all these inputs are given toP ARTITION they are subject to the same ing the hint,we then knowthat after all of the distinct inputs are run through P ARTITION that they will produceall distinct outputs.From part we know that and are thesame size,and also we have proven that P ARTITION is onto,and therefore P ARTITIONmust be a bijection!(f)Suppose that before the call to P ARTITION,the input subarray is a randompermutation of,where.Argue that after P ARTITION,the tworesulting subarrays are random permutations of their respective elements.Solution:Using our solution from part(e),we know that after P ARTITION is run on,we getall values in the set.Therefore we get all permutations of the ones and allpermutations of the negative ones.Furthermore,we get each sub-array permu-tations an equal number of times and so the subarrays are also random permutations.(g)Use induction to show that,under the assumption that all input permutations areequally likely,at each recursive call of Q UICKSORT,every element of be-longing to is equally likely to be the pivot.Solution:The base case for the initial array;we know that it is randomly permuted,and so bypart and each of its subarrays will also be randomly permuted after P ARTITION.Therefore we can inductively apply at each partition to prove that every subarraywill also be randomly permuted.(h)Use the analysis of R ANDOMIZED-Q UICKSORT to conclude that the average-caserunning time of Q UICKSORT on elements is.Solution:By part(g)we know that under the assumption that the input pattern is random everyelement is equally likely to be chosen as the pivot at each recursive call which then pro-duces the same random distribution of quicksort traces as R ANDOMIZED-Q UICKSORT.Therefore as their distribution is the same,the expected-case analysis for R ANDOMIZED-Q UICKSORT will apply to the average case of Q UICKSORT.Therefore the average case of Q UICKSORTalso takes time.Problem2-2.Analysis of-ary heapsA-ary heap is like a binary heap,but(with one possible exception)nonleaf nodes have children instead of2children.(a)How would you represent a-ary heap in an array?Solution:The-ary heap would be similar to a binary heap with the parent and child indexescalculated as follows:th-Child[]whereThe root of the tree would be at index.Alternate Solution:A-ary heap can be represented in a-dimensional array asfollows.The root is kept in,its children are kept in order in through,their children are kept in order in through,and so on.The two procedures that map a node with index to its parent and to its th child(for),respectively,are;D-ARY-P ARENTreturnD-ARY-C HILDreturnTo convince yourself that these procedures really work,verify thatD-ARY-P ARENT D-ARY-C HILDfor any.Notice that the binary heap procedures are a special case of the above procedures when.(b)What is the height of a-ary heap of elements in terms of and?Solution:CORRECTIONA-ary heap would have a height of.We know thatwhich solves to.(c)Give an efficient implementation of E XTRACT-M AX in a-ary max-heap.Analyze itsrunning time in terms of and.Solution:H EAPIFY12for to3if and4then5if6then7Exchange8H EAPIFYThe running time of H EAPIFY is because at each depth we are doing loops,and we recurse to the depth of the tree.In H EAPIFY we compare the the th node and each of its children tofind the maximum value for all of the nodes.Then ifthe maximum child is greater than the th node,we switch the two nodes and recurse on the child.E XTRACT-M AX(A,n)1234H EAPIFY(A,1,n,d)5returnThe running time of this algorithm,is clearly constant work plus the time of H EAPIFY which as shown above is.E XTRACT-M AX works by storing the value of the maximum element,moving the minimum element into the root of the heap,and then calling heapify to restore the heap property.(d)Give an efficient implementation of I NSERT in a-ary max-heap.Analyze its runningtime in terms of and.Solution:See next problem part for I NCREASE-K EY definition.I NSERT123I NCREASE-K EYFrom the following problem part,we know I NCREASE-K EY runs in time, therefore since I NSERT only adds constant time operations it is also.It is rather trivially correct as the algorithm has not changed because all calculations involving the number of children are performed by I NCREASE K EY.(e)Give an efficient implementation of I NCREASE-K EY,whichfirst setsand then updates the-ary max-heap structure appropriately.Analyze its running time in terms of and.Solution:I NCREASE-K EY12if3while and 6。
算法导论习题答案

算法导论习题答案算法导论习题答案算法导论是一本经典的计算机科学教材,讲述了算法设计与分析的基本原理。
在学习过程中,习题是不可或缺的一部分,通过解答习题可以帮助我们巩固所学的知识。
本文将针对算法导论中的一些习题进行解答,帮助读者更好地理解算法导论的内容。
习题1-1:证明对于任意两个实数a和b,有|a + b| ≤ |a| + |b|。
解答:根据绝对值的定义,我们可以将|a + b|、|a|和|b|分别表示为以下三种情况:1. 当a + b ≥ 0,a ≥ 0,b ≥ 0时,|a + b| = a + b,|a| = a,|b| = b。
此时,|a + b| ≤ |a| + |b| 成立。
2. 当a + b < 0,a < 0,b < 0时,|a + b| = -(a + b),|a| = -a,|b| = -b。
此时,|a + b| ≤ |a| + |b| 成立。
3. 当a + b ≥ 0,a < 0,b < 0时,|a + b| = a + b,|a| = -a,|b| = -b。
此时,|a + b| ≤ |a| + |b| 成立。
综上所述,无论a和b的取值如何,都有|a + b| ≤ |a| + |b| 成立。
习题2-1:证明插入排序的运行时间是O(n^2)。
解答:插入排序是一种简单直观的排序算法,它的基本思想是将待排序的元素一个个地插入到已排好序的序列中。
在最坏情况下,即待排序的序列是逆序排列时,插入排序的运行时间最长。
假设待排序的序列长度为n,那么第一次插入需要比较1次,第二次插入需要比较2次,依次类推,第n次插入需要比较n-1次。
总的比较次数为1 + 2 + 3+ ... + (n-1) = n(n-1)/2。
因此,插入排序的运行时间是O(n^2)。
习题3-1:证明选择排序的运行时间是O(n^2)。
解答:选择排序是一种简单直观的排序算法,它的基本思想是每次从待排序的序列中选择最小的元素,放到已排序序列的末尾。
算法导论答案

算法导论答案算法导论是计算机科学领域的经典教材,它介绍了算法设计和分析的基本原理和方法。
通过学习算法导论,我们可以深入理解算法的运行机制,并且能够运用这些知识解决实际问题。
本文将介绍一些算法导论的常见问题,并给出相应的答案。
第一部分:算法基础在算法导论中,我们首先学习了算法的基础概念和表达方法。
其中最重要的是时间复杂度和空间复杂度的概念。
时间复杂度衡量了算法运行所需的时间,而空间复杂度则衡量了算法所需要的额外空间。
通过计算复杂度,我们可以估算出算法的效率和资源使用情况。
Q1:什么是时间复杂度和空间复杂度?A1:时间复杂度是指算法解决问题所需要的时间代价,通常以大O表示。
空间复杂度是指算法解决问题所需要的额外空间,通常也以大O表示。
时间复杂度和空间复杂度可以帮助我们评估算法的效率和资源使用情况。
Q2:如何计算时间复杂度?A2:时间复杂度可以通过分析算法中的基本操作的执行次数来计算。
通常,我们可以统计算法中循环、递归和条件判断等操作的执行次数,并根据问题规模n来表示。
然后,我们可以将执行次数与n的关系用大O表示法表示。
第二部分:排序算法算法导论中介绍了多种排序算法,包括插入排序、归并排序、快速排序等等。
不同的排序算法适用于不同的问题场景,并且它们的时间复杂度和稳定性也不同。
Q3:什么是稳定的排序算法?A3:稳定的排序算法是指当原始序列中有两个相等的元素时,排序后它们的相对位置不发生改变。
例如,插入排序和归并排序是稳定的排序算法,而快速排序不是稳定的排序算法。
Q4:如何选择合适的排序算法?A4:选择合适的排序算法需要考虑多个因素,包括数据规模、稳定性要求和系统资源等。
对于小规模数据,可以使用插入排序或者冒泡排序。
对于数据规模较大且对稳定性要求较高的情况,可以选择归并排序。
而快速排序则适用于大规模数据和对稳定性没有要求的场景。
第三部分:动态规划动态规划是算法导论中的重要主题,它是一种解决多阶段决策问题的方法。
算法导论第二次作业答案

Solution: First, let’s analyze the Activity Selection Problem again: This is a problem using greedy algorithm: Input: 1. n activities a1, a2, . . . , an; 2. for i = 1, 2, . . . , n, the activity ai has a start time si and a finish time fi: [si, fi) Output: Pick a maximum set of activities that are compatible in time.
Solution: First, let’s analyze the Huffman Code again:
This is a problem using greedy algorithm:
Input: 1. n symbols s1, s2, . . . , sn ; 2. every symbol si has a frequency fi
Assume the time period of each activity is shown as following table:
i
123
si
157
fi
469
duration 3 1 2
We will choose the a2 which has the least duration among all three activities. However, it is obvious that the best arrangement is to choose {a1, a3}. So the approach of selecting the activity of least duration from among those that are compatible with previously selected activities does not work.
算法导论第三版_习题_第二章_C语言实现

目录第二章 (2)练习题 (2)2.1-1 (2)2.1-2 (2)2.1-3 (2)2.1-4 (3)2.2-1 (3)2.2-2 (3)2.2-3 (4)2.2-4 (4)2.3-1 (4)2.3-2 (4)2.3-3 (4)2.3-4 (5)2.3-5 (5)2.3-6 (6)*2.3-7 (6)思考题 (6)2-1 ( (6)第二章练习题2.1-1以图2-2为模型,说明INSERTION-SORT在数组A=(31,41,59,26, 41,58〉上的执行过程。
略2.1-2重写过程INSERTION-SORT,使之按非升序(而不是非降序)排序。
//2.1-2.c 插入排序-非升序void InsertionSortNI(int ar[],int n){int i,j,key;for(j=1;j<n;j++){key = ar[j];i = j-1;while(i>=0&& ar[i]<key){ar[i+1]= ar[i];i=i-1;}ar[i+1]= key;}}2.1-3思考下面的查找问题:输入:有n个元素的序列A= (a1,a2,…,an)和一个值v。
输出:下标i(当v=A[i]), 或者特殊值NIL(当v不在A中出现时)。
写出线性查找的伪代码,它扫描整个序列来查找v。
使用一个循环不变式来证明你的算法是正确的。
确保你的循环不变式满足三条必要的性质。
代码://2.1-3.c 线性查找int serchv(int A[],int n,int v){int i;for(i=0;i<n;i++){if(A[i]==v)return i;}return 'NULL';}证明:循环不变式:在每一次for循环开始时,子数组A[0, ..., i-1]中的元素都不等于v;初始化:在第一次迭代之前,i=0,子数组是空数组,易知循环不定式是成立的;保持:在每次循环内部,将A[i] 与v比较,若符合,则返回正确结果i;在每次循环结束时,得知子数组A[0, ..., i-2]不含有v,循环不定式维持成立;递增i,使循环遍历数组中的元素,直到找到v或到达数组尽头;终止:终止条件为1. i>=n 或者2. A[i]==v。
算法导论 答案

算法导论答案算法导论是计算机科学领域中一门重要的课程,其目的是介绍和探讨算法的基本原理和设计方法。
本文将从几个不同角度来讨论算法导论的主题,包括算法的定义、算法分析、算法设计等,并提供一些常见的算法应用示例。
一、算法的定义与特点算法是一个用于完成特定任务的有限一系列指令或规则的集合。
算法的设计应具备以下几个特点:确定性、有穷性、可行性和输入/输出。
1. 确定性:对于给定的输入,算法的每个步骤都必须是明确且唯一的。
不同的输入应得到不同的输出。
2. 有穷性:算法必须经过有限的步骤后能终止。
这是因为计算机程序需要在有限时间内完成,而无限循环的算法则无法给出结果。
3. 可行性:算法的每个步骤都应该是可行执行的,即能够在可接受的时间内完成。
4. 输入/输出:算法应该具有输入和输出,即根据给定的输入,通过算法能够得到相应的输出。
二、算法的分析与效率评估算法的分析与效率评估是算法导论的重要内容。
在设计和选择算法时,需要考虑到其执行时间和内存需求等方面。
1. 时间复杂度:用于衡量算法所需执行的时间。
常见的时间复杂度表示方法有大O记法,例如O(n)、O(nlogn)等。
时间复杂度越低,算法执行效率越高。
2. 空间复杂度:用于衡量算法所需的内存空间。
空间复杂度的衡量单位通常是字节或比特。
与时间复杂度一样,空间复杂度越低,算法的内存需求越小。
三、算法的设计方法算法设计是算法导论的核心内容之一,主要包括贪心算法、分治算法、动态规划算法等。
1. 贪心算法:贪心算法是一种基于每一步局部最优解的策略来解决问题的算法。
它常应用于问题的最优解为局部最优解的情况,但不一定能得到全局最优解。
2. 分治算法:分治算法是一种将问题划分为多个相互独立的子问题,再合并子问题的解来得到整体解的算法。
适合解决规模较大且问题可被划分的情况。
3. 动态规划算法:动态规划算法是一种将复杂问题拆分为更小、更简单的子问题来解决的算法。
它利用了子问题的重叠性质,将子问题的解保存起来,避免了重复计算。
算法导论第二章答案

第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。
给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。
插入排序算法伪代码INSERTION-SORT(A)1 for j ←2 to length[A]2 do key ←A[j]3 Insert A[j] into the sorted sequence A[1..j-1]4 i ←j-15 while i > 0 and A[i] > key6 do A[i+1]←A[i]7 i ←i − 18 A[i+1]←keyC#对揑入排序算法的实现:public static void InsertionSort<T>(T[] Input) where T:IComparable<T>{T key;int i;for (int j = 1; j < Input.Length; j++){key = Input[j];i = j - 1;for (; i >= 0 && Input[i].CompareTo(key)>0;i-- )Input[i + 1] = Input[i];Input[i+1]=key;}}揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j]这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1.如果按照大部分计算机编程语言的思路,修改为:INSERTION-SORT(A)1 for j ← 1 to length[A]2 do key ←A[j]3 i ←j-14 while i ≥ 0 and A[i] > key5 do A[i+1]←A[i]6 i ← i − 17 A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。
算法导论中文版答案

24.2-3
24.2-4
24.3-1 见图 24-6 24.3-2
24.3-3
24.3-4 24.3-5 24.3-6
24.3-7
24.3-8 这种情况下不会破坏已经更新的点的距离。 24.4**** 24.5****
25.1-1 见图 25-1 25.1-2 为了保证递归定义式 25.2 的正确性 25.1-3
8.3-3 8.3-4
8.3-5(*) 8.4-1 见图 8-4 8.4-2
8.4-3 3/2,1/2 8.4-4(*) 8.4-5(*)
9.1-1
9.1-2 9.2-1 9.3-1
第九章
9.3-2 9.3-3
9.3-4 9.3-5
9.3-6 9.3-7
9.3-8
9.3-9
15.1-1
6.4-4
6.4-5
6.5-1 据图 6-5 6.5-2
6.5-3 6.5-4 6.5-5
6.5-6 6.5-7
6.5-8
7.1-1 见图 7-1 7.1-2
7.1-3 7.1-4 7.2-1 7.2-2
7.2-3 7.2-4 7.2-5
第七章
7.2-6 7.3-1
7.3-2
7.4-1 7.4-2
5.3-6
6.1-1 6.1-2 6.1-3 6.1-4 6.1-5 6.1-6
第6章
6.1-7
6.2-1 见图 6-2 6.2-2
6.2-3
6.2-4
6.2-5 对以 i 为根结点的子树上每个点用循环语句实现 6.2-6
6.3-1
见图 6-3 6.3-2
6.3-3
6.4-1 见图 6-4 6.4-2 HEAPSORT 仍然正确,因为每次循环的过程中还是会运行 MAX-HEAP 的过程。 6.4-3
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
b. T (n) = T (n − 1) + 1/n. c. T (n) = T (n − 1) + lg n. d. T (n) = 2T (n/2) + n lg n. e. T (n) = 5T (n/5) + n/ lg n. Solution 3: For these recurrences, the master theorem does not apply. a. Assume T (1) = 1, then unroll the recurrence: T (n) = T (n − 1) + n = n + (n − 1) + (n − 2) + · · · + 2 + T (1)
n
T (n) = ≥
1
k=1 n+1
1 k
n
T (n) =
k=1
1 k
n
1 dx x
=1+
k=2 n
1 k 1 dx x
= ln(n + 1) ≥ ln n = Ω(ln n)
≤1+
1
= 1 + ln n = O(ln n)
T (n) = Θ(ln n) = Θ(lg n)
2
c. Assume T (1) = 1, then unroll the recurrence: T (n) = T (n − 1) + lg n = lg n + lg(n − 1) + lg(n − 2) + · · · + lg 2 + lg 1
n 5
+ n/ lg n
n 25 n +n 5 / lg 5 + n/ lg n n n 25 / lg 25 n +n 5 / lg 5 + n/ lg n
= 5 5T
= 5 5 · · · 5 (5 (5T (1) + 5/ lg 5) + 25/ lg 25) + · · · +
n n = n(c + 1/ lg 5 + 1/ lg 52 + · · · + 1/ lg 5 2 + 1/ lg 5 + 1/ lg n log5 n 1 = n c + lg 5k k=1
CSCI 303 Homework 2
Problem 1 (4.3-2): The recurrence T (n) = 7T (n/2) + n2 describes the running time of an algorithm A. A competing algorithm A has a running time of T (n) = aT (n/4) + n2 . What is the largest integer value for a such that A is asymptotically faster than A? Solution 1: By the master theorem, the worst-case asymptotic complexity of A is Θ(nlg 7 ) ≈ Θ(n2.80735 ), and the worst-case asymptotic complexity of A is Θ(nlog4 a ), if a > 16. For A to be asymptotically faster than A, log4 a < lg 7 = log4 49. Therefore, the largest integer value for a such that A is asymptotically faster than A is 48. Problem 2 (Derived from 4-1 and 4-4): Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Make your bounds as tight as possible, and justify your answers. a. T (n) = 2T (n/2) + n3 . b. T (n) = 16T (n/4) + n2 . c. T (n) = 7T (n/3) + n2 . d. T (n) = 7T (n/2) + n2 . √ e. T (n) = 2T (n/4) + n. f. T (n) = 3T (n/2) + n lg n. √ g. T (n) = 4T (n/2) + n2 n. h. T (n) = T (9n/10) + n. Solution 2: Use the master method to solve these recurrences. a. Case 3 of master theorem. T (n) = Θ(n3 ). b. Case 2 of master theorem. T (n) = Θ(n2 lg n). c. Case 3 of master theorem. T (n) = Θ(n2 ). d. Case 1 of master theorem. T (n) = Θ(nlg 7 ). √ e. Case 2 of master theorem. T (n) = Θ( n lg n). f. Case 1 of master theorem. T (n) = Θ(nlg 3 ). √ g. Case 3 of master theorem. T (n) = Θ(n2 n). h. Case 3 of master theorem. T (n) = Θ(n). Problem 3 (Derived from 4-1 and 4-4): Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Make your bounds as tight as possible, and justify your answers. You may assume that T (1) is a constant. a. T (n) = T (n − 1) + n.
n
=
k=1
lg k
n
= lg
k=1
k
= lg(n!) = Θ(n lg n) By Stirling’s approximation (see section 3.2 in the book, equation 3.18). d. Assume n = 2m for some m and T (1) = c, then unroll the recurrence: T (n) = 2T
3
Given a list of numbers A and a target number t, the algorithm returns 1 if t it is in the list, and 0 otherwise. Unsorted-Search(A, t, p, q ) if q − p < 1 if A[p] = t return 1 else return 0 q if Unsorted-Search(A, t, p, p+ 2 ) = 1 return 1 p+q if Unsorted-Search(A, t, 2 + 1, q ) = 1 return 1 return 0 Analyze this algorithm with respect to worst-case asymptotic complexity, and give the worst-case running time in terms of Θ notation. How does this algorithm compare to the naive algorithm that simply iterates through the list to look for the target? Solution 4: The algorithm is given an array of n numbers and a target number. The first two lines take constant time, call it c. The next two lines recursively call Unsorted-Search on inputs of size n/2. Therefore, the worst-case asymptotic complexity is T (n) = 2T (n/2) + c Using case 1 of the master theorem, we see that T (n) = Θ(n). This is the same worst-case asymptotic complexity as the naive algorithm, although in practice the naive algorithm would probably run more quickly. Problem 5 (28.2-4): V. Pan has discovered a way of multiplying 68 × 68 matrices using 132,464 multiplications, a way of multiplying 70 × 70 matrices using 143,640 multiplications, and a way of multiplying 72 × 72 matrices using 155,424 multiplications. Which method yields the best asymptotic running time when used in a divide-and-conquer matrix-multiplication algorithm? How does it compare to Strassen’s algorithm? Solution 5: For each case, we write the recurrence and solve it using the master theorem: (1) (2) (3) T (n) = 132464T (n/68) + n2 T (n) = 143640T (n/70) + n2 T (n) = 155424T (n/72) + n2 ⇒ ⇒ ⇒ T (n) = Θ(nlog68 132464 ) ≈ Θ(n2.795128 ) T (n) = Θ(nlog70 143640 ) ≈ Θ(n2.795123 ) T (n) = Θ(nlog72 155424 ) ≈ Θ(n2.795147 )