Skip to main content

BREADTH FIRST SEARCH IN JAVA

BREADTH FIRST SEARCH IN JAVA:-


package com.problems.graph;

import java.awt.DisplayMode;
import java.util.Iterator;
import java.util.LinkedList;




public class BFSGraph {

 int maxsize;
 Vertex vertexlist[];
 int matrixlist[][];
 int vertexcount;
 @SuppressWarnings({ "rawtypes", "unused" })
 LinkedList queue;
 
 public BFSGraph()
 {
  maxsize=20;
  matrixlist=new int[maxsize][maxsize];
  vertexlist=new Vertex[maxsize];
  for(int i=0;i<maxsize;i++)
  {
   for(int j=0;j<maxsize;j++)
   {
    matrixlist[i][j]=0;
   }
  }
  queue= new LinkedList();
  
 }
 
 public void addVertex(char label)
 {
  vertexlist[vertexcount++]=new Vertex(label);
 }
 public void addEdge(int i,int j)
 {
  matrixlist[i][j]=1;
  matrixlist[j][i]=1;
 }
 
 public void displayVertex(int v)
 {
  System.out.println(vertexlist[v].label);
 }
 
 public int adjVertex(int v)
 {
  for(int i=0;i<maxsize;i++)
  {
   if(matrixlist[v][i]==1 && vertexlist[i].visited==false )
    return i;
  }
  return -1;
 }
 
 public void bfs()
 {
  System.out.println("in bfs");
  vertexlist[0].visited=true;
  displayVertex(0);
  queue.add(0);
  int v2;
  while(!queue.isEmpty())
  {
   int v1=(Integer) queue.remove();
   System.out.println("removed"+v1);
   while( (v2=adjVertex(v1))!=-1)
   {
   vertexlist[v2].visited=true;
   displayVertex(v2);
   queue.add(v2);
   }
  }
  for(int j=0;j<vertexcount;j++)
  {
   vertexlist[j].visited=false;
  }
 }
}

Comments

.

Popular posts from this blog

How to do Effective Programming?

There is no secret to doing effective programming but following a sequence of steps and procedures can help in building great programs. Today I will take you on a tour to some of the best practices that are integrants of perfect programs. Algorithms are important: - You can’t deny the fact that algorithms are important and before start to write any program, Algorithms are must to write. For example, if there is a program about finding the sum of two numbers? What will you write ?The steps can be :- 1)   Get the first number. 2)   Get the second number. 3)   Add both numbers. This is what algorithm is writing about ,a list of statements .From the above three statements you can conclude boundary cases (at least two number should be there in input), mathematical function(Sum is needed here) , storage capacity(amount of memory to be assign to the variables), number of methods by analyzing repeat steps (reduce replete codes) and many other things. During algorithm only yo

Shortest Path from source to Vertex :- Dijkstra Algorithm

Shortest Path from source to Vertex :- Dijkstra Algorithm:- Dijkstra  Algorithms is an algorithm use to find the shortest path from source vertex to a given vertex. package Graph ; import java.util.HashMap ; abstract public class DirectedGraph { Vertex [] vertexlist = new Vertex [ 10 ]; HashMap < Character , HashMap < Character , Integer >> edgelist = new HashMap <>(); Vertex vertex ; //count of vertex and edge static int vertexcount = 0 ; int edgecount = 0 ; /* * This function takes a label and insert in the vertex list as well as edge list since it is new vertex it will add * null to its adjoining vertices */ int addVertex ( char label ) { vertex = new Vertex ( label ); vertexlist [ vertexcount ]= vertex ; System . out . println ( vertexlist [ vertexcount ]. label ); edgelist . put ( vertex . label , null ); vertexcount ++; return vertexcount ; } int addEdge ( char label , char []