patternMatching.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. # coding: utf-8
  2. """
  3. Author: Sten Vercamman
  4. Univeristy of Antwerp
  5. Example code for paper: Efficient model transformations for novices
  6. url: http://msdl.cs.mcgill.ca/people/hv/teaching/MSBDesign/projects/Sten.Vercammen
  7. The main goal of this code is to give an overview, and an understandable
  8. implementation, of known techniques for pattern matching and solving the
  9. sub-graph homomorphism problem. The presented techniques do not include
  10. performance adaptations/optimizations. It is not optimized to be efficient
  11. but rather for the ease of understanding the workings of the algorithms.
  12. The paper does list some possible extensions/optimizations.
  13. It is intended as a guideline, even for novices, and provides an in-depth look
  14. at the workings behind various techniques for efficient pattern matching.
  15. """
  16. import collections
  17. import itertools
  18. class PatternMatching(object):
  19. """
  20. Returns an occurrence of a given pattern from the given Graph
  21. """
  22. def __init__(self, optimize=True):
  23. # store the type of matching we want to use
  24. self.optimize = optimize
  25. def matchNaive(self, pattern, vertices, edges, pattern_vertices=None):
  26. """
  27. Try to find an occurrence of the pattern in the Graph naively.
  28. """
  29. # allow call with specific arguments
  30. if pattern_vertices == None:
  31. pattern_vertices = pattern.vertices
  32. def visitEdge(pattern_vertices, p_edge, inc, g_edges, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  33. """
  34. Visit a pattern edge, and try to bind it to a graph edge.
  35. (If the first fails, try the second, and so on...)
  36. """
  37. for g_edge in g_edges:
  38. # only reckon the edge if its in edges and not visited
  39. # (as the graph might be a subgraph of a more complex graph)
  40. if g_edge not in edges.get(g_edge.type, []) or g_edge in visited_g_edges:
  41. continue
  42. if g_edge.type == p_edge.type and g_edge not in visited_g_edges:
  43. visited_p_edges[p_edge] = g_edge
  44. visited_g_edges.add(g_edge)
  45. if inc:
  46. p_vertex = p_edge.src
  47. else:
  48. p_vertex = p_edge.tgt
  49. if visitVertices(pattern_vertices, p_vertex, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  50. return True
  51. # remove added edges if they lead to no match, retry with others
  52. del visited_p_edges[p_edge]
  53. visited_g_edges.remove(g_edge)
  54. # no edge leads to a possitive match
  55. return False
  56. def visitEdges(pattern_vertices, p_edges, inc, g_edges, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  57. """
  58. Visit all edges of the pattern vertex (edges given as argument).
  59. We need to try visiting them for all its permutations, as matching
  60. v -e1-> first and v -e2-> second and v -e3-> third, might not result
  61. in a matching an occurrence of the pattern, but matching v -e2->
  62. first and v -e3-> second and v -e1-> third might.
  63. """
  64. def removePrevEdge(visitedEdges, visited_p_edges, visited_g_edges):
  65. """
  66. Undo the binding of the brevious edge, (the current bindinds do
  67. not lead to an occurrence of the pattern in the graph).
  68. """
  69. for wrong_edge in visitedEdges:
  70. # remove binding (pattern edge to graph edge)
  71. wrong_g_edge = visited_p_edges.get(wrong_edge)
  72. del visited_p_edges[wrong_edge]
  73. # remove visited graph edge
  74. visited_g_edges.remove(wrong_g_edge)
  75. for it in itertools.permutations(p_edges):
  76. visitedEdges = []
  77. foundallEdges = True
  78. for edge in it:
  79. if visited_p_edges.get(edge) == None:
  80. if not visitEdge(pattern_vertices, edge, inc, g_edges, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  81. # this did not work, so we have to undo all added edges
  82. # (the current edge is not added, as it failed)
  83. # we then can try a different permutation
  84. removePrevEdge(visitedEdges, visited_p_edges, visited_g_edges)
  85. foundallEdges = False
  86. break # try other order
  87. # add good visited (we know it succeeded)
  88. visitedEdges.append(edge)
  89. else:
  90. # we visited this pattern edge, and have the coressponding graph edge
  91. # if it is an incoming pattern edge, we need to make sure that
  92. # the graph target that is map from the pattern target
  93. # (of this incoming pattern edge, which has to be bound at this point)
  94. # has the graph adge as an incoming edge,
  95. # otherwise the graph is not properly connected
  96. if inc:
  97. if not visited_p_edges[edge] in visited_p_vertices[edge.tgt].incoming_edges:
  98. # did not work
  99. removePrevEdge(visitedEdges, visited_p_edges, visited_g_edges)
  100. foundallEdges = False
  101. break # try other order
  102. else:
  103. # analog for an outgoing edge
  104. if not visited_p_edges[edge] in visited_p_vertices[edge.src].outgoing_edges:
  105. # did not work
  106. removePrevEdge(visitedEdges, visited_p_edges, visited_g_edges)
  107. foundallEdges = False
  108. break # try other order
  109. # all edges are good, look no further
  110. if foundallEdges:
  111. break
  112. return foundallEdges
  113. def visitVertex(pattern_vertices, p_vertex, g_vertex, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  114. """
  115. Visit a pattern vertex, and try to bind it to the graph vertex
  116. (both are given as argument). A binding is successful if all the
  117. pattern vertex his incoming and outgoing edges can be bound
  118. (to the graph vertex).
  119. """
  120. if g_vertex in visited_g_vertices:
  121. return False
  122. # save visited graph vertex
  123. visited_g_vertices.add(g_vertex)
  124. # map pattern vertex to visited graph vertex
  125. visited_p_vertices[p_vertex] = g_vertex
  126. if visitEdges(pattern_vertices, p_vertex.incoming_edges, True, g_vertex.incoming_edges, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  127. if visitEdges(pattern_vertices, p_vertex.outgoing_edges, False, g_vertex.outgoing_edges, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  128. return True
  129. # cleanup, remove from visited as this does not lead to
  130. # an occurrence of the pttern in the graph
  131. visited_g_vertices.remove(g_vertex)
  132. del visited_p_vertices[p_vertex]
  133. return False
  134. def visitVertices(pattern_vertices, p_vertex, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  135. """
  136. Visit a pattern vertex and try to bind a graph vertex to it.
  137. """
  138. # if already matched or if it is a vertex not in the pattern_vertices
  139. # (second is for when you want to match the pattern partionally)
  140. if visited_p_vertices.get(p_vertex) != None or p_vertex not in pattern_vertices.get(p_vertex.type, set()):
  141. return True
  142. # try visiting graph vertices of same type as pattern vertex
  143. for g_vertex in vertices.get(p_vertex.type, []):
  144. if g_vertex not in visited_g_vertices:
  145. if visitVertex(pattern_vertices, p_vertex, g_vertex, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  146. return True
  147. return False
  148. visited_p_vertices = {}
  149. visited_p_edges = {}
  150. visited_g_vertices = set()
  151. visited_g_edges = set()
  152. # for loop is need for when pattern consists of multiple not connected structures
  153. allVertices = []
  154. for _, p_vertices in pattern_vertices.items():
  155. allVertices.extend(p_vertices)
  156. foundIt = False
  157. for it_p_vertices in itertools.permutations(allVertices):
  158. foundIt = True
  159. for p_vertex in it_p_vertices:
  160. if not visitVertices(pattern_vertices, p_vertex, visited_p_vertices, visited_p_edges, visited_g_vertices, visited_g_edges, vertices, edges):
  161. foundIt = False
  162. # reset visited
  163. visited_p_vertices = {}
  164. visited_p_edges = {}
  165. visited_g_vertices = set()
  166. visited_g_edges = set()
  167. break
  168. if foundIt:
  169. break
  170. if foundIt:
  171. return (visited_p_vertices, visited_p_edges)
  172. else:
  173. return None
  174. def createAdjacencyMatrixMap(self, graph, pattern):
  175. """
  176. Return adjacency matrix and the order of the vertices.
  177. """
  178. matrix = collections.OrderedDict() # { vertex, (index, [has edge from index to pos?]) }
  179. # contains all vertices we'll use for the AdjacencyMatrix
  180. allVertices = []
  181. if self.optimize:
  182. # insert only the vertices from the graph which have a type
  183. # that is present in the pattern
  184. for vertex_type, _ in pattern.vertices.items():
  185. graph_vertices = graph.vertices.get(vertex_type)
  186. if graph_vertices != None:
  187. allVertices.extend(graph_vertices)
  188. else:
  189. # we will not be able to find the pattern
  190. # as the pattern contains a vertex of a certain type
  191. # that is not present in the host graph
  192. return False
  193. else:
  194. # insert all vertices from the graph
  195. for _, vertices in graph.vertices.items():
  196. allVertices.extend(vertices)
  197. # create squared zero matrix
  198. index = 0
  199. for vertex in allVertices:
  200. matrix[vertex] = (index, [False] * len(allVertices))
  201. index += 1
  202. for _, edges in graph.edges.items():
  203. for edge in edges:
  204. if self.optimize:
  205. if edge.tgt not in matrix or edge.src not in matrix:
  206. # skip adding edge if the target or source type
  207. # is not present in the pattern
  208. # (and therefor not added to the matrix)
  209. continue
  210. index = matrix[edge.tgt][0]
  211. matrix[edge.src][1][index] = True
  212. AM = []
  213. vertices_order = []
  214. for vertex, row in matrix.items():
  215. AM.append(row[1])
  216. vertices_order.append(vertex)
  217. return AM, vertices_order
  218. def matchVF2(self, pattern, graph):
  219. class VF2_Obj(object):
  220. """
  221. Structor for keeping the VF2 data.
  222. """
  223. def __init__(self, len_graph_vertices, len_pattern_vertices):
  224. # represents if n-the element (h[n] or p[n]) matched
  225. self.core_graph = [False]*len_graph_vertices
  226. self.core_pattern = [False]*len_pattern_vertices
  227. # save mapping from pattern to graph
  228. self.mapping = {}
  229. # preference lvl 1
  230. # ordered set of vertices adjecent to M_graph connected via an outgoing edge
  231. self.N_out_graph = [-1]*len_graph_vertices
  232. # ordered set of vertices adjecent to M_pattern connected via an outgoing edge
  233. self.N_out_pattern = [-1]*len_pattern_vertices
  234. # preference lvl 2
  235. # ordered set of vertices adjecent to M_graph connected via an incoming edge
  236. self.N_inc_graph = [-1]*len_graph_vertices
  237. # ordered set of vertices adjecent to M_pattern connected via an incoming edge
  238. self.N_inc_pattern = [-1]*len_pattern_vertices
  239. # preference lvl 3
  240. # not in the above
  241. def findM(H, P, h, p, VF2_obj, index_M=0):
  242. """
  243. Find an isomorphic mapping for the vertices of P to H.
  244. This mapping is represented by a matrix M if,
  245. and only if M(MH)^T = P^T.
  246. This operates in a simular way as Ullmann. Ullmann has a predefind
  247. order for matching (sorted on most edges first). VF2's order is to
  248. first try to match the adjacency vertices connected via outgoing
  249. edges, then thos connected via incoming edges and then those that
  250. not connected to the currently mathed vertices.
  251. """
  252. def addOutNeighbours(neighbours, N, index_M):
  253. """
  254. Given outgoing neighbours (a row from an adjacency matrix),
  255. label them as added by saving when they got added (index_M
  256. represents this, otherwise it is -1)
  257. """
  258. for neighbour_index in range(0, len(neighbours)):
  259. if neighbours[neighbour_index]:
  260. if N[neighbour_index] == -1:
  261. N[neighbour_index] = index_M
  262. def addIncNeighbours(G, j, N, index_M):
  263. """
  264. Given the adjacency matrix, and the colum j, representing that
  265. we want to add the incoming edges to vertex j,
  266. label them as added by saving when they got added (index_M
  267. represents this, otherwise it is -1)
  268. """
  269. for i in range(0, len(G)):
  270. if G[i][j]:
  271. if N[i] == -1:
  272. N[i] = index_M
  273. def delNeighbours(N, index_M):
  274. """
  275. Remove neighbours that where added at index_M.
  276. If we call this function, we are backtracking and we want to
  277. remove the added neighbours from the just tried matching (n, m)
  278. pair (whiched failed).
  279. """
  280. for n in range(0, len(N)):
  281. if N[n] == index_M:
  282. N[n] = -1
  283. def feasibilityTest(H, P, h, p, VF2_obj, n, m):
  284. """
  285. Examine all the nodes connected to n and m; if such nodes are
  286. in the current partial mapping, check if each branch from or to
  287. n has a corresponding branch from or to m and vice versa.
  288. If the nodes and the branches of the graphs being matched also
  289. carry semantic attributes, another condition must also hold for
  290. F(s, n, m) to be true; namely the attributes of the nodes and of
  291. the branches being paired must be compatible.
  292. Another pruning step is to check if the nr of ext_edges between
  293. the matched_vertices from the pattern and its adjecent vertices
  294. are less than or equal to the nr of ext_edges between
  295. matched_vertices from the graph and its adjecent vertices.
  296. And if the nr of ext_edges between those adjecent vertices from
  297. the pattern and the not connected vertices are less than or
  298. equal to the nr of ext_edges between those adjecent vertices from
  299. the graph and its adjecent vertices.
  300. """
  301. # Get all neighbours from graph node n and pattern node m
  302. # (including n and m)
  303. neighbours_graph = {}
  304. neighbours_graph[h[n].type] = set([h[n]])
  305. neighbours_pattern = {}
  306. neighbours_pattern[p[m].type] = set([p[m]])
  307. # add all neihgbours of pattern vertex m
  308. for i in range(0, len(P)): # P is a nxn-matrix
  309. if (P[m][i] or P[i][m]) and VF2_obj.core_pattern[i]:
  310. neighbours_pattern.setdefault(p[i].type, set()).add(p[i])
  311. # add all neihgbours of graph vertex n
  312. for i in range(0, len(H)): # P is a nxn-matrix
  313. if (H[n][i] or H[i][n]) and VF2_obj.core_graph[i]:
  314. neighbours_graph.setdefault(h[i].type, set()).add(h[i])
  315. # take a coding shortcut,
  316. # use self.matchNaive function to see if it is feasable.
  317. # this way, we immidiatly test the semantic attributes
  318. if not self.matchNaive(pattern, pattern_vertices=neighbours_pattern, vertices=neighbours_graph, edges=graph.edges):
  319. return False
  320. # count ext_edges from core_graph to a adjecent vertices and
  321. # cuotn ext_edges for adjecent vertices and not matched vertices
  322. # connected via the ext_edges
  323. ext_edges_graph_ca = 0
  324. ext_edges_graph_an = 0
  325. # for all core vertices
  326. for x in range(0, len(VF2_obj.core_graph)):
  327. # for all its neighbours
  328. for y in range(0, len(H)):
  329. if H[x][y]:
  330. # if it is a neighbor and not yet matched
  331. if (VF2_obj.N_out_graph[y] != -1 or VF2_obj.N_inc_graph[y] != -1) and VF2_obj.core_graph[y]:
  332. # if we matched it
  333. if VF2_obj.core_graph[x] != -1:
  334. ext_edges_graph_ca += 1
  335. else:
  336. ext_edges_graph_an += 1
  337. # count ext_edges from core_pattern to a adjecent vertices
  338. # connected via the ext_edges
  339. ext_edges_pattern_ca = 0
  340. ext_edges_pattern_an = 0
  341. # for all core vertices
  342. for x in range(0, len(VF2_obj.core_pattern)):
  343. # for all its neighbours
  344. for y in range(0, len(P)):
  345. if P[x][y]:
  346. # if it is a neighbor and not yet matched
  347. if (VF2_obj.N_out_pattern[y] != -1 or VF2_obj.N_inc_pattern[y] != -1) and VF2_obj.core_pattern[y]:
  348. # if we matched it
  349. if VF2_obj.core_pattern[x] != -1:
  350. ext_edges_pattern_ca += 1
  351. else:
  352. ext_edges_pattern_an += 1
  353. # The nr of ext_edges between matched_vertices from the pattern
  354. # and its adjecent vertices must be less than or equal to the nr
  355. # of ext_edges between matched_vertices from the graph and its
  356. # adjecent vertices, otherwise we wont find an occurrence
  357. if ext_edges_pattern_ca > ext_edges_graph_ca:
  358. return False
  359. # The nr of ext_edges between those adjancent vertices from the
  360. # pattern and its not connected vertices must be less than or
  361. # equal to the nr of ext_edges between those adjacent vertices
  362. # from the graph and its not connected vertices,
  363. # otherwise we wont find an occurrence
  364. if ext_edges_pattern_an > ext_edges_graph_an:
  365. return False
  366. return True
  367. def matchPhase(H, P, h, p, index_M, VF2_obj, n, m):
  368. """
  369. The matching fase of the VF2 algorithm. If the chosen n, m pair
  370. passes the feasibilityTest, the pair gets added and we start
  371. to search for the next matching pair.
  372. """
  373. # all candidate pair (n, m) represent graph x pattern
  374. candidate = frozenset(itertools.chain(
  375. ((i, j) for i,j in VF2_obj.mapping.items()),
  376. # ((self.reverseMapH[i], self.reverseMapP[j]) for i,j in VF2_obj.mapping.items()),
  377. [(h[n],p[m])],
  378. ))
  379. if candidate in self.alreadyVisited:
  380. # print(self.indent*" ", "candidate:", candidate)
  381. # for match in self.alreadyVisited.get(index_M, []):
  382. # if match == candidate:
  383. return False # already visited this (partial) match -> skip
  384. if feasibilityTest(H, P, h, p, VF2_obj, n, m):
  385. print(self.indent*" ","adding to match:", n, "->", m)
  386. # adapt VF2_obj
  387. VF2_obj.core_graph[n] = True
  388. VF2_obj.core_pattern[m] = True
  389. VF2_obj.mapping[h[n]] = p[m]
  390. addOutNeighbours(H[n], VF2_obj.N_out_graph, index_M)
  391. addIncNeighbours(H, n, VF2_obj.N_inc_graph, index_M)
  392. addOutNeighbours(P[m], VF2_obj.N_out_pattern, index_M)
  393. addIncNeighbours(P, m, VF2_obj.N_inc_pattern, index_M)
  394. if index_M > 0:
  395. # remember our partial match (shallow copy) so we don't visit it again
  396. self.alreadyVisited.add(frozenset([ (i, j) for i,j in VF2_obj.mapping.items()]))
  397. # self.alreadyVisited.setdefault(index_M, set()).add(frozenset([ (self.reverseMapH[i], self.reverseMapP[j]) for i,j in VF2_obj.mapping.items()]))
  398. # print(self.alreadyVisited)
  399. self.indent += 1
  400. matched = yield from findM(H, P, h, p, VF2_obj, index_M + 1)
  401. if matched:
  402. # return True
  403. # print(self.indent*" ","found match", len(self.results), ", continuing...")
  404. pass
  405. self.indent -= 1
  406. if True:
  407. # else:
  408. print(self.indent*" ","backtracking... remove", n, "->", m)
  409. # else, backtrack, adapt VF2_obj
  410. VF2_obj.core_graph[n] = False
  411. VF2_obj.core_pattern[m] = False
  412. del VF2_obj.mapping[h[n]]
  413. delNeighbours(VF2_obj.N_out_graph, index_M)
  414. delNeighbours(VF2_obj.N_inc_graph, index_M)
  415. delNeighbours(VF2_obj.N_out_pattern, index_M)
  416. delNeighbours(VF2_obj.N_inc_pattern, index_M)
  417. return False
  418. def preferred(H, P, h, p, index_M, VF2_obj, N_graph, N_pattern):
  419. """
  420. Try to match the adjacency vertices connected via outgoing
  421. or incoming edges. (Depending on what is given for N_graph and
  422. N_pattern.)
  423. """
  424. for n in range(0, len(N_graph)):
  425. # skip graph vertices that are not in VF2_obj.N_out_graph
  426. # (or already matched)
  427. if N_graph[n] == -1 or VF2_obj.core_graph[n]:
  428. # print(self.indent*" "," skipping")
  429. continue
  430. print(self.indent*" "," n:", n)
  431. for m in range(0, len(N_pattern)):
  432. # skip graph vertices that are not in VF2_obj.N_out_pattern
  433. # (or already matched)
  434. if N_pattern[m] == -1 or VF2_obj.core_pattern[m]:
  435. continue
  436. print(self.indent*" "," m:", m)
  437. matched = yield from matchPhase(H, P, h, p, index_M, VF2_obj, n, m)
  438. if matched:
  439. return True
  440. return False
  441. def leastPreferred(H, P, h, p, index_M, VF2_obj):
  442. """
  443. Try to match the vertices that are not connected to the curretly
  444. matched vertices.
  445. """
  446. for n in range(0, len(VF2_obj.N_out_graph)):
  447. # skip vertices that are connected to the graph
  448. # (or already matched)
  449. if not (VF2_obj.N_out_graph[n] == -1 and VF2_obj.N_inc_graph[n] == -1) or VF2_obj.core_graph[n]:
  450. # print(self.indent*" "," skipping")
  451. continue
  452. print(" n:", n)
  453. for m in range(0, len(VF2_obj.N_out_pattern)):
  454. # skip vertices that are connected to the graph
  455. # (or already matched)
  456. if not (VF2_obj.N_out_pattern[m] == -1 and VF2_obj.N_inc_pattern[m] == -1) or VF2_obj.core_pattern[m]:
  457. # print(self.indent*" "," skipping")
  458. continue
  459. print(self.indent*" "," m:", m)
  460. matched = yield from matchPhase(H, P, h, p, index_M, VF2_obj, n, m)
  461. if matched:
  462. return True
  463. return False
  464. print(self.indent*" ","index_M:", index_M)
  465. # We are at the end, we found an candidate.
  466. if index_M == len(p):
  467. print(self.indent*" ","end...")
  468. bound_graph_vertices = {}
  469. for vertex_bound, _ in VF2_obj.mapping.items():
  470. bound_graph_vertices.setdefault(vertex_bound.type, set()).add(vertex_bound)
  471. result = self.matchNaive(pattern, vertices=bound_graph_vertices, edges=graph.edges)
  472. if result != None:
  473. yield result
  474. return result != None
  475. if index_M > 0:
  476. # try the candidates is the preffered order
  477. # first try the adjacent vertices connected via the outgoing edges.
  478. print(self.indent*" ","preferred L1")
  479. matched = yield from preferred(H, P, h, p, index_M, VF2_obj, VF2_obj.N_out_graph, VF2_obj.N_out_pattern)
  480. if matched:
  481. return True
  482. print(self.indent*" ","preferred L2")
  483. # then try the adjacent vertices connected via the incoming edges.
  484. matched = yield from preferred(H, P, h, p, index_M, VF2_obj, VF2_obj.N_inc_graph, VF2_obj.N_inc_pattern)
  485. if matched:
  486. return True
  487. print(self.indent*" ","leastPreferred")
  488. # and lastly, try the vertices not connected to the currently matched vertices
  489. matched = yield from leastPreferred(H, P, h, p, index_M, VF2_obj)
  490. if matched:
  491. return True
  492. return False
  493. # create adjecency matrix of the graph
  494. H, h = self.createAdjacencyMatrixMap(graph, pattern)
  495. # create adjecency matrix of the pattern
  496. P, p = self.createAdjacencyMatrixMap(pattern, pattern)
  497. VF2_obj = VF2_Obj(len(h), len(p))
  498. # Only for debugging:
  499. self.indent = 0
  500. self.reverseMapH = { h[i] : i for i in range(len(h))}
  501. self.reverseMapP = { p[i] : i for i in range(len(p))}
  502. # Set of partial matches already explored - prevents us from producing the same match multiple times
  503. # Encoded as a mapping from match size to the partial match
  504. self.alreadyVisited = set()
  505. yield from findM(H, P, h, p, VF2_obj)