算法分析与设计-算法分析题-第1章-题目答案与解析

第1章 算法概述 算法分析题 题目+答案解析

算法分析题

笔记参考:
算法设计与分析-笔记-第1章-算法概述

1-1 求下列函数的渐近表达式

(1) 3 n 2 + 10 n 3n^2+10n 3n2+10n

∵ O ( n ) < O ( n 2 ) \because \Omicron(n)<\Omicron(n^2) O(n)<O(n2)

∵ 3 n 2 + 10 n = O ( n 2 ) \because 3n^2+10n=\Omicron(n^2) 3n2+10n=O(n2)

(2) n 2 10 + 2 n \frac{n^2}{10}+2^n 10n2+2n

∵ O ( n 2 ) < O ( 2 n ) \because \Omicron(n^2)<\Omicron(2^n) O(n2)<O(2n)

∴ n 2 10 + 2 n = O ( 2 n ) \therefore\frac{n^2}{10}+2^n=\Omicron(2^n) 10n2+2n=O(2n)

(3) 21 + 1 n 21+\frac{1}{n} 21+n1

∵ O ( 1 n ) < O ( 1 ) \because \Omicron(\frac{1}{n})<\Omicron(1) O(n1)<O(1)

∴ 21 + 1 n = O ( 1 ) \therefore 21+\frac{1}{n}=\Omicron(1) 21+n1=O(1)

(3) l o g n 3 logn^3 logn3

∵ l o g n 3 = 3 l o g n \because logn^3=3logn logn3=3logn

∴ l o g n 3 = O ( l o g n ) \therefore logn^3=\Omicron(logn) logn3=O(logn)

(4) 10 l o g 3 n 10log3^n 10log3n

∵ 10 l o g 3 n = 10 n l o g 3 \because 10log3^n=10nlog3 10log3n=10nlog3

∴ 10 l o g 3 n = O ( n ) \therefore 10log3^n=\Omicron(n) 10log3n=O(n)

1-2 O ( 2 ) 与 O ( 2 ) 的 区 别 \Omicron(2)与\Omicron(2)的区别 O(2)O(2)

O ( 1 ) = O ( 2 ) \Omicron(1)=\Omicron(2) O(1)=O(2),可以表示同一个函数,但其常数因子不同

1-3 按渐近阶从低到高排列: 4 n 2 , l o g n , 3 n , 20 n , 2 , n 2 3 , n ! 4n^2,logn,3^n,20n,2,n^{\frac{2}{3}},n! 4n2,logn,3n,20n,2,n32,n!

2 , l o g n , n 2 3 , 20 n , 4 n 2 , 3 n , n ! 2,logn,n^{\frac{2}{3}},20n,4n^2,3^n,n! 2,logn,n32,20n,4n2,3n,n!

1-4 某算法在输入规模为 n n n时的计算时间为 T ( n ) T(n) T(n)。在某计算机上实现并完成该算法的时间为 t t t秒,那在运行速度为该计算机的64倍的计算机上用同一算法在 t t t秒内能解决多大规模的问题?

设能解决 n 1 n1 n1规模的问题

(1) T ( n ) = 3 × 2 n T(n)=3\times 2^n T(n)=3×2n

∵ t = 3 × 2 n = 3 × 2 n 1 64 \because t=3\times2^n=\frac{3\times2^n1}{64} t=3×2n=643×2n1

∴ n 1 = n + 6 \therefore n1=n+6 n1=n+6

(2) T ( n ) = n 2 T(n)=n^2 T(n)=n2

∵ t = n 2 = n 1 2 64 \because t=n^2=\frac{n1^2}{64} t=n2=64n12

∴ n 1 = 8 n \therefore n1=8n n1=8n

(3) T ( n ) = 8 T(n)=8 T(n)=8

计算时间与问题规模无关,可以解决任意规模的问题

1-5计算机XYZ的运行速度是计算机ABC的100倍,对于复杂度为 n , n 2 , n 3 和 n ! n,n^2,n^3和n! n,n2,n3n!的算法,若用计算机XYZ在1小时能解决输入规模为 n n n的问题,那么用计算机ABC能在1小时解决多大规模的问题?

设能解决 n 1 n1 n1规模的问题

