算法导论第三版 第1章习题答案
2020/10/25:初稿
2020/10/28:增加對問題2、問題3、思考題1的求解過程Python代碼。
參考文獻(xiàn):https://ita.skanev.com/?
1.The role of Algorithm in Computing
1.1 Algorithms
1.Give a real-world example that requires sorting or a real-world example that requires computing a convex hull.
An example of a data set that might need sorting is an online store's sale items by the largest discount. An example of a convex hull that would need computing is a set of coordinates in which we want to find the shortest travel time to contain all the coordinates.
2.Other than speed, what other measures of efficiency might one use in a real-world setting?
Memory usage and other resources such as I/O consumption, network consumption, disk consumption, power consumption, etc.?
3.Select a data structure that you have seen previously, and discuss its strengths and limitations.
Let's take the singly-linked list.
Strengths:
- It does not need sequential space in memory
- We can insert a new element at any place with O(1)
Limitations:
- Random access is?O(n)
- It takes additional memory for the links
4.How are the shortest-path and traveling-salesman problems given above similar? How are they different?
They are similar, because each of them?has to walk a graph and find a path in them.
The difference is the constraint on the solution. The shortest-path requires just a path between two points, while the traveling salesman requires a path between more points that returns to the first point.
5.Come up with a real-world problem in which only the best solution will do. Then come up with one in which a solution that is “approximately” the best is good enough.
Sorting a catalog is a problem, where only the best solution will do. An "approximately" sorted catalog won't be that useful.
Finding the shortest path between two points in a city is a problem, where good-enough will do. It might not be the fastest way, but you will still get there.
1.2 Algorithms as a technology
1.Give an example of an application that requires algorithmic content at the application level, and discuss the function of the algorithms involved.
Google Maps when finding a route between two places. The algorithms are an essential part of this use case,?since the route is what the user cares for the most.
2.Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size?n, insertion sort runs in??steps, while merge sort runs in?64nlgn?steps. For which values of?n?does insertion sort beat merge sort?
We could find the approriate n by solving the function?
from scipy.optimize import fsolve import numpy as np import matplotlib.pyplot as pltdef f1(x):return 8*x**2 - 64*x*np.log2(x)root = fsolve(f1,x0=100)print('root is: {0:.2f}'.format(root[0])) print('double check whether the root is correct:',np.isclose(f1(root), [0.0]))t = np.linspace(1,100,100) y1 = 8*t**2 y2 = 64*t*np.log2(t) plt.plot(t,y1,label='$8x^2$') plt.plot(t,y2,label='$64x*\log_{2} x$') plt.legend() plt.show()?
The root is approximately 43.56, thus?when n < 44, insertion sort beats merge sort. When?, merge sort beats insertion sort.
3.What is the smallest value of?n?such that an algorithm whose running time is??runs faster than an algorithm whose running time is? on the same machine?
Similar to question 2, we could find n by solving?
from scipy.optimize import fsolve import numpy as np import matplotlib.pyplot as pltdef f1(x):return 100*x**2 - 2**xroot = fsolve(f1,x0=100)print('root is :{0:.2f}'.format(root[0])) print('double check whether the root is correct:',np.isclose(f1(root), [0.0]))t = np.linspace(1,20,100) y1 = 100*t**2 y2 = 2**t plt.plot(t,y1,label='$100x^2$') plt.plot(t,y2,label='$2^x$') plt.legend() plt.show()The root is approximately 14.32, thus when n >14, the first algorithm runs faster.That is to say, the smallest value of n is 15.
Problems
1.Comparison of running times:For each function f(n) and time t in the following table, determine the largest size n of a problem that can be solved in time t, assuming that the algorithm to solve the problem takes f(n)?microseconds.
Note1: lgn is the best while the n! is the worst.
Note2: 1minute=60seconds 1hour=60minutes 1day=24hours 1month=30days 1year=365days 1century=365*100+24(for leap years)=36524days
Note3:We could get nlogn by solving the equations,similar to question 2 and question 3.
from scipy.optimize import fsolve import numpy as npdef f1(x,*arg):return x*np.log2(x) - arg[0]results = [[p, fsolve(f1, x0=[10**3,10**4,10**5,10**6,10**7], args=(p))[0]] for p in [10**6,60*10**6,3600*10**6,24*3600*10**6,30*24*3600*10**6,365*24*3600*10**6,36524*24*3600*10**6]]print('results is :') for item in results:print(item)results is :
[1000000, 62746.126469697854]
[60000000, 2801417.883946006]
[3600000000, 133378058.86445554]
[86400000000, 2755147513.2252975]
[2592000000000, 71870856403.9747]
[31536000000000, 797633893349.0258]
[3155673600000000, 68654697441062.086]
?
Note4: We could got n! using below Python code:
import numpy as npfactorial_result = [] n = [] for i in range(0,20):factorial_result.append(np.math.factorial(i))for duration in [10**6,60*10**6,3600*10**6,24*3600*10**6,30*24*3600*10**6,365*24*3600*10**6,36524*24*3600*10**6]:for i in range(len(factorial_result)):if duration <= factorial_result[i]:n.append(i-1)breakprint(n)The output is:[9, 11, 12, 13, 15, 16, 17]
總結(jié)
以上是生活随笔為你收集整理的算法导论第三版 第1章习题答案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【2016计概A期末】照亮房间
- 下一篇: python根据文件路径获取上级目录路径