0

I can't find the error in my code. When I run it in the terminal, it says a warning message, not an error, but still doesn't display any results. I am working on a dfsrecursive, BFS, bfs helper, and DFS function. Please help!

void
Graph::dfs()
{
    // reset the labels of all nodes and edges, then calls dfsRecursive on every unexplored node


    Node* v=0;

    resetLabels();

    map<string, Node*>::iterator w;


    vector<Edge*> incidentEdges;

    for(w= this->nodeTable->begin(); w != this-> nodeTable-> end(); w++)
    {
        if(w->second->getLabel() == (NodeLabel::unexplored))
            dfsRecursive(w->second);
    }

    }

    void
    Graph::dfsRecursive(Node* v)
     {
    // recursively perform a depth-first search starting from the input node v.


    vector<Edge*> incidentEdges = v->getIncidentEdges();

    for(int e=0; e < incidentEdges.size(); e++)
    {
        if(incidentEdges[e]-> getLabel() == EdgeLabel::unexplored){


            Node* w = incidentEdges[e]-> opposite(v);
            if(w->getLabel() == NodeLabel::unexplored){

                incidentEdges[e]-> getLabel() == (EdgeLabel::discovery);
                w-> setLabel(NodeLabel::visited);
            }

        }

        else
            incidentEdges[e]-> setLabel(EdgeLabel::back);

        }

    }

    void
    Graph::bfs()
    {


    int size = 0;
    int* bfsarray[size];

    int begin = 0;
    int** adjMatrix;
    int noOfNodes;

    bool* visited = new bool[noOfNodes];

    for(int i =0; i <noOfNodes; i++)
    {

        visited[i] = false;

        list<int> queue;
        visited[begin] =true;
        queue.push_back(begin);

        while(!queue.empty()){

            begin  = queue.front();
            cout << begin;

            queue.pop_front();
            for(int i =0; i < noOfNodes; i++){

                if(adjMatrix[begin][i] == 1 && !visited[i]){

                    visited[i] = true;
                    queue.push_back(i);
                }


            }

        }

    }




    }

    void
    Graph::bfsHelper(Node* s)
   {

    vector<Edge*> incidentEdges = s-> getIncidentEdges();
    if(s->getLabel() == NodeLabel::unexplored && s == nodeTable->begin()->second){

        s->setLabel(NodeLabel::visited);
    }

    for(int i =0; i < s-> getIncidentEdges().size(); i++)
    {

        if(incidentEdges[i]-> getLabel() == (EdgeLabel:: unexplored))
        {

            Node* e = incidentEdges[i] -> opposite(s);
            if(e->getLabel() == NodeLabel::unexplored)
            {
                incidentEdges[i] -> setLabel(EdgeLabel::discovery);
                e->setLabel(NodeLabel::visited);


            }
            else{

                incidentEdges[i]->setLabel(EdgeLabel::cross);
            }
        }


    }


    }
}
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • 1
    "When I run it in the terminal, it says a warning message" I guess you mean when you compile. What warning, anyway? – Federico klez Culloca Dec 11 '19 at 16:29
  • 1
    `noOfNodes` is Uninitialized in `bfs` resulting in Undefined Behavior. – 1201ProgramAlarm Dec 11 '19 at 16:30
  • 1
    `int size = 0; int* bfsarray[size];` -- This is not valid C++. Arrays in C++ must have their sizes denoted by a constant expression, not a runtime variable. Second, even if this were legal C++, you are creating an array with maximum size of 0 elements. – PaulMcKenzie Dec 11 '19 at 16:35
  • I got it to work more but my bfs function is only printing out zeros. I initialized size, begin, adjMatrix, noOfNodes to zero, which one of these should I not initialize to zero? – softwares12 Dec 11 '19 at 23:20
  • I do not see a recursive call in you `dfsRecursive` method. Also I am not sure why you are using two for loop, IMO first `for(int i =0; i – Vinod Krishnan Dec 12 '19 at 18:06

0 Answers0