-
Notifications
You must be signed in to change notification settings - Fork 0
/
arvore_otima.py
76 lines (61 loc) · 2.1 KB
/
arvore_otima.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
################################################################
# #
# Lucas Airam Castro de Souza #
# Universidade Federal do Rio de Janeiro #
# Algoritmos e Estruturas de Dados #
# #
################################################################
def printar_matriz(matriz):
print '\n'
for i in matriz:
linha = ""
for j in i:
l = str(j)
linha+=l.center(5)
print linha
def criar_matriz(n,m):
matriz = []
for i in range(n):
linha = []
for j in range(m):
linha+=['-']
matriz+=[linha]
return matriz
def criar_matriz_quadrada(n):
return criar_matriz(n,n)
def custo_minimo(i,j):
minimo = 0
for k in range(1,j+1):
custo = C[i][k-1] + C[k][j]
if (custo < minimo) or (K[i][j] == 0):
minimo = custo
K[i][j] = k
return minimo
def encontrar_arvore_otima(frequencia_j,frequencia_j_linha):
n = len(frequencia_j);
K = criar_matriz_quadrada(n);
F = criar_matriz_quadrada(n);
C = criar_matriz_quadrada(n);
for j in range(n):
F[j][j] = frequencia_j_linha[j]
C[j][j] = 0
for d in range(1,n+1):
for i in range(n-d):
j = i + d
F[i][j] = F[i][j-1] + frequencia_j[j] + frequencia_j_linha[j]
minimo = C[i][i] + C[i+1][j]
for k in range(i+1,j+1):
custo = C[i][k-1] + C[k][j]
if (custo <= minimo):
minimo = custo
K[i][j] = k
C[i][j] = minimo + F[i][j]
print "\n\t\tMatriz F"
printar_matriz(F)
print "\n\t\tMatriz C"
printar_matriz(C)
print "\n\t\tMatriz K"
printar_matriz(K)
return K
if (__name__ == '__main__'):
encontrar_arvore_otima([0,20,30,15,15,10,5,2,1],[1,1,1,1,1,1,1,1,1])