Advertisement
jTruBela

BFS/Queue

Nov 14th, 2021
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. package bfs;
  2.  
  3. import stack_queue_linkedlist.Queue;
  4.  
  5. public class Graph {
  6.     public int n;   //number of vertice
  7.     public int[][] A;   //the adjacency matrix
  8.     private final int WHITE = 2;
  9.     private final int GRAY = 3;
  10.     private final int BLACK = 4;
  11.    
  12.     public Graph () {
  13.         n = 0;
  14.         A = null;
  15.     }
  16.    
  17.     public Graph (int _n, int[][] _A) {
  18.         this.n = _n;
  19.         this.A = _A;
  20.     }
  21.    
  22.     /*
  23.      * Input: s denotes the index of the source node
  24.      * Output: the array dist, where dist[i] is the distance between the i-th node to s
  25.      */
  26.     public int[] bfs (int s) {
  27.         int [] color = new int[n];
  28.         int [] distance =  new int[n];
  29.         Queue queue = new Queue(n);
  30.         int u = 0;
  31.        
  32.  
  33.         for(int i=0; i<n; i++) {
  34.             if(i!=s) {
  35.                 color[i]=WHITE;
  36.                 distance[i]=Integer.MAX_VALUE;
  37.             }
  38.         }
  39.         color[s]=GRAY;
  40.         distance[s]=0;
  41.        
  42.         queue.enqueue(s);  
  43.                
  44.         while (queue != null) {
  45.             System.out.println(queue);
  46.  
  47.             u = queue.dequeue();
  48.  
  49.  
  50.             for(int v=0;v<n;v++) {
  51.                 if (color[v]==WHITE && A[u][v]==1) {
  52.                     color[v]=GRAY;
  53.                     distance[v] = distance[u]+1;
  54.                     queue.enqueue(v);
  55.                 }
  56.                
  57.             }
  58.             color[u] = BLACK;
  59.         }
  60.  
  61.  
  62.         return distance;
  63.     }
  64.    
  65.     public void print_array (int[] array) {
  66.         for (int i = 0; i < array.length; i++)
  67.             System.out.println(i + ": " + array[i]);
  68.     }
  69.    
  70.     /**
  71.      * @param args
  72.      */
  73.     public static void main(String[] args) {
  74.         // TODO Auto-generated method stub
  75.         int n = 8;
  76.         int[][] A =
  77.             {
  78.             {0, 1, 0, 0, 1, 0, 0, 0},
  79.             {1, 0, 0, 0, 0, 1, 0, 0},
  80.             {0, 0, 0, 1, 0, 1, 1, 0},
  81.             {0, 0, 1, 0, 0, 0, 1, 1},
  82.             {1, 0, 0, 0, 0, 0, 0, 0},
  83.             {0, 1, 1, 0, 0, 0, 1, 0},
  84.             {0, 0, 1, 1, 0, 1, 0, 1},
  85.             {0, 0, 0, 1, 0, 0, 1, 0}
  86.             };
  87.         Graph g = new Graph(n, A);
  88.         g.print_array(g.bfs(1));
  89.     }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96. //QUEUE CLASS
  97. /*****************************************************************
  98.  *
  99.  * CSIT212 - Queue.java     Justin Trubela      10/12/21
  100.  *
  101.  * Purpose: Queue
  102.  *
  103.  * Task 2 (30 pts.).   Implement  the enqueue(int x), dequeue()
  104.  *      function forQueue in Queue.java as discussed in Lecture 5.
  105.  * Task 5 (10 pts. Extra Credit). In the enqueue(int x) function of
  106.  *      Queue.java,we do not check if the queue is already full.  
  107.  *      Implement the capacity check feature for enqueue(int x) to
  108.  *      print  the error message.)
  109.  * Task 6 (10 pts Extra Credit).  In the dequeue()function of
  110.  *      Queue.java,we do not check if the queue is empty.  
  111.  *      If we dequeue an empty queue,we should also get an error.  
  112.  *      Implement the empty check feature for enqueue(int x).
  113.  *
  114.  *****************************************************************/
  115.  
  116.  
  117. package stack_queue_linkedlist;
  118.  
  119. public class Queue {
  120.    
  121.     public int size;
  122.     public int[] array;
  123.     public int head;
  124.     public int tail;
  125.    
  126.     public Queue () {
  127.         size = 0;
  128.         array = null;
  129.         head = 0;
  130.         tail = 0;
  131.     }
  132.    
  133.     public Queue (int _size) {
  134.         size = _size;
  135.         array = new int[size];
  136.         head = 0;
  137.         tail = 0;
  138.     }
  139.    
  140.     /*
  141.      * Implement the ENQUEUE(Q, x) function
  142.      */
  143.     public void enqueue (int x) {
  144.        
  145.             array[tail] = x;
  146.  
  147.             if (array.length==0) {
  148.                 tail = 1;
  149.             }
  150.            
  151.             if (tail == array.length-1) {
  152.                 System.err.println("Queue is full already");
  153.             }
  154.             else {
  155.                 tail++;
  156.             }
  157.  
  158.     }
  159.    
  160.     /*
  161.      * Implement the DEQUEUE(Q) function
  162.      */
  163.  
  164.     public int dequeue () {
  165.         int x = array[head];
  166.        
  167.         if (head > tail||head < 0) {
  168.             System.err.println("Queue is empty already");
  169.             return x;
  170.         }
  171.         else {
  172.             head++;
  173.         }
  174.         return x;
  175.     }
  176.    
  177.    
  178.     /*
  179.      * Convert queue to string in the format of #size, head, tail, [#elements]
  180.      */
  181.     public String toString () {
  182.         String str;
  183.        
  184.         str = size + ", " + head + ", " + tail + ", [";
  185.         for (int i = head; i%size < tail; i++)
  186.             str += array[i] + ",";
  187.        
  188.         str += "]";
  189.         return str;
  190.     }
  191.  
  192.     /**
  193.      * @param args
  194.      */
  195.     public static void main(String[] args) {
  196.         // TODO Auto-generated method stub
  197.         Queue q;
  198.        
  199.         q = new Queue(10);
  200.         for (int i = 0; i < 5; i++)
  201.             q.enqueue(i);
  202.         System.out.println(q.toString());
  203.         for (int i = 0; i < 7; i++)
  204.             q.dequeue();
  205.         System.out.println(q.toString());
  206.     }
  207.  
  208. }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement