java - Code is not traversing correctly when using BFS -
my main .java code below
package phase4and5; import java.io.bufferedreader; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; import java.util.linkedlist; import java.util.queue; public class phase4and5 { public static void creategraph(file a, graph g) throws ioexception { //arraylist<node> nodeofgraph = new arraylist<node>(); string st1 , st2 , line; integer vs , es; try { bufferedreader br = new bufferedreader(new filereader(a)); st1 = br.readline(); st2 = br.readline(); vs = integer.parseint(st1); es = integer.parseint(st2); while ((line = br.readline()) != null) { string[] splited = line.split("\\s+"); integer namev = integer.valueof(splited[0]); node vtemp = new node(namev); integer adjv = integer.valueof(splited[1]); node adjtemp = new node(adjv); if(g.ingraph(namev) == false) { vtemp.addchildnode(adjtemp); g.addnode(vtemp); // nodeofgraph.add(vtemp); } else { g.findnode(namev).addchildnode(adjtemp); } } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } public static void bfs(node root) { //since queue interface queue<node> queue = new linkedlist<node>(); if(root == null) return; root.state = state.visited; //adds end of queue queue.add(root); integer count = 0; while(!queue.isempty()) { count++; system.out.println("count : " + count); //removes front of queue node r = queue.remove(); system.out.println(r.getvertex() + "\t"); // system.out.println(r.getchild().get(0).getvertex() + "\t"); // visit child first before grandchild for(node n: r.getchild()) { system.out.println("n "+n.getvertex()); if(n.state == state.unvisited) { queue.add(n); n.state = state.visited; } } } } public static void main(string[] args) throws ioexception { file file = new file("/users/mehran/desktop/filee.txt"); graph g = new graph(); creategraph(file , g); // system.out.println("ffff "+ g.getnode().get(0).getvertex()); //g.printgraph(); system.out.println("he : "+ g.getnode().get(1).getvertex()); system.out.println("hey : " + g.getnode().get(1).getchild().get(1).getvertex()); bfs(g.getnode().get(0)); } } my graph .java code below:
import java.util.arraylist; import java.util.list; public class graph { public int count; // num of vertices private arraylist<node> vertices ; public void printgraph() { for(int = 0; < vertices.size(); i++) { vertices.get(i).print(); } } public graph() { vertices = new arraylist<node>(); count = 0; } public void addnode(node n) { ((list) vertices).add(n); } public arraylist<node> getnode() { return vertices; } public node findnode(integer v) { for(int i=0; < vertices.size() ; i++) { if(vertices.get(i).getvertex() == v) return vertices.get(i).getnode(); } return null; } public boolean ingraph(integer v) { for(int = 0; < vertices.size(); i++) { // system.out.println("i " + i); if (vertices.get(i).getvertex() == v) return true; } return false; } } my node .java code below:
import java.lang.thread.state; import java.util.arraylist; public class node { public integer length = 0; public arraylist<node> child; public int childcount = 0; private integer vertex; public state state; public void print() { system.out.println("v : " + vertex); for(int = 0 ; < child.size(); i++) { system.out.println("childs "); system.out.println(child.get(i).getvertex()); // system.out.println(child.get(i).state); } } public node(integer vertex) { // state = false; this.vertex = vertex; child = new arraylist<node>(); } public node getnode() { return this; } public void addchildnode(node adj) { // adj.state = false; adj.state = state.unvisited; child.add(adj); } public arraylist<node> getchild() { return child; } public integer getvertex() { return vertex; } } and state .java code below :
public enum state { unvisited,visiting,visited; } i try traverse graph using bfs . have graph: 0 2, 0 3, 2 4, 1 3, 2 1 5 edges . when pass 0 vertex bfs function queue add 2 , 3 , not add children of 2 , 3 . can't understand why bfs function isn't working correctly. idea please?
public static void bfs(node root) { //since queue interface queue<node> queue = new linkedlist<node>(); if(root == null) return; //adds end of queue queue.add(root); integer count = 0; while(!queue.isempty()) { count++; system.out.println("count : " + count); //removes front of queue node r = queue.remove(); system.out.println(r.getvertex() + "\t"); // system.out.println(r.getchild().get(0).getvertex() + "\t"); // visit child first before grandchild for(node n: r.getchild()) { system.out.println("n "+n.getvertex()); if(n.state == state.unvisited) { queue.add(n); } } r.state = state.visited; } }
Comments
Post a Comment