首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BFS分段故障

BFS分段故障
EN

Stack Overflow用户
提问于 2015-04-01 14:21:12
回答 1查看 318关注 0票数 0
代码语言:javascript
复制
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <list>
// #include "BST.h"

using namespace std;

class Graph
{
  int V; // Number of vertices
  list<int> *adj; // Pointer to the array of adjacency list

public:
  Graph(int V); // Constructor
  void addEdge(int v, int w); // function to add an edge to the graph
  void BFS(int s); // Prints the Breadth first Search for the graph from s.
};

//Defining the constructor
Graph::Graph(int V)
{
  this->V = V;
  adj = new list<int>[V];
}


// Function to add Edges to the vertice.
void Graph::addEdge(int v, int w)
{
  adj[v].push_back(w); // Adding w to v's list
}


// Function to print out the Breadth First Search for the given graph starting at s.
void Graph::BFS(int s)
{
  bool *visited = new bool[V];

  cout << "Value of V: " << V << endl;
  for(int i = 0; i < V; i++)
    visited[i] = false;

  // Create a queue for BFS
  list<int> queue;

  //Marking the starting node as visited and adding it to the queue.
  visited[s] = true;
  queue.push_back(s);

  // Iterator to iterate over the adjacent list vertices
  list<int>::iterator i;

  while(!queue.empty())
  {

    // Printing the current vertex and removing it from the queue
    s = queue.front();
    cout << s << " ";
    queue.pop_front();


    // Going through the adjacency the list and adding it to the queue if it has not been visited.
      for (i = adj[s].begin(); i != adj[s].end(); i++)
      {
        if(!visited[*i] )
        {
          visited[*i] = true;
          queue.push_back(*i);
        }
      }
  }
}



int main(int argc, char* argv[])
{
  // If the user didn't provide a filename command line argument,
  // print an error and exit.
  if (argc != 3)
  {
      cout << "Usage: " << argv[0] << " <Filename>  <starting node index>" << endl;
      exit(1);
  }

  string line;
  int size;
  int starting_vertice;
  char colon;
  int vertex;
  ifstream myfile (argv[1]);
  if (myfile.is_open())
  {
    getline(myfile, line);
    istringstream iss(line);
    iss >> size;

    // Initializing a graph of size taken in.
    Graph g(size);

    cout << "Vertex: " << "Connected Vertices" << endl;
    for (int i = 0; i < size; i++)
    {
      getline(myfile, line);
      istringstream iss(line);
      iss >> starting_vertice;
      iss >> colon;

      while (iss >> vertex)
      {
        g.addEdge(starting_vertice, vertex);
      }
      cout << endl;
    }

    myfile.close();

    cout << "Breadth First Search Starting at vertex " << argv[2] << " : " << endl;
    // cout << atoi(argv[2]) << endl;
    g.BFS( atoi(argv[2]) );
  }

  else
    cout << "Unable to open file" << endl;

  return 0;

}

这是我的代码,实现对特定输入文件的广度优先搜索。输入文件如下:

代码语言:javascript
复制
4
1:2 3 4
2:4
3:4
4:

我知道我在遍历最后一个顶点的邻接表时遇到了分段错误,但我无论如何也找不到解决这个问题的方法。有什么帮助吗?

编辑:另外,我给出的起始节点索引是1。

EN

回答 1

Stack Overflow用户

发布于 2015-04-01 14:24:18

欢迎来到off-by-one俱乐部。数组是零索引的。在Graph中,创建一个大小为V的列表,然后通过Graph::addEdge访问V-sized数组adjV-th元素。若要解决此问题,您有两个选择-将顶点编号从0增加到V-1,或者将adj的大小增加到V+1。要执行后一种操作:

代码语言:javascript
复制
Graph::Graph(int V)
{
  this->V = V;
  adj = new list<int>[V+1];
                     vvvvvv 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29384092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档