vf2pp.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. """
  2. ***************
  3. VF2++ Algorithm
  4. ***************
  5. An implementation of the VF2++ algorithm [1]_ for Graph Isomorphism testing.
  6. The simplest interface to use this module is to call:
  7. `vf2pp_is_isomorphic`: to check whether two graphs are isomorphic.
  8. `vf2pp_isomorphism`: to obtain the node mapping between two graphs,
  9. in case they are isomorphic.
  10. `vf2pp_all_isomorphisms`: to generate all possible mappings between two graphs,
  11. if isomorphic.
  12. Introduction
  13. ------------
  14. The VF2++ algorithm, follows a similar logic to that of VF2, while also
  15. introducing new easy-to-check cutting rules and determining the optimal access
  16. order of nodes. It is also implemented in a non-recursive manner, which saves
  17. both time and space, when compared to its previous counterpart.
  18. The optimal node ordering is obtained after taking into consideration both the
  19. degree but also the label rarity of each node.
  20. This way we place the nodes that are more likely to match, first in the order,
  21. thus examining the most promising branches in the beginning.
  22. The rules also consider node labels, making it easier to prune unfruitful
  23. branches early in the process.
  24. Examples
  25. --------
  26. Suppose G1 and G2 are Isomorphic Graphs. Verification is as follows:
  27. Without node labels:
  28. >>> import networkx as nx
  29. >>> G1 = nx.path_graph(4)
  30. >>> G2 = nx.path_graph(4)
  31. >>> nx.vf2pp_is_isomorphic(G1, G2, node_label=None)
  32. True
  33. >>> nx.vf2pp_isomorphism(G1, G2, node_label=None)
  34. {1: 1, 2: 2, 0: 0, 3: 3}
  35. With node labels:
  36. >>> G1 = nx.path_graph(4)
  37. >>> G2 = nx.path_graph(4)
  38. >>> mapped = {1: 1, 2: 2, 3: 3, 0: 0}
  39. >>> nx.set_node_attributes(G1, dict(zip(G1, ["blue", "red", "green", "yellow"])), "label")
  40. >>> nx.set_node_attributes(G2, dict(zip([mapped[u] for u in G1], ["blue", "red", "green", "yellow"])), "label")
  41. >>> nx.vf2pp_is_isomorphic(G1, G2, node_label="label")
  42. True
  43. >>> nx.vf2pp_isomorphism(G1, G2, node_label="label")
  44. {1: 1, 2: 2, 0: 0, 3: 3}
  45. References
  46. ----------
  47. .. [1] Jüttner, Alpár & Madarasi, Péter. (2018). "VF2++—An improved subgraph
  48. isomorphism algorithm". Discrete Applied Mathematics. 242.
  49. https://doi.org/10.1016/j.dam.2018.02.018
  50. """
  51. import collections
  52. import networkx as nx
  53. __all__ = ["vf2pp_isomorphism", "vf2pp_is_isomorphic", "vf2pp_all_isomorphisms"]
  54. _GraphParameters = collections.namedtuple(
  55. "_GraphParameters",
  56. [
  57. "G1",
  58. "G2",
  59. "G1_labels",
  60. "G2_labels",
  61. "nodes_of_G1Labels",
  62. "nodes_of_G2Labels",
  63. "G2_nodes_of_degree",
  64. ],
  65. )
  66. _StateParameters = collections.namedtuple(
  67. "_StateParameters",
  68. [
  69. "mapping",
  70. "reverse_mapping",
  71. "T1",
  72. "T1_in",
  73. "T1_tilde",
  74. "T1_tilde_in",
  75. "T2",
  76. "T2_in",
  77. "T2_tilde",
  78. "T2_tilde_in",
  79. ],
  80. )
  81. def vf2pp_isomorphism(G1, G2, node_label=None, default_label=None):
  82. """Return an isomorphic mapping between `G1` and `G2` if it exists.
  83. Parameters
  84. ----------
  85. G1, G2 : NetworkX Graph or MultiGraph instances.
  86. The two graphs to check for isomorphism.
  87. node_label : str, optional
  88. The name of the node attribute to be used when comparing nodes.
  89. The default is `None`, meaning node attributes are not considered
  90. in the comparison. Any node that doesn't have the `node_label`
  91. attribute uses `default_label` instead.
  92. default_label : scalar
  93. Default value to use when a node doesn't have an attribute
  94. named `node_label`. Default is `None`.
  95. Returns
  96. -------
  97. dict or None
  98. Node mapping if the two graphs are isomorphic. None otherwise.
  99. """
  100. try:
  101. mapping = next(vf2pp_all_isomorphisms(G1, G2, node_label, default_label))
  102. return mapping
  103. except StopIteration:
  104. return None
  105. def vf2pp_is_isomorphic(G1, G2, node_label=None, default_label=None):
  106. """Examines whether G1 and G2 are isomorphic.
  107. Parameters
  108. ----------
  109. G1, G2 : NetworkX Graph or MultiGraph instances.
  110. The two graphs to check for isomorphism.
  111. node_label : str, optional
  112. The name of the node attribute to be used when comparing nodes.
  113. The default is `None`, meaning node attributes are not considered
  114. in the comparison. Any node that doesn't have the `node_label`
  115. attribute uses `default_label` instead.
  116. default_label : scalar
  117. Default value to use when a node doesn't have an attribute
  118. named `node_label`. Default is `None`.
  119. Returns
  120. -------
  121. bool
  122. True if the two graphs are isomorphic, False otherwise.
  123. """
  124. if vf2pp_isomorphism(G1, G2, node_label, default_label) is not None:
  125. return True
  126. return False
  127. def vf2pp_all_isomorphisms(G1, G2, node_label=None, default_label=None):
  128. """Yields all the possible mappings between G1 and G2.
  129. Parameters
  130. ----------
  131. G1, G2 : NetworkX Graph or MultiGraph instances.
  132. The two graphs to check for isomorphism.
  133. node_label : str, optional
  134. The name of the node attribute to be used when comparing nodes.
  135. The default is `None`, meaning node attributes are not considered
  136. in the comparison. Any node that doesn't have the `node_label`
  137. attribute uses `default_label` instead.
  138. default_label : scalar
  139. Default value to use when a node doesn't have an attribute
  140. named `node_label`. Default is `None`.
  141. Yields
  142. ------
  143. dict
  144. Isomorphic mapping between the nodes in `G1` and `G2`.
  145. """
  146. if G1.number_of_nodes() == 0 or G2.number_of_nodes() == 0:
  147. return False
  148. # Create the degree dicts based on graph type
  149. if G1.is_directed():
  150. G1_degree = {
  151. n: (in_degree, out_degree)
  152. for (n, in_degree), (_, out_degree) in zip(G1.in_degree, G1.out_degree)
  153. }
  154. G2_degree = {
  155. n: (in_degree, out_degree)
  156. for (n, in_degree), (_, out_degree) in zip(G2.in_degree, G2.out_degree)
  157. }
  158. else:
  159. G1_degree = dict(G1.degree)
  160. G2_degree = dict(G2.degree)
  161. if not G1.is_directed():
  162. find_candidates = _find_candidates
  163. restore_Tinout = _restore_Tinout
  164. else:
  165. find_candidates = _find_candidates_Di
  166. restore_Tinout = _restore_Tinout_Di
  167. # Check that both graphs have the same number of nodes and degree sequence
  168. if G1.order() != G2.order():
  169. return False
  170. if sorted(G1_degree.values()) != sorted(G2_degree.values()):
  171. return False
  172. # Initialize parameters and cache necessary information about degree and labels
  173. graph_params, state_params = _initialize_parameters(
  174. G1, G2, G2_degree, node_label, default_label
  175. )
  176. # Check if G1 and G2 have the same labels, and that number of nodes per label is equal between the two graphs
  177. if not _precheck_label_properties(graph_params):
  178. return False
  179. # Calculate the optimal node ordering
  180. node_order = _matching_order(graph_params)
  181. # Initialize the stack
  182. stack = []
  183. candidates = iter(
  184. find_candidates(node_order[0], graph_params, state_params, G1_degree)
  185. )
  186. stack.append((node_order[0], candidates))
  187. mapping = state_params.mapping
  188. reverse_mapping = state_params.reverse_mapping
  189. # Index of the node from the order, currently being examined
  190. matching_node = 1
  191. while stack:
  192. current_node, candidate_nodes = stack[-1]
  193. try:
  194. candidate = next(candidate_nodes)
  195. except StopIteration:
  196. # If no remaining candidates, return to a previous state, and follow another branch
  197. stack.pop()
  198. matching_node -= 1
  199. if stack:
  200. # Pop the previously added u-v pair, and look for a different candidate _v for u
  201. popped_node1, _ = stack[-1]
  202. popped_node2 = mapping[popped_node1]
  203. mapping.pop(popped_node1)
  204. reverse_mapping.pop(popped_node2)
  205. restore_Tinout(popped_node1, popped_node2, graph_params, state_params)
  206. continue
  207. if _feasibility(current_node, candidate, graph_params, state_params):
  208. # Terminate if mapping is extended to its full
  209. if len(mapping) == G2.number_of_nodes() - 1:
  210. cp_mapping = mapping.copy()
  211. cp_mapping[current_node] = candidate
  212. yield cp_mapping
  213. continue
  214. # Feasibility rules pass, so extend the mapping and update the parameters
  215. mapping[current_node] = candidate
  216. reverse_mapping[candidate] = current_node
  217. _update_Tinout(current_node, candidate, graph_params, state_params)
  218. # Append the next node and its candidates to the stack
  219. candidates = iter(
  220. find_candidates(
  221. node_order[matching_node], graph_params, state_params, G1_degree
  222. )
  223. )
  224. stack.append((node_order[matching_node], candidates))
  225. matching_node += 1
  226. def _precheck_label_properties(graph_params):
  227. G1, G2, G1_labels, G2_labels, nodes_of_G1Labels, nodes_of_G2Labels, _ = graph_params
  228. if any(
  229. label not in nodes_of_G1Labels or len(nodes_of_G1Labels[label]) != len(nodes)
  230. for label, nodes in nodes_of_G2Labels.items()
  231. ):
  232. return False
  233. return True
  234. def _initialize_parameters(G1, G2, G2_degree, node_label=None, default_label=-1):
  235. """Initializes all the necessary parameters for VF2++
  236. Parameters
  237. ----------
  238. G1,G2: NetworkX Graph or MultiGraph instances.
  239. The two graphs to check for isomorphism or monomorphism
  240. G1_labels,G2_labels: dict
  241. The label of every node in G1 and G2 respectively
  242. Returns
  243. -------
  244. graph_params: namedtuple
  245. Contains all the Graph-related parameters:
  246. G1,G2
  247. G1_labels,G2_labels: dict
  248. state_params: namedtuple
  249. Contains all the State-related parameters:
  250. mapping: dict
  251. The mapping as extended so far. Maps nodes of G1 to nodes of G2
  252. reverse_mapping: dict
  253. The reverse mapping as extended so far. Maps nodes from G2 to nodes of G1. It's basically "mapping" reversed
  254. T1, T2: set
  255. Ti contains uncovered neighbors of covered nodes from Gi, i.e. nodes that are not in the mapping, but are
  256. neighbors of nodes that are.
  257. T1_out, T2_out: set
  258. Ti_out contains all the nodes from Gi, that are neither in the mapping nor in Ti
  259. """
  260. G1_labels = dict(G1.nodes(data=node_label, default=default_label))
  261. G2_labels = dict(G2.nodes(data=node_label, default=default_label))
  262. graph_params = _GraphParameters(
  263. G1,
  264. G2,
  265. G1_labels,
  266. G2_labels,
  267. nx.utils.groups(G1_labels),
  268. nx.utils.groups(G2_labels),
  269. nx.utils.groups(G2_degree),
  270. )
  271. T1, T1_in = set(), set()
  272. T2, T2_in = set(), set()
  273. if G1.is_directed():
  274. T1_tilde, T1_tilde_in = (
  275. set(G1.nodes()),
  276. set(),
  277. ) # todo: do we need Ti_tilde_in? What nodes does it have?
  278. T2_tilde, T2_tilde_in = set(G2.nodes()), set()
  279. else:
  280. T1_tilde, T1_tilde_in = set(G1.nodes()), set()
  281. T2_tilde, T2_tilde_in = set(G2.nodes()), set()
  282. state_params = _StateParameters(
  283. {},
  284. {},
  285. T1,
  286. T1_in,
  287. T1_tilde,
  288. T1_tilde_in,
  289. T2,
  290. T2_in,
  291. T2_tilde,
  292. T2_tilde_in,
  293. )
  294. return graph_params, state_params
  295. def _matching_order(graph_params):
  296. """The node ordering as introduced in VF2++.
  297. Notes
  298. -----
  299. Taking into account the structure of the Graph and the node labeling, the nodes are placed in an order such that,
  300. most of the unfruitful/infeasible branches of the search space can be pruned on high levels, significantly
  301. decreasing the number of visited states. The premise is that, the algorithm will be able to recognize
  302. inconsistencies early, proceeding to go deep into the search tree only if it's needed.
  303. Parameters
  304. ----------
  305. graph_params: namedtuple
  306. Contains:
  307. G1,G2: NetworkX Graph or MultiGraph instances.
  308. The two graphs to check for isomorphism or monomorphism.
  309. G1_labels,G2_labels: dict
  310. The label of every node in G1 and G2 respectively.
  311. Returns
  312. -------
  313. node_order: list
  314. The ordering of the nodes.
  315. """
  316. G1, G2, G1_labels, _, _, nodes_of_G2Labels, _ = graph_params
  317. if not G1 and not G2:
  318. return {}
  319. if G1.is_directed():
  320. G1 = G1.to_undirected(as_view=True)
  321. V1_unordered = set(G1.nodes())
  322. label_rarity = {label: len(nodes) for label, nodes in nodes_of_G2Labels.items()}
  323. used_degrees = {node: 0 for node in G1}
  324. node_order = []
  325. while V1_unordered:
  326. max_rarity = min(label_rarity[G1_labels[x]] for x in V1_unordered)
  327. rarest_nodes = [
  328. n for n in V1_unordered if label_rarity[G1_labels[n]] == max_rarity
  329. ]
  330. max_node = max(rarest_nodes, key=G1.degree)
  331. for dlevel_nodes in nx.bfs_layers(G1, max_node):
  332. nodes_to_add = dlevel_nodes.copy()
  333. while nodes_to_add:
  334. max_used_degree = max(used_degrees[n] for n in nodes_to_add)
  335. max_used_degree_nodes = [
  336. n for n in nodes_to_add if used_degrees[n] == max_used_degree
  337. ]
  338. max_degree = max(G1.degree[n] for n in max_used_degree_nodes)
  339. max_degree_nodes = [
  340. n for n in max_used_degree_nodes if G1.degree[n] == max_degree
  341. ]
  342. next_node = min(
  343. max_degree_nodes, key=lambda x: label_rarity[G1_labels[x]]
  344. )
  345. node_order.append(next_node)
  346. for node in G1.neighbors(next_node):
  347. used_degrees[node] += 1
  348. nodes_to_add.remove(next_node)
  349. label_rarity[G1_labels[next_node]] -= 1
  350. V1_unordered.discard(next_node)
  351. return node_order
  352. def _find_candidates(
  353. u, graph_params, state_params, G1_degree
  354. ): # todo: make the 4th argument the degree of u
  355. """Given node u of G1, finds the candidates of u from G2.
  356. Parameters
  357. ----------
  358. u: Graph node
  359. The node from G1 for which to find the candidates from G2.
  360. graph_params: namedtuple
  361. Contains all the Graph-related parameters:
  362. G1,G2: NetworkX Graph or MultiGraph instances.
  363. The two graphs to check for isomorphism or monomorphism
  364. G1_labels,G2_labels: dict
  365. The label of every node in G1 and G2 respectively
  366. state_params: namedtuple
  367. Contains all the State-related parameters:
  368. mapping: dict
  369. The mapping as extended so far. Maps nodes of G1 to nodes of G2
  370. reverse_mapping: dict
  371. The reverse mapping as extended so far. Maps nodes from G2 to nodes of G1. It's basically "mapping" reversed
  372. T1, T2: set
  373. Ti contains uncovered neighbors of covered nodes from Gi, i.e. nodes that are not in the mapping, but are
  374. neighbors of nodes that are.
  375. T1_tilde, T2_tilde: set
  376. Ti_tilde contains all the nodes from Gi, that are neither in the mapping nor in Ti
  377. Returns
  378. -------
  379. candidates: set
  380. The nodes from G2 which are candidates for u.
  381. """
  382. G1, G2, G1_labels, _, _, nodes_of_G2Labels, G2_nodes_of_degree = graph_params
  383. mapping, reverse_mapping, _, _, _, _, _, _, T2_tilde, _ = state_params
  384. covered_neighbors = [nbr for nbr in G1[u] if nbr in mapping]
  385. if not covered_neighbors:
  386. candidates = set(nodes_of_G2Labels[G1_labels[u]])
  387. candidates.intersection_update(G2_nodes_of_degree[G1_degree[u]])
  388. candidates.intersection_update(T2_tilde)
  389. candidates.difference_update(reverse_mapping)
  390. if G1.is_multigraph():
  391. candidates.difference_update(
  392. {
  393. node
  394. for node in candidates
  395. if G1.number_of_edges(u, u) != G2.number_of_edges(node, node)
  396. }
  397. )
  398. return candidates
  399. nbr1 = covered_neighbors[0]
  400. common_nodes = set(G2[mapping[nbr1]])
  401. for nbr1 in covered_neighbors[1:]:
  402. common_nodes.intersection_update(G2[mapping[nbr1]])
  403. common_nodes.difference_update(reverse_mapping)
  404. common_nodes.intersection_update(G2_nodes_of_degree[G1_degree[u]])
  405. common_nodes.intersection_update(nodes_of_G2Labels[G1_labels[u]])
  406. if G1.is_multigraph():
  407. common_nodes.difference_update(
  408. {
  409. node
  410. for node in common_nodes
  411. if G1.number_of_edges(u, u) != G2.number_of_edges(node, node)
  412. }
  413. )
  414. return common_nodes
  415. def _find_candidates_Di(u, graph_params, state_params, G1_degree):
  416. G1, G2, G1_labels, _, _, nodes_of_G2Labels, G2_nodes_of_degree = graph_params
  417. mapping, reverse_mapping, _, _, _, _, _, _, T2_tilde, _ = state_params
  418. covered_successors = [succ for succ in G1[u] if succ in mapping]
  419. covered_predecessors = [pred for pred in G1.pred[u] if pred in mapping]
  420. if not (covered_successors or covered_predecessors):
  421. candidates = set(nodes_of_G2Labels[G1_labels[u]])
  422. candidates.intersection_update(G2_nodes_of_degree[G1_degree[u]])
  423. candidates.intersection_update(T2_tilde)
  424. candidates.difference_update(reverse_mapping)
  425. if G1.is_multigraph():
  426. candidates.difference_update(
  427. {
  428. node
  429. for node in candidates
  430. if G1.number_of_edges(u, u) != G2.number_of_edges(node, node)
  431. }
  432. )
  433. return candidates
  434. if covered_successors:
  435. succ1 = covered_successors[0]
  436. common_nodes = set(G2.pred[mapping[succ1]])
  437. for succ1 in covered_successors[1:]:
  438. common_nodes.intersection_update(G2.pred[mapping[succ1]])
  439. else:
  440. pred1 = covered_predecessors.pop()
  441. common_nodes = set(G2[mapping[pred1]])
  442. for pred1 in covered_predecessors:
  443. common_nodes.intersection_update(G2[mapping[pred1]])
  444. common_nodes.difference_update(reverse_mapping)
  445. common_nodes.intersection_update(G2_nodes_of_degree[G1_degree[u]])
  446. common_nodes.intersection_update(nodes_of_G2Labels[G1_labels[u]])
  447. if G1.is_multigraph():
  448. common_nodes.difference_update(
  449. {
  450. node
  451. for node in common_nodes
  452. if G1.number_of_edges(u, u) != G2.number_of_edges(node, node)
  453. }
  454. )
  455. return common_nodes
  456. def _feasibility(node1, node2, graph_params, state_params):
  457. """Given a candidate pair of nodes u and v from G1 and G2 respectively, checks if it's feasible to extend the
  458. mapping, i.e. if u and v can be matched.
  459. Notes
  460. -----
  461. This function performs all the necessary checking by applying both consistency and cutting rules.
  462. Parameters
  463. ----------
  464. node1, node2: Graph node
  465. The candidate pair of nodes being checked for matching
  466. graph_params: namedtuple
  467. Contains all the Graph-related parameters:
  468. G1,G2: NetworkX Graph or MultiGraph instances.
  469. The two graphs to check for isomorphism or monomorphism
  470. G1_labels,G2_labels: dict
  471. The label of every node in G1 and G2 respectively
  472. state_params: namedtuple
  473. Contains all the State-related parameters:
  474. mapping: dict
  475. The mapping as extended so far. Maps nodes of G1 to nodes of G2
  476. reverse_mapping: dict
  477. The reverse mapping as extended so far. Maps nodes from G2 to nodes of G1. It's basically "mapping" reversed
  478. T1, T2: set
  479. Ti contains uncovered neighbors of covered nodes from Gi, i.e. nodes that are not in the mapping, but are
  480. neighbors of nodes that are.
  481. T1_out, T2_out: set
  482. Ti_out contains all the nodes from Gi, that are neither in the mapping nor in Ti
  483. Returns
  484. -------
  485. True if all checks are successful, False otherwise.
  486. """
  487. G1 = graph_params.G1
  488. if _cut_PT(node1, node2, graph_params, state_params):
  489. return False
  490. if G1.is_multigraph():
  491. if not _consistent_PT(node1, node2, graph_params, state_params):
  492. return False
  493. return True
  494. def _cut_PT(u, v, graph_params, state_params):
  495. """Implements the cutting rules for the ISO problem.
  496. Parameters
  497. ----------
  498. u, v: Graph node
  499. The two candidate nodes being examined.
  500. graph_params: namedtuple
  501. Contains all the Graph-related parameters:
  502. G1,G2: NetworkX Graph or MultiGraph instances.
  503. The two graphs to check for isomorphism or monomorphism
  504. G1_labels,G2_labels: dict
  505. The label of every node in G1 and G2 respectively
  506. state_params: namedtuple
  507. Contains all the State-related parameters:
  508. mapping: dict
  509. The mapping as extended so far. Maps nodes of G1 to nodes of G2
  510. reverse_mapping: dict
  511. The reverse mapping as extended so far. Maps nodes from G2 to nodes of G1. It's basically "mapping" reversed
  512. T1, T2: set
  513. Ti contains uncovered neighbors of covered nodes from Gi, i.e. nodes that are not in the mapping, but are
  514. neighbors of nodes that are.
  515. T1_tilde, T2_tilde: set
  516. Ti_out contains all the nodes from Gi, that are neither in the mapping nor in Ti
  517. Returns
  518. -------
  519. True if we should prune this branch, i.e. the node pair failed the cutting checks. False otherwise.
  520. """
  521. G1, G2, G1_labels, G2_labels, _, _, _ = graph_params
  522. (
  523. _,
  524. _,
  525. T1,
  526. T1_in,
  527. T1_tilde,
  528. _,
  529. T2,
  530. T2_in,
  531. T2_tilde,
  532. _,
  533. ) = state_params
  534. u_labels_predecessors, v_labels_predecessors = {}, {}
  535. if G1.is_directed():
  536. u_labels_predecessors = nx.utils.groups(
  537. {n1: G1_labels[n1] for n1 in G1.pred[u]}
  538. )
  539. v_labels_predecessors = nx.utils.groups(
  540. {n2: G2_labels[n2] for n2 in G2.pred[v]}
  541. )
  542. if set(u_labels_predecessors.keys()) != set(v_labels_predecessors.keys()):
  543. return True
  544. u_labels_successors = nx.utils.groups({n1: G1_labels[n1] for n1 in G1[u]})
  545. v_labels_successors = nx.utils.groups({n2: G2_labels[n2] for n2 in G2[v]})
  546. # if the neighbors of u, do not have the same labels as those of v, NOT feasible.
  547. if set(u_labels_successors.keys()) != set(v_labels_successors.keys()):
  548. return True
  549. for label, G1_nbh in u_labels_successors.items():
  550. G2_nbh = v_labels_successors[label]
  551. if G1.is_multigraph():
  552. # Check for every neighbor in the neighborhood, if u-nbr1 has same edges as v-nbr2
  553. u_nbrs_edges = sorted(G1.number_of_edges(u, x) for x in G1_nbh)
  554. v_nbrs_edges = sorted(G2.number_of_edges(v, x) for x in G2_nbh)
  555. if any(
  556. u_nbr_edges != v_nbr_edges
  557. for u_nbr_edges, v_nbr_edges in zip(u_nbrs_edges, v_nbrs_edges)
  558. ):
  559. return True
  560. if len(T1.intersection(G1_nbh)) != len(T2.intersection(G2_nbh)):
  561. return True
  562. if len(T1_tilde.intersection(G1_nbh)) != len(T2_tilde.intersection(G2_nbh)):
  563. return True
  564. if G1.is_directed() and len(T1_in.intersection(G1_nbh)) != len(
  565. T2_in.intersection(G2_nbh)
  566. ):
  567. return True
  568. if not G1.is_directed():
  569. return False
  570. for label, G1_pred in u_labels_predecessors.items():
  571. G2_pred = v_labels_predecessors[label]
  572. if G1.is_multigraph():
  573. # Check for every neighbor in the neighborhood, if u-nbr1 has same edges as v-nbr2
  574. u_pred_edges = sorted(G1.number_of_edges(u, x) for x in G1_pred)
  575. v_pred_edges = sorted(G2.number_of_edges(v, x) for x in G2_pred)
  576. if any(
  577. u_nbr_edges != v_nbr_edges
  578. for u_nbr_edges, v_nbr_edges in zip(u_pred_edges, v_pred_edges)
  579. ):
  580. return True
  581. if len(T1.intersection(G1_pred)) != len(T2.intersection(G2_pred)):
  582. return True
  583. if len(T1_tilde.intersection(G1_pred)) != len(T2_tilde.intersection(G2_pred)):
  584. return True
  585. if len(T1_in.intersection(G1_pred)) != len(T2_in.intersection(G2_pred)):
  586. return True
  587. return False
  588. def _consistent_PT(u, v, graph_params, state_params):
  589. """Checks the consistency of extending the mapping using the current node pair.
  590. Parameters
  591. ----------
  592. u, v: Graph node
  593. The two candidate nodes being examined.
  594. graph_params: namedtuple
  595. Contains all the Graph-related parameters:
  596. G1,G2: NetworkX Graph or MultiGraph instances.
  597. The two graphs to check for isomorphism or monomorphism
  598. G1_labels,G2_labels: dict
  599. The label of every node in G1 and G2 respectively
  600. state_params: namedtuple
  601. Contains all the State-related parameters:
  602. mapping: dict
  603. The mapping as extended so far. Maps nodes of G1 to nodes of G2
  604. reverse_mapping: dict
  605. The reverse mapping as extended so far. Maps nodes from G2 to nodes of G1. It's basically "mapping" reversed
  606. T1, T2: set
  607. Ti contains uncovered neighbors of covered nodes from Gi, i.e. nodes that are not in the mapping, but are
  608. neighbors of nodes that are.
  609. T1_out, T2_out: set
  610. Ti_out contains all the nodes from Gi, that are neither in the mapping nor in Ti
  611. Returns
  612. -------
  613. True if the pair passes all the consistency checks successfully. False otherwise.
  614. """
  615. G1, G2 = graph_params.G1, graph_params.G2
  616. mapping, reverse_mapping = state_params.mapping, state_params.reverse_mapping
  617. for neighbor in G1[u]:
  618. if neighbor in mapping:
  619. if G1.number_of_edges(u, neighbor) != G2.number_of_edges(
  620. v, mapping[neighbor]
  621. ):
  622. return False
  623. for neighbor in G2[v]:
  624. if neighbor in reverse_mapping:
  625. if G1.number_of_edges(u, reverse_mapping[neighbor]) != G2.number_of_edges(
  626. v, neighbor
  627. ):
  628. return False
  629. if not G1.is_directed():
  630. return True
  631. for predecessor in G1.pred[u]:
  632. if predecessor in mapping:
  633. if G1.number_of_edges(predecessor, u) != G2.number_of_edges(
  634. mapping[predecessor], v
  635. ):
  636. return False
  637. for predecessor in G2.pred[v]:
  638. if predecessor in reverse_mapping:
  639. if G1.number_of_edges(
  640. reverse_mapping[predecessor], u
  641. ) != G2.number_of_edges(predecessor, v):
  642. return False
  643. return True
  644. def _update_Tinout(new_node1, new_node2, graph_params, state_params):
  645. """Updates the Ti/Ti_out (i=1,2) when a new node pair u-v is added to the mapping.
  646. Notes
  647. -----
  648. This function should be called right after the feasibility checks are passed, and node1 is mapped to node2. The
  649. purpose of this function is to avoid brute force computing of Ti/Ti_out by iterating over all nodes of the graph
  650. and checking which nodes satisfy the necessary conditions. Instead, in every step of the algorithm we focus
  651. exclusively on the two nodes that are being added to the mapping, incrementally updating Ti/Ti_out.
  652. Parameters
  653. ----------
  654. new_node1, new_node2: Graph node
  655. The two new nodes, added to the mapping.
  656. graph_params: namedtuple
  657. Contains all the Graph-related parameters:
  658. G1,G2: NetworkX Graph or MultiGraph instances.
  659. The two graphs to check for isomorphism or monomorphism
  660. G1_labels,G2_labels: dict
  661. The label of every node in G1 and G2 respectively
  662. state_params: namedtuple
  663. Contains all the State-related parameters:
  664. mapping: dict
  665. The mapping as extended so far. Maps nodes of G1 to nodes of G2
  666. reverse_mapping: dict
  667. The reverse mapping as extended so far. Maps nodes from G2 to nodes of G1. It's basically "mapping" reversed
  668. T1, T2: set
  669. Ti contains uncovered neighbors of covered nodes from Gi, i.e. nodes that are not in the mapping, but are
  670. neighbors of nodes that are.
  671. T1_tilde, T2_tilde: set
  672. Ti_out contains all the nodes from Gi, that are neither in the mapping nor in Ti
  673. """
  674. G1, G2, _, _, _, _, _ = graph_params
  675. (
  676. mapping,
  677. reverse_mapping,
  678. T1,
  679. T1_in,
  680. T1_tilde,
  681. T1_tilde_in,
  682. T2,
  683. T2_in,
  684. T2_tilde,
  685. T2_tilde_in,
  686. ) = state_params
  687. uncovered_successors_G1 = {succ for succ in G1[new_node1] if succ not in mapping}
  688. uncovered_successors_G2 = {
  689. succ for succ in G2[new_node2] if succ not in reverse_mapping
  690. }
  691. # Add the uncovered neighbors of node1 and node2 in T1 and T2 respectively
  692. T1.update(uncovered_successors_G1)
  693. T2.update(uncovered_successors_G2)
  694. T1.discard(new_node1)
  695. T2.discard(new_node2)
  696. T1_tilde.difference_update(uncovered_successors_G1)
  697. T2_tilde.difference_update(uncovered_successors_G2)
  698. T1_tilde.discard(new_node1)
  699. T2_tilde.discard(new_node2)
  700. if not G1.is_directed():
  701. return
  702. uncovered_predecessors_G1 = {
  703. pred for pred in G1.pred[new_node1] if pred not in mapping
  704. }
  705. uncovered_predecessors_G2 = {
  706. pred for pred in G2.pred[new_node2] if pred not in reverse_mapping
  707. }
  708. T1_in.update(uncovered_predecessors_G1)
  709. T2_in.update(uncovered_predecessors_G2)
  710. T1_in.discard(new_node1)
  711. T2_in.discard(new_node2)
  712. T1_tilde.difference_update(uncovered_predecessors_G1)
  713. T2_tilde.difference_update(uncovered_predecessors_G2)
  714. T1_tilde.discard(new_node1)
  715. T2_tilde.discard(new_node2)
  716. def _restore_Tinout(popped_node1, popped_node2, graph_params, state_params):
  717. """Restores the previous version of Ti/Ti_out when a node pair is deleted from the mapping.
  718. Parameters
  719. ----------
  720. popped_node1, popped_node2: Graph node
  721. The two nodes deleted from the mapping.
  722. graph_params: namedtuple
  723. Contains all the Graph-related parameters:
  724. G1,G2: NetworkX Graph or MultiGraph instances.
  725. The two graphs to check for isomorphism or monomorphism
  726. G1_labels,G2_labels: dict
  727. The label of every node in G1 and G2 respectively
  728. state_params: namedtuple
  729. Contains all the State-related parameters:
  730. mapping: dict
  731. The mapping as extended so far. Maps nodes of G1 to nodes of G2
  732. reverse_mapping: dict
  733. The reverse mapping as extended so far. Maps nodes from G2 to nodes of G1. It's basically "mapping" reversed
  734. T1, T2: set
  735. Ti contains uncovered neighbors of covered nodes from Gi, i.e. nodes that are not in the mapping, but are
  736. neighbors of nodes that are.
  737. T1_tilde, T2_tilde: set
  738. Ti_out contains all the nodes from Gi, that are neither in the mapping nor in Ti
  739. """
  740. # If the node we want to remove from the mapping, has at least one covered neighbor, add it to T1.
  741. G1, G2, _, _, _, _, _ = graph_params
  742. (
  743. mapping,
  744. reverse_mapping,
  745. T1,
  746. T1_in,
  747. T1_tilde,
  748. T1_tilde_in,
  749. T2,
  750. T2_in,
  751. T2_tilde,
  752. T2_tilde_in,
  753. ) = state_params
  754. is_added = False
  755. for neighbor in G1[popped_node1]:
  756. if neighbor in mapping:
  757. # if a neighbor of the excluded node1 is in the mapping, keep node1 in T1
  758. is_added = True
  759. T1.add(popped_node1)
  760. else:
  761. # check if its neighbor has another connection with a covered node. If not, only then exclude it from T1
  762. if any(nbr in mapping for nbr in G1[neighbor]):
  763. continue
  764. T1.discard(neighbor)
  765. T1_tilde.add(neighbor)
  766. # Case where the node is not present in neither the mapping nor T1. By definition, it should belong to T1_tilde
  767. if not is_added:
  768. T1_tilde.add(popped_node1)
  769. is_added = False
  770. for neighbor in G2[popped_node2]:
  771. if neighbor in reverse_mapping:
  772. is_added = True
  773. T2.add(popped_node2)
  774. else:
  775. if any(nbr in reverse_mapping for nbr in G2[neighbor]):
  776. continue
  777. T2.discard(neighbor)
  778. T2_tilde.add(neighbor)
  779. if not is_added:
  780. T2_tilde.add(popped_node2)
  781. def _restore_Tinout_Di(popped_node1, popped_node2, graph_params, state_params):
  782. # If the node we want to remove from the mapping, has at least one covered neighbor, add it to T1.
  783. G1, G2, _, _, _, _, _ = graph_params
  784. (
  785. mapping,
  786. reverse_mapping,
  787. T1,
  788. T1_in,
  789. T1_tilde,
  790. T1_tilde_in,
  791. T2,
  792. T2_in,
  793. T2_tilde,
  794. T2_tilde_in,
  795. ) = state_params
  796. is_added = False
  797. for successor in G1[popped_node1]:
  798. if successor in mapping:
  799. # if a neighbor of the excluded node1 is in the mapping, keep node1 in T1
  800. is_added = True
  801. T1_in.add(popped_node1)
  802. else:
  803. # check if its neighbor has another connection with a covered node. If not, only then exclude it from T1
  804. if not any(pred in mapping for pred in G1.pred[successor]):
  805. T1.discard(successor)
  806. if not any(succ in mapping for succ in G1[successor]):
  807. T1_in.discard(successor)
  808. if successor not in T1:
  809. if successor not in T1_in:
  810. T1_tilde.add(successor)
  811. for predecessor in G1.pred[popped_node1]:
  812. if predecessor in mapping:
  813. # if a neighbor of the excluded node1 is in the mapping, keep node1 in T1
  814. is_added = True
  815. T1.add(popped_node1)
  816. else:
  817. # check if its neighbor has another connection with a covered node. If not, only then exclude it from T1
  818. if not any(pred in mapping for pred in G1.pred[predecessor]):
  819. T1.discard(predecessor)
  820. if not any(succ in mapping for succ in G1[predecessor]):
  821. T1_in.discard(predecessor)
  822. if not (predecessor in T1 or predecessor in T1_in):
  823. T1_tilde.add(predecessor)
  824. # Case where the node is not present in neither the mapping nor T1. By definition it should belong to T1_tilde
  825. if not is_added:
  826. T1_tilde.add(popped_node1)
  827. is_added = False
  828. for successor in G2[popped_node2]:
  829. if successor in reverse_mapping:
  830. is_added = True
  831. T2_in.add(popped_node2)
  832. else:
  833. if not any(pred in reverse_mapping for pred in G2.pred[successor]):
  834. T2.discard(successor)
  835. if not any(succ in reverse_mapping for succ in G2[successor]):
  836. T2_in.discard(successor)
  837. if successor not in T2:
  838. if successor not in T2_in:
  839. T2_tilde.add(successor)
  840. for predecessor in G2.pred[popped_node2]:
  841. if predecessor in reverse_mapping:
  842. # if a neighbor of the excluded node1 is in the mapping, keep node1 in T1
  843. is_added = True
  844. T2.add(popped_node2)
  845. else:
  846. # check if its neighbor has another connection with a covered node. If not, only then exclude it from T1
  847. if not any(pred in reverse_mapping for pred in G2.pred[predecessor]):
  848. T2.discard(predecessor)
  849. if not any(succ in reverse_mapping for succ in G2[predecessor]):
  850. T2_in.discard(predecessor)
  851. if not (predecessor in T2 or predecessor in T2_in):
  852. T2_tilde.add(predecessor)
  853. if not is_added:
  854. T2_tilde.add(popped_node2)