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::init_empty_graph(graph& g, size_t size) { g.clear(); if (g.is_directed()) g.convert_to_undirected(); if (g.is_weighted()) g.convert_to_unweighted(0); for (id_type i = 0; i < size; i++) g.add_node(); }
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; }