Created
May 9, 2020 13:28
-
-
Save liudonghua123/c2352a90f1f9d9538233b5ebd1cf17ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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