def testFromSnap(self): name = "testFromSnap" utils.remove_if_file_exit(config.get_data_file_path(name), is_dir=True) G = snap.GenRndGnm(snap.PNGraph, 100, 1000) d = convert.from_snap(name, G, overide=True) print ("testFromSnap", d) utils.remove_if_file_exit(config.get_data_file_path(name), is_dir=True) G = snap.GenRndGnm(snap.PUNGraph, 100, 1000) d = convert.from_snap(name, G, overide=True) print ("testFromSnap", d)
def net_rnd(nodes, edges, is_directed=False, is_multigraph=False, rnd=None): if rnd is None: rnd = sp.TRnd() else: rnd = sp.TRnd(*rnd) if is_directed: if is_multigraph: return sp.GenRndGnm(sp.PNEANet, nodes, edges, is_directed, rnd) else: return sp.GenRndGnm(sp.PNGraph, nodes, edges, is_directed, rnd) else: return sp.GenRndGnm(sp.PUNGraph, nodes, edges, is_directed, rnd)
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 gen_data(): graph = snap.GenRndGnm(snap.PNGraph, 300, 2400, True) snap.SaveEdgeList(graph, "../data/Erdos-Renyi.txt") graph = snap.GenPrefAttach(300, 8) snap.SaveEdgeList(graph, "../data/PrefAttach.txt") graph = snap.GenRndPowerLaw(300, 1.2) snap.SaveEdgeList(graph, "../data/power-law.txt")
def gen_G(D, Pi_minus, Pi_exo, V_exo, theta2, N): """ Returns pairwise-stable network on N nodes. D, Pi_minus, Pi_exo = outputs of gen_D(). V_exo = 'exogenous' part of joint surplus (output of gen_V_exo). theta2 = transitivity parameter (theta[2]). """ G = snap.GenRndGnm(snap.PUNGraph, N, 0) # initialize empty graph Components = snap.TCnComV() snap.GetWccs(D, Components) # collects components of D NIdV = snap.TIntV() # initialize vector for C in Components: if C.Len() > 1: NIdV.Clr() for i in C: NIdV.Add(i) tempnet = gen_G_subgraph(NIdV, D, Pi_minus, Pi_exo, V_exo, theta2) for edge in tempnet.Edges(): G.AddEdge(edge.GetSrcNId(), edge.GetDstNId()) # add robust links for edge in Pi_exo.Edges(): G.AddEdge(edge.GetSrcNId(), edge.GetDstNId()) return G
def gen_D(Pi, V_exo, theta2): """ Returns a triplet of three snap graphs: D = opportunity graph with robust links removed. Pi_minus = subgraph of Pi without robustly absent potential links. Pi_exo = subgraph of Pi with only robust links. NB: This function is specific to the joint surplus used in our simulations. Pi = opportunity graph (in our case, the output of gen_RGG). V_exo = 'exogenous' part of joint surplus (output of gen_V_exo). theta2 = transitivity parameter (theta[2]). """ N = V_exo.shape[0] D = snap.ConvertGraph(snap.PUNGraph, Pi) Pi_minus = snap.ConvertGraph(snap.PUNGraph, Pi) Pi_exo = snap.GenRndGnm(snap.PUNGraph, N, 0) for edge in Pi.Edges(): i = min(edge.GetSrcNId(), edge.GetDstNId()) j = max(edge.GetSrcNId(), edge.GetDstNId()) if V_exo[i, j] + min(theta2, 0) > 0: D.DelEdge(i, j) Pi_exo.AddEdge(i, j) if V_exo[i, j] + max(theta2, 0) <= 0: D.DelEdge(i, j) Pi_minus.DelEdge(i, j) return (D, Pi_minus, Pi_exo)
def test_snap(self): """Test that snap.py installed correctly. """ import snap num_nodes = 20 # Generate different undirected graphs full_graph = snap.GenFull(snap.PUNGraph, num_nodes) star_graph = snap.GenStar(snap.PUNGraph, num_nodes) random_graph = snap.GenRndGnm(snap.PUNGraph, num_nodes, num_nodes * 3) # Basic statistics on the graphs self.assertEqual(snap.CntInDegNodes(full_graph, num_nodes - 1), num_nodes) self.assertEqual(snap.CntOutDegNodes(full_graph, num_nodes - 1), num_nodes) self.assertEqual(snap.GetMxInDegNId(star_graph), 0) self.assertEqual(snap.GetMxOutDegNId(star_graph), 0) # Iterator degree_to_count = snap.TIntPrV() snap.GetInDegCnt(full_graph, degree_to_count) # There should be only one entry (num_nodes - 1, num_nodes) for item in degree_to_count: self.assertEqual(num_nodes - 1, item.GetVal1()) self.assertEqual(num_nodes, item.GetVal2()) # Rewiring rewired_graph = snap.GenRewire(random_graph) for n1 in random_graph.Nodes(): for n2 in rewired_graph.Nodes(): if n1.GetId() == n2.GetId(): self.assertEqual(n1.GetOutDeg() + n1.GetInDeg(), n2.GetOutDeg() + n2.GetInDeg())
def differentNumberOfResultsEnumInNotConnected(): b = open('differentNumberOfResultsEnumInNotConnected.csv', 'w') a = csv.writer(b) data = [["Number of results", "EnumIncNotConnected"]] print "Building a random graph with 1000000 nodes and 10000000 edges." G1 = snap.GenRndGnm(snap.PUNGraph, 1000000, 10 * 1000000) neighbors_dic = InitNeighbors(G1) num_of_nodes = 1000000 noprun.k = 5 time2 = noprun.run(G1, 10000, neighbors_dic) print "%f" % time2 time2 = 0 for i in (1, 10, 100, 1000, 10000): times = [] num_of_nodes = 1000000 prun2.k = 5 time2 = prun2.run(G1, i, neighbors_dic, "") data.append([i, time2]) a.writerows(data) b.close() pretty_file.pretty_file( "differentNumberOfResultsEnumInNotConnected.csv", header=True, border=True, delimiter=",", new_filename="differentNumberOfResultsEnumInNotConnected.txt")
def differentNumberOfResultsWithAllTypes(): b = open('diffrent_number_of_results_for_all_types.csv', 'w') a = csv.writer(b) data = [[ "Number of results", "EnumIncExcConnected", "EnumConnected", "EnumIncExcUnConnected", "EnumUnConnected" ]] print "Building a random graph with 1000000 nodes and 10000000 edges." G1 = snap.GenRndGnm(snap.PUNGraph, 1000000, 10 * 1000000) neighbors_dic = InitNeighbors(G1) for i in (1, 10, 100, 1000, 10000): times = [] num_of_nodes = 1000000 prunconnected.k = 5 time1 = prunconnected.run(G1, i, neighbors_dic, "")[0] noprun.k = 5 time2 = noprun.run(G1, i, neighbors_dic) prun2.k = 5 time3 = prun2.run(G1, i, neighbors_dic, "") noprun.k = 5 time4 = noprun.run(G1, i, neighbors_dic) data.append([i, time1, time2, time3, time4]) a.writerows(data) b.close() pretty_file.pretty_file( "diffrent_number_of_results_for_all_types.csv", header=True, border=True, delimiter=",", new_filename="diffrent_number_of_results_for_all_types.txt")
def comparisionWithStateOfArt(): print "Graph is ready" b = open('comarisionWithStateOfArt.csv', 'w') a = csv.writer(b) data = [['Nodes', 'Edges', "k = 1", "k = 2", "k = 3", "k = 4"]] import all for n, m in [(1000, 14432), (2000, 28709), (4000, 58063), (8000, 116276), (16000, 231622)]: print "Generating a random graph with %s nodes and %s edges" % (n, m) #G1 = BuildGraphFromFile('%sn_%sm' % (n,m)) G1 = snap.GenRndGnm(snap.PUNGraph, n, m) neighbors_dic = InitNeighbors(G1) times = [n, m] for i in (1, 2, 3, 4): imp.reload(all) all.k = i time = all.run(G1, 100000, neighbors_dic, "") times.append(time) data.append(times) a.writerows(data) b.close() pretty_file.pretty_file("comarisionWithStateOfArt.csv", header=True, border=True, delimiter=",", new_filename="comparisionWithStateOfArt.txt")
def differentNumberOfNodesEnum(): b = open('diffrent_number_of_nodes.csv', 'w') a = csv.writer(b) data = [["Number of nodes", "EnumIncExc"]] for i in (1, 10, 100, 1000, 10000): times = [] num_of_nodes = i * 1000 times.append(num_of_nodes) print "Building a random graph with %s nodes and %s edges." % ( num_of_nodes, 10 * num_of_nodes) G1 = snap.GenRndGnm(snap.PUNGraph, num_of_nodes, 10 * num_of_nodes) neighbors_dic = InitNeighbors(G1) noprun.k = 5 time = noprun.run(G1, 1000, neighbors_dic) times.append(time) data.append(times) a.writerows(data) b.close() pretty_file.pretty_file("diffrent_number_of_nodes.csv", header=True, border=True, delimiter=",", new_filename="diffrent_number_of_nodes.txt") print "Results can be found in: diffrent_number_of_nodes.csv"
def createGraphAndWriteToFile(): for n, m in [(1000, 14432), (2000, 28709), (4000, 58063), (8000, 116276), (16000, 131622)]: G1 = snap.GenRndGnm(snap.PUNGraph, n, m) f1 = open('%sn_%sm' % (n, m), 'w') for EI in G1.Edges(): f1.write("%d\t%d\n" % (EI.GetSrcNId(), EI.GetDstNId()))
def init(): G = snap.GenRndGnm(snap.PUNGraph, int(N_NODES), int(N_EDGES)) rioting = [ True if x == 1 else False for x in np.random.binomial(1, PROB, N_NODES) ] return [], G, rioting
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 draw(self): self.read_nodes() self.Network = self.var_network.get() self.nPoints = sum(int(i) for i in self.lst_Nodes) if self.Network == "GenStar": print "GenStar is the network with points ", self.nPoints self.graph = snap.GenStar(snap.PNGraph, self.nPoints, True) if self.Network == "GenRndGnm": print "GenRndGnm is the network with points ", self.nPoints self.graph = snap.GenRndGnm(snap.PNGraph, self.nPoints, self.nPoints) if self.Network == "GenForestFire": print "GenForestFire is the network with points ", self.nPoints self.graph = snap.GenForestFire(self.nPoints, 0.5, 0.5) if self.Network == "GenFull": print "GenFull is the network with points ", self.nPoints self.graph = snap.GenFull(snap.PNGraph, self.nPoints) if self.Network == "GenCircle": print "GenCircle is the network with points ", self.nPoints self.graph = snap.GenCircle(snap.PNGraph, self.nPoints, 10, 10) self.create_nodes(self.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 loadTestNetwork(): print 'Loading test Network' Network = snap.GenRndGnm(snap.PNEANet, 10, 6) print Network.GetEdges() # attr = snap.AddSAttrDat(1, "timestamp", 104) # attr2 = snap.AddSAttrDat(1, "timestamp", 107) for edge in Network.Edges(): # Network.AddIntAttrDatE(edge, 107, 'timestamp') src = edge.GetSrcNId() dst = edge.GetDstNId() print src, dst # if Network.IsNode(src) and Network.IsNode(dst): # Network.AddEdge(src, dst) Network.AddIntAttrDatE(edge.GetId(), 107, 'timestamp') Network.AddIntAttrDatE(edge.GetId(), 35, 'infect') for edge in Network.Edges(): print edge.GetId(), Network.GetIntAttrDatE(edge.GetId(), 'timestamp'), Network.GetIntAttrDatE(edge.GetId(), 'infect') # print edge.GetId(), Network.GetIntAttrDatE(edge.GetId(), 'timestamp') for edge2 in Network.Edges(): # Network.AddIntAttrDatE(edge, 107, 'timestamp') src2 = edge2.GetSrcNId() dst2 = edge2.GetDstNId() print src, dst # if Network.IsNode(src) and Network.IsNode(dst): eid = Network.AddEdge(src2, dst2) Network.AddIntAttrDatE(eid, 407, 'timestamp') # Network.AddIntAttrDatE(edge.GetId(), 35, 'infect') for edge in Network.Edges(): print edge.GetId(), Network.GetIntAttrDatE(edge.GetId(), 'timestamp'), Network.GetIntAttrDatE(edge.GetId(), 'infect')
def main(): if len(sys.argv) != 2: print('File name must be provided') sys.exit(-1) filename = sys.argv[1] filenameWOExt = filename.split('.')[0].split('/')[-1] graph = snap.LoadEdgeList(snap.PNEANet, filename, 0, 1, '\t') snap.PrintInfo(graph, "New York", filenameWOExt + '_info.txt', False) Rnd = snap.TRnd(123124) erdosRenyi = snap.GenRndGnm(snap.PNEANet, graph.GetNodes(), graph.GetEdges(), True, Rnd) snap.PrintInfo(erdosRenyi, "Erdos-Renyi", 'erdos_renyi_info.txt', False) grid = snap.GenGrid(snap.PNEANet, 220, 250, False) snap.PrintInfo(grid, "Grid", 'grid_info.txt', False) printGenericInformation(graph, 'New York street network') printGenericInformation(erdosRenyi, 'Erdos-Renyi random graph') printGenericInformation(grid, 'Grid random graph') # Plot everything in the plots directory os.chdir(os.path.join(os.path.abspath(sys.path[0]), 'plots')) saveDegreeDistribution(graph, 'deg_dist_ny.tab') saveDegreeDistribution(erdosRenyi, 'deg_dist_er.tab') saveDegreeDistribution(grid, 'deg_dist_gr.tab') testRobustnessAll([graph, erdosRenyi, grid]) call(['gnuplot', 'deg_dist.plt']) call(['gnuplot', 'robustness_rand.plt']) call(['gnuplot', 'robustness_max.plt'])
def run(): # Make usre the number of nodes and edges is valid. assert ((args.n * (args.n - 1)) / 2 > args.m) # Generate a random graph with n nodes and m edges. print "Generating a random graph with %s nodes and %s edges" % (args.n, args.m) G1 = snap.GenRndGnm(snap.PUNGraph, args.n, args.m) find_kplex(G1)
def __init__(self, nodes, edges, edge_list=None): if edge_list is None: G = snap.GenRndGnm(snap.PUNGraph, nodes, edges) self.graph = snap.GetMxWcc(G) else: self.graph = snap.LoadEdgeList(snap.PUNGraph, edge_list, 0, 1) self.assignment = {} self.max_type, self.min_type = None, None
def BfsDfsTest(NNodes, NEdges): Graph = snap.GenRndGnm(NNodes, NEdges, 1) snap.GetBfsTree(Graph, False, False, 1) G2 = snap.GetBfsTree(Graph, False, False, 1) return G2
def random_network(n_edges, size, trnd): if int(n_edges) > size * (size - 1) / 2: raise ValueError("{} is too many edges for {} nodes".format( int(n_edges), size)) graph = snap.GenRndGnm( snap.PUNGraph, int(size), int(n_edges), False, trnd ) #This should raise an error when too many edges but instead it just hangs return graph
def allGraphs(self): #loading of ten graphs to mine - Im sure there is faster way to go across the data set #.edges gives ego network - how many friendships person has self.G1 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G2 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G3 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G4 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G5 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G6 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G7 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G8 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G9 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.G10 = snap.GenRndGnm(snap.PNGraph, 10000, 5000, False) self.graphs = [ self.G1, self.G2, self.G3, self.G4, self.G5, self.G6, self.G7, self.G8, self.G9, self.G10 ] ##
def GVizTest(NNodes, NEdges): Graph = snap.GenRndGnm(NNodes, NEdges, 1) FName = "test.png" snap.DrawGViz(Graph, 1, snap.TStr(FName), snap.TStr("Snap Ringo Dot"), 1) return os.path.exists(FName)
def erdos_renyi(): GUn = transform_directed_to_undirected() # Erdos-Renyi random graph GER = snap.GenRndGnm(snap.PNGraph, G.GetNodes(), G.GetEdges()) snap.PrintInfo(GER, "Tweets Random Stats", "Tweets_Random-info.txt", False) GUn.GetEdges() f = open('Tweets_Random-info.txt', 'r') file_contents = f.read() print(file_contents) f.close()
def __generate_SNAP_graphs(self, graphs_num): graphs = [] for i in range(graphs_num): graphs.append( snap.GenRndGnm( (snap.PNGraph if self.__graph_generator_config['directed'] else snap.PUNGraph), self.__graph_generator_config['nodes_num'], self.__graph_generator_config['edges_num'], self.__graph_generator_config['directed'])) return graphs
def generate_has_euler_path_but_not_circuit(self, num_nodes: int = 10): # Generate an Erdos-Renyi random graph with num_nodes and zero edges. graph = snap.GenRndGnm(snap.PUNGraph, num_nodes, 0) # Create a connected graph. graph = self.create_connected_graph(graph) # Create a graph with an Euler path. graph = self.create_euler_graph(graph) return graph
def random_network(self): d = RnDialog(self.root) self.root.wait_window(d.top) nodes = d.nodes edges = d.edges self.graph = snap.GenRndGnm(snap.PUNGraph, nodes, edges) self.graph_name = "Random Network" self.add_characteristics(self.graph, self.graph_name)
def gen_er(args): """Generate a ER Graph""" for i in range(args.num_graphs): num_edges = int( np.random.uniform((args.num_vertices / 2), (args.num_vertices * 2))) Graph = snap.GenRndGnm(snap.PNGraph, args.num_vertices, num_edges) snap.SaveEdgeList(Graph, f'{args.data_loc}/ER/ER_{i}.edges') print(f"ER Graph {i} Generated and Saved")
def gen_RGG(positions, r): """ Returns an RGG from a given N-vector of dx1 positions (Nxd matrix). positions = vector of node positions. r = linking threshold. """ kdtree = spatial.KDTree(positions) pairs = kdtree.query_pairs(r) # default is Euclidean norm RGG = snap.GenRndGnm(snap.PUNGraph, len(positions), 0) for edge in (i for i in list(pairs)): RGG.AddEdge(edge[0], edge[1]) return RGG