本文整理汇总了C++中graph::add_edge方法的典型用法代码示例。如果您正苦于以下问题:C++ graph::add_edge方法的具体用法?C++ graph::add_edge怎么用?C++ graph::add_edge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph
的用法示例。
在下文中一共展示了graph::add_edge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: generate_barabasi_albert_model
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));
}
示例2: generate_LEET_chord_graph
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();
}
示例3: generate_chord_graph
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));
}
示例4: generate_spoke_graph
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));
}
示例5: generate_clique_graph
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));
}
示例6: main
int main()
{
std::freopen("circle.in", "r", stdin);
std::freopen("circle.out", "w", stdout);
int N, M;
std::scanf("%d %d", &N, &M);
gp.init(N);
for(int i = 0; i != M; ++i)
{
int u, v;
double w;
std::scanf("%d %d %lf", &u, &v, &w);
gp.add_edge(u, v, w);
}
double epsilon = 1.0e-9;
double r = 1.0e8, l = -1.0e8;
while(r - l > epsilon)
{
double m = (l + r) * 0.5;
if(gp.has_neg_circle(m))
r = m;
else l = m;
}
std::printf("%.8f\n", l);
return 0;
}
示例7: read_edgelist
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;
}
示例8: generate_vertex_copying_model
/*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;
}
}
示例9: out
void
program2graph(vector<stmt*> const& p,
map<string,type*>& inputs,
map<string,type*>& inouts,
map<string,type*>& outputs,
graph& g)
{
map<string, type*>::iterator i;
for (i = inouts.begin(); i != inouts.end(); ++i) {
inputs.insert(*i);
outputs.insert(*i);
}
for (i = inputs.begin(); i != inputs.end(); i++) {
set_container_size(i->first, *(i->second));
}
for (i = outputs.begin(); i != outputs.end(); i++) {
set_container_size(i->first, *(i->second));
}
map<string, vertex> env;
for (unsigned int i = 0; i != p.size(); ++i) {
stmt* s = p[i];
vertex tmp = s->rhs->to_graph(env, inputs, outputs, g);
env[s->lhs] = tmp;
}
///// DEBUG
#ifdef DEBUG
std::ofstream out("lower0.dot");
print_graph(out, g);
#endif
for (map<string,type*>::iterator i = outputs.begin(); i != outputs.end(); ++i) {
vertex def = env[i->first];
vertex_info vi; vi.t = *i->second; vi.op = output; vi.label = i->first;
vi.eval = evaluate; vi.trivial = true;
string name = i->first;
set_container_size(name, vi.t);
vertex v = g.add_vertex(vi);
g.add_edge(def, v);
}
// push out types up to operation types
for (vertex i = 0; i < g.num_vertices(); i++) {
if (g.info(i).op == output) {
std::vector<vertex> const& iav = g.inv_adj(i);
for (vertex j = 0; j < iav.size(); j++) {
if (g.info(iav[j]).t.k == unknown) {
g.info(iav[j]).t = g.info(i).t;
}
}
}
}
}
示例10: generate_kademlia_graph
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));
}
示例11: read_adjacencylist
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;
}
示例12: generate_erdos_renyi_graph
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));
}
}
示例13: generate_de_bruijn_graph
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));
}
}
示例14: generate_planted_partition_graph
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));
}
}
示例15: generate_scale_free_graph
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));
}