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.

GED.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from pygraph.ged.costfunctions import ConstantCostFunction, RiesenCostFunction
  2. from pygraph.ged.costfunctions import NeighboorhoodCostFunction
  3. from pygraph.ged.bipartiteGED import computeBipartiteCostMatrix, getOptimalMapping
  4. from scipy.optimize import linear_sum_assignment
  5. def ged(G1, G2, method='Riesen', rho=None, varrho=None,
  6. cf=ConstantCostFunction(1, 3, 1, 3),
  7. solver=linear_sum_assignment):
  8. """Compute Graph Edit Distance between G1 and G2 according to mapping
  9. encoded within rho and varrho. Graph's node must be indexed by a
  10. index which is used is rho and varrho
  11. NB: Utilisation de
  12. dictionnaire pour etre plus versatile ?
  13. """
  14. if ((rho is None) or (varrho is None)):
  15. if(method == 'Riesen'):
  16. cf_bp = RiesenCostFunction(cf,lsap_solver=solver)
  17. elif(method == 'Neighboorhood'):
  18. cf_bp = NeighboorhoodCostFunction(cf,lsap_solver=solver)
  19. elif(method == 'Basic'):
  20. cf_bp = cf
  21. else:
  22. raise NameError('Non existent method ')
  23. rho, varrho = getOptimalMapping(
  24. computeBipartiteCostMatrix(G1, G2, cf_bp), lsap_solver=solver)
  25. n = G1.number_of_nodes()
  26. m = G2.number_of_nodes()
  27. ged = 0
  28. for i in G1.nodes():
  29. phi_i = rho[i]
  30. if(phi_i >= m):
  31. ged += cf.cnd(i, G1)
  32. else:
  33. ged += cf.cns(i, phi_i, G1, G2)
  34. for j in G2.nodes():
  35. phi_j = varrho[j]
  36. if(phi_j >= n):
  37. ged += cf.cni(j, G2)
  38. for e in G1.edges(data=True):
  39. i = e[0]
  40. j = e[1]
  41. phi_i = rho[i]
  42. phi_j = rho[j]
  43. if (phi_i < m) and (phi_j < m):
  44. mappedEdge = len(list(filter(lambda x: True if
  45. x == phi_j else False, G2[phi_i])))
  46. if(mappedEdge):
  47. e2 = [phi_i, phi_j, G2[phi_i][phi_j]]
  48. min_cost = min(cf.ces(e, e2, G1, G2),
  49. cf.ced(e, G1) + cf.cei(e2, G2))
  50. ged += min_cost
  51. else:
  52. ged += cf.ced(e, G1)
  53. else:
  54. ged += cf.ced(e, G1)
  55. for e in G2.edges(data=True):
  56. i = e[0]
  57. j = e[1]
  58. phi_i = varrho[i]
  59. phi_j = varrho[j]
  60. if (phi_i < n) and (phi_j < n):
  61. mappedEdge = len(list(filter(lambda x: True if x == phi_j
  62. else False, G1[phi_i])))
  63. if(not mappedEdge):
  64. ged += cf.cei(e, G2)
  65. else:
  66. ged += cf.ced(e, G2)
  67. return ged, rho, varrho

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