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.

compute_graph_edit_distance.py 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. """compute_graph_edit_distance.ipynb
  3. Automatically generated by Colaboratory.
  4. Original file is located at
  5. https://colab.research.google.com/drive/1Wfgn7WVuyOQQgwOvdUQBz0BzEVdp0YM3
  6. **This script demonstrates how to compute a graph edit distance.**
  7. ---
  8. **0. Install `graphkit-learn`.**
  9. """
  10. """**1. Get dataset.**"""
  11. from gklearn.utils import Dataset
  12. # Predefined dataset name, use dataset "MUTAG".
  13. ds_name = 'MUTAG'
  14. # Initialize a Dataset.
  15. dataset = Dataset()
  16. # Load predefined dataset "MUTAG".
  17. dataset.load_predefined_dataset(ds_name)
  18. graph1 = dataset.graphs[0]
  19. graph2 = dataset.graphs[1]
  20. print(graph1, graph2)
  21. """**2. Compute graph edit distance.**"""
  22. from gklearn.ged.env import GEDEnv
  23. ged_env = GEDEnv() # initailize GED environment.
  24. ged_env.set_edit_cost('CONSTANT', # GED cost type.
  25. edit_cost_constants=[3, 3, 1, 3, 3, 1] # edit costs.
  26. )
  27. ged_env.add_nx_graph(graph1, '') # add graph1
  28. ged_env.add_nx_graph(graph2, '') # add graph2
  29. listID = ged_env.get_all_graph_ids() # get list IDs of graphs
  30. ged_env.init(init_type='LAZY_WITHOUT_SHUFFLED_COPIES') # initialize GED environment.
  31. options = {'initialization_method': 'RANDOM', # or 'NODE', etc.
  32. 'threads': 1 # parallel threads.
  33. }
  34. ged_env.set_method('BIPARTITE', # GED method.
  35. options # options for GED method.
  36. )
  37. ged_env.init_method() # initialize GED method.
  38. ged_env.run_method(listID[0], listID[1]) # run.
  39. pi_forward = ged_env.get_forward_map(listID[0], listID[1]) # forward map.
  40. pi_backward = ged_env.get_backward_map(listID[0], listID[1]) # backward map.
  41. dis = ged_env.get_upper_bound(listID[0], listID[1]) # GED bewteen two graphs.
  42. print(pi_forward)
  43. print(pi_backward)
  44. print(dis)

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