-
Notifications
You must be signed in to change notification settings - Fork 0
/
genMina2.py
executable file
·123 lines (103 loc) · 3.12 KB
/
genMina2.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/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=i
break
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)
plt.show()
return True
def main():
LOG = True
#if (len(sys.argv) != 3):
# print "ERROR: genRandomGeorml <nodes> <raio> <delta>"
# sys.exit(1)
NMAX = int(sys.argv[1])
RAIO = float(sys.argv[2])
delta = float(sys.argv[3])
#NMAX=40
#RAIO=0.1
ALCANCE=250
c=0
first=True
while True:
G=nx.random_geometric_graph(NMAX,RAIO,2)
while not nx.is_connected(G):
c+=1
if first: RAIO=RAIO+.005
G=nx.random_geometric_graph(NMAX,RAIO,2)
if LOG: print c,"- Radius: Graph is not full connected"
first=False
pos=nx.get_node_attributes(G,'pos')
#network(G,pos,1)
#Remove vizinhos que estejam demasiado perto
while nodeNear(G,delta)<1000 :
G.remove_node(nodeNear(G,delta))
pos=nx.get_node_attributes(G,'pos')
#network(G,pos,2)
if nx.is_connected(G):
#Remove no que tem mais vizinhos
candidate=nodeMaxDegree(G)
while nx.degree(G,candidate)> 4 :
if not nodeSolo(G,candidate):
G.remove_node(candidate)
candidate=nodeMaxDegree(G)
#network(G,pos,3)
if nx.is_connected(G):
pos=nx.get_node_attributes(G,'pos')
#Remove
for n in G.neighbors(nodeMaxDegree(G)):
if nx.degree(G,n)== 2 :
degree=nx.degree(G,n)
node=n
print "Extra node=",n
if not nodeSolo(G,n): G.remove_node(n)
#break
if nx.is_connected(G):
if LOG: print "Start:",NMAX,"End:",G.number_of_nodes()
pos=nx.get_node_attributes(G,'pos')
network(G,pos,4)
break
else:
c+=1
if LOG: print c,"- MaxDegree: Split Graph"
else:
c+=1
if LOG: print c,"- NearNode: Split Graph"
if __name__ == "__main__":
print main()