-
Notifications
You must be signed in to change notification settings - Fork 0
/
STDP.py
99 lines (70 loc) · 2.11 KB
/
STDP.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
import networkx as nx
import Greedy
import random
import os
class SpanningForest:
def __init__(self, graph: nx.DiGraph):
self.G = graph
def spanning_tree(self) -> nx.DiGraph:
node = self.G.nodes()
self.G.add_node('root')
for n in node:
self.G.add_edge('root', n)
# sapnning tree
InTree = {}
Tree = {}
node = self.G.nodes()
for i in node:
InTree[i] = False
Tree['root'] = 'nil'
InTree['root'] = True
for i in self.G.nodes():
u = i
while not InTree[u]:
Tree[u] = random.choice(list(self.G.predecessors(u)))
# print(list(self.G.predecessors(u)))
u = Tree[u]
print(u)
u = i
while not InTree[u]:
InTree[u] = True
u = Tree[u]
# generate the spanning tree
spanning = nx.DiGraph()
spanning.add_nodes_from(self.G.nodes())
for k in Tree.keys():
if not k == 'root':
spanning.add_edge(Tree[k], k)
return spanning
def output(self, path):
file = open(path, 'w')
spanning = self.spanning_tree()
for e in spanning.edges():
file.writelines(str(e[0]) + ' ' + str(e[1]) + '\n')
file.close()
#G = nx.DiGraph([(1, 2), (2, 3), (1,3), (3,4), (2,4), (5,1), (2,5), (6,3), (7, 4), (5,7), (1,8), (8,5)])
#s = SpanningForest(G)
#s.output('')
'''
for i in range(0, 5):
file = open('./SF/' + 'SF_' + str(i) + '.txt')
g = nx.DiGraph()
g.add_nodes_from(range(1, 1000))
for l in file.readlines():
# print(l)
nodes = l.replace('\n', '').split(' ')
g.add_edge(int(nodes[0]), int(nodes[1]))
for j in range(0, 100):
span = SpanningForest(g)
span.output('SF_' + str(i) + '_tree_' + str(j) + '.txt')
file.close()
'''
'''
diam = 0.0
radius = 0.0
for i in range(0,10):
g = nx.connected_watts_strogatz_graph(1000,4,0.4)
diam += nx.diameter(g)
radius += nx.radius(g)
print(diam/10, radius/10)
'''