void CDLib::generate_clique_graph(graph& g, id_type size) { init_empty_graph(g, size); for (id_type i = 0; i < g.get_num_nodes(); i++) for (id_type j = 0; j < i; j++) if (i != j) g.add_edge(i, j, 1); g.set_graph_name("clique_" + T2str<id_type > (size)); }
void CDLib::generate_barabasi_albert_model(graph& g, size_t num_nodes, size_t min_degree_of_node) { init_empty_graph(g, num_nodes); vector<id_type> vertices_with_edges; UniformRandomGeneratorAkash<id_type> randint; UniformRandomGeneratorAkash<double> randdouble; for (id_type k = 1; k <= min_degree_of_node; k++) { g.add_edge(0, k, 1); vertices_with_edges.push_back(k); } for (id_type i = 1; i < num_nodes; i++) { while (g.get_node_in_degree(i) < min_degree_of_node) { double R1 = randdouble.next(1); if (R1 < 0.5) { id_type R2 = randint.next(vertices_with_edges.size()); g.add_edge(i, vertices_with_edges[R2], 1); vertices_with_edges.push_back(vertices_with_edges[R2]); } else { id_type R3 = randint.next(i); g.add_edge(i, R3, 1); vertices_with_edges.push_back(R3); } } } g.set_graph_name("ba_" + T2str<size_t > (num_nodes) + "_" + T2str<size_t > (min_degree_of_node)); }
bool CDLib::read_edgelist(graph& g, const string& filepath) { g.clear(); ifstream ifs; ifs.open(filepath.c_str()); if (ifs.is_open()) { vector<string> units; double weight; while (!ifs.eof()) { weight = 1; string line; getline(ifs, line); if ((line.size() > 0) && (line[0] != '#')) { split(line, units); if (units.size() != 0) { if ((units.size() < 2) || (units.size() > 3)) return false; if (units.size() == 3) weight = str2T<double>(units[2]); g.add_node(units[0]); g.add_node(units[1]); g.add_edge(units[0], units[1], weight); } } } g.set_graph_name(filename(filepath)); return true; } return false; }
void CDLib::generate_LEET_chord_graph(graph& g, id_type num_nodes) { // init_empty_graph(g,num_nodes); // id_type cluster_size = log2(g.get_num_nodes()); // for (id_type i=0; i<g.get_num_nodes(); i++) // { // g.add_edge(i,(i-1) % g.get_num_nodes(),1); // g.add_edge(i,(i+1) % g.get_num_nodes(),1); // id_type id_in_cluster = g.get_num_nodes() % static_cast<id_type>(cluster_size); // g.add_edge(i,id_in_cluster*(1+static_cast<id_type>(cluster_size)),1); // } init_empty_graph(g, num_nodes); id_type num_clusters = num_nodes / log2(num_nodes); id_type num_nodes_per_cluster = static_cast<id_type> (log2(num_nodes)); for (id_type i = 0; i < num_clusters; i++) { id_type start_id = i*num_nodes_per_cluster; //Adding the individual Clusters for (id_type j = 0; j < num_nodes_per_cluster; j++) { g.add_edge(start_id + j, start_id + ((j - 1) % num_nodes_per_cluster), 1); for (id_type k = 1; k <= num_nodes_per_cluster / 2; k *= 2) g.add_edge(start_id + j, start_id + ((j + k) % num_nodes_per_cluster), 1); } //Adding the long range links for (id_type j = log2(num_clusters) - 1, k = 0; j > 0; j--, k++) { id_type next_cluster_id = ((i + j) % num_clusters) * num_nodes_per_cluster; g.add_edge(start_id + (k % num_nodes_per_cluster), next_cluster_id + (j % num_nodes_per_cluster), 1); } } g.set_graph_name("leet_chord_" + T2str<id_type > (num_nodes)); g.remove_isolates(); }
void CDLib::generate_kademlia_graph(graph& g, id_type num_nodes) { init_empty_graph(g, num_nodes); unsigned int num_bits = log2(num_nodes); for (unsigned int i = 0; i < num_nodes; i++) for (unsigned int j = 0; j < num_bits; j++) g.add_edge(i, (i ^ ((1 << j))) % num_nodes, 1); g.set_graph_name("kademlia_" + T2str<id_type > (num_nodes)); }
void CDLib::generate_chord_graph(graph& g, id_type num_nodes) { init_empty_graph(g, num_nodes); for (id_type i = 0; i < g.get_num_nodes(); i++) { g.add_edge(i, (i - 1) % g.get_num_nodes(), 1); for (id_type j = 1; j <= g.get_num_nodes() / 2; j *= 2) g.add_edge(i, (i + j) % g.get_num_nodes(), 1); } g.set_graph_name("chord_" + T2str<id_type > (num_nodes)); }
void CDLib::generate_spoke_graph(graph& g, id_type size) { generate_star_graph(g, size); id_type last_id = g.get_num_nodes() - 1; if (last_id > 1) { for (id_type i = 1; i < last_id; i++) g.add_edge(i, (i + 1), 1); g.add_edge(last_id, 1, 1); } g.set_graph_name("spoke_" + T2str<id_type > (size)); }
void CDLib::generate_erdos_renyi_graph(graph& g, id_type num_nodes, double p) { if (p >= 0 && p <= 1) { init_empty_graph(g, num_nodes); RandomGenerator<double> p_gen(0, 1, 1); for (id_type i = 0; i < num_nodes; i++) for (id_type j = 0; j < num_nodes; j++) if ((!g.is_directed() && i < j) && p_gen.next() <= p) g.add_edge(i, j, 1); g.set_graph_name("er_" + T2str<id_type > (num_nodes) + "_" + T2str<double>(p)); } }
void CDLib::generate_de_bruijn_graph(graph& g, id_type num_symbols, id_type sequence_length) { if (num_symbols && sequence_length) { id_type size = (unsigned long) pow((double) num_symbols, (double) sequence_length); init_empty_graph(g, size); for (id_type i = 0; i < g.get_num_nodes(); i++) { id_type basis = (i * num_symbols) % g.get_num_nodes(); for (id_type j = 0; j < num_symbols; j++) g.add_edge(i, basis + j, 1); } g.set_graph_name("db_" + T2str<id_type > (num_symbols) + "_" + T2str<id_type > (sequence_length)); } }
void CDLib::generate_scale_free_graph(graph& g, id_type num_nodes, id_type num_edges, double alpha, double beta) { init_empty_graph(g, num_nodes); RandomGenerator<id_type> x_gen(0, num_nodes), from_gen(0, num_nodes), to_gen(0, num_nodes); vector<id_type> outdegrees(num_nodes, 0.0); for (id_type i = 0; i < num_nodes; i++) outdegrees[i] = (id_type) x_gen.exp_next(alpha, beta); for (id_type i = 0; i < num_edges; i++) { id_type from_id = from_gen.next(), to_id = to_gen.next(); if (outdegrees[from_id] && outdegrees[to_id] && from_id != to_id && !g.get_edge_weight(from_id, to_id)) g.add_edge(from_id, to_id, 1); } g.set_graph_name("sf_" + T2str<id_type > (num_nodes) + "_" + T2str<id_type > (num_edges) + "_" + T2str<double>(alpha) + "_" + T2str<double>(beta)); }
/*degree distribution of this model follows power law distribution. * this is a most suitable synthetic model to real-world network like peer to peer networks and citation networks.*/ bool CDLib::generate_vertex_copying_model(graph& g, size_t num_nodes, size_t num_of_out_degree, size_t num_of_vertices_at_initial, double probability_to_copy_from_existing_vertex) { if (num_of_vertices_at_initial > num_of_out_degree && probability_to_copy_from_existing_vertex >= 0 && probability_to_copy_from_existing_vertex <= 1) { UniformRandomGeneratorAkash<wt_t> randdouble; UniformRandomGeneratorAkash<id_type> randint; init_empty_graph(g, num_nodes); for (id_type i = 0; i < num_of_vertices_at_initial; i++) { while (g.get_node_out_degree(i) < num_of_out_degree) { back: id_type R1 = randint.next(num_of_vertices_at_initial); if (R1 == i) goto back; else { g.add_edge(i, R1, 1); } } } for (id_type i = num_of_vertices_at_initial; i < num_nodes; i++) { id_type R2 = randint.next(i); vector<id_type> vertices_pointed_by_R2; for (adjacent_edges_iterator aeit = g.out_edges_begin(R2); aeit != g.out_edges_end(R2); aeit++) { vertices_pointed_by_R2.push_back(aeit->first); } while (g.get_node_out_degree(i) < num_of_out_degree) { wt_t R3 = randdouble.next(1); if (R3 < probability_to_copy_from_existing_vertex) { g.add_edge(i, vertices_pointed_by_R2[g.get_node_out_degree(i)], 1); } else { A: id_type R4 = randint.next(num_nodes); if (R4 != i) { g.add_edge(i, R4, 1); } else goto A; } } } g.set_graph_name("vc_" + T2str<size_t > (num_nodes) + "_" + T2str<size_t > (num_of_out_degree) + "_" + T2str<size_t > (num_of_vertices_at_initial) + "_" + T2str<double>(probability_to_copy_from_existing_vertex)); return 1; } else { // cout<<"\nnum_of_vertices_at_initial should greater than num_of_out_degree\n"; return 0; } }
bool CDLib::read_adjacencylist(graph& g, const string& filepath) { g.clear(); ifstream ifs; ifs.open(filepath.c_str()); if (ifs.is_open()) { int type = 0; id_type nid = 0, estart = 0; string line; getline(ifs, line); vector<id_type> units; split(line, units); if ((units.size() > 2) && (units[0] == 0) && (units[1] == units.size() - 2)) { type = 0; estart = 2; } else if ((units.size() > 1) && (units[0] == 0)) { type = 1; estart = 1; } else { type = 2; estart = 0; } g.add_node(to_string(nid)); for (id_type i = estart; i < units.size(); i++) { g.add_node(to_string(units[i])); g.add_edge(to_string(nid), to_string(units[i]), 1); } while (!ifs.eof()) { string line; getline(ifs, line); vector<id_type> units; split(line, units); if (units.size() > 0) { if ((type == 0) || (type == 1)) nid = units[0]; else nid++; g.add_node(to_string(nid)); for (id_type i = estart; i < units.size(); i++) { g.add_node(to_string(units[i])); g.add_edge(to_string(nid), to_string(units[i]), 1); } } } g.set_graph_name(filename(filepath)); return true; } return false; }
bool CDLib::read_matlab_sp(graph& g, const string& filepath) { g.clear(); ifstream ifs; ifs.open(filepath.c_str()); if (ifs.is_open()) { id_type from, to; double weight = 1; while (!ifs.eof()) { ifs >> from >> to >> weight; while (max(from, to) > g.get_num_nodes()) { g.add_node(); } g.add_edge(from - 1, to - 1, weight); } g.set_graph_name(filename(filepath)); return true; }
void CDLib::generate_planted_partition_graph(graph& g, id_type num_comms, id_type comm_size, double pin, double pout, vector< node_set>& communities) { if (pin >= 0 && pout >= 0 && pin <= 1 && pout <= 1) { id_type num_nodes = comm_size*num_comms; init_empty_graph(g, num_nodes); communities.assign(num_comms, node_set()); Uniform01RandomGeneratorMT p_gen; for (id_type i = 0; i < num_nodes; i++) if (i % comm_size) communities[i / comm_size].insert(i); for (id_type i = 0; i < g.get_num_nodes(); i++) { id_type comm_id_i = i / comm_size; for (id_type j = 0; j < g.get_num_nodes(); j++) { id_type comm_id_j = j / comm_size; double p = p_gen.next(); if (comm_id_i == comm_id_j && p > pin) g.add_edge(i, j, 1); else if (p > pout) g.add_edge(i, j, 1); } } g.set_graph_name("pp_" + T2str<id_type > (num_comms) + "_" + T2str<id_type > (comm_size) + "_" + T2str<double>(pin) + "_" + T2str<double>(pout)); } }
void CDLib::generate_configuration_model(graph& g, vector<id_type>& degree_sequence) { if (degree_sequence.size() > 0) { init_empty_graph(g, degree_sequence.size()); vector<id_type> nodes_with_non_0_degree; for (id_type i = 0; i < degree_sequence.size(); i++) { if (degree_sequence[i] != 0) { nodes_with_non_0_degree.push_back(i); } } UniformRandomGeneratorAkash<id_type> rand; for (id_type i = 0; i < degree_sequence.size(); i++) { id_type remaining_degree = degree_sequence[i]; for (id_type j = 0; j < remaining_degree; j++) { back: id_type R = rand.next(nodes_with_non_0_degree.size()); if (degree_sequence[i] == 1 && i == nodes_with_non_0_degree[R]) goto back; g.add_edge(i, nodes_with_non_0_degree[R], 1); degree_sequence[i]--; degree_sequence[nodes_with_non_0_degree[R]]--; if (degree_sequence[nodes_with_non_0_degree[R]] == 0) { if (nodes_with_non_0_degree[R] == i) break; nodes_with_non_0_degree.erase(nodes_with_non_0_degree.begin() + R); } if (degree_sequence[i] == 0) break; } if (remaining_degree != 0) { nodes_with_non_0_degree.erase(nodes_with_non_0_degree.begin()); } } g.set_graph_name("configuration_model"); } }
void CDLib::generate_prices_model(graph& g, size_t num_nodes, size_t num_of_out_degree, size_t in_degree_constant) { init_empty_graph(g, num_nodes); vector<id_type> vertices_pointed_by_edges; UniformRandomGeneratorAkash<id_type> randint; UniformRandomGeneratorAkash<double> randdouble; double probability = num_of_out_degree / (num_of_out_degree + in_degree_constant); for (id_type i = 0; i < num_nodes; i++) { while (g.get_node_out_degree(i) < num_of_out_degree) { double R1 = randdouble.next(1); if (R1 < probability) { id_type R2 = randint.next(vertices_pointed_by_edges.size()); g.add_edge(i, vertices_pointed_by_edges[R2], 1); vertices_pointed_by_edges.push_back(vertices_pointed_by_edges[R2]); } else { id_type R3 = randint.next(num_nodes); g.add_edge(i, R3, 1); vertices_pointed_by_edges.push_back(R3); } } } g.set_graph_name("price_" + T2str<size_t > (num_nodes) + "_" + T2str<size_t > (num_of_out_degree) + "_" + T2str<size_t > (in_degree_constant)); }
/*this model is used to generate graph with high clustering coefficient. this model matches sports network like american football league.*/ bool CDLib::generate_small_world_model(graph& g, size_t num_nodes, size_t degree_of_each_vertex, double probability_to_replace_edge) { if (probability_to_replace_edge >= 0 && probability_to_replace_edge <= 1) { UniformRandomGeneratorAkash<id_type> randint; UniformRandomGeneratorAkash<double> randdouble; init_empty_graph(g, num_nodes); for (id_type i = 0; i < num_nodes; i++) { for (id_type j = 0; j < degree_of_each_vertex / 2; j++) { g.add_edge(i, (i + j + 1) % num_nodes, 1); } if (degree_of_each_vertex % 2 == 1) { g.add_edge(i, (i + (degree_of_each_vertex / 2) + 1) % num_nodes, 1); } } for (id_type i = 0; i < num_nodes; i++) { for (id_type j = 0; j < degree_of_each_vertex; j++) { double R1 = randdouble.next(1); if (R1 < probability_to_replace_edge) { back: id_type R2 = randint.next(num_nodes); if (R2 == i) goto back; else { g.add_edge(i, R2, 1); } } } } g.set_graph_name("sw_" + T2str<size_t > (num_nodes) + "_" + T2str<size_t > (degree_of_each_vertex) + "_" + T2str<double>(probability_to_replace_edge)); return 1; } else { // cout<<"\nprobability value might be wrong"; return 0; } }
void CDLib::generate_erdos_renyi_graph(graph& g, id_type num_nodes, id_type num_edges) { double p = (double) num_edges / (num_nodes * (num_nodes - 1)); generate_erdos_renyi_graph(g, num_nodes, p); g.set_graph_name("er_" + T2str<id_type > (num_nodes) + "_" + T2str<id_type > (num_edges)); }
void CDLib::generate_star_graph(graph& g, id_type size) { init_empty_graph(g, size); for (id_type i = 1; i < g.get_num_nodes(); i++) g.add_edge(0, i, 1); g.set_graph_name("star_" + T2str<id_type > (size)); }
void CDLib::generate_ring_graph(graph& g, id_type size) { init_empty_graph(g, size); for (id_type i = 0; i < g.get_num_nodes(); i++) g.add_edge(i, (i + 1) % size, 1); g.set_graph_name("ring_" + T2str<id_type > (size)); }