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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 np.keys():
  7. for j in np[i].keys():
  8. distances[i, j] = len(sp[i][j])-1
  9. return distances
  10. def getSPGraph(G):
  11. """Transform graph G to its corresponding shortest-paths graph.
  12. Parameters
  13. ----------
  14. G : NetworkX graph
  15. The graph to be tramsformed.
  16. Return
  17. ------
  18. S : NetworkX graph
  19. The shortest-paths graph corresponding to G.
  20. Notes
  21. ------
  22. 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.
  23. References
  24. ----------
  25. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  26. """
  27. return floydTransformation(G)
  28. def floydTransformation(G):
  29. """Transform graph G to its corresponding shortest-paths graph using Floyd-transformation.
  30. Parameters
  31. ----------
  32. G : NetworkX graph
  33. The graph to be tramsformed.
  34. Return
  35. ------
  36. S : NetworkX graph
  37. The shortest-paths graph corresponding to G.
  38. References
  39. ----------
  40. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  41. """
  42. spMatrix = nx.floyd_warshall_numpy(G) # @todo weigth label not considered
  43. S = nx.Graph()
  44. S.add_nodes_from(G.nodes(data=True))
  45. for i in range(0, G.number_of_nodes()):
  46. for j in range(0, G.number_of_nodes()):
  47. S.add_edge(i, j, cost = spMatrix[i, j])
  48. return S

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