Last active
December 24, 2018 04:08
-
-
Save JSeam2/fa65188ef35e45373bce391ed821e655 to your computer and use it in GitHub Desktop.
Generate adjacency matrix given edge rule, edge rule = {{v_i, v_j} | i - j === 1 or -1 or 2 or -2 (mod 9)}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
gen adjacency matrix | |
edge rule = {{v_i, v_j} | i - j === 1 or -1 or 2 or -2 (mod 9)} | |
Format | |
1 2 3 4 ... | |
1 | |
2 | |
3 | |
4 | |
. | |
. | |
. | |
""" | |
import numpy as np | |
def gen_edge(v): | |
""" | |
Given the list of vertices generate a list of sets of edges | |
""" | |
e = [] | |
for i in v: | |
for j in v: | |
val = (i - j) % 9 | |
if val == 1 or val == 2: | |
e.append((i,j)) | |
if val == 8 or val == 7: | |
e.append((i,j)) | |
return e | |
def gen_array(e, size=9): | |
""" | |
Generates array base on a list of set | |
""" | |
arr = np.zeros((size, size)) | |
for el in e: | |
i = el[0] - 1 | |
j = el[1] - 1 | |
arr[i][j] = 1 | |
return arr | |
def main(): | |
v = [i for i in range (1,10)] | |
e = gen_edge(v) | |
adj = gen_array(e) | |
print(adj) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment