The iteration of the node in the non-direction graph gets all the paths from the starting node to the end node, and obtains the shortest path from the node __ recursion

Source: Internet
Author: User

The iteration of a node in a non-permission graph gets all the paths from the starting node to the end node and obtains the shortest path from the node. Because it is a power-not graph, all the path weights can be treated as 1. Just need to get all the possible paths with the smallest number of nodes is the shortest path.
This example can be used as a universal example of finding a node to another node path in a connected graph. The idea is recursive invocation .

Constructing entity class
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 (); }/** * min path/private void Getshortpath () {arraylist<node> Minnode = allnodelist.get (0
        ); for (int i=1;i<allnodelist.size (); i++) {if Allnodelist.get (i). Size () <minnode.size ()) {min
            Node=allnodelist.get (i);
        } System.out.print ("Shortest path is:");

            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 ());  /** * Find all possible paths * @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 EN
        ODE) {//Define an intermediate node Centernode;
        First consider the loop 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 = = Snod e | | 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)) {//Get the last node of the path to meet the criteria under subscript
                        Move the iteration to compute the Nodelist.remove (nodelist.size ()-1);
                    If there is a return means to find a path edge end//return true;
                    } i++;
                    if (I>=cnode.getconnectnode (). Size ()) {centernode=null;
                    }else{Centernode=cnode.getconnectnode (). get (i); The last node is removed when all the associated nodes of the current node are traversed Nodelist.remove (nodeList.size ()-1);
            return false; 
        }}else{return false; 

        }/** * is enough to have the node * @param nodes * @return/private Boolean Isnodeinstack (node node) {
            for (Node n:nodelist) {if (N==node) {true;
    return false; /** * Save node Path */private void Savepath () {for (node Node:nodelist) {SYSTEM.OUT.P
        Rint (Node.getid () + ""); 
        } System.out.println ("\ r \ n-----------");
        Dump arraylist<node> nodes=new arraylist<> (nodelist);
    Allnodelist.add (nodes); /** * Construct Data * @return * * Private list<node> Constructnodes () {list<node> No
        Des=new arraylist<node> ();
        Arraylist<node> nodes0=new arraylist<node> ();
        Arraylist<node> nodes1=new arraylist<node> (); Arraylist&lT
        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 ();
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.