n 1 = 100 n ⇒ n 1 = 100 n n1=100n\Rightarrow n1=100n n1=100nn1=100n

n 1 2 = 100 n 2 ⇒ n 1 = 10 n n1^2=100n^2\Rightarrow n1=10n n12=100n2n1=10n

n 1 3 = 100 n 3 ⇒ n 1 = 100 3 n = 4.64 n n1^3=100n^3\Rightarrow n1=\sqrt[3]{100}n=4.64n n13=100n3n1=3100 n=4.64n

n 1 ! = 100 n ! ⇒ n 1 < n + l o g 100 = n + 6.64 n1!=100n!\Rightarrow n1n1!=100n!n1<n+log100=n+6.64

∵ ( S t i r i n g l i n g 公 式 ) n ! ≈ 2 π n ( n e ) n \because(Stiringling公式)\quad n!\approx\sqrt{2\pi n}({\frac{n}{e}})^n (Stiringling)n!2πn (en)n

∴ n 1 ! = 100 n ! l o g n 1 ! = l o g n ! + l o g 100 l o g 2 π n 1 ( n 1 e ) n 1 = l o g 2 π n ( n e ) n + l o g 100 n 1 l o g 2 π n 1 ( n 1 e ) = n l o g 2 π n ( n e ) + l o g 100 n 1 l o g 2 π n 1 ( n 1 e ) < n l o g 2 π n 1 ( n 1 e ) + l o g 100 n 1 < n + l o g 100 = n + 6.64 \begin{aligned} \therefore n1!&=100n!\\ logn1!&=logn!+log100\\log\sqrt{2\pi n1}(\frac{n1}{e})^{n1}&=log\sqrt{2\pi n}(\frac{n}{e})^{n}+log100\\n1log\sqrt{2\pi n1}(\frac{n1}{e})&=nlog\sqrt{2\pi n}(\frac{n}{e})+log100\\n1log\sqrt{2\pi n1}(\frac{n1}{e})&n1!logn1!log2πn1 (en1)n1n1log2πn1 (en1)n1log2πn1 (en1)n1=100n!=logn!+log100=log2πn (en)n+log100=nlog2πn (en)+log100<nlog2πn1 (en1)+log100<n+log100=n+6.64

1-6对下列各组函数 f ( n ) f(n) f(n) g ( n ) g(n) g(n),确定 f ( n ) = O ( g ( n ) ) f(n)=\Omicron(g(n)) f(n)=O(g(n)) f ( n ) = Ω ( g ( n ) ) f(n)=\Omega(g(n)) f(n)=Ω(g(n)) f ( n ) Θ ( g ( n ) ) f(n)\Theta(g(n)) f(n)Θ(g(n))

(1) f ( n ) = l o g n 2 , g ( n ) = l o g n + 5 f(n)=logn^2,g(n)=logn+5 f(n)=logn2,g(n)=logn+5

∵ f ( n ) = 2 l o g n , 当 n → ∞ 时 , l o g n = l o g n \because f(n)=2logn,当n\rightarrow\infty时,logn=logn f(n)=2logn,nlogn=logn

∴ f ( n ) = Θ ( g ( n ) ) \therefore f(n)=\Theta(g(n)) f(n)=Θ(g(n))

(2) f ( n ) = l o g n 2 , g ( n ) = n f(n)=logn^2,g(n)=\sqrt{n} f(n)=logn2,g(n)=n

∵ f ( n ) = 2 l o g n , 当 n → ∞ 时 , l o g n < n \because f(n)=2logn,当n\rightarrow\infty时,logn<\sqrt{n} f(n)=2logn,nlogn<n

∴ f ( n ) = O ( g ( n ) ) \therefore f(n)=\Omicron(g(n)) f(n)=O(g(n))

(3) f ( n ) = n , g ( n ) = l o g 2 n f(n)=n,g(n)=log^2n f(n)=n,g(n)=log2n

∵ 当 n → ∞ 时 , n > l o g n 即 n > l o g 2 n \because当n\rightarrow\infty时,\sqrt{n}>logn即n>log2^n nn >lognn>log2n

∴ f ( n ) = Ω ( g ( n ) ) \therefore f(n)=\Omega(g(n)) f(n)=Ω(g(n))

