Saturday, April 27, 2013

Breadth First Search (BFS) and Depth First Search (DFS) Program in C++/CPP


#include <iostream>
#include <fstream>

using namespace std;


template <class T>
class Stack{
private:
int max_size, top;
T * list;
public:
Stack(T size){
max_size = size;
top = 0;
list = new T[max_size];
}
bool isEmpty(){
return (top <= 0);
}
bool isFull(){
return (top >=max_size);
}
void push(const T& item){
if(!isFull()){
list[top] = item;
top++;
}
}
T pop(){
if(!isEmpty()){
top--;
}
return list[top];
}

T Top(){
return list[top-1];
}
};

//Queue Implementation
template <class T>
class Queue {
private:
int Qmax, count, Qf, Qr;
T * list;

public:
Queue(int size){
Qmax = size;
Qf = 0;
Qr = Qmax - 1;
count = 0;
list = new T[Qmax];
}

bool isEmpty(){
return (count == 0);
}

bool isFull(){
return (count == Qmax);
}

void addQueue(const T &element){
if(!isFull()){
Qr = (Qr + 1 ) % Qmax;
count++;
list[Qr] = element;
}
}

T FindFront(){
return list[Qf];
}

T DeleteQueue(){
T x;
if(!isEmpty()){
x = list[Qf];
count--;
Qf = (Qf + 1) % Qmax;
return x;
}else
return 100;
}


};



void revisit(int *visited, int size){
for(int i=0; i<size; i++){
visited[i] = 0;
}
}

int main(int argc, char **argv)
{
if(argc < 2){
cout<<"\n Please Enter a File Name:\n\n\n";
return 0;
}
argv++;

fstream input(*argv);  //Input as a file name at run time( from command line)

int size;

if(input>>size) cout<<"\nMatrix of: "<<size<<"*"<<size<<endl;
else return 0;

int adjmat[size][size];
int visited[size];

for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
input>>adjmat[i][j];
}
}



cout<<"\nAdj. Matrix:\n";
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
cout<<adjmat[i][j]<<" ";
}
cout<<"\n";
}


//BFS(adjmat,size,visited);
cout<<"\n\nBreadth First Search:\n";
revisit(visited,size);
Queue<int> bfs(size);
{
int v = 0, w ;

bfs.addQueue(v);
visited[v] = 1;


while(!bfs.isEmpty()){

w = bfs.DeleteQueue();

cout<<w<<"--";

if(w==100){continue;}

for(int i = 0; i<size; i++){
if(adjmat[w][i] == 1 && visited[i] == 0){
bfs.addQueue(i);
visited[i] = 1;
}
}


}
cout<<"\n";
}
//bfs code ends here


//DFS Code Starts From Here
cout<<"\n\nDepth First Search:\n";
Stack<int> dfs(size);
revisit(visited,size);
{
int v = 0;
cout<<v<<"--";
dfs.push(v);
visited[0] = 1;

while(!dfs.isEmpty()){

for(int i=0; i<size; i++){
if(adjmat[v][i] == 1 && visited[i] == 0){
cout<<i<<"--";
dfs.push(i);
visited[i] = 1;
v = i;
i=0;
}
}

v = dfs.pop();

}
cout<<"\n";
//DFS Code ends Here



//Biparted Graph
{
revisit(visited,size);
int colored[size];
revisit(colored,size);
Queue<int> Bipart(size);
int v = 0, x;
visited[v] = 1;
colored[v] = 3;

for(int i = 0; i<size; i++){
if(adjmat[v][i] == 1 && visited[i] == 0){
Bipart.addQueue(i);
visited[i] = 1;
colored[i] = 4;
}
}

while(!Bipart.isEmpty()){
x = Bipart.DeleteQueue();
for(int i=0; i<size; i++){
if(adjmat[x][i] == 1 && visited[i] == 1 && colored[x]==colored[i]){
cout<<"\n\n Not a Biparted Graph\n\n";
return 0;
}
}

for(int j=0; j<size; j++){
if(adjmat[x][j] == 1 && visited[j] == 0){
Bipart.addQueue(j);
visited[j] = 1;
if(colored[x] == 3){
colored[j] = 4;
}else colored[j] = 3;
}
}
}

cout<<"\n\nBiparted Graph\n";

}
//Biparted graph ends here
}
return 0;
}