Java Data Structures Graph breadth-first search

Description

Java Data Structures Graph breadth-first search



class Queue {
   private final int SIZE = 20;
   private int[] queArray;
   private int front;
   private int rear;

   public Queue() {
      queArray = new int[SIZE];
      front = 0;/*  w  w w  .j  a va 2 s.  c  om*/
      rear = -1;
   }

   public void insert(int j) {
      if (rear == SIZE - 1)
         rear = -1;
      queArray[++rear] = j;
   }

   public int remove() {
      int temp = queArray[front++];
      if (front == SIZE)
         front = 0;
      return temp;
   }

   public boolean isEmpty() {
      return (rear + 1 == front || (front + SIZE - 1 == rear));
   }
}

class Vertex {
   public char label;
   public boolean wasVisited;

   public Vertex(char lab) {
      label = lab;
      wasVisited = false;
   }
}

class Graph {
   private final int MAX_VERTS = 20;
   private Vertex vertexList[]; // list of vertices
   private int adjMat[][]; // adjacency matrix
   private int nVerts; // current number of vertices
   private Queue theQueue;

   public Graph() {
      vertexList = new Vertex[MAX_VERTS];
      adjMat = new int[MAX_VERTS][MAX_VERTS];
      nVerts = 0;
      for (int j = 0; j < MAX_VERTS; j++) // set adjacency
         for (int k = 0; k < MAX_VERTS; k++) // matrix to 0
            adjMat[j][k] = 0;
      theQueue = new Queue();
   }

   public void addVertex(char lab) {
      vertexList[nVerts++] = new Vertex(lab);
   }

   public void addEdge(int start, int end) {
      adjMat[start][end] = 1;
      adjMat[end][start] = 1;
   }

   public void displayVertex(int v) {
      System.out.print(vertexList[v].label);
   }

   public void bfs() // breadth-first search
   {
      vertexList[0].wasVisited = true; // mark it
      displayVertex(0); // display it
      theQueue.insert(0); // insert at tail
      int v2;

      while (!theQueue.isEmpty()) // until queue empty,
      {
         int v1 = theQueue.remove(); // remove vertex at head
         while ((v2 = getAdjUnvisitedVertex(v1)) != -1) {
            vertexList[v2].wasVisited = true;
            displayVertex(v2);
            theQueue.insert(v2); // insert it
         }
      } // (queue not empty)

      for (int j = 0; j < nVerts; j++) // reset flags
         vertexList[j].wasVisited = false;
   }

   // returns an unvisited vertex adj to v
   public int getAdjUnvisitedVertex(int v) {
      for (int j = 0; j < nVerts; j++)
         if (adjMat[v][j] == 1 && vertexList[j].wasVisited == false)
            return j;
      return -1;
   }
}

public class Main {
   public static void main(String[] args) {
      Graph theGraph = new Graph();
      theGraph.addVertex('A'); // 0 (start for bfs)
      theGraph.addVertex('B'); // 1
      theGraph.addVertex('C'); // 2
      theGraph.addVertex('D'); // 3
      theGraph.addVertex('E'); // 4

      theGraph.addEdge(0, 1); // AB
      theGraph.addEdge(1, 2); // BC
      theGraph.addEdge(0, 3); // AD
      theGraph.addEdge(3, 4); // DE

      System.out.print("Visits: ");
      theGraph.bfs(); // breadth-first search
      System.out.println();
   }
}



PreviousNext

Related