无向图中节点的迭代得到从起始节点到结束节点之间的所有路径,并从中得到最短路径的节点

无向无权图中节点的迭代得到从起始节点到结束节点之间的所有路径,并从中得到最短路径的节点。由于是无权图,则所有的路径的权值可以当做是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();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值