广度优先遍历

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class extentTravel {

	public static void main(String[] args) {
		//图的邻接表定义
		int[][] g={
				{0,1,1,0,0,0,0},
				{1,0,0,1,0,0,1},
				{1,0,0,0,0,1,1},
				{0,1,0,0,1,0,0},
				{0,0,0,1,0,1,1},
				{0,0,1,0,1,0,0},
				{0,1,1,0,1,0,0}
		};
		<img src="https://img-blog.youkuaiyun.com/20160315163624455?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

		//广度优先遍历 
		List lst = new ArrayList();//等待遍历
		Set set = new HashSet();//已经遍历节点的集合
		lst.add(0);
		
		while(true){
			if(lst.isEmpty()) break;
			
			int node = (Integer)lst.get(0);
			System.out.println(node);
			set.add(node);
			lst.remove(0);
			
			for(int i=0;i<g[node].length;i++){
				if(g[node][i] == 1 && set.contains(i) == false && lst.indexOf(i)<0){
					lst.add(i);
				}
			}
		}
	}

}

最短路径问题

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

public class minRoot {
	static class Cell {
		int node;// 连接到哪个节点
		int weight;// 边的权值

		public Cell(int node, int weight) {
			this.node = node;
			this.weight = weight;
		}
	}

	// 已找到的最短路径信息

	public static void main(String[] args) {
		List[] g = new List[11];
		for (int i = 0; i < g.length; i++)
			g[i] = new ArrayList();
		g[0].add(new Cell(1, 3));
		g[0].add(new Cell(4, 1));
		g[1].add(new Cell(2, 1));
		g[1].add(new Cell(6, 3));
		g[1].add(new Cell(9, 4));
		g[1].add(new Cell(5, 5));
		g[1].add(new Cell(0, 3));
		g[2].add(new Cell(1, 1));
		g[2].add(new Cell(3, 1));
		g[2].add(new Cell(6, 7));
		g[3].add(new Cell(2, 1));
		g[3].add(new Cell(10, 2));
		g[4].add(new Cell(0, 1));
		g[4].add(new Cell(5, 2));
		g[5].add(new Cell(4, 2));
		g[5].add(new Cell(1, 5));
		g[5].add(new Cell(7, 2));
		g[5].add(new Cell(8, 3));
		g[6].add(new Cell(2, 3));
		g[6].add(new Cell(3, 7));
		g[6].add(new Cell(8, 2));
		g[6].add(new Cell(10, 1));
		g[7].add(new Cell(5, 2));
		g[8].add(new Cell(5, 3));
		g[8].add(new Cell(6, 2));
		g[9].add(new Cell(1, 4));
		g[9].add(new Cell(10, 2));
		g[10].add(new Cell(3, 2));
		g[10].add(new Cell(6, 1));
		g[10].add(new Cell(9, 2));
                <img src="https://img-blog.youkuaiyun.com/20160315163732063?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


		// 求0号节点开始的所有最小路径

		Map map = new HashMap();// 节点号-->最小路径值

		while (true) {
			int min = Integer.MAX_VALUE;// 最小路径值
			int min_no = -1;// 对应的节点号

			// 所有与0号节点邻接,并且不在map中的点
			for (int i1 = 0; i1 < g[0].size(); i1++) {
				Cell t = (Cell) g[0].get(i1);
				if (map.get(t.node) == null && t.weight < min) {
					min_no = t.node;
					min = t.weight;
				}
			}

			// 与map中点邻接所有不在map中的点
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				int k = (Integer) it.next();
				int v = (Integer) map.get(k);// 集合中的节点对应的最小路径值

				for (int i1 = 0; i1 < g[k].size(); i1++) {
					Cell t = (Cell) g[k].get(i1);
					if (map.get(t.node) == null && t.weight + v < min) {
						min_no = t.node;
						min = t.weight + v;
					}
				}
			}

			if (min < Integer.MAX_VALUE) {
				map.put(min_no, min);
			} else {
				break;
			}
		}

		System.out.println(map);

	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值