C ++程序中的某些代码使程序崩溃。我正在执行BFS算法

问题描述 投票:1回答:1

我正在实现BFS算法。我在IDE中编写了此代码。SYSTEM:Windows 10开发C ++可能是bfs()函数中的错误。我只是出于引用而发布了所有代码。我的C ++ code:


#include<bits/stdc++.h>
using namespace std;

class Graph{
    int v; // number of vertices
    vector<int>*adj;
    public:
        /**
         * Constructor to initialize the graph.
         * @param v an integer: number of nodes in the graph
         */
        Graph(int V){
            this->v=V;
            this->adj=new vector<int>[V];
        }

        /**
         * This function add a new edge to the graph.
         * @param u an integer: representing a node in the graph.
         * @param v an integer: representing a node in the graph.
         * @param biDir a bool: true denotes bidirectional edge, unidirectional otherwise.
         */
        void addEdge(int u,int v,bool biDir=false){
            adj[u].push_back(v);
            if(biDir)
                adj[v].push_back(u);
        }
        /**
         * This function traverse the given graph using BFS traversal technique.
         * @param src a node: function starts traversing from this node.
         */
        void bfs(int src){
            // create a array of boolean to keep track of visited nodes.
            vector<bool>visited(this->v,false);
           // visited[0]=true;
            // make a queue and insert src into it
            queue<int>q;
            q.push(src); // insert src into queue
            // mark first node as visited
            visited[src]=true;
            while(!q.empty()){
                int node=q.front();
                // visit
                cout<<node<<" ";
                // remove this node from queue
                q.pop(); 
                // visit every node adjacent to node 'node' 
                // if that node not visited then visit and enque it.
                for(int adjNode:adj[node]){
                    if(!visited[adjNode]){
                        visited[adjNode]=true;
                        q.push(adjNode);
                    }
                }
            }

        }
         void print(){
            // for every vertex
            for(int i=1;i<this->v;i++){
                // every connected edge from vertex
                cout<<i<<"-> ";
                for(int node:adj[i]){
                    cout<<node<<" ";
                }
                cout<<endl;
            }
        }
};
int main(){
    Graph g(5+1);
    g.addEdge(1,2);
    g.addEdge(1,4);
    g.addEdge(4,6);
    g.addEdge(3,6);
    g.addEdge(2,5);
    g.addEdge(5,2);
    g.addEdge(6,3);
    g.addEdge(6,4);
    g.print();
    cout<<endl;cout<<endl;cout<<endl;cout<<endl;
    g.bfs(1);
    return 0;
}

尽管该程序可以正常编译。但是在运行时,仅执行print()函数。函数bfs()不执行。它产生由print()函数生成的以下OUTPUT:] >>。

1-> 2 4
2-> 5
3-> 6
4-> 6
5-> 2

当我在bfs()

中更改此代码时
vector<bool>visited(this->v,false);

到此

 bool visited[this->v];
            for(int i=0;i<this->v;i++)
                visited[i]=false;

此代码也不会执行bfs()。

] >>

我正在执行BFS算法。我在IDE中编写了这段代码。系统:Windows 10 Dev c ++可能在bfs()函数中出错。我只是出于参考而发布了所有代码。我的C ++代码:#include <...>

c++ breadth-first-search dev-c++
1个回答
1
投票
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

class Graph {
    int v; // number of vertices
    vector<int>* adj;
public:
    /**
     * Constructor to initialize the graph.
     * @param v an integer: number of nodes in the graph + 1
     */
    Graph(int V) {
        this->v = V;
        this->adj = new vector<int>[V];
    }

    /**
     * This function add a new edge to the graph.
     * @param u an integer: representing a node in the graph.
     * @param v an integer: representing a node in the graph.
     * @param biDir a bool: true denotes bidirectional edge, unidirectional otherwise.
     */
    void addEdge(int u, int v, bool biDir = false) {
        adj[u].push_back(v);
        if (biDir)
            adj[v].push_back(u);
    }
    /**
     * This function traverse the given graph using BFS traversal technique.
     * @param src a node: function starts traversing from this node.
     */
    void bfs(int src) {
        // create a array of boolean to keep track of visited nodes.
        vector<bool>visited(this->v, false);
        // visited[0]=true;
         // make a queue and insert src into it
        queue<int>q;
        q.push(src); // insert src into queue
        // mark first node as visited
        visited[src] = true;
        while (!q.empty()) {
            int node = q.front();
            // visit
            cout << node << " ";
            // remove this node from queue
            q.pop();
            // visit every node adjacent to node 'node' 
            // if that node not visited then visit and enque it.
            for (int adjNode : adj[node]) {
                if (!visited[adjNode]) {
                    visited[adjNode] = true;
                    q.push(adjNode);
                }
            }
        }

    }
    void print() {
        // for every vertex
        for (int i = 1; i < this->v; i++) {
            // every connected edge from vertex
            cout << i << "-> ";
            for (int node : adj[i]) {
                cout << node << " ";
            }
            cout << endl;
        }
    }
};
int main() {
    Graph g(7);
    g.addEdge(1, 2);
    g.addEdge(1, 4);
    g.addEdge(4, 6);
    g.addEdge(3, 6);
    g.addEdge(2, 5);
    g.addEdge(5, 2);
    g.addEdge(6, 3);
    g.addEdge(6, 4);
    g.print();
    cout << endl; cout << endl; cout << endl; cout << endl;
    g.bfs(1);
    return 0;
}

只需将大小设置为7即可使用。我没有阅读完整的代码,也没有验证算法。但是我可以看到您将大小设置为6并访问了第7个元素(因为索引从0开始,所以6是第7个元素)。

© www.soinside.com 2019 - 2024. All rights reserved.