DFS program recursive call problem

Last night doing curriculum design, I wrote this recursive call dfs always displayed an error, a start puzzling, found after several observation program is running, it is because vector iterator caused by cross-border, and Why iterator it out of bounds, and I have written here before about the Code.

Dfs original program:

typedef char vertextype; // define vertices storage type 
typedef int arctype; // weight edges defined type 

struct ANode // edge list node 
{ 
    int ID; // neighbor domain, the storage corresponding to the vertex index 
    arctype wigth; // for storing weights 
}; 

struct // vnode vertex node list 
{ 
    String data; // information storing vertex data 
    vector <ANode> q; // each point of contact points with adjacent weights 
}; 

vector <ANode> :: iterator it;
Graph class 
{ 
Private: 
    int SUM = 0; // count the number of access points 
    VNode VNodeList [maxn]; // vertex node list, each point information stored 
    BOOL VIS [MAXN]; 
public: 
    int pointnum, eagenum; // points, number of edges 
    Graph (int n-, int m); 
    void initdfs (int n-); 
    void DFS (int the begin); 
    void BFS (int the begin); 
    int is_connected (); 
    int get_degree (int A []); 
    void Print () ; 
};
Graph :: DFS void (n-int) 
{ 
    COUT << VNodeList [n-] .data << endl; 
    SUM ++; 
    VIS [n-] =. 1; 
    IF (SUM> = this-> pointnum) 
        return; 
    for (IT = VNodeList [ n-] .q.begin (); IT <VNodeList [n-] .q.end (); I ++) 
    { 
        IF (VIS [IT-!> ID]) 
        { 
            DFS (IT-> ID); 
        } 
    } 
} 

can be found the original dfs this program, it is global iterator (in order to facilitate ....), but continue on this global variable modified in the course of the program is running behind, and thus contribute to the problem, so the iterator Affirming placed inside a for loop to solve this problem.

/ * 
    STL Version 
    existing queue stack and the implementation 
    of FIG adopt a data structure: the adjacency table 
    adjacency table with the vector to achieve 
    a directed graph 
    maximum number of points is 107 
    points from the subscript zero input by default 0, process data 
* / 

#include <the iostream> 
#include <sstream> 
#include <cstdio> 
#include <CString> 
#include <algorithm> 
#include <Stack> 
#include <Queue> 
#include <Vector> 
const int = 1E2 + MAXN. 7; 
the using namespace STD; 

typedef char vertextype; // define vertices storage type 
typedef int arctype; // weight type definition side 

struct ANode // edge list node 
{ 
    int ID; // neighbor domain, the storage corresponding to the vertex index 
    arctype wigth; // for storing weights 
}; 

struct // vertex table node vnode
{ 
    String Data; // information storing vertex data 
    vector <ANode> q; // each point of contact points with adjacent weights 
}; 

Vector <ANode> :: Iterator IT; 

class Graph 
{ 
Private: 
    int SUM = 0; // count the number of access points 
    vNode VNodeList [maxn]; // vertex node list, each point information stored 
    BOOL VIS [MAXN]; 
public: 
    int pointnum, eagenum; // number of points, the number of sides 
    Graph (int n, m int); 
    void initdfs (n-int); 
    void DFS (the begin int); 
    void BFS (the begin int); 
    int is_connected (); 
    int get_degree (A int []); 
    void Print (); 
}; 

Graph :: Graph (int n, int m) // constructor, enter 
{ 
    this-> = n-pointnum, this-> eagenum = m; 
    // the printf ( "Please enter the name of the point of% d: \ n-", n-); 
    for (int I = 0; I <this-> pointnum; I ++)
    { 
        // the printf ( "% d name of the first point is:", I); 
        CIN >> VNodeList [I] .data; 
    } 
    // the printf ( "% d sides enter the information, the format: number starting end No weight: \ n-', m); 
    int the begin, End, Wight; 
    for (int I = 0; I <this-> eagenum; I ++) 
    { 
        // the printf ( "% d sides of the information:", I); 
        CIN End >> >> >> the begin Wight; 
        VNodeList [the begin] .q.push_back ({End, Wight}); 
    } 
} 

void Graph :: initdfs (n-int) 
{ 
    SUM = 0; 
    Memset (VIS, 0, the sizeof (VIS)); 
} 

void Graph :: DFS (n-int) 
{ 
    String VNodeList = S [n-] .data; 
    COUT << VNodeList [n-] .data << endl; <endl;
    sum++;
    VIS [n-] =. 1;
    if(sum>=this->pointnum)
        return;
    for(vector<ANode>::iterator i=VNodeList[n].q.begin();i<VNodeList[n].q.end();i++)
    {
        if(!vis[i->id])
        {
            dfs(i->id);
        }
    }
}

void Graph::print()//输出
{
    for(int i=0;i<pointnum;i++)
    {
        cout<<VNodeList[i].q.end()-VNodeList[i].q.begin()<<endl;
        cout<<VNodeList[i].data<<" ";
        for(it=VNodeList[i].q.begin();it<VNodeList[i].q.end();it++)
        {
            cout<<i<<" "<<it->id<<" "<<it->wigth<<"   ";
        }
        cout<<endl;
    }
}

int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        Graph mmp(n,m);
        mmp.print();
        int x;
        cin>>x;
        mmp.initdfs(x);
        mmp.dfs(x);
        cout<<"endl"<<endl;
    }
}

Guess you like

Origin blog.csdn.net/hrbust_cxl/article/details/88594444