使用BFS时,代码无法正确遍历

时间:2015-05-11 16:16:48

标签: java breadth-first-search

我的主要.java代码位于

之下
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 is a interface
        Queue<node> queue = new LinkedList<node>();

        if(root == null) return;

        root.State = state.Visited;
        //Adds to end of queue
        queue.add(root);
        Integer count = 0;
        while(!queue.isEmpty()) {
            count++;
            System.out.println("count   : " + count);
            //removes from 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));
    }
}

我的图.java代码如下:

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 i = 0; i < 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; i < vertices.size() ; i++) {
            if(vertices.get(i).getVertex() == V)
                return vertices.get(i).getNode();
        }
        return null;
    }

    public boolean InGraph(Integer V) {
        for(int i = 0; i < vertices.size(); i++) {
        // System.out.println("i  " + i);
        if (vertices.get(i).getVertex() == V)
            return true;
        }
        return false;
    }
}

我的节点.java代码如下:

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 i = 0 ; i < 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;
    }
}

我的状态.java代码如下:

 public enum state {
     Unvisited,Visiting,Visited;
 }

我尝试使用BFS遍历图表。我有这个图: 0 20 32 41 32 1 有5个边缘。当我将0顶点传递给bfs函数队列时,只需添加23,并且不会添加23的子项。我无法理解为什么这个bfs功能无法正常工作。好吗?

1 个答案:

答案 0 :(得分:1)

public static void bfs(node root) {
    //Since queue is a interface
    Queue<node> queue = new LinkedList<node>();

    if(root == null) return;


    //Adds to end of queue
    queue.add(root);
    Integer count = 0;
    while(!queue.isEmpty()) {
        count++;
        System.out.println("count   : " + count);
        //removes from 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;
    }
}