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.5 kB

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

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