void Hypergraph::saveHypergraph(const QString &fileName) { ofstream graphFile (fileName.toStdString().c_str()); graphFile <<_nVertices<<"\n"; for (int i=0; i<_edges.size(); ++i) { for (int j=0; j<_edges[i].size(); ++j) { graphFile <<_edges[i][j] <<"\t"; } graphFile <<"\n"; } graphFile.close(); }
void Hypergraph::loadHypergraph(const QString &fileName) { ifstream graphFile (fileName.toStdString().c_str()); graphFile >>_nVertices; setNumOfVertices(_nVertices); string line; _edges.clear(); while(getline(graphFile,line)) { stringstream strLine(line); HyperEdge e(0); int fibIdx; while (strLine >>fibIdx) { e.push_back(fibIdx); } if (!e.empty()) { _edges.push_back(e); } } graphFile.close(); }
Graph GMLGraphReader::read(const std::string& path) { std::ifstream graphFile(path); Aux::enforceOpened(graphFile); std::string line; std::unordered_map<std::string,node> nodeMap; auto ignoreWhitespaces = [](std::string& s, index i) { index newIdx = i; index end = s.size(); while (s[newIdx] == ' ' && newIdx < end) ++newIdx; return newIdx; }; auto getPair = [&ignoreWhitespaces](std::string& s) { if (s.find("[") < s.size()) { throw std::runtime_error("found opening bracket"); } else if (s.find("]") < s.size()) { throw std::runtime_error("found closing bracket"); } //DEBUG(s); index length = s.size(); index start = ignoreWhitespaces(s,0); index i = start; while (s[i] != ' ' && i < length) ++i; index end = i; std::string key = s.substr(start,end-start); //DEBUG(key, ", ",start, ", ", end); i = ignoreWhitespaces(s,i+1); // TODO: get next line if value is not in the current line ? not really necessary. start = i; while (s[i] != ' ' && i < length) ++i; end = i; std::string value = s.substr(start, end-start); //DEBUG(value, ", ",start, ", ", end); return std::make_pair(key,value); }; auto parseNode = [&](Graph& G) { bool closed = false; if (line.find("node") < line.size() && line.find("[") < line.size()) { std::getline(graphFile, line); closed = line.find("]") < line.size(); while (!closed) { try { auto pair = getPair(line); if (pair.first == "id") { // currently, node attributes are ignored and only the id is relevant for edges later on node u = G.addNode(); nodeMap.insert(std::make_pair(pair.second,u)); DEBUG("added node: ",u,", ",pair.second); } } catch (std::exception e) { //throw std::runtime_error("something went wrong when parsing a node"); return false; } std::getline(graphFile, line); closed = line.find("]") < line.size(); } } else { return false; } if (closed) { //std::getline(graphFile, line); return true; } else { return false; } }; auto parseEdge = [&](Graph& G) { //DEBUG("trying to parse an edge"); if (line.find("edge") < line.size() && line.find("[") < line.size()) { std::getline(graphFile, line); node u = 0; node v = 0; while (!(line.find("]") < line.size())) { try { auto pair = getPair(line); if (pair.first == "source") { // currently, node attributes are ignored and only the id is relevant for edges later on u = nodeMap[pair.second]; } else if (pair.first == "target") { v = nodeMap[pair.second]; } } catch (std::exception e) { //throw std::runtime_error("something went wrong when parsing an edge"); return false; } std::getline(graphFile,line); } G.addEdge(u,v); DEBUG("added edge ",u ,", ", v); } else { return false; } if (line.find("]") < line.size()) { //std::getline(graphFile, line); return true; } else { return false; } }; auto parseGraph = [&]() { Graph G; int key_type = 0; // 0 for graph keys, 1 for node, 2 for edges bool directed = false; std::getline(graphFile, line); index pos = line.find("graph"); bool graphTagFound = pos < line.size(); if (graphTagFound) { if (line.find("[",pos) < line.size()) { while (graphFile.good()) { std::getline(graphFile,line); switch (key_type) { case 0: try { auto pair = getPair(line); // currently, it is only interesting, if the graph is directed if (pair.first == "directed" && pair.second == "1") { directed = true; DEBUG("set directed to true"); } break; } catch (std::exception e) { if (directed) { G = Graph(0,false,true); } else { G = Graph(0,false,false); } ++key_type; } //break; case 1: if (parseNode(G)) { //DEBUG("parsed node successfully"); break; } else { ++key_type; DEBUG("couldn't parse node; key type is now: ",key_type); } //break; case 2: if (parseEdge(G)) { //DEBUG("parsed edge successfully"); break; } else { DEBUG("parsing edge went wrong"); ++key_type; } default: if (!(line.find("]") < line.size())) { DEBUG("expected closing bracket \"]\", got: ",line); } } } // at the end of the file, make sure the closing bracket is there. } else { throw std::runtime_error("GML graph file broken"); } } else { throw std::runtime_error("graph key not found"); } return G; }; // try { Graph G = parseGraph(); // } catch (std::exception e) { // std::string msg = "reading GML file went wrong: "; // msg+=e.what(); // throw std::runtime_error(msg); // } std::string graphName = Aux::StringTools::split(Aux::StringTools::split(path, '/').back(), '.').front(); G.setName(graphName); G.shrinkToFit(); return G; }
void generateGraph(GraphData *graph){ std::ifstream graphFile("USA-road-d.NY.gr"); std::string line,buf; AdjacencyList* adj_list; int gr_lineCount = 1; while(graphFile.good()){ getline(graphFile,line); if(line[0]=='c') continue; if(line[0]=='p'){ std::vector<std::string> graph_info; split( graph_info, line, ' ' ); int num_vertices = atoi((graph_info[2]).c_str()); graph->vertexCount = num_vertices; graph->edgeCount = atoi((graph_info[3]).c_str()); adj_list = (AdjacencyList*)malloc((num_vertices)*(sizeof (AdjacencyList))); memset(adj_list, 0 ,num_vertices*(sizeof (AdjacencyList))); continue; } if(gr_lineCount > graph->edgeCount) break; std::vector<std::string> edge_info; split( edge_info, line, ' ' ); int from_vertex = atoi((edge_info[1]).c_str()); int to_vertex = atoi((edge_info[2]).c_str()); float weight = atof((edge_info[3]).c_str()); AdjacencyList temp_adj = adj_list[from_vertex-1]; for(int i=0;i<8;i++){ if(temp_adj.adjList[i]==0){ temp_adj.adjList[i]=to_vertex; temp_adj.weight[i]=weight; break; } } adj_list[from_vertex-1] = temp_adj; gr_lineCount++; } graphFile.close(); graph->vertexArray = (int*) malloc(graph->vertexCount * sizeof(int)); graph->edgeArray = (int*)malloc(graph->edgeCount * sizeof(int)); graph->weightArray = (float*)malloc(graph->edgeCount * sizeof(float)); int current_edge = 0; for(int i=0;i<graph->vertexCount;i++){ AdjacencyList temp_adj = *(adj_list+i); graph->vertexArray[i] = current_edge; for(int j=0;j<8;j++){ if(temp_adj.adjList[j]!=0){ graph->edgeArray[current_edge] = temp_adj.adjList[j]; graph->weightArray[current_edge] = temp_adj.weight[j]; current_edge++; }else{ break; } } } free(adj_list); std::ifstream coordFile("USA-road-d-mod.NY.co"); int co_lineCount = 1; int num_vertices_co = 0; while(coordFile.good()){ getline(coordFile,line); if(line[0]=='c') continue; if(line[0]=='p'){ std::vector<std::string> coordinate_info; split( coordinate_info, line, ' ' ); num_vertices_co = atoi((coordinate_info[4]).c_str()); graph->latitudes = (float*)malloc(num_vertices_co*(sizeof (float))); graph->longitudes = (float*)malloc(num_vertices_co*(sizeof (float))); continue; } if(co_lineCount > num_vertices_co) break; std::vector<std::string> coordinate_info; split( coordinate_info, line, ' ' ); int vertex_num = atoi((coordinate_info[1]).c_str()); float longitude = atof((coordinate_info[2]).c_str()); float latitude = atof((coordinate_info[3]).c_str()); graph->latitudes[vertex_num-1] = latitude; graph->longitudes[vertex_num-1] = longitude; co_lineCount++; } coordFile.close(); }
//! load the graph description from a file //! @param[in] fileName name of the file with the graph description void AOgraph::loadFromFile(string fileName) { // raise an error if the graph is not empty if (graph.size() != 0) { cout<<"[ERROR] The graph is not empty." <<"Do you really want to overwrite the current graph?" <<endl; return; } ifstream graphFile(fileName.c_str()); cout <<"Loading graph description from file: " <<fileName <<endl; while (!graphFile.eof()) { // the first line contains: // 1. the name of the graph // 2. the number N=numNodes of nodes // 3. the name of the head node (corresponding to the final assembly) string name; int numNodes; string headName; graphFile >>name >>numNodes >>headName; if (!graphFile) break; gName = name; // the next N lines contain the name and cost of all the nodes in the graph string nameNode; int cost; for (int i=0; i < numNodes; i++) { graphFile >>nameNode >> cost; if (!graphFile) break; addNode(nameNode, cost); } // the next ?? lines contain the descriptions of the hyperarcs in the graph int hyperarcIndex = 0; while (!graphFile.eof()) { AOnode* father; string nameFather; int numChildren; int hyperarcCost; vector<AOnode*> childNodes; graphFile >>numChildren >>nameFather >>hyperarcCost; if (!graphFile) break; father = findByName(nameFather); //DEBUG:cout<<"nameFather = " <<nameFather <<endl; // the next numChildren lines contain the names of the child nodes for (int i=0; i < numChildren; i++) { AOnode* temp; string nameChild; graphFile >>nameChild; if (!graphFile) break; temp = findByName(nameChild); childNodes.push_back(temp); } father->addArc(hyperarcIndex, childNodes, hyperarcCost); hyperarcIndex = hyperarcIndex+1; } // identify the head node in the graph head = findByName(headName); } graphFile.close(); // set up the graph (nodes feasibility, paths costs) setupGraph(); }