-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive.py
61 lines (49 loc) · 1.21 KB
/
naive.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
from itertools import permutations
import random
class Graph:
def __init__(self, n, m=0, edges=[]):
self.num_nodes = n
self.num_edges = m
self.graph = [[0] * self.num_nodes for _ in range(self.num_nodes)]
for edge in edges:
self.add_edge(edge[0], edge[1])
def add_edge(self, u, v):
self.graph[u][v] = 1
def isomorphic(G, H):
if (G.num_nodes != H.num_nodes or G.num_edges != H.num_edges):
return False
n = G.num_nodes
numbers = list(range(0, n))
perms = permutations(numbers)
# check every every bijective mapping of nodes
for perm in perms:
bij = list(perm)
f = lambda i: bij[i]
found = True
for i in range(n):
for j in range(n):
if (G.graph[i][j] != H.graph[f(i)][f(j)]):
found = False
if (found == True):
return True
return False
def __main__():
line = input().split()
n = int(line[0])
m = int(line[1])
G = Graph(n, m)
for i in range(m):
line = input().split()
G.add_edge(int(line[0]), int(line[1]))
line = input().split()
n = int(line[0])
m = int(line[1])
H = Graph(n, m)
for i in range(m):
line = input().split()
H.add_edge(int(line[0]), int(line[1]))
if (isomorphic(G, H) == True):
print("YES", end='')
else:
print("NO", end='')
__main__()