-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Script.py
More file actions
104 lines (81 loc) · 2.56 KB
/
Python_Script.py
File metadata and controls
104 lines (81 loc) · 2.56 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/local/bin/python
from __future__ import division, absolute_import
import sys
range = xrange
import os
from pylab import *
from numpy.random import *
#Graph tool
from graph_tool.all import *
# Empezamos con una grafica vacia
g = Graph()
g.set_directed(False)
# Creamos "property maps" para los vertices y las aristas
v_age = g.new_vertex_property("int")
e_age = g.new_edge_property("int")
# La cantidad de nodos de la red
N = 10
# Generamos un vertice
v = g.add_vertex()
v_age[v] = 0
# Creamos una lista con los vertices que nos diga la cantidad de veces
# que ha aparecido el vertice en una adicion, lo que se traduce en la
# probabilidad que sea elegido para conectarse
vlist = [v]
for i in range(1, N):
# creando los vertices
v = g.add_vertex()
v_age[v] = i
# generamos una arista basandonos en una eleccion aleatoria de
# un elemento en vlist
i = randint(0, len(vlist))
target = vlist[i]
# agregamos la arista
e = g.add_edge(v, target)
e_age[e] = i
# ponemos target y v en la lista. Asi la frecuencia de target
# aumenta por haber sido elegido
vlist.append(target)
vlist.append(v)
# hagamos una caminata aleatoria sobre la red
v = g.vertex(randint(0, g.num_vertices()))
while True:
print "vertex:", int(v), "degree:", v.out_degree(), "age:", v_age[v]
if v.out_degree() < 0:
print("No hay a donde mas ir :(")
break
n_list = []
for w in v.out_neighbours():
n_list.append(w)
v = n_list[randint(0, len(n_list))]
# guardamos la grafica para escribir a un archivo
g.vertex_properties["age"] = v_age # hay que almacenar las propiedes en la grafica,
g.edge_properties["age"] = e_age # para que tambien se escriban
g.save("price.xml.gz") # escribiendo..
# grafiquemos la distribucion de los grados
hist = vertex_hist(g,"out")
y = hist[0]
err = sqrt(hist[0])
err[err >= y] = y[err >= y] - 1e-2
figure(figsize=(6,4))
errorbar(hist[1][:-1], hist[0], fmt="o", yerr=err,
label="grados")
gca().set_yscale("log")
gca().set_xscale("log")
gca().set_ylim(1e-1, 1e5)
gca().set_xlim(0.8, 1e3)
subplots_adjust(left=0.2, bottom=0.2)
xlabel("$k$")
ylabel("$NP(k)$")
tight_layout()
# savefig("price-deg-dist.pdf")
savefig("price-deg-dist.png")
######
raw_input("Presione ENTER para continuar..")
# Dibujando el grafo
g = load_graph("price.xml.gz")
age = g.vertex_properties["age"]
#pos = sfdp_layout(g)
#graph_draw(g, pos, output_size=(1000, 1000), vertex_color=[1,1,1,0],
# vertex_fill_color=age, vertex_size=1, edge_pen_width=1.2,
# vcmap=matplotlib.cm.gist_heat_r, output="price.png")