在无向图BFS - Java中查找路径

时间:2015-12-12 23:03:41

标签: java graph path vertices

我正在制作一个程序来处理带有未加权边缘的无向图,因为我是一个学习者,我遇到了一些问题。

我必须创建一个接收图形的方法(与main相同的类),一个初始顶点和一个顶点。然后我必须找到是否存在从顶点1到顶点2的路径,并将中间顶点存储在队列中然后打印它(它不必是最短的,如果它&#39更好,那么&# 39;可能但不是真的需要它。)

我们说我有:

Graph

我想从

获得唯一的路径

我已经实现了一个bfs方法,它是以下用于我也有的其他方法,但我不知道如何从我需要的方法开始。 我的bfs方法:

    public static Queue<DecoratedInmate> bfs (Graph gr, Vertex<DecoratedInmate> v){
    Queue<Vertex<DecoratedInmate>> vertices = new LinkedList<Vertex<DecoratedInmate>>();   //temporal queue
    Queue<DecoratedInmate> traversal = new LinkedList<DecoratedInmate>();   //traversal queue
    Vertex<DecoratedInmate> u;  //vertex taken from queue
    Vertex<DecoratedInmate> z;  //opposite vertex of u
    Edge e; //edge between vertices
    Iterator<Edge<DecoratedInmate>> it; //to store incident edges
    v.getElement().setVisited(true);    //set received vertex to visited
    vertices.offer(v); //add origin vertex to queue
    while (!vertices.isEmpty()) {  //if queue isn't empty
        u = vertices.remove(); //take vertex from queue
        traversal.offer(u.getElement());    //add element to list
        it = gr.incidentEdges(u);   //get incident edges of u
        while (it.hasNext()) {  //check if there are incident edges
            e = it.next();      //assign the edge
            z = gr.opposite(u, e);  //assign opposite vertex of u
            if (!z.getElement().getVisited()) { //check if the opposite is not visited
                z.getElement().setVisited(true);    //set to visited
                vertices.offer(z); //add to queue
            }
        }
    }
    return traversal;
}

提前致谢

2 个答案:

答案 0 :(得分:1)

我对您的问题的理解是,您正在尝试找到从一个节点到另一个节点的路径,而不一定是如何访问它们。所以这是一个实现。运行bfs时,存储每个顶点父类,即

    public static void Bfs(Vertex source) {
    vertex = GraphifyGUI.getNode();
    reset();
    q = new LinkedList<>(); // FIFO
    source.wasVisited = true; // marked as visited
    q.add(source); // put into queue
    source.parent = source; // set parent
    conn = new ArrayList<>();
    while (!q.isEmpty()) { // source
        Vertex current = q.poll(); // remove first 
        conn.add(current.getId());
        Iterator<Vertex> currentList = current.vList().iterator();
        while (currentList.hasNext()) {
            Vertex next = currentList.next();
            if (next.wasVisited == false) {
                next.wasVisited = true;
                q.add(next);
                next.parent = current;
                GG.printlnConsole(next.getName() + " has type of " + next.getType());
            }
        }
    }
    GG.printlnConsole("Order is " + conn);
}

然后获得最短路径的方法将如下所示

   public void shortestPath(int v, int e) {
    if (e == v) {
        GG.printlnConsole(v + "-->" + v);
        return;
    }
    for (int i = e; i >= 0; i = vertex.get(i).getParent().getId()) {
        if (i == v) {
            break;
        }
        if (vertex.get(i).getParent().getId() != -1) {
            set.put(vertex.get(i).getParent().getId(), i);
        }
    }
}

上面的shortestPath说明

if this source is the same as destination then that is shortest path
for(i = destination; i >= 0; i = parent of i){
    if(i == source) we are done;
    if(parent of i is a node) add as path;

答案 1 :(得分:0)

谢谢你,我已经解决了它。我所做的是将所有相邻节点设置为结束顶点并将它们存储在堆栈中,而它们不同以启动顶点。然后我做了一段时间,直到堆栈为空并开始检查哪一个与起始顶点相邻,然后哪一个在它们之间相邻(因为堆栈的第一个与顶点相邻)然后将那些邻接添加到一个队列。它并不需要成为最短的路径,但谢谢=)

相关问题