Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Primero abrimos el archivo donde tenemos la matriz
- def cargar(file):
- fl = open(file, "r")
- matrix = []
- for i in fl:
- temp = []
- i = i.replace(" ", "")
- i = i[:-1]
- for j in i:
- temp.append( int(j) )
- matrix.append(temp)
- fl.close()
- return matrix
- # Obtenemos los grados de la matriz
- def obtener_grados(file):
- matrix = cargar(file) # carga la matriz
- grados = []
- for i in matrix:
- contador = 0
- for j in i:
- contador += j # suma los números
- grados.append(contador)
- return grados
- def distribucion_grado(file, k):
- grados = obtener_grados(file)
- probabilidad = 0
- for i in grados:
- if i == k:
- probabilidad += 1
- probabilidad = float(probabilidad) / len(grados) # Formula para obtener Pr(k) = probabilidad entre el número de grados
- return probabilidad
- def densidad(file):
- matrix = cargar(file)
- vertices = float(len(matrix))
- aristas = 0.0
- for i in matrix:
- for j in i:
- aristas += j
- aristas = aristas/2
- return ( (2*aristas)/(vertices*(vertices-1))) # Formula para obtener la Densidad = (2)M / n(n-1)
- def main():
- fl = open("distribucion.dat", "w") # Guardamos la tabla generada en un archivo .dat para graficarlo con gnuplot
- total_probabilidad = 0.0
- for i in range(0, 21):
- temp = distribucion_grado("zachary.txt", i)
- fl.write(str(i)+" "+str(temp)+"\n")
- total_probabilidad += temp
- print "Sumatoria de la distribucion: ", total_probabilidad
- print "Densidad del grafo: ", densidad("zachary.txt")
- fl.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement