-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (54 loc) · 1.58 KB
/
Copy pathmain.py
File metadata and controls
65 lines (54 loc) · 1.58 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 26 13:52:43 2019
@author: RicardoChCz
"""
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from numpy.random import randint
import imageio
from utiles import draw_pretty_graph
def generate(G):
"""
Simula una caminata aleatoría simple en G, y obtiene un árbol usando el
algoritmo generate.
Input: Gráfica G (objeto en python de networkX)
Output: Árbol T (objeto en python de networkX)
"""
#punto inicial
u = randint(len(G.nodes))
visited = [u]
T = nx.empty_graph(n)
while len(visited) != len(G.nodes):
#Obtener v vecino aleatorio de u
N=list(G.neighbors(u))
v = N[randint(len(N))]
if v not in visited:
visited.append(v)
T.add_edge(u,v)
#Actualiza a nuevo punto
u=v
print(T)
return T
if __name__ == "__main__":
"""
Generar gráfica aleatoria con ER y graficar un árbol maximal con el algorit-
mo Generate.
"""
n = 10
p = 0.5
m = 25
G = nx.gnm_random_graph(n, m)
filenames = []
k=15
for i in range(0,k):
draw_pretty_graph(G, generate(G), colors.to_hex(plt.cm.viridis(i/float(k))))
plt.savefig('figures/RandomTree'+str(i)+'.png')
filenames.append('figures/RandomTree'+str(i)+'.png')
plt.show()
images = []
for filename in filenames:
images.append(imageio.imread(filename))
imageio.mimsave('figures/sample.gif', images, duration=1)