图最短距离

本文提供了一个图算法的具体实现案例,通过定义一个简单的图结构并利用Dijkstra算法的变种来计算从指定起点到其它各点的最短路径。文章详细介绍了如何初始化图的数据结构、设置起点,并逐步迭代更新各个节点的最短距离。
摘要由CSDN通过智能技术生成
package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GraphTest {

	static final int m = Integer.MAX_VALUE >> 4; // 无限大距离
	
	public static void main(String[] args) {
		int[][] arr = new int[8][8];
		init(arr);
		List<PathNode> list = getShortDistance(arr,4);
		//最短路径暂时先不求解了
		list.forEach(o -> System.out.println(o));
	}

	static class PathNode{
		char c;
		List<Character> shortestPath = new ArrayList<>();
		int shortestDistince = m;
		boolean isIterator = false;
		@Override
		public String toString() {
			return "PathNode [c=" + c + ", shortestPath=" + shortestPath + ", shortestDistince=" + shortestDistince
					+ "]";
		}
	}
	
	private static List<PathNode> getShortDistance(int[][] arr , int from) {
		List<PathNode> pns = new ArrayList<>();
		Map<Integer,PathNode> cmap = new HashMap<>();
		int length = arr.length;
		for(int i=0;i<length;i++){
			if(i != from){
				PathNode pn = new PathNode();
				pn.c = (char)(i+65);
				pn.shortestPath.add((char)(from+65));
				cmap.put(i, pn);
				pns.add(pn);
			}
		}
		int i = from;
		int min = 0;
		for(;;){
		  for(int j=0; j<length ; j++){
			if(i == j || j == from)
				 continue;
			 int a = arr[i][j] + min;
			 int b = cmap.get(j).shortestDistince;
			 if(a < b){
				 cmap.get(j).shortestDistince = a;
			 }
		  }
		  //找到最短距离的节点,即下一趟要遍历的节点
		  min = m;
		  for(PathNode pn : pns){
			  if(!pn.isIterator && pn.shortestDistince < min){
				  min = pn.shortestDistince;
				  i = pn.c - 65;
			  }
		  }
		  if(min == m)
			  break;
		  else
			  cmap.get(i).isIterator = true;
		}
		return pns;
	}


	private static void init(int[][] arr) {
		arr[0][0] = 0;
		arr[0][1] = 20;
		arr[0][2] = m;
		arr[0][3] = 80;
		arr[0][4] = m;
		arr[0][5] = m;
		arr[0][6] = 90;
		arr[0][7] = m;
		
		arr[1][0] = m;
		arr[1][1] = 0;
		arr[1][2] = m;
		arr[1][3] = m;
		arr[1][4] = m;
		arr[1][5] = 10;
		arr[1][6] = m;
		arr[1][7] = m;
		
		arr[2][0] = m;
		arr[2][1] = m;
		arr[2][2] = 0;
		arr[2][3] = 10;
		arr[2][4] = m;
		arr[2][5] = 50;
		arr[2][6] = m;
		arr[2][7] = 20;
		
		arr[3][0] = m;
		arr[3][1] = m;
		arr[3][2] = 10;
		arr[3][3] = 0;
		arr[3][4] = m;
		arr[3][5] = m;
		arr[3][6] = 20;
		arr[3][7] = m;
		
		arr[4][0] = m;
		arr[4][1] = 50;
		arr[4][2] = m;
		arr[4][3] = m;
		arr[4][4] = 0;
		arr[4][5] = m;
		arr[4][6] = 30;
		arr[4][7] = m;
		
		arr[5][0] = m;
		arr[5][1] = m;
		arr[5][2] = 10;
		arr[5][3] = 40;
		arr[5][4] = m;
		arr[5][5] = 0;
		arr[5][6] = m;
		arr[5][7] = m;
		
		arr[6][0] = 20;
		arr[6][1] = m;
		arr[6][2] = m;
		arr[6][3] = m;
		arr[6][4] = m;
		arr[6][5] = m;
		arr[6][6] = 0;
		arr[6][7] = m;
		
		arr[7][0] = m;
		arr[7][1] = m;
		arr[7][2] = m;
		arr[7][3] = m;
		arr[7][4] = m;
		arr[7][5] = m;
		arr[7][6] = m;
		arr[7][7] = 0;		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值