Escolar Documentos
Profissional Documentos
Cultura Documentos
Complexidade
Prof. Sérgio A. Monteiro
Questão 1
Dada função F definida como:
F(1) = 1
{ N
F(N) = 4F ( ) + N
2
Suponha N=2k, para k=1, 2, 3, ... obtenha, detalhadamente, a fórmula fechada
para F.
n
f(n) = 4f ( ) + n
2
n
n n n n
f ( ) = 4f ( 2 ) + = 4f ( 2 ) +
2 2 2 2 2
n
n 2 n n n
f ( 2 ) = 4f ( 2 ) + 2 = 4f ( 3 ) + 2
2 2 2 2 2
n
n 23 n n n
f ( 3 ) = 4f ( ) + 3 = 4f ( 4 ) + 3
2 2 2 2 2
...
n
f(1) = 1 = f ( )=1
2log2 𝑛
Então:
n n n
f(n) = 4 [4f (22 ) + 2] + n = 42 f (22 ) + 2n + n
n n n n
f(n) = 4 [4 [4f (23 ) + 22 ] + 2] + n = 43 f (23 ) + 4n + 2𝑛 + n
n n n n n
f(n) = 4 [4 [4 [4f (24 ) + 23 ] + 22 ] + 2] + n = 44 f (24 ) + 8𝑛 + 4n + 2n + n
n
f(n) = 44 f (24 ) + 23 n + 22 n + 21 n + 20 𝑛
𝑘
n
f(n) = 4 f ( 𝑘 ) + n ∑ 2𝑖−1
𝑘
2
𝑖=1
1
𝑘
n
f(n) = 4𝑘 f ( 𝑘 ) + n ∑ 2𝑖−1
2
𝑖=1
𝑘 n
f(n) = 4 f (2𝑘) + nZ
log2 𝑛
𝑍 = ∑ 2𝑖
𝑖=0
𝑍 = 20 + 21 +22 + ⋯ + 2log2 𝑛
𝑎1 (𝑄𝑛 −1)
Utilizando a formula da PG temos que
𝑄−1
1(2log2 𝑛 − 1) 2log2 𝑛 − 1
𝑍= = = 2log2 𝑛 − 1 = n − 1
2−1 1
Encontrado Z iremos substitui-lo:
n
f(n) = 4𝑘 f ( ) + n(n − 1)
2𝑘
Lembrando que k = log 2 𝑛 então
n
f(n) = 4log2 𝑛 f ( ) + n(n − 1)
2log2 𝑛
log2 𝑛 n
f(n) = 22 f ( ) + 𝑛2 − n
n
f(n) = 2𝑛2 − n
Questão 2
Dada a soma
𝑛
∑ 5𝑖 9𝑖
𝑖=1
2
𝑛
5 ∑ 𝑖 9𝑖 = 5𝑤
𝑖=1
𝑤 = ∑ 𝑖 9𝑖
𝑖=1
∑ 𝑖 9𝑖 = 1. 91 + 2. 92 + 3. 93 + ⋯ + 𝑛. 9𝑛
𝑖=1
𝑤 = 1. 91 + 2. 92 + 3. 93 + ⋯ + 𝑛. 9𝑛
9𝑤 = 1. 92 + 2. 93 + 3. 94 + ⋯ + (𝑛 − 1). 9𝑛 + 𝑛. 9𝑛+1
𝑤 − 9𝑤 = 1. 91 + 1. 92 + 1. 93 + ⋯ + 1. 9𝑛 − 𝑛. 9𝑛+1
−8𝑤 = ∑ 9𝑖 − 𝑛. 9𝑛+1
𝑖=1
𝑎1 (𝑄𝑛 −1)
Utilizando a formula da PG temos que
𝑄−1
9(9𝑛 − 1)
−8𝑤 = − 𝑛. 9𝑛+1
9−1
9(9𝑛 − 1)
−8𝑤 = − 𝑛. 9𝑛+1
9−1
(9𝑛+1 − 9)
−8𝑤 = − 𝑛. 9𝑛+1
8
(9𝑛+1 − 9) 𝑛. 9𝑛+1
−8𝑤 = −
8 1
𝑛. 9𝑛+1 (9𝑛+1 − 9)
8𝑤 = −
1 8
3
8𝑛. 9𝑛+1 − 9𝑛+1 + 9
8𝑤 =
8
9𝑛+1 (8𝑛 − 1) + 9
8𝑤 =
8
9𝑛+1 (8𝑛 − 1) + 9
𝑤=
82
5 ∑ 𝑖 9𝑖 = 5𝑤
𝑖=1
𝑛
9𝑛+1 (8𝑛 − 1) + 9
5 ∑ 𝑖 9𝑖 = 5 ( )
82
𝑖=1
Questão 3
1) Se f(n) = O(𝑛(log𝑏 𝑎)−ε ) para alguma constante ε > 0, então T(n) = Θ(𝑛log𝑏 𝑎 ).
log 𝑏 𝑎 = log 3 3 = 1
O(𝑛(log𝑏 𝑎)−ε ) = O(𝑛(1)−ε )
f(n) = 𝑛2 = O(𝑛1−ε )
𝑛2 ≤ k(𝑛1−ε )
Como não conseguimos uma constante ε > 0 que satisfaça esta condição,
passaremos para o próximo caso.
4
log 𝑏 𝑎 = log 3 3 = 1
Θ(𝑛(log𝑏 𝑎) ) = Θ(𝑛1 ) = Θ(n)
f(n) = 𝑛2 = Θ(n)
𝑘1 𝑛 ≤ 𝑛2 ≤ 𝑘2 𝑛
Como não conseguimos uma constante que satisfaça esta condição, passaremos
para o próximo caso.
𝑛
3) Se f(n) = Ω(𝑛log𝑏 𝑎+ε ) para alguma constante ε > 0, e se 𝑎𝑓 (𝑏 ) ≤ 𝑐𝑓(𝑛) para
alguma constante c<1 e todos os n suficientemente grandes, então T(n)=Θ(f(n))
log 𝑏 𝑎 = log 3 3 = 1
Ω(𝑛1+ε )
f(n) = 𝑛2 = Ω(𝑛1+ε )
𝑛2 ≥ k(𝑛1+ε )
𝑛
𝑎𝑓 ( ) ≤ 𝑐𝑓(𝑛); a = 3, b = 3, f(n) = 𝑛2
𝑏
𝑛 2
3 ( ) ≤ 𝑐𝑛2
3
𝑛 𝑛
3 ( . ) ≤ 𝑐𝑛2
3 3
𝑛2
3( ) ≤ 𝑐𝑛2
9
1 2
𝑛 ≤ 𝑐𝑛2
3
1
Para 𝑐 = 3 temos:
1 2 1
𝑛 ≤ 𝑛2
3 3
1
Sendo assim 𝑐 = 3 satisfaz a condição exigida e concluímos que T(n)=Θ(f(n)) =
Θ(𝑛2 )
Questão 4
5
Considerando que 𝑛3 + 3𝑛−1 ≤ 𝑤3𝑛 ; 𝑤 > 0 𝑛 ≥ 5 e que 𝑤 = 1 satisfaz.
Passo 2: Hipótese 𝑛 = 𝑘
𝑘 3 + 3𝑘−1 ≤ 3𝑘
(𝑘 + 1)3 + 3𝑘 =
(𝑘 3 + 𝐴) + (3𝑘−1 . 3) =
(𝑘 3 + 𝐴) + (3𝑘−1 + 3𝑘−1 + 3𝑘−1 ) =
𝑘 3 + 3𝑘−1 + (𝐴 + 3𝑘−1 + 3𝑘−1 ) =
𝑘 3 + 3𝑘−1 + 𝐵 (I)
Questão 5
Seja uma árvore B de ordem 2 com 1101 elementos. Qual a altura da árvore?
6
Quantidade de elementos por nível de altura:
ℎ−1
𝑛 = 2𝑑 ∑(2𝑑 + 1)𝑖
𝑖=0
(2𝑑 + 1)ℎ = 𝑛 + 1
ℎ = log (2𝑑+1) 𝑛 + 1
Raiz ➔1
1° ➔ 2𝑑(𝑑 + 1)0
2° ➔ 2𝑑(𝑑 + 1)1
7
...
h ➔ 2𝑑(𝑑 + 1)ℎ−2
ℎ−2
𝑛 = 1 + 2𝑑 ∑(𝑑 + 1)𝑖
𝑖=0
𝑎1 (𝑄𝑛 − 1)
𝑛 = 1 + 2𝑑 ( )
𝑄−1
(𝑑 + 1)0 ((𝑑 + 1)ℎ−1 − 1)
𝑛 = 1 + 2𝑑 ( )
𝑑+1−1
(𝑑 + 1)ℎ−1 − 1
𝑛 = 1 + 2𝑑 ( )
𝑑
𝑛 = 1 + 2 [(𝑑 + 1)ℎ−1 − 1]
𝑛 = 1 + 2(𝑑 + 1)ℎ−1 − 2
𝑛 = 2(𝑑 + 1)ℎ−1 − 1
2(𝑑 + 1)ℎ−1 = 𝑛 + 1
𝑛+1
(𝑑 + 1)ℎ−1 =
2
𝑛+1
ℎ = 1 + log 𝑑+1
2
1101 + 1 1102
ℎ = 1 + log 2+1 = 1 + log 3
2 2
ℎ = 1 + log 3 551
ℎ ≅ 6,75