UNDIRECTED GRAPHS (create, BFS, DFS, adjacent nodes)

 BFS uses Queue and DFS uses Stack for implementation.

#include<bits/stdc++.h>
using namespace std;
int adj[15][15];
void dfs(int v, int n)
{
  //before Exploring the first node completely(push it to stack), 
  //move on to the exploration its of another connected node, 
  //if no node is connected or left further for exploration than pop from stack,
  //and print to visit that node.
  int stk[n],i,top=-1,visited[n+1]{0};
  top++;
  stk[top]=v;
  cout<<"DFS is : "<<v<<" ";
  visited[v]=1;
  while(top>=0)
  {
      v=stk[top];
      top-=1;
      if(visited[v]==0)
        {cout<<v<<" "; visited[v]=1;}
 
      for(i=1;i<=n;i++)
        if(adj[v][i]==1 && visited[i]==0)
      {top++;stk[top]=i;}
  }
}
void display(int n)
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {cout<<adj[i][j]<<" ";}
        cout<<endl;

    }
}
void adjacent(int v, int n)
{
    int i;
    cout<<"\nAdjacent nodes are : ";
    for(i=1;i<=n;i++)
        if(adj[v][i]==1)
            cout<<i<<" ";
}
void bfs(int v, int n)
{
// First completely explore the ongoing node(add all its connected nodes to queue), 
//and than print that node to make it visited,
//Now pick up next node from queue(by moving front ahead),
// and explore it completely(push its connected nodes if not already present in queue), 
//now again print that node to make it visited and 
//move on to the next node until no node is left in the queue. 
     int qu[n], visited[n+1]{0}, frnt=-1, rear=-1, i;
     cout<<"BFS is : "<<v<<" ";
     visited[v]=1;
     rear++; frnt++;
     qu[rear]=v;
     while(frnt<=rear)
     {
         v=qu[frnt];
         frnt++;
         for(i=1;i<=n;i++) // checking unvisited nodes;
         {
             if(adj[v][i]==1 && visited[i]==0)
             {
                 cout<<i<<" ";
                 visited[i]=1;
                 rear++;
                 qu[rear]=i;
             }
         }
     }

}
void create(int n)
{
    int maxnodes=n*((n-1)/2),i,u,v,j;
    for(i=0;i<maxnodes;i++)
    {
        cout<<"\nOrigin & dest:(0,0 to exit): ";
        cin>>u>>v;
        if(u==0 && v==0)
            break;
        else if(  u> n || v > n || u<=0 || v<=0)
{
cout<<"Invalid edge!\n";
i--;
}
else
        {
            adj[u][v]=1;
            adj[v][u]=1;
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {cout<<adj[i][j]<<" ";}
        cout<<endl;

    }
}
int main()
{
    int n,i,j,vertex,choice,ch;
    cout<<"enter the number of nodes: ";
    cin>>n;
    create(n);

    while(ch)
    {
    cout<<"\nEnter 1. for BFS";
    cout<<"\nEnter 2. for DFS";
    cout<<"\nEnter 3. for adjacent nodes";
    cout<<"\nEnter 4. to display adjacency matrix";
    cout<<"\nEnter your choice";
    cin>>choice;
    if(choice==1)
    {
        cout<<"\nenter the starting vertex : ";
        cin>>vertex;
        bfs(vertex,n);
    }
    else if(choice==2)
    {
        cout<<"\nenter the starting vertex : ";
        cin>>vertex;
        dfs(vertex,n);
    }
    else if(choice==3)
    {
        cout<<"\nenter the vertex to find adjacent node : ";
        cin>>vertex;
        adjacent(vertex,n);
    }
    else if(choice==4)
    {
        display(n);
    }
    else
        cout<<"\nInvalid choice";

    cout<<"\nDo you want to continue(0 to exit) : ";
    cin>>ch;
    }
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree