给定起点S和终点E,求两点间的最短路径:广搜+贪心。
单源最短路径(起点S到其他所有点)
边权相等:双向广搜
边权不等,且无负权:dijikstra+fibonacci堆优化
有负权:bellman-ford,SPFA
dijikstra程序如下:
思路:从起点S的邻接节点中找权值最小的加入到集合C(已找到最短路径长度的节点序号)
候选1:该节点的其他直接相邻的节点
候选2:集合C中相邻的节点
从候选1和候选2中找权值最小的加入到集合C中,直到不会产生的新的候选节点(最短路径长度不变)。
找路径长度递增序列,
package Lesson1;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class MyPath {
static class Cell {
private int node;// 节点序号
private 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];// 有11个节点
for (int i = 0; i < g.length; i++) {
g[i] = new ArrayList();// 图的邻接表表示
}
// 使用节点序号来表示节点0~10-----对应字母abcd...
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));
// 求从0号节点开始的所有最小路径
Map mp = new HashMap();// 存放当前已找到的节点序号集合---对应的最小路径长度
while (true) {
int min = Integer.MAX_VALUE;// 最小路径长度
int min_no = -1;// 对应的节点号
// 候选1:所有与0号节点邻接,并且不在已找到的集合map中的节点
for (int i = 0; i < g[0].size(); i++) {
Cell t = (Cell) g[0].get(i);
if (mp.get(t.node) == null && t.weight < min) {
min_no = t.node;
min = t.weight;
}
}
// 候选2:与map中节点邻接的,并且不在map中的节点
Iterator it = mp.keySet().iterator();// map的键集合(没有重复)
while (it.hasNext()) { // 枚举map
int k = (Integer) it.next();// 已经找到最短路径的节点号
int v = (Integer) mp.get(k);// 该节点对应的最小路径长度
for (int i = 0; i < g[k].size(); i++) {
Cell t = (Cell) g[k].get(i);
if (mp.get(t.node) == null && t.weight + v < min) {// 不在map集合中,并且加上它后路径长度小于当前最小路径长度
min_no = t.node;
min = t.weight + v;
}
}
}
if (min < Integer.MAX_VALUE) {
mp.put(min_no, min);
} else {
break;
}
}
System.out.println(mp);
}
}
最小生成树:贪心策略,每次找边的权值最优,把节点加入到集合中,直到集合中包含所有的节点。(最小生成树不唯一)