無向圖中節點的迭代得到從起始節點到結束節點之間的所有路徑,並從中得到最短路徑的節點__遞迴

來源:互聯網
上載者:User

無向無權圖中節點的迭代得到從起始節點到結束節點之間的所有路徑,並從中得到最短路徑的節點。由於是無權圖,則所有的路徑的權值可以當做是1.只需要得到所有可能路徑中包含節點最小的便是最短的路徑了。
本例子可以作為在一個串連的圖中尋找一個節點到另一個節點路徑的萬能例子。思路是遞迴調用。

//構造實體類package com.bh.ResearchersTrack.test;import java.util.ArrayList;public class Node {    private int id;    private ArrayList<Node>connectNode;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public Node(int id, ArrayList<Node> connectNode) {        super();        this.id = id;        this.connectNode = connectNode;    }    public ArrayList<Node> getConnectNode() {        return connectNode;    }    public void setConnectNode(ArrayList<Node> connectNode) {        this.connectNode = connectNode;    }    public Node(int id) {        super();        this.id = id;    }    public Node() {        super();    }}
package com.bh.ResearchersTrack.test;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Test {    private ArrayList<Node> nodeList;    private ArrayList<ArrayList<Node>> allNodeList;    public Test() {        nodeList=new ArrayList<Node>();        allNodeList=new ArrayList<ArrayList<Node>>();        List<Node> nodes = constructNodes();        getPaths(nodes.get(0),null,nodes.get(0),nodes.get(5));        getShortPath();    }    /**     * 最小路徑     */    private void getShortPath() {        ArrayList<Node> minNode = allNodeList.get(0);        for(int i=1;i<allNodeList.size();i++){            if(allNodeList.get(i).size()<minNode.size()){                minNode=allNodeList.get(i);            }        }        System.out.print("最短路徑是:");        for (int i = 0; i < minNode.size(); i++) {            Node nNode =minNode.get(i);            if(i < (minNode.size() - 1))                System.out.print(nNode.getId() + "->");            else                System.out.print(nNode.getId());        }    }    /**     *尋找所有可能的路徑     * @param cNode: current node     * @param pNode: previous node     * @param sNode: start node     * @param eNode: end node     */    private boolean getPaths(Node cNode, Node pNode, Node sNode, Node eNode) {        //定義一個中間節點        Node centerNode;        //先考慮迴路        if(cNode!=null&&pNode!=null&&cNode==pNode){            return false;        }        if(cNode!=null){            int i=0;            nodeList.add(cNode);            if(cNode==eNode){//is  finded                savePath();                return true;            }else{                centerNode=cNode.getConnectNode().get(i);                while(centerNode!=null){                    if (pNode != null                            && (centerNode == sNode || centerNode == pNode || isNodeInStack(centerNode))) {                        i++;                        if (i >= cNode.getConnectNode().size()){                            centerNode = null;                        }                        else{                            centerNode = cNode.getConnectNode().get(i);                        }                        continue;                    }                    if(getPaths(centerNode, cNode, sNode, eNode)){                        //得到滿足條件的一條路徑 移除路徑的最後一個節點 下標下移 重新迭代計算                        nodeList.remove(nodeList.size()-1);                        //如果有return 表示找到一條路徑邊結束                        //return true;                    }                    i++;                    if(i>=cNode.getConnectNode().size()){                        centerNode=null;                    }else{                        centerNode=cNode.getConnectNode().get(i);                    }                }                //當前節點的所有相關聯的節點遍曆完的時候 移除最後一個節點                nodeList.remove(nodeList.size()-1);                return false;            }        }else{            return false;         }    }    /**     * 判斷是夠已有該節點     * @param node     * @return     */    private boolean isNodeInStack(Node node) {        for(Node n:nodeList){            if(n==node){                return true;            }        }         return false;    }    /**     * 儲存節點路徑     */    private void savePath() {        for(Node node :nodeList){            System.out.print(node.getId()+"  ");        }        System.out.println("\r\n ----------- ");         //轉儲        ArrayList<Node> nodes=new ArrayList<>(nodeList);        allNodeList.add(nodes);    }    /**     * 構造資料     * @return     */    private List<Node> constructNodes() {        List<Node> nodes=new ArrayList<Node>();        ArrayList<Node> nodes0=new ArrayList<Node>();        ArrayList<Node> nodes1=new ArrayList<Node>();        ArrayList<Node> nodes2=new ArrayList<Node>();        ArrayList<Node> nodes3=new ArrayList<Node>();        ArrayList<Node> nodes4=new ArrayList<Node>();        ArrayList<Node> nodes5=new ArrayList<Node>();        Node node0=new Node(0);        Node node1=new Node(1);        Node node2=new Node(2);        Node node3=new Node(3);        Node node4=new Node(4);        Node node5=new Node(5);        nodes0.add(node1);        node0.setConnectNode(nodes0);        nodes1.add(node0);        nodes1.add(node2);        nodes1.add(node3);        nodes1.add(node5);        node1.setConnectNode(nodes1);        nodes2.add(node1);        nodes2.add(node4);        node2.setConnectNode(nodes2);        nodes3.add(node1);        nodes3.add(node4);        node3.setConnectNode(nodes3);        nodes4.add(node2);        nodes4.add(node3);        nodes4.add(node5);        node4.setConnectNode(nodes4);        nodes5.add(node1);        nodes5.add(node4);        node5.setConnectNode(nodes5);        nodes.add(node0);        nodes.add(node1);        nodes.add(node2);        nodes.add(node3);        nodes.add(node4);        nodes.add(node5);        return nodes;    }    public static void main(String[] args) {        new Test();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.