Skip to content

Instantly share code, notes, and snippets.

@liudonghua123
Created May 9, 2020 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liudonghua123/c2352a90f1f9d9538233b5ebd1cf17ad to your computer and use it in GitHub Desktop.
Save liudonghua123/c2352a90f1f9d9538233b5ebd1cf17ad to your computer and use it in GitHub Desktop.
import networkx as nx
import matplotlib.pyplot as plt
def generateGraph(nodeCount, edgesPerNode):
# https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.generators.random_graphs.powerlaw_cluster_graph.html
G = nx.random_graphs.powerlaw_cluster_graph(nodeCount, edgesPerNode, 0.5)
nodes = [x for x in G.nodes()]
edges = [x for x in G.edges()]
return nodes, edges, G
def writeFile(fileName, nodes, edges):
file = open(fileName, 'w')
file.writelines('nodes:\n')
file.writelines('{}\n'.format(node) for node in nodes)
file.writelines('edges:\n')
file.writelines('{},{}\n'.format(
str(edge[0]), str(edge[1])) for edge in edges)
file.close()
def drawGraph(G):
nx.draw(G, with_labels=True)
plt.show()
if __name__ == "__main__":
nodes, edges, G = generateGraph(100, 3)
writeFile('data-{nodeLength}-{edgeLength}.txt'.format(
nodeLength=len(nodes), edgeLength=len(edges)), nodes, edges)
drawGraph(G)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment