import numpy as np
import matplotlib.pyplot as plt
import random
plt.rcParams['figure.figsize']=(12,8)
import graphPaths
# this function creates the adjancy list and the dictionary with the order of edges
def listTograph(grafo , dicionario , lista , initvalue=0):
i = initvalue
for arco in lista:
grafo.addEdge(arco[0],arco[1])
dicionario[arco[0],arco[1]] = i
i += 1
def invertList(lista):
out = lista.copy()
for i in range(len(lista)):
out.append(lista[i][::-1])
return out
grafo01 = graphPaths.Graph(5)
lista01 = [[0,1],[0,2],[1,2],[2,3],[2,4],[3,4]]
dicio01 = {}
listTograph(grafo01 , dicio01 , lista01)
print(grafo01.graph)
print(dicio01)
caminhos01 = []
grafo01.printAllPaths(0,4,caminhos01)
print(caminhos01)
narcos = len(lista01)
ncamin = len(caminhos01)
matrix = np.zeros(ncamin * narcos).reshape(ncamin , narcos)
for i in range(ncamin):
for k in range(len(caminhos01[i]) - 1):
matrix[i,dicio01[caminhos01[i][k],caminhos01[i][k+1]]] = 1
print(matrix)
sigmas = np.linspace(10,3,num=narcos)
mus = np.linspace(1,10,num=narcos)
arcos_randomize = np.random.exponential
print(mus)
print(sigmas)
# Epaneshnikov Kernel
xx10 = np.linspace(-1,1)
yy10 = np.array(list(map(lambda x : .75 * (1 - x**2),xx10)))
plt.plot(xx10,yy10)
plt.title('Epanechnikov Kernel')
plt.show()
# triweight kernel
xx20 = np.linspace(-1,1)
yy20 = np.array(list(map(lambda x : 35.0 *(1-x**2)**3/32.0, xx20)))
plt.plot(xx20,yy20)
plt.title('Triweight Kernel')
plt.show()
def triweightKfunc(x,x0,h):
t = (x - x0)/h
out = 35.0 *(1-t**2)**3/32.0
return out/h
triweightKfunc(0.0,1,3)
npts = 100
xx0= 10 * np.ones(npts)
hh = 3 * np.ones(npts)
xx30 = np.linspace(xx0[0] - hh[0] , xx0[0] + hh[0] , num = npts)
yy30 = np.array(list(map(lambda x , y , z: triweightKfunc(x,y,z),xx30, xx0 , hh)))
plt.plot(xx30,yy30)
plt.show()
# now we generate points for this random variable using the rejection acceptance method.
npts = 100
centralpt = 10
hhi = 3
xx40 = np.linspace(centralpt - hhi, centralpt + hhi , num = npts)
yy40 = np.array(list(map(lambda x : triweightKfunc(x,centralpt,hhi),xx40)))
# now we are going to generate random points for this distribution
nrpts = 50000
contador = 0
randpts = []
while (contador < nrpts):
x = centralpt - hhi + 2.0 * hhi * random.uniform(0.0 , 1.0)
y = random.random()
t = triweightKfunc(x , centralpt , hhi)
if (y < t):
randpts.append(x)
contador += 1
plt.plot(xx40,yy40)
plt.hist(np.array(randpts),bins = 60 ,density=True,rwidth=0.8)
plt.show()
grafo02 = graphPaths.Graph(5)
lista02 = [[0,1],[0,2],[1,2],[2,3],[2,4],[3,4]]
dicio02 = {}
listTograph(grafo02,dicio02,lista02)
caminhos02 = []
grafo02.printAllPaths(4,2,caminhos02)
print(caminhos02)
lista021 = [x[::-1] for x in lista02 if x[1]== 2]
lista021.append([x for x in lista02 if x[1]!= 2])
print(lista02)
print(lista021)
lista02[::]