You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

node_map.py 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Apr 22 11:31:26 2020
  5. @author: ljia
  6. """
  7. import numpy as np
  8. class NodeMap(object):
  9. def __init__(self, num_nodes_g, num_nodes_h):
  10. self.__forward_map = [np.inf] * num_nodes_g
  11. self.__backward_map = [np.inf] * num_nodes_h
  12. self.__induced_cost = np.inf
  13. def num_source_nodes(self):
  14. return len(self.__forward_map)
  15. def num_target_nodes(self):
  16. return len(self.__backward_map)
  17. def image(self, node):
  18. if node < len(self.__forward_map):
  19. return self.__forward_map[node]
  20. else:
  21. raise Exception('The node with ID ', str(node), ' is not contained in the source nodes of the node map.')
  22. return np.inf
  23. def pre_image(self, node):
  24. if node < len(self.__backward_map):
  25. return self.__backward_map[node]
  26. else:
  27. raise Exception('The node with ID ', str(node), ' is not contained in the target nodes of the node map.')
  28. return np.inf
  29. def get_forward_map(self):
  30. return self.__forward_map
  31. def get_backward_map(self):
  32. return self.__backward_map
  33. def as_relation(self, relation):
  34. relation.clear()
  35. for i in range(0, len(self.__forward_map)):
  36. k = self.__forward_map[i]
  37. if k != np.inf:
  38. relation.append(tuple((i, k)))
  39. for k in range(0, len(self.__backward_map)):
  40. i = self.__backward_map[k]
  41. if i == np.inf:
  42. relation.append(tuple((i, k)))
  43. def add_assignment(self, i, k):
  44. if i != np.inf:
  45. if i < len(self.__forward_map):
  46. self.__forward_map[i] = k
  47. else:
  48. raise Exception('The node with ID ', str(i), ' is not contained in the source nodes of the node map.')
  49. if k != np.inf:
  50. if k < len(self.__backward_map):
  51. self.__backward_map[k] = i
  52. else:
  53. raise Exception('The node with ID ', str(k), ' is not contained in the target nodes of the node map.')
  54. def set_induced_cost(self, induced_cost):
  55. self.__induced_cost = induced_cost
  56. def induced_cost(self):
  57. return self.__induced_cost

A Python package for graph kernels, graph edit distances and graph pre-image problem.