-
Notifications
You must be signed in to change notification settings - Fork 4
/
angluin_expander.py
78 lines (51 loc) · 1.95 KB
/
angluin_expander.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
# Dana Angluin's algorithm for generating expander graphs
# Author: Vlad Burca
# Date: November 23, 2013
# Updated: November 24, 2013
# ANGLUIN EXPANDERS
# Description:
# Generates the H matrix from matrices A and B.
# H is an adjacency matrix constructed by adding edges between nodes in A and B
# on the following rules:
# (x, y) of A is connected to the following nodes of B:
# (x, y)
# (x + y, y)
# (y + 1, -x)
import numpy
import helpers
from methods import ANGLUIN
NAME = '[' + ANGLUIN.upper() + ']'
K = 3
def GENERATE_ANGLUIN_EXPANDERS(size, A_indices, n, EPSILON):
size_H = 2 * size
print NAME + " Generating H (adjacency list matrix) of size " + str(size_H) + " x " + str(K) + " ... "
H = numpy.empty(shape=(size_H, K), dtype=numpy.int32) # Generate H, empty adjacency list matrix
for row in A_indices:
for element_index in row: # Get the tuple index from the matrix of indices (A)
x0 = element_index / n # Grab first value
y0 = element_index % n # Grab second value
i = element_index # Grab the index of the (x0, y0) element
# connect to (x, y) in B
x = x0
y = y0
j = (x * n + y % n) + size # add the shift in the H indexing
H[i][0] = j # node with index i is connected to node with index j
H[j][0] = i # vice-versa
# connect to (x + y, y) in B
x = (x0 + y0) % n
y = y0
j = (x * n + y % n) + size
H[i][1] = j
H[j][1] = i
# connect to (y + 1, -x) in B
x = (y0 + 1) % n
y = (-x0) % n
j = (x * n + y % n) + size
H[i][2] = j
H[j][2] = i
print NAME + " Generated adjacency list matrix H."
print NAME + " Calculating second highest eigenvalue of H ... "
eigenvalue = helpers.generate_eigenvalue(H, size_H, K, EPSILON, NAME)
print NAME + " Calculated second highest eigenvalue of H."
helpers.write_result(NAME, size_H, K, eigenvalue)
helpers.cleanup(".aux")