簡體   English   中英

在無向圖BFS中查找路徑-Java

[英]Find path in an undirected graph BFS - Java

我正在編寫一個程序來處理具有未加權邊的無向圖,並且由於我是一個學習者,所以遇到了一些問題。

我必須創建一個方法(與主類在同一類中)來接收圖形,初始頂點和結束頂點。 然后,我必須查找是否存在從頂點1到頂點2的路徑,並將中間頂點存儲在隊列中以進行打印(它不一定要最短,如果可能的話最好這樣做,但實際上並不需要它)。

假設我有:

圖形

我想從中得到唯一的一條路

我已經實現了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;
}

提前致謝

我對您的問題的理解是,您正在嘗試查找從一個節點到另一個節點的路徑,而不一定是如何訪問它們。 所以這是一個實現。 運行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;

謝謝Fafore,我已經解決了。 我要做的是將所有相鄰節點設置為最終頂點,並將它們存儲在堆棧中,而它們之間的區別在於開始頂點不同。 然后我花了一段時間,直到堆棧為空,然后開始檢查哪個堆棧與起始頂點相鄰,然后檢查哪個堆棧彼此相鄰(因為堆棧的第一個與結束頂點相鄰),然后將相鄰的相加到一個隊列。 它不一定非要成為最短路徑,但謝謝=)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM