Skip to content

Instantly share code, notes, and snippets.

@tonicanada
Last active January 15, 2022 15:39
Show Gist options
  • Save tonicanada/5f689540d30e51a9837d660b801ef9cb to your computer and use it in GitHub Desktop.
Save tonicanada/5f689540d30e51a9837d660b801ef9cb to your computer and use it in GitHub Desktop.
Brute-force python code to check isomorphism between graphs
import itertools
import numpy as np
def get_graph_order(adj_matrix):
if len(adj_matrix) != len(adj_matrix[0]):
return -1
else:
return len(adj_matrix)
def get_degree_sequence(adj_matrix):
degree_sequence = []
for vertex in range(len(adj_matrix)):
degree_sequence.append(sum(adj_matrix[vertex]))
degree_sequence.sort(reverse=True)
return degree_sequence
def get_all_vertex_permutations(adj_matrix):
if get_graph_order(adj_matrix) > 8:
print("This function is too inefficient for graph order > 8")
return -1
all_adj_matrix = []
idx = list(range(len(adj_matrix)))
possible_idx_combinations = [
list(i) for i in itertools.permutations(idx, len(idx))
]
for idx_comb in possible_idx_combinations:
a = adj_matrix
a = a[idx_comb]
a = np.transpose(np.transpose(a)[idx_comb])
all_adj_matrix.append({
"perm_vertex":
idx_comb,
"adj_matrix":
a
})
return all_adj_matrix
def brute_force_test_graph_isomporphism(adj_1, adj_2):
degree_sequence_1 = get_degree_sequence(adj_1)
degree_sequence_2 = get_degree_sequence(adj_2)
if get_graph_order(adj_1) != get_graph_order(adj_1):
return False
elif np.array_equal(degree_sequence_1, degree_sequence_2) == False:
return False
else:
for adj_matrix in list(
map(lambda matrix: matrix["adj_matrix"],
get_all_vertex_permutations(adj_2))):
if np.array_equal(adj_1, adj_matrix) == True:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment