-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpielman_Sparse.py
206 lines (183 loc) · 6.09 KB
/
Spielman_Sparse.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import random as ran
import numpy as np
import scipy.sparse as sparse
from EffRApprox import Mtrx_Elist
# from virtualenvs.AdaptiveAlgo import Adapt1
# from virtualenvs.Spielman_EffR import Mtrx_Elist
# Normalize probs such that sum(probs)=1
# Input:
# P - list of probs
# Output:
# P_n - list of probs' that sum to 1
def normprobs(P):
prob_fac = 1 / sum(P)
P_n = [prob_fac * p for p in P]
return np.array(P_n)
# Create a list of edge R_eff
# Input:
# R - Array of R_effs
# adj - Adj matrix
# Output:
# R_list - list of edge R_eff
# NOT NEEDED WITH NEW EFFR CODE - MAY, 2021
# def EffR_List(R, adj):
# R_list = []
# adj = np.triu(adj)
# R = np.triu(R)
# for i in range(len(adj)):
# for j in range(len(adj)):
# if adj[i][j] > 0:
# R_list.append(R[i][j])
# return R_list
# Create a effective resistance sparsifer
# From Spielman and Srivastava 2008
# Input:
# adj - Adj matrix
# q - number of samples
# R - Matrix of effective resistances (other types of edge importance in the future, possibly)
# Output:
# H - effective resistance sparsifer adj matrix
def Spl_EffRSparse(n, E_list, weights, q, effR, seed=None):
ran.seed(seed)
P = []
for i in range(len(E_list)):
w_e = weights[i]
R_e = effR[i]
P.append((w_e * R_e) / (n - 1))
Pn = np.array(normprobs(P))
C = ran.choices(list(zip(E_list, weights, Pn)), Pn, k=q)
H = np.zeros(shape=(n, n))
for x in range(q):
e, w_e, p_e = C[x][0], C[x][1], C[x][2]
H[e[0]][e[1]] += w_e / (q * p_e)
return H + np.transpose(H)
# Create a effective resistance sparsifer with sparse matrix
# From Spielman and Srivastava 2008
# Input:
# adj - Adj matrix
# q - number of samples
# R - Matrix of effective resistances (other types of edge importance in the future, possibly)
# Output:
# H - effective resistance sparsifer adj matrix
def Spl_EffRSparse_s(n, E_list, weights, q, effR, seed=None):
ran.seed(seed)
P = []
H_list = np.zeros((len(E_list),3))
for i in range(len(E_list)):
w_e = weights[i]
R_e = effR[i]
P.append((w_e * R_e) / (n - 1))
Pn = np.array(normprobs(P))
C = ran.choices(list(zip(E_list, weights, Pn, range(len(E_list)))), Pn, k=q)
for x in range(q):
e, w_e, p_e, i = C[x][0], C[x][1], C[x][2], C[x][3]
H_list[i,2] += w_e / (q * p_e)
H_list[i,0:2] = e[0], e[1]
H = sparse.csr_matrix((H_list[:,2], (E_list[:,0], E_list[:,1])), shape=(n,n))
return H + H.transpose()
# Create a random uniform sparsifier
# Input:
# adj - Adj matrix
# q - number of samples
# Output:
# H
def UniSampleSparse(n, E_list, weights, q, seed=None):
ran.seed(seed)
Pn = [1 / len(E_list)] * len(E_list)
C = ran.choices(list(zip(E_list, weights, Pn)), Pn, k=q)
H = np.zeros(shape=(n, n))
for x in range(q):
e = C[x][0]
w_e = C[x][1]
p_e = C[x][2]
H[e[0]][e[1]] += w_e / (q * p_e)
return H + H.transpose()
# Create a random uniform sparsifier
# Input:
# adj - Adj matrix
# q - number of samples
# Output:
# H
def UniSampleSparse_s(n, E_list, weights, q, seed=None):
ran.seed(seed)
Pn = [1 / len(E_list)] * len(E_list)
C = ran.choices(list(zip(E_list, weights, Pn, range(len(E_list)))), Pn, k=q)
H_list = np.zeros((len(E_list),3))
for x in range(q):
e, w_e, p_e, i = C[x][0], C[x][1], C[x][2], C[x][3]
H_list[i,2] += w_e / (q * p_e)
H_list[i,0:2] = e[0], e[1]
H = sparse.csr_matrix((H_list[:, 2], (E_list[:, 0], E_list[:, 1])), shape=(n, n))
return H + H.transpose()
# Create a sparsifier with edge weights
# Input:
# adj - Adj matrix
# q - number of samples
# Output:
# H
def WeightSparse_s(n, E_list, weights, q, seed=None):
ran.seed(seed)
Pn = normprobs(weights)
C = ran.choices(list(zip(E_list, weights, Pn, range(len(E_list)))), Pn, k=q)
H_list = np.zeros((len(E_list),3))
for x in range(q):
e, w_e, p_e, i = C[x][0], C[x][1], C[x][2], C[x][3]
H_list[i,2] += w_e / (q * p_e)
H_list[i,0:2] = e[0], e[1]
H = sparse.csr_matrix((H_list[:, 2], (E_list[:, 0], E_list[:, 1])), shape=(n, n))
return H + np.transpose(H)
def Thresh(n, E_list, weights, per):
m = int(np.ceil(per * len(weights)))
n_weights = [0] * len(weights)
weights = list(enumerate(weights))
weights = sorted(weights, key=lambda tup: tup[1], reverse=True)
weights = weights[0:m]
for i in weights:
n_weights[i[0]] = i[1]
H = sparse.csr_matrix((n_weights, (E_list[:,0], E_list[:,1])), shape=(n,n))
return H
# Create a S-S sparsifier with a specified number of edges.
# Input:
# adj - Adj matrix
# e - number of edges
# Output:
# H - effective resistance sparsifer adj matrix
def SSEdge(adj, R, e):
H = np.zeros(shape=(len(adj), len(adj)))
r_tick = int(1.25 * e)
while len(Mtrx_Elist(H)[0]) != e:
H = Spl_EffRSparse(adj, r_tick, R)
if len(Mtrx_Elist(H)[0]) > e:
r_tick = r_tick - (len(Mtrx_Elist(H)[0]) - e)
if len(Mtrx_Elist(H)[0]) < e:
r_tick = r_tick + (e - len(Mtrx_Elist(H)[0]))
print(len(Mtrx_Elist(H)[0]))
return H
# Create a Uni sparsifier with a specificed number of edges.
# Input:
# adj - Adj matrix
# e - number of edges
# Output:
# H - uni sparsifer adj matrix
def UniEdge(adj, e):
H = np.zeros(shape=(len(adj), len(adj)))
r_tick = int(0.9 * e)
while len(Mtrx_Elist(H)[0]) != e:
H = UniSampleSparse(adj, r_tick)
if len(Mtrx_Elist(H)[0]) > e:
r_tick = r_tick - (len(Mtrx_Elist(H)[0]) - e)
if len(Mtrx_Elist(H)[0]) < e:
r_tick = r_tick + (e - len(Mtrx_Elist(H)[0]))
print(len(Mtrx_Elist(H)[0]))
return H
# def AdaptEdge(adj, R, T, e):
# H = np.zeros(shape=(len(adj), len(adj)))
# r_tick = int(1.5 * e)
# while len(Mtrx_Elist(H)[0]) != e:
# H = Adapt1(adj, r_tick, R, T)
# if len(Mtrx_Elist(H)[0]) > e:
# r_tick = int(r_tick - (len(Mtrx_Elist(H)[0]) - (e * 1/T)))
# if len(Mtrx_Elist(H)[0]) < e:
# r_tick = int(r_tick + (e - (len(Mtrx_Elist(H)[0]) * (1/T))))
# print(len(Mtrx_Elist(H)[0]))
# return H