-
Notifications
You must be signed in to change notification settings - Fork 0
/
genMina3.py
executable file
·109 lines (92 loc) · 2.61 KB
/
genMina3.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
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
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import scipy as sp
import numpy as np
import networkx as nx
import random as rnd
import matplotlib.pyplot as plt
import math
import sys
def nodeMaxDegree(G):
degree=0
for n in G.nodes():
if nx.degree(G,n)> degree :
degree=nx.degree(G,n)
node=n
return node
def nodeSolo(G,n):
solo=False
for test in G.neighbors(n):
if nx.degree(G,test)==1 :
solo=True
return solo
def nodeNear(G,delta):
f=10000
pos=nx.get_node_attributes(G,'pos')
for n in pos:
x1,y1=pos[n]
for i in G.neighbors(n):
x2,y2=pos[i]
d=(x1-x2)**2+(y1-y2)**2
if d<delta:
f=n #f=i
return f
return f
def network(G,pos,seq):
plt.clf()
nx.draw_networkx_nodes(G,pos,node_color='#FFFF00')
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos)
plt.axis('off')
plt.savefig("./network%d.png" % seq, dpi = 300)
plt.show()
return True
def main():
LOG = True
if (len(sys.argv) != 5):
print "ERROR: genMina3.py <nodes> <radius> <delta> <maxdegree>"
sys.exit(1)
NMAX = int(sys.argv[1])
RAIO = float(sys.argv[2])
delta = float(sys.argv[3])
degree = float(sys.argv[4])
#NMAX=40
#RAIO=0.1
ALCANCE=250
c=0
run=True
first=True
while run:
c+=1
G=nx.random_geometric_graph(NMAX,RAIO,2)
while not nx.is_connected(G):
if first:
RAIO=RAIO+.005
G=nx.random_geometric_graph(NMAX,RAIO,2)
if LOG: print c,"- Radius: Graph is not full connected R=",RAIO
first=False
#Remove vizinhos que estejam demasiado pertoc
candidate=nodeNear(G,delta)
while not candidate==10000 :
G.remove_node(candidate)
candidate=nodeNear(G,delta)
if nx.is_connected(G):
#Remove no que tem mais vizinhos
candidate=nodeMaxDegree(G)
while nx.degree(G,candidate)> degree :
G.remove_node(candidate)
candidate=nodeMaxDegree(G)
if nx.is_connected(G):
run=False
else:
if LOG: print c,"- MaxDegree: Split Graph"
else:
if LOG: print c,"- nodeNear: Split Graph"
pos=nx.get_node_attributes(G,'pos')
network(G,pos,5)
if LOG: print "Raio =",RAIO
if LOG: print "NMAX =",NMAX
if LOG: print "Nodes =",nx.number_of_nodes(G)
if __name__ == "__main__":
print main()