Java实现Dijkstra(迪杰斯特拉)算法

话不多说,上代码:


public class Dijkstra {

	private Map<Object, DijkstaNode> topoMap = new HashMap<Object, DijkstaNode>();

	private List<List<Object>> resultPath = new ArrayList<List<Object>>();
	private List<Object> shortestPath = new ArrayList<Object>();
	private Stack<Object> currentPath = new Stack<Object>();
	private DijkstaNode headNode = null;
	private DijkstaNode endNode = null;
	private int shortestLength = Integer.MAX_VALUE;
	private int currentLength = 0;
	
	
	Dijkstra(){
		topoMap.clear();
		headNode = null;
		endNode = null;
		resultPath.clear();
		shortestPath.clear();
		currentPath.clear();
		shortestLength = Integer.MAX_VALUE;
		currentLength = 0;
	}
	
	private class DijkstaNode {
		boolean hasCross;
		Object origNode;
		List<Object> nextHop;

		DijkstaNode(Object obj, List<Object> list) {
			origNode = obj;
			hasCross = false;
			nextHop = list;
		}

		@Override
		public boolean equals(Object arg0) {
			// TODO Auto-generated method stub
			return origNode.equals(arg0);
		}
	}

	private void calculatePath(DijkstaNode head) {
		List<Object> list = head.nextHop;
		if (list.isEmpty())
		{
			return;
		}
		currentPath.push(head.origNode);
		currentLength++;
		System.out.println("-->push:" + head.origNode);
		
		for (Object obj : list) {
			DijkstaNode next = topoMap.get(obj);
			if (next.hasCross) {
				continue;
			}
			if (next.equals(endNode)){
				currentPath.push(next.origNode);
				System.out.println("-->push:" + next.origNode);
				List<Object> finishList = new ArrayList<Object>(currentPath);
				if (currentLength < shortestLength) {
					shortestLength = currentLength;
					shortestPath = finishList;
				}
				
				resultPath.add(finishList);
				
				currentPath.pop();
				System.out.println("-->pop:" + next.origNode);
				
				
				continue;
			}

			next.hasCross = true;
			this.calculatePath(next);
			next.hasCross = false;
		}
		
		currentPath.pop();
		currentLength--;
		System.out.println("-->pop:" + head.origNode);
	}

	public void addTopoMapNode(Object node, List<Object> nextHopList) {

		DijkstaNode newNode = new DijkstaNode(node, nextHopList);
		topoMap.put(node, newNode);
	}

	public void setStartNode(Object node) {
		if (headNode != null) {
			throw new RuntimeException();
		}
		DijkstaNode newNode = topoMap.get(node);
		headNode = newNode;
	}

	public void setEndNode(Object node) {
		if (endNode != null) {
			throw new RuntimeException();
		}
		DijkstaNode newNode = topoMap.get(node);
		endNode = newNode;
	}
	
	
	public void startCalculate(){
		
		if (headNode == null || endNode == null){
			throw new RuntimeException();
		}
		
		
		
		this.calculatePath(headNode);
	}

	public void resetCalculateResult() {
		headNode = null;
		endNode = null;
		resultPath.clear();
		shortestPath.clear();
		currentPath.clear();
		shortestLength = Integer.MAX_VALUE;
		currentLength = 0;
	}
	public void resetTopoMap(){
		topoMap.clear();
	}
	
	public List<Object> getShortestPath(){
		return shortestPath;
	}
	
	public List<List<Object>> getAllPath(){
		return resultPath;
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值