日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

算法导论第三版 第1章习题答案

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法导论第三版 第1章习题答案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2020/10/25:初稿

2020/10/28:增加對問題2、問題3、思考題1的求解過程Python代碼。

參考文獻: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]

總結

以上是生活随笔為你收集整理的算法导论第三版 第1章习题答案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。