i'm attempting write implementation of breadth-first search in java (not complete) running problem when try iterate through of adjacent vertices starting vertex.
the program iterating through of adjacent vertices, when reaches if statement checks see if targetvertex has been visited returns true instead of false , therefore never runs code inside brackets.
i'm not sure why occurring because in vertex class have initialized value of visited false new vertices. see error have made or know why might occurring? i've looked @ of other methods/classes , none of them @ point change value of "visited". appreciated!!
public void dobfs(string s){ //create queue queue<vertex> q = new linkedlist<vertex>(); //create arraylist keep track of visited vertices arraylist<string> visited = new arraylist<string>(); // retrieve starting vertex u, using string name entered vertex u = vertices.get(s); //enqueue starting node , add visited set visited.add(u.name); u.visited = true; u.distance = 0; q.add(u); while (! q.isempty() ) { vertex current = q.remove(); (edge e : current.getedges()) { //here problem. says it's true. if(e.targetvertex.visited = false) { visited.add(e.targetvertex.name); q.add(e.targetvertex); e.targetvertex.visited=true; } current = e.targetvertex; } }
}
the beginning of vertex class :
public class vertex { public string name; private list<edge> adjacent; public int posx = 0; public int posy = 0; public boolean visited = false; public double distance = double.positive_infinity; /** * construct new vertex containing adjacency list. * * @param vertexname * unique identifier vertex. * @param x * x coordinate vertex * @param y * y coordinate vertex */ public vertex(string vertexname, int x, int y) { name = vertexname; adjacent = new linkedlist<edge>(); posx = x; posy = y; distance = double.positive_infinity; visited = false; }
if(e.targetvertex.visited = false) {
...is assignment. use
if(e.targetvertex.visited == false) {
...or just
if(!e.targetvertex.visited) {