mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
File clean-up
This commit is contained in:
@ -2,27 +2,27 @@ import java.util.*;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of a Breadth First Search
|
* Implementation of a Breadth First Search
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class bfs{
|
public class bfs{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The BFS implemented in code to use.
|
* The BFS implemented in code to use.
|
||||||
*
|
*
|
||||||
* @param a Structure to perform the search on
|
* @param a Structure to perform the search on a graph, adjacency matrix etc.
|
||||||
* @param vertices The vertices to use
|
* @param vertices The vertices to use
|
||||||
* @param source The Source
|
* @param source The Source
|
||||||
*/
|
*/
|
||||||
public static void bfsImplement(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
public static void bfsImplement(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
||||||
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
||||||
Arrays.fill(b,(byte)-1); //status initialization
|
Arrays.fill(b,(byte)-1); //status initialization
|
||||||
/* code status
|
/* code status
|
||||||
-1 = ready
|
-1 = ready
|
||||||
0 = waiting
|
0 = waiting
|
||||||
1 = processed */
|
1 = processed */
|
||||||
|
|
||||||
Stack st = new Stack(vertices); //operational stack
|
Stack st = new Stack(vertices); //operational stack
|
||||||
st.push(source); //assigning source
|
st.push(source); //assigning source
|
||||||
while(!st.isEmpty()){
|
while(!st.isEmpty()){
|
||||||
@ -30,18 +30,18 @@ public class bfs{
|
|||||||
System.out.println(st.peek());
|
System.out.println(st.peek());
|
||||||
int pop=st.peek();
|
int pop=st.peek();
|
||||||
b[pop]=(byte)1; //assigning processed status
|
b[pop]=(byte)1; //assigning processed status
|
||||||
st.pop(); //removing head of the queue
|
st.pop(); //removing head of the queue
|
||||||
for(int i=0;i<vertices;i++){
|
for(int i=0;i<vertices;i++){
|
||||||
if(a[pop][i]!=0 && b[i]!=(byte)0 && b[i]!=(byte)1 ){
|
if(a[pop][i]!=0 && b[i]!=(byte)0 && b[i]!=(byte)1 ){
|
||||||
st.push(i);
|
st.push(i);
|
||||||
b[i]=(byte)0; //assigning waiting status
|
b[i]=(byte)0; //assigning waiting status
|
||||||
}}}
|
}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main method
|
* The main method
|
||||||
*
|
*
|
||||||
* @param args Command line arguments
|
* @param args Command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
@ -49,9 +49,9 @@ public class bfs{
|
|||||||
int vertices=in.nextInt(),source=in.nextInt();
|
int vertices=in.nextInt(),source=in.nextInt();
|
||||||
byte [][]a=new byte [vertices][vertices];
|
byte [][]a=new byte [vertices][vertices];
|
||||||
//initially all elements of a are initialized with value zero
|
//initially all elements of a are initialized with value zero
|
||||||
|
|
||||||
for(int i=0;i<vertices;i++){
|
for(int i=0;i<vertices;i++){
|
||||||
int size =in.nextInt();
|
int size =in.nextInt();
|
||||||
for(int j=0;j<size;j++){
|
for(int j=0;j<size;j++){
|
||||||
a[i][in.nextInt()]=1; //taking adjacency entries by assigning 1
|
a[i][in.nextInt()]=1; //taking adjacency entries by assigning 1
|
||||||
}
|
}
|
Reference in New Issue
Block a user