rootUser

Bfs Dfs (java)

May 24th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.81 KB | None | 0 0
  1. package bfsdfs;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main
  6. {
  7.     static int top=-1,front=-1,rear=-1;
  8.     static int[][] a=new int[20][20];
  9.     static int[] q=new int[20];
  10.     static int[] stack=new int[20];
  11.     static int[] vis = new int[20];
  12.  
  13.     static void bfs(int s,int n)
  14.     {
  15.         int p,i;
  16.         add(s);
  17.         vis[s]=1;
  18.         p=delete();
  19.         if(p!=0)
  20.             System.out.format(" %d",p);
  21.         while(p!=0)
  22.         {
  23.             for(i=1; i<=n; i++)
  24.                 if((a[p][i]!=0)&&(vis[i]==0))
  25.                 {
  26.                     add(i);
  27.                     vis[i]=1;
  28.                 }
  29.             p=delete();
  30.             if(p!=0)
  31.                 System.out.format(" %d ",p);
  32.         }
  33.         for(i=1; i<=n; i++)
  34.             if(vis[i]==0)
  35.                 bfs(i,n);
  36.     }
  37.  
  38.  
  39.     static void add(int item)
  40.     {
  41.         if(rear==19)
  42.             System.out.format("QUEUE FULL");
  43.         else
  44.         {
  45.             if(rear==-1)
  46.             {
  47.                 q[++rear]=item;
  48.                 front++;
  49.             }
  50.             else
  51.                 q[++rear]=item;
  52.         }
  53.     }
  54.     static int delete()
  55.     {
  56.         int k;
  57.         if((front>rear)||(front==-1))
  58.             return(0);
  59.         else
  60.         {
  61.             k=q[front++];
  62.             return(k);
  63.         }
  64.     }
  65.  
  66.  
  67. //***************DFS(depth-first search) code******************//
  68.     static void dfs(int s,int n)
  69.     {
  70.         int i,k;
  71.         push(s);
  72.         vis[s]=1;
  73.         k=pop();
  74.         if(k!=0)
  75.             System.out.format(" %d ",k);
  76.         while(k!=0)
  77.         {
  78.             for(i=1; i<=n; i++)
  79.                 if((a[k][i]!=0)&&(vis[i]==0))
  80.                 {
  81.                     push(i);
  82.                     vis[i]=1;
  83.                 }
  84.             k=pop();
  85.             if(k!=0)
  86.                 System.out.format(" %d ",k);
  87.         }
  88.         for(i=1; i<=n; i++)
  89.             if(vis[i]==0)
  90.                 dfs(i,n);
  91.     }
  92.     static void push(int item)
  93.     {
  94.         if(top==19)
  95.             System.out.format("Stack overflow ");
  96.         else
  97.             stack[++top]=item;
  98.     }
  99.     static int pop()
  100.     {
  101.         int k;
  102.         if(top==-1)
  103.             return(0);
  104.         else
  105.         {
  106.             k=stack[top--];
  107.             return(k);
  108.         }
  109.     }
  110.  
  111.     public static void main(String[] args)
  112.     {
  113.         Scanner scanner = new Scanner(System.in);
  114.         int n,i,s,ch,j;
  115.         char c,dummy;
  116.         System.out.format("\nENTER THE NUMBER VERTICES : ");
  117.  
  118.         n = scanner.nextInt();
  119.         for(i=1; i<=n; i++)
  120.         {
  121.             for(j=1; j<=n; j++)
  122.             {
  123.                 System.out.format("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 : ",i,j);
  124.  
  125.                 a[i][j] = scanner.nextInt();
  126.             }
  127.         }
  128.         System.out.format("THE ADJACENCY MATRIX IS\n");
  129.         for(i=1; i<=n; i++)
  130.         {
  131.             for(j=1; j<=n; j++)
  132.             {
  133.                 System.out.format(" %d",a[i][j]);
  134.             }
  135.             System.out.format("\n");
  136.         }
  137.  
  138.         do
  139.         {
  140.             for(i=1; i<=n; i++)
  141.                 vis[i]=0;
  142.             System.out.format("\nMENU");
  143.             System.out.format("\n1.B.F.S");
  144.             System.out.format("\n2.D.F.S");
  145.             System.out.format("\nENTER YOUR CHOICE : ");
  146.             ch = scanner.nextInt();
  147.            
  148.             System.out.format("ENTER THE SOURCE VERTEX : ");
  149.             s = scanner.nextInt();
  150.  
  151.             switch(ch)
  152.             {
  153.             case 1:
  154.                 bfs(s,n);
  155.                 break;
  156.             case 2:
  157.                 dfs(s,n);
  158.                 break;
  159.             }
  160.             System.out.format("\nDO U WANT TO CONTINUE(Y/N) ? ");
  161.             c = scanner.next().charAt(0);
  162.         }
  163.         while((c=='y')||(c=='Y'));
  164.     }
  165. }
Add Comment
Please, Sign In to add comment