-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance3.py
46 lines (39 loc) · 1.21 KB
/
instance3.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
import networkx as nx
from random import random
def instance3():
lst = []
count = 0
size = []
leaves = 0
while(count < 100):
x = int(random()*100)%10
if(count + x + 1 > 100):
break
lst.append(nx.star_graph(x))
count = count + x + 1
r = 100 - 1 - count
lst.append(nx.star_graph(r-1))
L = nx.Graph()
for item in lst:
leaves += nx.number_of_nodes(item)-1
size.append(nx.number_of_nodes(item))
L = nx.disjoint_union(L,item)
L.add_node(99)
totaling = 0
for item in size:
L.add_edge(99, totaling)
totaling += item
for i in range(1500):
x = int(random()*100)
y = int(random()*100)
L.add_edge(x,y)
for n in L.nodes():
new = n + 100 + int(random()*1000)
while(new in L.nodes()):
new = n + 100 + int(random()*1000)
L = nx.relabel_nodes(L,{n:new})
print("Number of nodes in each star: " + str(size))
print("Total num of leaves: " + str(leaves))
print("Total num of edges: " + str(L.number_of_edges()))
print("ratio of leaves/edges: " + str(float(leaves)/L.number_of_edges()))
return L