从流到邻接列表的向量读取图形

Reading graph from stream to vector of adjacency list

本文关键字:向量 读取 图形 列表      更新时间:2023-10-16

我正在尝试通过读取下面显示的文件来创建图形作为邻接列表

这里的第一行是顶点数。在下面的示例中,我们有 12 个顶点,在以下行中 例如,1 2 3 4 表示顶点 1 的边为 2、3 和 4。

12
1 2 3 4
2 1 5 6
3 1 6
4 1 7
5 2 9 10
6 2 3
7 4 8 
8 4 7
9 5 10
10 5 9
11 12
12 11

C++程序

#include <iostream>
#include <fstream>
#include <strstream>
#include <sstream> // for std::getline
#include "Graph.h"
using namespace std;
// Note: vector index is will match the vertex id.
std::vector <std::vector<std::pair<int, int> > > vecIdxedGraph;

void storeEdges(const string& strEdges) {
std::istringstream iss(strEdges);
int FromVertex = 0;
iss >> FromVertex;
std::cout << "From Vertex " << FromVertex << endl;
vector<pair<int, int>> edges;
string toVertex;
while (std::getline(iss, toVertex, ' ')) {
std::cout << "to Vertex " << toVertex << " ";
edges.push_back(std::make_pair(atoi(toVertex.c_str()), 0));
}
std::cout << std::endl;
vecIdxedGraph.push_back(edges);
return;
}
void printGraph() {
for (int i = 0; i < vecIdxedGraph.size(); i++) {
cout << "Vertex " << i + 1 << " ";
for (int j = 0; j < vecIdxedGraph[i].size(); j++) {
cout << vecIdxedGraph[i][j].first << " ";
}
cout << endl;
}
}
int main() {
// create Graph
ifstream inFile("Graph.txt");
if (!inFile) {
cout << "Open input file failed." << endl;
}
// Set input stream to file.
std::streambuf* pOldCinBuf = cin.rdbuf();
cin.set_rdbuf(inFile.rdbuf());
int iNumberOfNodes;
cin >> iNumberOfNodes;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Number of nodes in graph are: " << iNumberOfNodes << endl;
vecIdxedGraph.reserve(iNumberOfNodes);

for (int iVertexId = 1; iVertexId <= iNumberOfNodes; iVertexId++) {
std::string vertIdDetails;
std::getline(cin, vertIdDetails);
// cout << (vertIdDetails.c_str()) << endl;
// parse line and store to the vector.
storeEdges(vertIdDetails);
// cin.ignore(numeric_limits<streamsize>::max(), 'n');
}
printGraph();
return 0;
}

以下是输出

Vertex 1 0 2 3 4
Vertex 2 0 1 5 6
Vertex 3 0 1 6
Vertex 4 0 1 7
Vertex 5 0 2 9 10
Vertex 6 0 2 3
Vertex 7 0 4 8
Vertex 8 0 4 7
Vertex 9 0 5 10
Vertex 10 0 5 9
Vertex 11 0 12
Vertex 12 0 11

想要删除 0。我认为 0 在读取一行时来自空白区域

while (std::getline(iss, toVertex, ' '))

我不知道如何清除它。

另一个问题是我如何创建一个默认构造的向量,以便我可以使用

vecIdxedGraph[FromVertex] = ...,而不是push_back。

请帮忙

看起来你在这里把事情复杂化了。你只需要,像下面这样的东西。

// Some handy names
using Pair = std::pair<int, int>;
using Graph = std::vector<std::vector<Pair>> ;
// The stream handle
std::ifstream fileHandle("Graph.txt");
size_t iNumberOfNodes  = 0;
// 1. Get # of Vertices
fileHandle >> iNumberOfNodes;
// 2. Create a graph, pre allocate the # of vertices
// One node extra since your vertices starts from 1
Graph g{iNumberOfNodes + 1, std::vector<Pair>{}}; 
std::string lineStr;
int currNode = 0, vertex;
// 3. Iterate over file, reading line by line
while ( std::getline(fileHandle, lineStr) ) {
std::stringstream ss{lineStr};
ss >> currNode;
while ( currNode != 0 && ss >> vertex ) {
g[currNode].emplace_back( vertex, 0 ) ; // weight = 0;
}   
}

一切就绪!Demo Here