I am facing a problem with Queue. In my code, queue is adding items currently but have problems in removing items. I have 3 class Point and Queue, C. Point class holds the value of x, coordinate and distance. And queue Class is implemented with array.
Here is my functional code.
public class wormhole {
public static int [][] ara=new int [21][21];
public static boolean[][] visited=new boolean[21][21];
public static int [][] ans=new int[21][21];
public static int [] nx={1,-1,0,0};
public static int [] ny={0,0,1,-1};
public static int n;
public static boolean is_safe(int x,int y){
if(x>=0 && x<n&& y>=0&&y<n && ara[x][y]==1&& !visited[x][y])
return true;
return false;
}
public static void BFS(int x,int y){
Queue queue=new Queue(1000);
Point in1=new Point(x,y,0);
in1.x=x;
in1.y=y;
in1.dis=0;
queue.add(in1);
visited[x][y]=true;
while(!queue.is_Empty(queue)) {
Point r = queue.remove();
int a = r.x;
int b = r.y;
int d = r.dis;
System.out.printf("remove %d %d %d\n",a,b,d);
ans[a][b] = d;
for (int i = 0; i < 4; i++) {
int nxx = a + nx[i];
int nyy = b + ny[i];
if (is_safe(nxx, nyy)) {
visited[a][b] = true;
in1.x = nxx;
in1.y = nyy;
in1.dis = d + 1;
System.out.printf("add %d %d %d\n",in1.x,in1.y,in1.dis);
queue.add(in1);
}
}
}
}
public static void main(String [] args){
Scanner in = new Scanner(System.in);
n=in.nextInt();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ara[i][j]=in.nextInt();
}
}
int q=in.nextInt();
C rare[]=new C[q];
for(int i=0;i<q;i++)
{
int a,b;
a=in.nextInt();
b=in.nextInt();
rare[i]=new C(a,b);
}
BFS(0,2);
}
}
and my Point and Queue class look like bellow.
class Point{
public int x,y,dis;
Point(int x,int y,int dis){
this.x=x;
this.y=y;
this.dis=dis;
}
}
class Queue {
int front, rear, size, capacity;
Point[] ara;
public Queue(int capacity) {
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
ara = new Point[this.capacity];
}
public boolean is_Full(Queue queue) {
return (queue.size == queue.capacity);
}
public boolean is_Empty(Queue queue) {
return (queue.size == 0);
}
public void add(Point item) {
if (is_Full(this))
return;
this.rear = (this.rear + 1) % this.capacity;
ara[this.rear] = item;
this.size = this.size + 1;
}
public Point remove() {
if (is_Empty(this))
return null;
Point item = this.ara[this.front];
this.front = (this.front + 1) % this.capacity;
this.size = this.size - 1;
return item;
}
public Point front() {
if(is_Empty(this))
return new Point(-1,-1,-1);
return(this.ara[this.front]);
}
public Point rear(){
if(is_Empty(this))
return new Point(-1,-1,-1);
return(this.ara[this.rear]);
}
}
class C{
public int x,y;
C(int x,int y){
this.x=x;
this.y=y;
}
}
Sample input & output
8
0 0 1 0 0 1 0 0
0 0 1 0 1 1 1 1
0 0 1 0 1 0 0 1
0 0 1 1 1 0 0 0
0 1 0 0 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
3
0 2
1 7
4 7
output
remove 0 2 0
1.add 1 2 1
remove 1 2 1
2.add 2 2 2
remove 2 2 2
3.add 3 2 3
remove 3 2 3
4.add 3 3 4
remove 3 3 4
5.add 3 4 5
remove 3 4 5
6.add 4 4 6
7.add 2 4 6
remove 2 4 6
8.add 1 4 7
remove 1 4 7
9.add 1 5 8
remove 1 5 8
10.add 0 5 9
11.add 1 6 9
remove 1 6 9
12.add 1 7 10
remove 1 7 10
13.add 2 7 11
remove 2 7 11
remove 2 7 11
remove 2 7 11
Queue is inserting item correctly. But after 6th and 7th insertion its removing the only 7th item. At the last stage, there is also some unnecessary remove. I cant find any reason to behave like this. Can anyone help me please?
N.B: I have also checked this code with default Queue class of java but the result is same.
The reason you are getting unexpected behaviour with your Queue (or any Queue for that matter), and thus unexpected behaviour doing a BFS, is that you do not add 'new' objects to the Queue.
At the start of BFS, you create exactly 1 Point:
Point in1=new Point(x,y,0);
And then throughout the BFS, you keep manipulating the same object. This becomes a problem when in your BFS, there is a point in the graph where there are multiple edges (Point #6 in your output). When you overwrite the same point, the Queue has lost the correct state of Nodes to traverse.
The solution will be to create a new Point(x,y,d) each time you add to the Queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With