-
Notifications
You must be signed in to change notification settings - Fork 1
/
DenseGraph.py
161 lines (107 loc) · 2.59 KB
/
DenseGraph.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 20 22:10:28 2017
@author: oem
"""
import networkx as nx
import matplotlib.pyplot as plt
import random
import math
import numpy as np
import collections
import scipy.stats as stats
G=nx.florentine_families_graph()
pos=nx.get_node_attributes(G,'pos')
p=nx.density(G)
pos=nx.spring_layout(G,k=0.1,iterations=50)
nx.draw_networkx(G,pos,with_labels=True)
plt.axis('off')
plt.show()
edges=nx.to_edgelist(G)
nodes=nx.nodes(G)
print(len(nodes))
print(len(edges))
print(p)
p=nx.density(G)
d=nx.degree(G)
dseq=sorted(d.values(),reverse=True)
degreecount=collections.Counter(dseq)
fit=stats.norm.pdf(dseq,np.mean(dseq),np.std(dseq))
plt.plot(dseq,fit,'-o')
plt.hist(dseq,normed=True)
plt.show()
deg,cnt =zip(*degreecount.items())
fig,ax=plt.subplots()
plt.bar(deg,cnt,width=0.8,color='b')
summ=0
for node in nodes:
summ=summ+d[node]
avg=math.floor(summ/len(nodes))
print(avg)
plt.title("Degree Histogram")
plt.ylabel("Count")
plt.xlabel("Degree")
ax.set_xticks([d+0.4 for d in deg])
ax.set_xticklabels(deg)
plt.axes([0.4, 0.4, 0.5, 0.5])
q=nx.degree_histogram(G)
dlist=[]
for key,value in d.items():
temp=[key,value]
dlist.append(temp)
c=np.argmax(q)
print(d)
maxi=max(d,key=d.get)
print(maxi,d[maxi])
minim=min(d,key=d.get)
print(minim,d[minim])
def finminre(graph):
visitnodes=[]
mini=None
val=0
while (1>0):
d=nx.degree(graph)
mini=min(d,key=d.get)
print(mini,d[mini])
print(nodes)
if avg <c:
val=avg
else:
val=c
print(val)
if (d[mini]==avg):
print("reached")
break
else:
nodes.remove(mini)
visitnodes.append(mini)
print(len(nodes))
return(nodes,mini)
def remegdes(edges,nodes):
unwanted=[]
for edge in edges:
s,d,_=edge
if s==mini or d==mini :
unwanted.append(edge)
for item in unwanted:
edges.remove(item)
print(len(edges))
return(edges)
def creatnewgraph(edges):
H= nx.Graph()
H.add_edges_from(edges)
pos=nx.spring_layout(G)
nx.draw_networkx(H,pos,with_labels=True)
plt.axis('off')
plt.show()
print(nx.density(G))
edges=nx.to_edgelist(H)
print(nx.degree(H))
return edges,H
for i in range(0,33):
edges,graph=creatnewgraph(edges)
graph
nodes,mini=finminre(graph)
remegdes(edges,mini)
print(nx.density(graph))