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.

utils.py 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import networkx as nx
  2. import numpy as np
  3. def getSPLengths(G1):
  4. sp = nx.shortest_path(G1)
  5. distances = np.zeros((G1.number_of_nodes(), G1.number_of_nodes()))
  6. for i in sp.keys():
  7. for j in sp[i].keys():
  8. distances[i, j] = len(sp[i][j])-1
  9. return distances
  10. def getSPGraph(G, edge_weight = 'bond_type'):
  11. """Transform graph G to its corresponding shortest-paths graph.
  12. Parameters
  13. ----------
  14. G : NetworkX graph
  15. The graph to be tramsformed.
  16. edge_weight : string
  17. edge attribute corresponding to the edge weight. The default edge weight is bond_type.
  18. Return
  19. ------
  20. S : NetworkX graph
  21. The shortest-paths graph corresponding to G.
  22. Notes
  23. ------
  24. For an input graph G, its corresponding shortest-paths graph S contains the same set of nodes as G, while there exists an edge between all nodes in S which are connected by a walk in G. Every edge in S between two nodes is labeled by the shortest distance between these two nodes.
  25. References
  26. ----------
  27. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  28. """
  29. return floydTransformation(G, edge_weight = edge_weight)
  30. def floydTransformation(G, edge_weight = 'bond_type'):
  31. """Transform graph G to its corresponding shortest-paths graph using Floyd-transformation.
  32. Parameters
  33. ----------
  34. G : NetworkX graph
  35. The graph to be tramsformed.
  36. edge_weight : string
  37. edge attribute corresponding to the edge weight. The default edge weight is bond_type.
  38. Return
  39. ------
  40. S : NetworkX graph
  41. The shortest-paths graph corresponding to G.
  42. References
  43. ----------
  44. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  45. """
  46. spMatrix = nx.floyd_warshall_numpy(G, weight = edge_weight)
  47. S = nx.Graph()
  48. S.add_nodes_from(G.nodes(data=True))
  49. for i in range(0, G.number_of_nodes()):
  50. for j in range(0, G.number_of_nodes()):
  51. S.add_edge(i, j, cost = spMatrix[i, j])
  52. return S

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