(4) f ( n ) = n l o g n + n , g ( n ) = l o g n f(n)=nlogn+n,g(n)=logn f(n)=nlogn+n,g(n)=logn

∵ 当 n → ∞ 时 , n l o g n > l o g n \because 当n\rightarrow\infty时,nlogn>logn nnlogn>logn

∴ f ( n ) = Ω ( g ( n ) ) \therefore f(n)=\Omega(g(n)) f(n)=Ω(g(n))

(5) f ( n ) = 10 , g ( n ) = l o g 10 f(n)=10,g(n)=log10 f(n)=10,g(n)=log10

∵ 1 = 1 \because 1=1 1=1

∴ f ( n ) = Θ ( g ( n ) ) \therefore f(n)=\Theta(g(n)) f(n)=Θ(g(n))

(6) f ( n ) = l o g 2 n , g ( n ) = l o g n f(n)=log^2n,g(n)=logn f(n)=log2n,g(n)=logn

当 n → ∞ 时 , l o g 2 n > l o g n 当n\rightarrow\infty时,log^2n>logn nlog2n>logn

∴ f ( n ) = Ω ( g ( n ) ) \therefore f(n)=\Omega(g(n)) f(n)=Ω(g(n))

(7) f ( n ) = 2 n , g ( n ) = 100 n 2 f(n)=2^n,g(n)=100n^2 f(n)=2n,g(n)=100n2

∵ 当 n → ∞ 时 , 2 n > n 2 \because当n\rightarrow\infty时,2^n>n^2 n2n>n2

∴ f ( n ) = Ω ( g ( n ) ) \therefore f(n)=\Omega(g(n)) f(n)=Ω(g(n))

(8) f ( n ) = 2 n , g ( n ) = 3 n f(n)=2^n,g(n)=3^n f(n)=2n,g(n)=3n

∵ 当 n → ∞ 时 , 2 n < 3 n \because当n\rightarrow\infty时,2^n<3^n n2n<3n

∴ f ( n ) = O ( g ( n ) ) \therefore f(n)=\Omicron(g(n)) f(n)=O(g(n))

1-7 证明: n ! = ο ( n n ) n!=\omicron(n^n) n!=ο(nn)

∵ ( S t i r i n g l i n g 公 式 ) n ! ≈ 2 π n ( n e ) n \because(Stiringling公式)\quad n!\approx\sqrt{2\pi n}({\frac{n}{e}})^n (Stiringling)n!2πn (en)n

∴ lim ⁡ n → ∞ n ! n n = 2 π n e n = 0 即 n ! = ο ( n n ) \therefore \lim\limits_{n\rightarrow\infty}{\frac{n!}{n^n}}=\frac{\sqrt{2\pi n}}{e^n}=0即n!=\omicron(n^n) nlimnnn!=en2πn =0n!=ο(nn)

1-8 3n+1问题:下面的算法用于确定n的初始值,试分析该算法片段所需计算时间的上界和下界

while (n > 1)
    if (odd(n))
        n = 3 * n + 1;
	else
        n = n / 2;

上诉代码的意思是:当 n n n为奇数时, n = 3 ∗ n + 1 n=3*n+1 n=3n+1,当 n n n为偶数时, n = n / 2 n=n/2 n=n/2,直到 n = 1 n=1 n=1,那么最快跳出循环的情况是 n n n为为2的幂次的情况,即所需时间的下界为 Ω ( l o g n ) \Omega(logn) Ω(logn),无法计算上界。

1-9证明:如果一个算法在平均情况下的计算时间复杂度为 Θ ( f ( n ) ) \Theta(f(n)) Θ(f(n)),则该算法在最坏情况下所需的计算时间为 Ω ( f ( n ) ) \Omega(f(n)) Ω(f(n))

∵ T a v g ( N ) ≤ T m a x ( N ) \because T_{avg}(N)\leq T_{max}(N) Tavg(N)Tmax(N)

∴ T m a x = Ω ( T a v g ( N ) ) = Ω ( Θ ( f ( n ) ) ) = Ω ( f ( n ) ) \therefore T_{max}= \Omega(T_{avg}(N))=\Omega(\Theta(f(n)))=\Omega(f(n)) Tmax=Ω(Tavg(N))=Ω(Θ(f(n)))=Ω(f(n))

你可能感兴趣的:(算法设计与分析,算法)