如何接受来自输入文件的图形

How to accept graph from input file

本文关键字:文件 图形 输入 何接受      更新时间:2023-10-16

>我接受来自输入文件的无向加权图。文件包含200节点。
我写了这样的代码。

typedef pair<int, int> ipair;     
class Graph
{
    int V;
    list< pair <int, int> > *adj;
    public:
    Graph(int V);     
};
Graph::Graph( int V)
{
    this-> V = V;
    adj = new list<ipair>[V];
}
bool Graph :: read_file()
{
   const long int N = 1000000;
   std::ifstream infile("Dijkstra.txt");
   if(!infile.is_open()) return false;
   std::string line;
   int i = 0;
   while ( i < N && getline(infile, line) )
   {
      std::istringstream str(line);
      int u;
      str >> u;
      if ( u > N )
      {
         // Problem.
         abort();
      }
      int v;
      int w;
      while ( str >> v >> w)
      {
         adj[u].push_back(make_pair(v,w));
      }
      ++i;
   }
}
int main()
{
    Graph g(200);
    g.read_file();
    g.print_graph();
    return 0;
}      

I/P file :      
1   80,982  163,8164    170,2620    145,648 200,8021    173,2069    92,647  26,4122 140,546 11,1913 160,6461    27,7905 40,9047 150,2183    61,9146 159,7420    198,1724    114,508 104,6647    30,4612 99,2367 138,7896    169,8700    49,2437 125,2909    117,2597    55,6399      
2   42,1689 127,9365    5,8026  170,9342    131,7005    172,1438    34,315  30,2455 26,2328 6,8847  11,1873 17,5409 157,8643    159,1397    142,7731    182,7908    93,8177        

节点 1 连接到权重为 982
的节点 80节点 1 连接到权重为 8164 的节点 163

节点 2 连接到权重为 1689
的节点 42节点 2 连接到权重为 9365
的节点 127等。。。。。
现在有了这个,我可以接受节点 1 连接到权重为 80 (1 80,982( 的节点 1 但是与节点 1 连接的其余节点呢?
即如何制作接受 v 和 w 的循环?

我认为您可以使用

vector<list< pair <int, int> >> adj来存储数据,而不是在 Graph 类中list< pair <int, int> > *adj

我稍微修改了您的程序以存储与节点相关的所有数据对。

#include <iostream>
#include <utility>
#include <list>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <vector>
using namespace std;
typedef pair<int, int> ipair;
class Graph
{
  int V;
  vector<list< pair <int, int> >> adj;
public:
  bool read_file();
  void print_graph();
  Graph(int V);
};
Graph::Graph( int V)
{
  this-> V = V;
  adj.reserve(V);
}
bool Graph:: read_file()
{
  const long int N = 1000000;
  std::ifstream infile("Dijkstra.txt");
  if(!infile.is_open()) return false;
  std::string line;
  int i = 0;
  while ( i < N && getline(infile, line) ) {
    std::istringstream str(line);
    int u;
    str >> u;
    if ( u > N ) {
      // Problem.
      abort();
    }
    int v;
    int w;
    char c;
    while ( str >> v >> c >> w) {
      if (u <= (int)adj.size()) { // index (u-1) exists
        auto list = adj[u-1]; // get the existing list
        list.push_back(make_pair(v,w)); // add new data
        adj[u-1] = list; // store it the same index
      } else { // index (u-1) doesn't exist
        list<ipair> list; // create a new list
        list.push_back(make_pair(v,w)); // store the values
        adj.push_back(list); // add it in the vector
      }
    }
    ++i;
  }
  return true;
}
void Graph::print_graph() {
  int node = 1;
  for (auto& x : adj) {
    cout << "from node: " << node++ << endl;
    for (auto it = begin(x); it != end(x); ++it) {
      cout << it->first << " " << it->second << "n";
    }
  }
}
int main() {
  Graph g(200);
  g.read_file();
  g.print_graph();
  return 0;
}

你可以正则表达式匹配,。

例如

std::regex pairs_regex("(d+),(d+)");
auto pairs_it = std::sregex_iterator(line.begin(), line.end(), pairs_regex);
auto pairs_end = std::sregex_iterator();
for(;pairs_it != pairs_end; ++pairs_it)
{
    std::string v = pairs_it->str(1);
    std::string w = pairs_it->str(2);
    adj[u].push_back(make_pair(std::stoi(v), std::stoi(w)));
}

注意:如果adj是一个std::容器,而不是一个原始数组,会更安全。环体将填充一个list<pair<int, int>> temp,然后adj.emplace_back(std::move(temp))