def generate_graph(NNodes, NEdges, Model, Rnd): Graph = None if Model == 'rand_ungraph': # GnRndGnm returns error, so manually generate Graph = snap.GenRndGnm_PUNGraph(NNodes, NEdges, 0) elif Model == 'rand_ngraph': Graph = snap.GenRndGnm_PNGraph(NNodes, NEdges, 1) elif Model == 'rand_neagraph': Graph = snap.GenRndGnm_PNEANet(NNodes, NEdges, 1) elif Model == 'syn_neagraph': Graph = snap.GenSyntheticGraph_PNEANet(NNodes, NEdges/NNodes, SYNTHETIC_DELTA) elif Model == 'syn_ngraph': Graph = snap.GenSyntheticGraph_PNGraph(NNodes, NEdges/NNodes, SYNTHETIC_DELTA) elif Model == 'rmat': Graph = snap.GenRMat(NNodes, NEdges, 0.40, 0.25, 0.2, Rnd) elif Model == 'sw': Graph = snap.GenSmallWorld(NNodes, NNodes/NEdges, 0.1) elif Model == 'pref': Graph = snap.GenPrefAttach(NNodes, NNodes/NEdges) else: print "Unknown model: %s" % Model sys.exit(1) return Graph
def main(): RANDOM_SEED = 23 SYNTHETIC_NW_NODES = 4846609 # How many nodes in the fake networks. SYNTHETIC_NW_EDGES = 42851237 # How many nodes in the fake networks. SYNTHETIC_NW_AVG_DEGREE = int(SYNTHETIC_NW_EDGES / SYNTHETIC_NW_NODES) random.seed(RANDOM_SEED) print "Generating preferential attachment graph..." tRnd = snap.TRnd() tRnd.PutSeed(RANDOM_SEED) # Re-seed every time. PAGraph = snap.GenPrefAttach(SYNTHETIC_NW_NODES, SYNTHETIC_NW_AVG_DEGREE, tRnd) filename = 'PrefAttachSynthetic-4.8M.txt' print "Saving edge list to file: %s" % filename snap.SaveEdgeList(PAGraph, filename, 'Synthetic preferential attachment graph') print "Generating random graph..." tRnd.PutSeed(RANDOM_SEED) # Re-seed every time. RndGraph = snap.GenRndGnm(snap.PUNGraph, SYNTHETIC_NW_NODES, SYNTHETIC_NW_EDGES, False, tRnd) filename = 'GnmRandomGraph-4.8M.txt' print "Saving edge list to file: %s" % filename snap.SaveEdgeList(RndGraph, filename, 'Random Gnm graph') print "Generating small world graph..." tRnd.PutSeed(RANDOM_SEED) # Re-seed every time. SWGraph = snap.GenSmallWorld(SYNTHETIC_NW_NODES, SYNTHETIC_NW_AVG_DEGREE, 0.1, tRnd) filename = 'SmallWorldGraph-4.8M.txt' print "Saving edge list to file: %s" % filename snap.SaveEdgeList(RndGraph, filename, 'Small world graph with rewire prob=0.1') print "Done" sys.exit(0)
def generate_friends_network(user_count=10, friends_count=get_friends_count(), prob=get_prob(), network_type='random', conf_model=False): # User list, probability p and network_type is given, construct a graph res = "empty" # generate specific graphs if network_type.lower().find("small") >= 0: # This is a small world network print "Creating a small world network with " + str( user_count) + " users" rnd = snap.TRnd(1, 0) new_graph = snap.GenSmallWorld(user_count, friends_count, prob, rnd) elif network_type.lower().find("ring") >= 0: # A ring graph print "Creating a ring with " + str(user_count) + " users" new_graph = snap.GenCircle(snap.PUNGraph, user_count, friends_count) elif network_type.lower().find("power") >= 0: # Power Law network print "Creating a powerlaw network with " + str(user_count) + " users" new_graph = snap.GenRndPowerLaw(user_count, friends_count, conf_model) else: # A random graph print "Creating a random network with " + str(user_count) + " users" edge_count = user_count * friends_count new_graph = snap.GenRndGnm(snap.PUNGraph, user_count, edge_count) save_graph_in_db(new_graph)
def generate_graph(NNodes, NEdges, Model, Type, Rnd): if Model == 'rand_ungraph': # GnRndGnm returns error, so manually generate Graph = snap.GenRndGnm_PUNGraph(NNodes, NEdges, 0) elif Model == 'rand_ngraph': Graph = snap.GenRndGnm_PNGraph(NNodes, NEdges, 1) elif Model == 'rand_neanet': Graph = snap.GenRndGnm(NNodes, NEdges, 1) elif Model == 'syn_neanet': Graph = snap.GenSyntheticGraph(NNodes, NEdges / NNodes, SYNTHETIC_DELTA) elif Model == 'syn_ngraph': Graph = snap.GenSyntheticGraph_PNGraph(NNodes, NEdges / NNodes, SYNTHETIC_DELTA) elif Model == 'rmat': Graph = snap.GenRMat(NNodes, NEdges, 0.40, 0.25, 0.2, Rnd) elif Model == 'sw': Graph = snap.GenSmallWorld(NNodes, NNodes / NEdges, 0.1) elif Model == 'pref': Graph = snap.GenPrefAttach(NNodes, NNodes / NEdges) return Graph
def gen_sw(args): """Generate a SW Graph""" for i in range(args.num_graphs): fp = np.random.uniform(0, 0.5) Rnd = snap.TRnd() Graph = snap.GenSmallWorld(args.num_vertices, 3, fp, Rnd) snap.SaveEdgeList(Graph, f'{args.data_loc}/SW/SW_{i}.edges') print(f"SW Graph {i} Generated and Saved")
def generate_watts_strogatz_model(self, num_nodes: int = 10, out_degree: int = None, rewire_prob: float = 0.5): if out_degree is None: # Generate a random out degree in [5, 20]. out_degree = random.randint(5, 20) rnd = snap.TRnd(1, 0) graph = snap.GenSmallWorld(num_nodes, out_degree, rewire_prob, rnd) return graph
def generate_graphs(): for path in GRAPHS: name = path.split('/')[-1].split('.')[0] metrics = Metrics(path, True).calculate_basic() print metrics # Generate Erdos-Renyi (Random) Graph # args: type, num_nodes, num_edges er = snap.GenRndGnm(snap.PNGraph, metrics.num_nodes, metrics.num_edges) snap.SaveEdgeList(er, "{}_er.elist".format(name)) # Generate Watts-Strogatz (Small World) Graph # args: num_nodes, node_out_degree (average out degree will be twice this value, rewire_prob) ws = snap.GenSmallWorld(metrics.num_nodes, int(metrics.avg_degree) / 2, 0.2) snap.SaveEdgeList(ws, "{}_ws.elist".format(name)) # Generate Barabasi-Albert model (scale-free with preferential attachment) Graph # args: (num_nodes, degree of each node desired) ba = snap.GenPrefAttach(metrics.num_nodes, int(metrics.avg_degree) / 2) snap.SaveEdgeList(ba, "{}_ba.elist".format(name)) # Generate Forest Fire model Graph # args: (num_nodes, forward_prob, backward_prob) if name == "USairport_2010": ff = snap.GenForestFire( metrics.num_nodes, 0.3599, 0.3599) # Selected value for US Airports data-set snap.SaveEdgeList(ff, "{}_ff.elist".format(name)) ff = snap.GenForestFire(int(metrics.num_nodes / 10), 0.3599, 0.3599) snap.SaveEdgeList(ff, "{}_ffdiv10.elist".format(name)) ff = snap.GenForestFire(metrics.num_nodes * 10, 0.3599, 0.3599) snap.SaveEdgeList(ff, "{}_ffx10.elist".format(name)) else: ff = snap.GenForestFire(metrics.num_nodes, 0.3467, 0.3467) # selected snap.SaveEdgeList(ff, "{}_ff.elist".format(name)) ff = snap.GenForestFire(int(metrics.num_nodes / 10), 0.3467, 0.3467) snap.SaveEdgeList(ff, "{}_ffdiv10.elist".format(name)) ff = snap.GenForestFire(metrics.num_nodes * 10, 0.3467, 0.3467) snap.SaveEdgeList(ff, "{}_ffx10.elist".format(name))
def __init__(self, beta, delta, graphType, rewireProbInput, numNodes, scalingFactor): self.graphType = graphType self.numNodes = numNodes # initialize snap.py graph: self.G # for degree, use reproductive number 1.5 = degree * beta: on avg, ebola patient should infect 1.5 other people # nodeDeg = math.ceil((1.5 / beta) / scalingFactor) nodeDeg = math.ceil( 10.0 / scalingFactor) # most real world graphs have node degree 2~15 if graphType == 'small world': rewireProb = rewireProbInput # probability that an edge will be rewired in Watts-Strogatz model self.G = snap.GenSmallWorld( numNodes, int(nodeDeg / 2), rewireProb) # for snap function, must divide degree by 2 elif graphType == 'random': numEdges = numNodes * nodeDeg / 2 self.G = snap.GenRndGnm(snap.PUNGraph, numNodes, numEdges) elif graphType == 'complete': self.G = snap.GenFull(snap.PUNGraph, numNodes) elif graphType == 'scale free': self.G = snap.GenPrefAttach(numNodes, nodeDeg) else: raise NotImplementedError( "graphType argument must be in {'random', 'small world', 'complete', 'scale free}" ) # initialize variables for SIR model self.beta, self.delta = beta, delta self.state = dict.fromkeys( xrange(numNodes), SUSCEPTIBLE ) # dict: int nodeID => string SUSCEPTIBLE or INFECTED or RECOVERED self.countyHasBeenInfected = False
def generate_graph(NNodes, NEdges, Model, Type, Rnd): if Model == 'rand_ungraph': # GnRndGnm returns error, so manually generate #Graph = snap.GenRndGnm_PUNGraph(NNodes, NEdges, 0) Graph = snap.GenRndGnm(snap.PUNGraph, NNodes, NEdges, 0) elif Model == 'rand_ngraph': #Graph = snap.GenRndGnm_PNGraph(NNodes, NEdges, 1) Graph = snap.GenRndGnm(snap.PNGraph, NNodes, NEdges, 1) elif Model == 'rand_neanet': print "1", NNodes, NEdges #Graph = snap.GenRndGnm_PNEANet(NNodes, NEdges, 1) Graph = snap.GenRndGnm(snap.PNEANet, NNodes, NEdges, 1) print "2" print "3", Graph.GetNodes(), Graph.GetEdges() elif Model == 'syn_neanet': Graph = snap.GenSyntheticGraph(NNodes, NEdges / NNodes, SYNTHETIC_DELTA) elif Model == 'syn_ngraph': Graph = snap.GenSyntheticGraph_PNGraph(NNodes, NEdges / NNodes, SYNTHETIC_DELTA) elif Model == 'rmat': Graph = snap.GenRMat(NNodes, NEdges, 0.40, 0.25, 0.2, Rnd) elif Model == 'sw': Graph = snap.GenSmallWorld(NNodes, NNodes / NEdges, 0.1) elif Model == 'pref': Graph = snap.GenPrefAttach(NNodes, NNodes / NEdges) return Graph
def SimpleNetworkGenerator(self, params): #print params Rnd = snap.TRnd(1, 0) G = None try: if params[0] == GlobalParameters.NetworkType[0]: G = snap.GenSmallWorld(int(params[1]), int(params[2]), float(params[3]), Rnd) if params[0] == GlobalParameters.NetworkType[1]: G = snap.GenPrefAttach(int(params[1]), int(params[2]), Rnd) if params[0] == GlobalParameters.NetworkType[2]: G = snap.GenForestFire(int(params[1]), float(params[2]), float(params[3])) if params[0] == GlobalParameters.NetworkType[3]: G = snap.GenRndGnm(snap.PUNGraph, int(params[1]), int(params[2]), False, Rnd) if params[0] == GlobalParameters.NetworkType[4]: G = snap.GenCircle(snap.PUNGraph, int(params[1]), int(params[2]), False) if params[0] == GlobalParameters.NetworkType[5]: G = snap.GenFull(snap.PUNGraph, int(params[1])) return G except: return None
import snap Rnd = snap.TRnd(1, 0) Graph = snap.GenSmallWorld(1000, 5, 0.12, Rnd) snap.SaveEdgeList(Graph, 'mygraph.txt')
with open("EROutput.txt", 'w+') as fp: header = str(ER_Nodes) + ',' + str(ER_Edges) + '\n' fp.write(header) timesteps = graphER.values_at_each for i in range(0, len(timesteps)): line = str(timesteps[i][0]) + ',' + str(timesteps[i][1]) + ',' + str( timesteps[i][2]) + ',' + str(timesteps[i][3]) + '\n' fp.write(line) print "Finished outputting\n" WS_Nodes = 50000 WS_Edges = 1000 #Single instance for WS graph. gen.PutSeed(current_seed) graphWS = Graph(alpha, zeta) WS = snap.GenSmallWorld(WS_Nodes, WS_Edges, 0, gen) for it in WS.Edges(): graphWS.AddEdge(it.GetSrcNId(), it.GetDstNId()) #Choose random infected. infected = random.randint(0, len(graphWS.verts)) graphWS.states[infected] = (0, 1, 0, 0) print "WS graph:" start = time.time() graphWS.do_simulation(10000) end = time.time() print "Simulation took", (end - start) print "Writing to output:" with open("WSOutput.txt", 'w+') as fp: header = str(WS_Nodes) + ',' + str(WS_Edges) + '\n' fp.write(header)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sun Aug 19 11:10:05 2018 Small world experiments @author: Erik G. Larsson, 2018-2019 """ import sys sys.path.append("/courses/tsks11/ht2019/snap-4.1.0-4.1-centos6.5-x64-py2.6/") import snap # circle network G1 = snap.GenSmallWorld(1000, 2, 0) print snap.GetBfsEffDiam(G1, 50, False) # Watt-Strogatz, 0.01 G2 = snap.GenSmallWorld(1000, 2, 0.001) print snap.GetBfsEffDiam(G2, 50, False) # Watt-Strogatz, 0.1 G3 = snap.GenSmallWorld(1000, 2, 0.1) print snap.GetBfsEffDiam(G3, 50, False) # Amazon network G = snap.LoadEdgeList( snap.PUNGraph, "/courses/TSKS11/ht2019/data_and_fcns/session4/task1/amazon0302.txt", 0, 1) # snap.PrintInfo(G, "amazon", "_amazon-info.txt", False)
def manage_graphs(out_degree, nodes, max_minutes): rnd = snap.TRnd(1, 0) graph = snap.GenSmallWorld(nodes, out_degree, 0.7, rnd) print(40 * "#") print(f"Starting Graph for #{nodes} Nodes.") # Save the graph in order to reload it after manipulation output_filename = f"temporary_graphs/{nodes}_ws_graph.graph" f_out = snap.TFOut(output_filename) graph.Save(f_out) f_out.Flush() # Highest rank Node max_degree_node = graph.GetNI(snap.GetMxDegNId(graph)) print(f"Highest Degree Node ID#{max_degree_node.GetId()}" f" with Degree={max_degree_node.GetDeg()}") # Gets Hubs and Authorities of all the nodes hubs_per_node = snap.TIntFltH() auths_per_node = snap.TIntFltH() snap.GetHits(graph, hubs_per_node, auths_per_node) max_hub_node = graph.GetNI( max(hubs_per_node, key=lambda h: hubs_per_node[h])) print(f"Highest Hub Score Node ID#{max_hub_node.GetId()}" f" with Score={hubs_per_node[max_hub_node.GetId()]}") max_authority_node = graph.GetNI( max(auths_per_node, key=lambda a: auths_per_node[a])) print(f"Highest Authority Score Node ID#{max_authority_node.GetId()}" f" with Score={hubs_per_node[max_authority_node.GetId()]}") exceed = False # CNM Community Detector cnm_community = snap.TCnComV() cnm_thread = threading.Thread(target=snap.CommunityCNM, args=(graph, cnm_community)) cnm_start_time = time.time() try: cnm_thread.start() cnm_thread.join(max_minutes) except MemoryError: exceed = True finally: cnm_stop_time = time.time() cnm_community_exec_time = cnm_stop_time - cnm_start_time exceed |= max_minutes <= cnm_community_exec_time # GN Community Detector if max_minutes > cnm_community_exec_time and not exceed: gn_community = snap.TCnComV() gn_thread = threading.Thread(target=snap.CommunityGirvanNewman, args=(graph, gn_community)) gn_start_time = time.time() try: gn_thread.start() gn_thread.join(max_minutes - cnm_community_exec_time) except MemoryError: exceed = True finally: gn_stop_time = time.time() gn_community_exec_time = gn_stop_time - gn_start_time exceed |= gn_community_exec_time >= max_minutes - cnm_community_exec_time else: gn_community_exec_time = 0.00 if not exceed: print( f"Execution Time for CNM Communities Detector is {round(cnm_community_exec_time, 4):.4f}" ) print( f"Execution Time for GN Communities Detector is {round(gn_community_exec_time, 4):.4f}" ) else: print( f"Graph with Nodes#{nodes_num} exceeded the valid calculation limits." ) print(40 * "#") # load graph in it's initial State f_in = snap.TFIn(output_filename) graph = snap.TUNGraph.Load(f_in) return graph, cnm_community_exec_time, gn_community_exec_time, exceed
for line in iter(fin): ugraph.AddNode(int(line)) numnod += 1 node_list.append(int(line)) fin.close() fin = open("An_Edges.txt", "rb") for line in iter(fin): ugraph.AddEdge(int(line.split(",", 1)[0]), int(line.split(",", 1)[1])) numedg += 1 fin.close() rand_grph = snap.GenRndGnm(snap.PUNGraph, numnod, numedg, False) snap.PlotInDegDistr(rand_grph, "degDistRand", "degDistRand") #rand_grph.GetBfsEffDiam(ugraph,1,False,) clust = snap.GetClustCf(rand_grph) print "C Coeff Rand" + str(clust) pref_grph = snap.GenPrefAttach(numnod, 4) snap.PlotInDegDistr(pref_grph, "degDistPref", "degDistPref") clust = snap.GetClustCf(pref_grph) print "C Coeff Pref" + str(clust) prob = (float(numnod) / (numedg * (numedg - 1))) * 2 print prob smal_grph = snap.GenSmallWorld(numnod, 4, prob) snap.PlotInDegDistr(smal_grph, "degDistSmal", "degDistSmal") clust = snap.GetClustCf(smal_grph) print "C Coeff Small" + str(clust)
def small_world(nodes, out_degree, rewire_pb, rnd=None): if rnd is None: rnd = sp.TRnd() else: rnd = sp.TRnd(*rnd) return sp.GenSmallWorld(nodes, out_degree, rewire_pb, rnd)
import snap Rnd = snap.TRnd(1,0) #smallWorldG = snap.GenSmallWorld(485, 9, 0, Rnd) #snap.SaveEdgeList(smallWorldG, "SampleSWGraph.txt", "SaveGraph") smallWorldG = snap.GenSmallWorld(4846609, 9, 0, Rnd) snap.SaveEdgeList(smallWorldG, "SWGraph.txt", "SaveGraph")
with open(file, "r") as f: for x in f: edges += 1 print "nodes: %d, edges: %d" % (nodes, edges) path = os.path.join(r"p3_data", filename[3:]) print "starting WS graph at time %s\n" % time.ctime() edgeList = [] outDegree = 0 OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(inGraph, OutDegV) #Sums the value of all out degrees of each node for item in OutDegV: outDegree += item.GetVal2() # print "node ID %d: out-degree %d" % (item.GetVal1(), item.GetVal2()) # averages the out degree in OutDeg Vector, and rounds it to the nearest integar avgOutDegree = round(outDegree / nodes) # print avgOutDegree Rnd = snap.TRnd(1, 0) smallWorldGraph = snap.GenSmallWorld(nodes, int(avgOutDegree), 0, Rnd) for EI in smallWorldGraph.Edges(): edgeList.append([EI.GetSrcNId(), EI.GetDstNId()]) with open(path, "w") as f: for x in edgeList: f.write('{0:4d} {1:9d}\n'.format(x[0], x[1])) print "finished WS graph at time %s\n" % time.ctime()
Created on Sun Aug 19 11:10:05 2018 Small world experiments @author: Erik G. Larsson, 2018-2019 """ import sys import snap sys.path.append("/courses/tsks11/ht2019/snap-4.1.0-4.1-centos6.5-x64-py2.6/") # circle network # Generates and returns a random small-world graph using the Watts-Strogatz model. # We assume a circle where each node creates links to NodeOutDeg other nodes. G1 = snap.GenSmallWorld(nodes=1000, nodeOutDeg=2, rewireProb=0) # Returns approximate Effective Diameter(90-th percentile of the distribution of shortest # path lengths) of a graph(by performing BFS from NTestNodes random starting nodes). print snap.GetBfsEffDiam(Graph=G1, NTestNodes=50, IsDir=False) # Watt-Strogatz, 0.01 G2 = snap.GenSmallWorld(Nodes=1000, NodeOutDeg=2, RewireProb=0.01) print snap.GetBfsEffDiam(Graph=G2, NTestNodes=50, IsDir=False) # Watt-Strogatz, 0.1 G3 = snap.GenSmallWorld(Nodes=1000, NodeOutDeg=2, RewireProb=0.01) print snap.GetBfsEffDiam(Graph=G3, NTestNodes=50, IsDir=False) # Amazon network # Loads a (directed, undirected or multi) graph from a text file InFNm
from snappyer import * import snap # not necessary, just for test # --- # Create a graph from an edge file graph = SnapGraph.fromEdgeFile("test_graph.txt", SnapGraph.TYPE_DIRECTED, 0, 1) # also TYPE_UNDIRECTED, TYPE_NETWORK # or make a blank graph blankGraph = SnapGraph.Empty(SnapGraph.TYPE_UNDIRECTED) blankGraph.addNode(1) blankGraph.addNode(2) blankGraph.addEdge(1, 2) # or use raw snap.py functions to create, then import to Snappyer rawGraph = snap.GenSmallWorld(100, 10, 0.5) graph = SnapGraph(rawGraph) # --- # the niceness of all functions is >= snap.py niceness # in general, we try to use properties wherever possible, instead of functions. nodeTen = graph.node(10) nodeTen = graph[10] # alias for .node() for node in graph.nodes: # graph.nodes returns an iterator of SnapNodes print node.id # or any other SnapNode property for edge in graph.edges: print "%s -> %s" % (edge.source, edge.destination ) # .source and .destination are both SnapNodes
def run_ws_simulation(num_nodes, num_edges, rewire_prob, alpha, zeta, *args, **kwargs): r = snap.GenSmallWorld(num_nodes, num_edges, rewire_prob, gen) return run_simulation(r, alpha, zeta, *args, **kwargs)
import snap import sys #load graph input_file = sys.argv[1] Graph = snap.LoadEdgeList(snap.PUNGraph,input_file, 0, 1) n = Graph.GetNodes() e = Graph.GetEdges() SumDeg = 0 InDegV = snap.TIntPrV() snap.GetNodeInDegV(Graph, InDegV) for item in InDegV: SumDeg += item.Val2() MeanDeg = SumDeg/n #ER Graph ERGraph = snap.GenRndGnm(snap.PUNGraph, n, e) snap.SaveEdgeList(ERGraph,'USairport_2010_er.elist.txt') #WS Graph WSGraph = snap.GenSmallWorld(n, MeanDeg, 0.12) snap.SaveEdgeList(WSGraph,'USairport_2010_ws.elist.txt') #BA Graph BAGraph = snap.GenPrefAttach(n, MeanDeg) snap.SaveEdgeList(BAGraph,'USairport_2010_ba.elist.txt')