-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphCondNum.py
170 lines (151 loc) · 3.94 KB
/
GraphCondNum.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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 21 13:31:59 2018
@author: maxxx971
"""
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from scipy import linalg
def condition_number(C):
"""
compute condition number of graph
C is the incidence matrix of size N by M
N is number of nodes and M is number of edges
"""
D = np.diag(C.sum(axis=1))
Einv = np.diag(1/C.sum(axis=0))
M1 = C.dot(Einv).dot(C.T)
M2 = D - M1
# find the largest and smallest eigvalues
N = C.shape[0]
Lbd = linalg.eigh(M1, eigvals_only=True, eigvals=(N-1, N-1))[0]
lbd = linalg.eigh(M2, eigvals_only=True, eigvals=(1, 1))[0]
return Lbd / lbd
# number of nodes of path graph
N = 11
G = nx.path_graph(N)
C = np.asarray(nx.incidence_matrix(G).todense())
Einv = np.diag(1 / C.sum(axis=0))
D = np.diag(C.sum(axis=1))
M1 = C.dot(Einv).dot(C.T)
M2 = D - M1
L = linalg.eigh(M1, eigvals_only=True, eigvals=(N-1, N-1))[0]
l = linalg.eigh(M2, eigvals_only=True, eigvals=(1, 1))[0]
Rd = L / l
print('D-ratio', Rd)
# H-CADMM
M = int((N-1) / 2) # number of edges
e_i = np.zeros((N,))
e_i[0:3] = 1
Ch = np.zeros((N, M))
for i in range(M):
Ch[:, i] = e_i
e_i = np.roll(e_i, 2)
Ehinv = np.diag(1 / Ch.sum(axis=0))
Dh = np.diag(Ch.sum(axis=1))
Mh1 = Ch.dot(Ehinv).dot(Ch.T)
Mh2 = Dh - Mh1
Lh = linalg.eigh(Mh1, eigvals_only=True, eigvals=(N-1, N-1))[0]
lh = linalg.eigh(Mh2, eigvals_only=True, eigvals=(1, 1))[0]
Rh = Lh / lh
print('H-ratio', Rh)
print('Acceleration: ', Rd / Rh)
M2 *= 2
Mh2 *= 3
#
#print("l / lh: ", l/lh)
#print("L / Lh: ", L/Lh)
#l_diff = np.abs(lh * .5 - l)
#L_diff = np.abs(Lh*1.2 - L)
#print('l relate to n', l_diff * N)
#print('L relate to n', L_diff * N)
def f(k):
return 1 - np.cos(np.pi * k / N)
#################################################################
# line graph
# D-CADMM
#G = nx.path_graph(N)
#C = np.asarray(nx.incidence_matrix(G).todense())
#k_1 = condition_number(C)
#
## H-CADMM
#M = int((N-1) / 2) # number of edges
#
#e_i = np.zeros((N,))
#e_i[0:3] = 1
#Ch = np.zeros((N, M))
#for i in range(M):
# Ch[:, i] = e_i
# e_i = np.roll(e_i, 2)
#
#k_2 = condition_number(Ch)
#
#print('\n=======================')
#print('Line Graph')
#print('D-CADMM: ', k_1)
#print('H-CADMM: ', k_2)
#print('Acceleration ratio: ', k_1 / k_2)
#print('=======================\n')
#################################################################
# star graph
#G = nx.star_graph(N-1)
#
## D-CADMM
#C = np.asarray(nx.incidence_matrix(G).todense())
#k_1 = condition_number(C)
#
## H-CADMM
#Ch = np.ones((N-1,1))
#k_2 = condition_number(Ch)
#
#print('\n=======================')
#print('Star Graph')
#print('D-CADMM: ', k_1)
#print('H-CADMM: ', k_2)
#print('Acceleration ratio: ', k_1 / k_2)
#print('=======================\n')
#################################################################
# cycle graph
# D-CADMM
#Nc = 9 + 1
#G = nx.cycle_graph(Nc)
#C = np.asarray(nx.incidence_matrix(G).todense())
#k_1 = condition_number(C)
#
## H-CADMM
#M = int(Nc / 2) # number of edges
#
#e_i = np.zeros((Nc,))
#e_i[0:3] = 1
#Ch = np.zeros((Nc, M))
#for i in range(M):
# Ch[:, i] = e_i
# e_i = np.roll(e_i, 2)
#
#k_2 = condition_number(Ch)
#
#print('\n=======================')
#print('Cycle Graph')
#print('D-CADMM: ', k_1)
#print('H-CADMM: ', k_2)
#print('Acceleration ratio: ', k_1 / k_2)
#print('=======================\n')
#
##################################################################
## complete graph
## D-CADMM
#G = nx.complete_graph(N)
#C = np.asarray(nx.incidence_matrix(G).todense())
#k_1 = condition_number(C)
#
## H-CADMM
#Ch = np.ones((N, 1))
#k_2 = condition_number(Ch)
#
#print('\n=======================')
#print('Complete Graph')
#print('D-CADMM: ', k_1)
#print('H-CADMM: ', k_2)
#print('Acceleration ratio: ', k_1 / k_2)
#print('=======================\n')