melissa melissa - 1 day ago 3
Java Question

Find height of a node in bfs tree

I want to find the height of a tree node in bfs but it's not working out, it's giving me the correct height for first 5 nodes but the last two nodes are not correct. Here is the code

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bfs;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class BreadthFirstSearchExample
{

private Queue<Node> queue;
static ArrayList<Node> nodes=new ArrayList<Node>();
static class Node
{
int data;
boolean visited;
int parent;
int height;

Node(int data){
this.data=data;


}
}

public BreadthFirstSearchExample()
{
queue = new LinkedList<Node>();
}

// find neighbors of node using adjacency matrix
// if adjacency_matrix[i][j]==1, then nodes at index i and index j are connected
public ArrayList<Node> findNeighbours(int adjacency_matrix[][],Node x)
{
int nodeIndex=-1;

ArrayList<Node> neighbours=new ArrayList<Node>();
for (int i = 0; i < nodes.size(); i++) {
if(nodes.get(i).equals(x))
{
nodeIndex=i;
break;
}
}

if(nodeIndex!=-1)
{
for (int j = 0; j < adjacency_matrix[nodeIndex].length; j++) {
if(adjacency_matrix[nodeIndex][j]==1)
{
neighbours.add(nodes.get(j));
}
}
}
return neighbours;
}

public void bfs(int adjacency_matrix[][], Node node)
{

queue.add(node);
node.visited=true;


int nf =0;

while (!queue.isEmpty())
{
nf = queue.size();
Node element=queue.remove();

System.out.print(element.data + "\t");


System.out.print("Height" + element.height + "\t");

ArrayList<Node> neighbours=findNeighbours(adjacency_matrix,element);
for (int i = 0; i < neighbours.size(); i++) {
Node n=neighbours.get(i);
int mf = neighbours.size();

if(n!=null && !n.visited)
{

n.parent=element.data;
queue.add(n);
n.visited=true;
}
n.height++;
element.height = n.height;
}

}
}

public static void main(String arg[])
{

Node node40 =new Node(40);
Node node10 =new Node(10);
Node node20 =new Node(20);
Node node30 =new Node(30);
Node node60 =new Node(60);
Node node50 =new Node(50);
Node node70 =new Node(70);

nodes.add(node40);
nodes.add(node10);
nodes.add(node20);
nodes.add(node30);
nodes.add(node60);
nodes.add(node50);
nodes.add(node70);
int adjacency_matrix[][] = {
{0,1,1,0,0,0,0}, // Node 1: 40
{1,0,0,1,0,0,0}, // Node 2 :10
{1,0,0,1,1,1,0}, // Node 3: 20
{0,1,1,0,1,0,0}, // Node 4: 30
{0,0,1,1,0,0,1}, // Node 5: 60
{0,0,1,0,0,0,1}, // Node 6: 50
{0,0,0,0,1,1,0}, // Node 7: 70
};
System.out.println("The BFS traversal of the graph is ");
BreadthFirstSearchExample bfsExample = new BreadthFirstSearchExample();
bfsExample.bfs(adjacency_matrix, node40);

}
}


The output of the code should be


40 Height0 10 Height1 20 Height1 30 Height2 60 Height2 50 Height2 70 Height3


but instead it is giving me the output


40 Height0 10 Height1 20 Height1 30 Height2 60 Height2 50 Height1 70 Height2


How can I solve this?

Answer

The for-loop of your bfs method has to look like this:

for (int i = 0; i < neighbours.size(); i++) {
  Node n = neighbours.get(i);
  int mf = neighbours.size();

  if (n != null && !n.visited) {
    n.parent = element.data;
    queue.add(n);
    n.visited = true;
    n.height = element.height + 1;    // <- this is right
  }

  // This was wrong
  //n.height++;                    
  //element.height = n.height;
}

Explanation:

The changed line sets the height of the visited node ("n") to the height of the parent node ("element") + 1, but only on the first visit (so inside the if statement). This is what you want, i.e. the height.

What you did before was increment the height of a node each time you saw it as a neighbor, independently of whether it was visited before or not. And then you change the height of the parent node to that, but never see it because it isn't printed again.

Comments