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.

graph_synthesizer.py 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 11 18:10:06 2020
  5. @author: ljia
  6. """
  7. import numpy as np
  8. import networkx as nx
  9. import random
  10. class GraphSynthesizer(object):
  11. def __init__(self):
  12. pass
  13. def unified_graphs(self, num_graphs=1000, num_nodes=100, num_edges=196, num_node_labels=0, num_edge_labels=0, seed=None, directed=False):
  14. max_num_edges = int((num_nodes - 1) * num_nodes / 2)
  15. if num_edges > max_num_edges:
  16. raise Exception('Too many edges.')
  17. all_edges = [(i, j) for i in range(0, num_nodes) for j in range(i + 1, num_nodes)] # @todo: optimize. No directed graphs.
  18. graphs = []
  19. for idx in range(0, num_graphs):
  20. g = nx.Graph()
  21. if num_node_labels > 0:
  22. for i in range(0, num_nodes):
  23. node_labels = np.random.randint(0, high=num_node_labels, size=num_nodes)
  24. g.add_node(str(i), node_label=node_labels[i])
  25. else:
  26. for i in range(0, num_nodes):
  27. g.add_node(str(i))
  28. if num_edge_labels > 0:
  29. edge_labels = np.random.randint(0, high=num_edge_labels, size=num_edges)
  30. for i in random.sample(range(0, max_num_edges), num_edges):
  31. node1, node2 = all_edges[i]
  32. g.add_edge(node1, node2, edge_label=edge_labels[i])
  33. else:
  34. for i in random.sample(range(0, max_num_edges), num_edges):
  35. node1, node2 = all_edges[i]
  36. g.add_edge(node1, node2)
  37. graphs.append(g)
  38. return graphs

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