0

Function definitions are :

// DFS algorithm
void Graph::DFS(int current) {


    visited[current] = true;
    cout<<current << " ";
        

    for (int u : adjLists[current])
        if (!visited[u])
            DFS(u);

}

// BFS algorithm

void Graph::BFS(void){
    
    for(int i=0;i<numVertices;++i)
        visited[i] = false;
        
    list<int> q;
    q.push_back(0);
    visited[0] = true;
    
    while(!q.empty())
    {
        int u = q.front();
        q.pop_front();
        cout<< u << "  ";
        
        for( int i : adjLists[u])
            if(!visited[i]){
                q.push_back(i);
                visited[i] = true;
            }   
    }
}

DFS is working fine without using for loop to assign each element of visited array equal to false but BFS is not. Why? Whole program code is - https://hastebin.com/ojuyogexof.cpp

Brijesh Joshi
  • 19
  • 1
  • 10
  • 1
    Sounds like [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub) to me. Try calling BFS first, and then see if DFS works. Sometimes bad programs work even though they are bad. This is how undefined behaviour can work. – john Dec 03 '20 at 10:32
  • @john yes you are right , calling BFS first DFS is not working fine. So I need to use for loop for both functions ( but how can I because DFS is recursive )? – Brijesh Joshi Dec 03 '20 at 10:42
  • 2
    You need to have a function which starts the depth first search by setting the visited array to false and then calling your recursive function. – john Dec 03 '20 at 10:45
  • @john fine everything sorted out now :) – Brijesh Joshi Dec 03 '20 at 10:50

1 Answers1

0

The visited array never got initialised to fasle before calling the DFS() and that is why the output for the DFS() is only single node (starting node), i.e., 2.

Output for the entire code (without initialising visited array to false):

DFS 
2 
BFS
0  2  1  3 

SUGGESTION: Define a function init() function and call it before performing DFS() or BFS():

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

class Graph {
    int numVertices;
    list<int> *adjLists;
    bool *visited;

    public:
    Graph(int V);
    void addEdge(int src, int dest);
    void DFS(int vertex);
    void BFS(void);
    void init();
};

// Initialize graph
Graph::Graph(int vertices) {
    numVertices = vertices;
    adjLists = new list<int>[vertices];
    visited = new bool[vertices];
    for(int i=0;i<numVertices;++i){
        visited[i] = false;
    }
}

void Graph::init() {
    for(int i=0;i<numVertices;++i)
        visited[i] = false;
}

// Add edges
void Graph::addEdge(int src, int dest) {
    adjLists[src].push_front(dest);
    adjLists[dest].push_front(src);
}

// DFS algorithm
void Graph::DFS(int current) {
        
    visited[current] = true;
    cout<<current << " ";
        
    for (int u : adjLists[current])
        if (!visited[u])
            DFS(u);

}

// BFS algorithm

void Graph::BFS(void){
    
    list<int> q;
    q.push_back(0);
    visited[0] = true;
    
    while(!q.empty())
    {
        int u = q.front();
        q.pop_front();
        cout<< u << "  ";
        
        for( int i : adjLists[u])
            if(!visited[i]){
                q.push_back(i);
                visited[i] = true;
            }    
    }
}

int main() {
    
    Graph g(4);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 3);


    g.init();
    int start_node = 2 ;
    cout<<"DFS \n";
    g.DFS(start_node) ;

    g.init();
    cout<<endl<<"BFS"<<endl;
    g.BFS();

    return 0;
}

Output:

DFS 
2 3 1 0 
BFS
0  2  1  3 
Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35