Java 使用BFS查找两个节点之间的所有路径

Java 使用BFS查找两个节点之间的所有路径,java,Java,我的问题有点奇怪,但我有一个学校的项目,我需要找到两个节点之间的所有路径,并保存在一个列表中。奇怪的是,我必须以BFS顺序遍历图。我知道还有其他算法可以更有效地解决我的问题,但我必须使用BFS。我将图表示为一个邻接矩阵,它是加权的,无向的。有谁能帮我提些建议吗 public class A { private static int[][] adjacency = new int [5][5]; static int n = 5; public static void m

我的问题有点奇怪,但我有一个学校的项目,我需要找到两个节点之间的所有路径,并保存在一个列表中。奇怪的是,我必须以BFS顺序遍历图。我知道还有其他算法可以更有效地解决我的问题,但我必须使用BFS。我将图表示为一个邻接矩阵,它是加权的,无向的。有谁能帮我提些建议吗

public class A {
    private static int[][] adjacency = new int [5][5];
    static int n = 5;

    public static void main(String[] args) {
        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
                adjacency[i][j] = 0;    
        adjacency[0][1] = 2;
        adjacency[0][3] = 1;
        adjacency[1][0] = 2;
        adjacency[1][2] = 5;
        adjacency[2][1] = 5;
        adjacency[2][3] = 1;
        adjacency[2][4] = 2;
        adjacency[3][0] = 1;
        adjacency[3][2] = 1;
        adjacency[4][2] = 2;

        List<Queue<Integer>> paths = findPath(0,2,adjacency);
        System.out.println(paths);
    }

    public static List<Integer> getNeighbors(int node, int[][] a) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<n;i++)
            if (a[node][i] != 0)
                list.add(i);
        return list;
    }

    public static List<Queue<Integer>> findPath(int start, int end, int[][] a) {
        List<Queue<Integer>> paths = new ArrayList<Queue<Integer>>();
        Queue<Integer> toVisit = new LinkedList<Integer>();
        Queue<Integer> visited = new LinkedList<Integer>();
        toVisit.add(start);
        while(!toVisit.isEmpty()) {
              int node = toVisit.remove();
              visited.add(node);
              List<Integer> neighbors = new ArrayList<Integer>();
              neighbors = getNeighbors(node,a);
        }
          return paths;
    }
}
公共A类{
私有静态int[][]邻接=新int[5][5];
静态int n=5;
公共静态void main(字符串[]args){
对于(int i=0;i
公共类图形结构{
私有映射映射=新的HashMap();
公共无效添加(字符串节点1、字符串节点2){
LinkedHashSet nexture=map.get(node1);
如果(相邻==null){
相邻=新LinkedHashSet();
地图放置(节点1,相邻);
}
添加(node2);
}
公共void addTwoWayVertex(字符串节点1、字符串节点2){
附录(节点1、节点2);
附录(节点2、节点1);
}
公共布尔值未连接(字符串节点1、字符串节点2){
设置相邻=map.get(node1);
如果(相邻==null){
返回false;
}
返回相邻的.contains(node2);
}
公共LinkedList邻接节点(最后一个字符串){
LinkedHashSet nexture=map.get(最后一个);
如果(相邻==null){
返回新的LinkedList();
}
返回新的LinkedList(相邻);
}
}
公共类简化{
公共静态整数计数;
公共静态哈希表;
私有静态最终字符串START=“B”;
私有静态最终字符串END=“E”;
公共设施简化(){
hash=新哈希表();
计数=0;
}
公共静态void main(字符串[]args){
GraphStructure graph=新的GraphStructure();
图.附录(“A”、“B”);
图.附录(“A”、“C”);
图.附录(“B”、“A”);
图.附录(“B”、“D”);
graph.addEdge(“B”、“E”);//这是唯一的单向连接
图.附录(“B”、“F”);
图.附录(“C”、“A”);
图.附录(“C”、“E”);
图.附录(“C”、“F”);
图.增补(“D”、“B”);
图.附录(“E”、“C”);
图.附录(“E”、“F”);
图.附录(“F”、“B”);
图.附录(“F”、“C”);
图.附录(“F”、“E”);
已访问LinkedList=新建LinkedList();
已访问。添加(开始);
新建bSimplementation().BroadthFirst(图形,已访问);
}      
公共静态空白宽度优先(图结构图,链接列表){
LinkedList节点=graph.adjacentNodes(visted.getLast());
用于(字符串节点:节点){
if(已访问.包含(节点)){
继续;
}
if(节点等于(结束)){
计数++;
添加(节点);
打印路径(计数,已访问);
removeLast();
打破
}
}
//在广度优先中,递归需要在访问相邻节点之后进行
用于(字符串节点:节点){
如果(已访问.包含(节点)| |节点.等于(结束)){
继续;
}
addLast(节点);
广度第一(图表,访问);
removeLast();
}
}
公共静态无效打印路径(int计数,已访问链接列表){
字符串temp=“”;
for(字符串节点:已访问){
温度=温度+节点+“,”;
//系统输出打印(节点);
//系统输出打印(“”);
}
System.out.println();
System.out.println(“可用路径”+count+:“+temp”);
hash.put(计数、温度);
//System.out.println(“exp=“+hash.toString());
}
} 

请向我们展示您的代码、输入示例和所需输出。呼吸优先搜索和深度优先搜索的主要区别在于BFS使用队列,DFS使用堆栈(通常会记住访问的节点以避免陷入循环)
public class GraphStructure {
private Map<String, LinkedHashSet<String>> map = new HashMap();

public void addEdge(String node1, String node2) {
    LinkedHashSet<String> adjacent = map.get(node1);
    if(adjacent==null) {
        adjacent = new LinkedHashSet();
        map.put(node1, adjacent);
    }
    adjacent.add(node2);
}

public void addTwoWayVertex(String node1, String node2) {
    addEdge(node1, node2);
    addEdge(node2, node1);
}

public boolean isConnected(String node1, String node2) {
    Set adjacent = map.get(node1);
    if(adjacent==null) {
        return false;
    }
    return adjacent.contains(node2);
}

public LinkedList<String> adjacentNodes(String last) {
    LinkedHashSet<String> adjacent = map.get(last);
    if(adjacent==null) {
        return new LinkedList();
    }
    return new LinkedList<String>(adjacent);
}
}

public class BFSImplementation{

public static int count;
public static Hashtable hash; 
private static final String START = "B";
private static final String END = "E";

public BFSImplementation() {
     hash=new Hashtable();
     count=0;
}
public static void main(String[] args) {  

    GraphStructure graph = new GraphStructure();

    graph.addEdge("A", "B");

    graph.addEdge("A", "C");

    graph.addEdge("B", "A");

    graph.addEdge("B", "D");

    graph.addEdge("B", "E"); // this is the only one-way connection

    graph.addEdge("B", "F");

    graph.addEdge("C", "A");

    graph.addEdge("C", "E");

    graph.addEdge("C", "F");

    graph.addEdge("D", "B");

    graph.addEdge("E", "C");

    graph.addEdge("E", "F");

    graph.addEdge("F", "B");

    graph.addEdge("F", "C");

    graph.addEdge("F", "E");

    LinkedList<String> visited = new LinkedList();
    visited.add(START);
    new BFSImplementation().breadthFirst(graph, visited);
   }      
   public static  void breadthFirst(GraphStructure graph, LinkedList<String> visited) {
    LinkedList<String> nodes = graph.adjacentNodes(visited.getLast());

    for (String node : nodes) {
        if (visited.contains(node)) {
            continue;
        }
        if (node.equals(END)) {
            count++;
            visited.add(node);
            printPath(count,visited);
            visited.removeLast();
            break;
        }
    }
    // in breadth-first, recursion needs to come after visiting adjacent nodes
    for (String node : nodes) {
        if (visited.contains(node) || node.equals(END)) {
            continue;
        }
        visited.addLast(node);
        breadthFirst(graph, visited);
        visited.removeLast();
    }
    }
    public static  void printPath(int count,LinkedList<String> visited) {       
    String temp="";
    for (String node : visited) {
        temp=temp+node+",";            
       // System.out.print(node);
       // System.out.print(" ");
    }
    System.out.println();
    System.out.println("Available Path  "+count+": : : :"+ temp);     
    hash.put(count,temp);
    //System.out.println("exp = " + hash.toString());       
    }
    }