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.

bipartiteGED.py 909 B

123456789101112131415161718192021222324252627282930313233
  1. import numpy as np
  2. from scipy.optimize import linear_sum_assignment
  3. from pygraph.ged.costfunctions import ConstantCostFunction
  4. def computeBipartiteCostMatrix(G1, G2, cf=ConstantCostFunction(1, 3, 1, 3)):
  5. """Compute a Cost Matrix according to cost function cf"""
  6. n = G1.number_of_nodes()
  7. m = G2.number_of_nodes()
  8. nm = n + m
  9. C = np.ones([nm, nm])*np.inf
  10. C[n:, m:] = 0
  11. for u in G1.nodes():
  12. for v in G2.nodes():
  13. cost = cf.cns(u, v, G1, G2)
  14. C[u, v] = cost
  15. for v in G1.nodes():
  16. C[v, m + v] = cf.cnd(v, G1)
  17. for v in G2.nodes():
  18. C[n + v, v] = cf.cni(v, G2)
  19. return C
  20. def getOptimalMapping(C, lsap_solver=linear_sum_assignment):
  21. """Compute an optimal linear mapping according to cost Matrix C
  22. inclure les progs C de Seb
  23. """
  24. row_ind, col_ind = lsap_solver(C)
  25. return col_ind, row_ind[np.argsort(col_ind)]

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