问题:node
There are N
network nodes, labelled 1
to N
.算法
Given times
, a list of travel times as directed edges times[i] = (u, v, w)
, where u
is the source node, v
is the target node, and w
is the time it takes for a signal to travel from source to target.数组
Now, we send a signal from a certain node K
. How long will it take for all nodes to receive the signal? If it is impossible, return -1
.网络
Note:spa
N
will be in the range [1, 100]
.K
will be in the range [1, N]
.times
will be in the range [1, 6000]
.times[i] = (u, v, w)
will have 1 <= u, v <= N
and 1 <= w <= 100
.解决:code
① 图的遍历。给定一张图中若干节点的连通性信息以及节点间的距离,以后要求咱们判断针对某个特定节点(第K个节点),其可否和其余全部结点连通,并求出要到达全部节点须要通过的网络时延。队列
通过分析,能够明确两个子问题,即:
- 判断两个节点之间是否连通
- 求出某个节点到图中其余任意可达节点所需的最短路径get
所以本题的最终目的是求单源最短路径。it
最短路径的经常使用解法有迪杰克斯特拉算法Dijkstra Algorithm, 弗洛伊德算法Floyd-Warshall Algorithm, 和贝尔曼福特算法Bellman-Ford Algorithm,其中,Floyd算法是多源最短路径,即求任意点到任意点到最短路径,而Dijkstra算法和Bellman-Ford算法是单源最短路径,即单个点到任意点到最短路径。io
Dijkstra算法处理有向权重图时,权重必须为正,而另外两种能够处理负权重有向图,可是不能出现负环,所谓负环,就是权值总和均为负的环。
这三个算法的核心思想,当有对边 (u, v) 是结点u到结点v,若是 dist(v) > dist(u) + w(u, v),那么 dist(v) 就能够被更新,这是全部这些的算法的核心操做。
Dijkstra算法是从一个顶点到其他各顶点的最短路径算法,解决的是有向图中最短路径问题。迪杰斯特拉算法主要特色是以起始点为中心向外层层扩展,直到扩展到终点为止,是一种广度优先的搜索方法。
普通的实现方法的时间复杂度为O(V^2),基于优先队列的实现方法的时间复杂度为O(E + VlogV),其中V和E分别为结点和边的个数。
dijkstra算法例子:求从结点0到各个结点的最短路径。
class Solution { //137ms
public int networkDelayTime(int[][] times, int N, int K) {
if (times == null || times.length == 0) return -1;
Map<Integer,Map<Integer,Integer>> path = new HashMap<>();//key为起始节点,value为相邻节点和两个节点的距离
for (int[] time : times){
Map<Integer,Integer> sourceMap = path.get(time[0]);
if (sourceMap == null){
sourceMap = new HashMap<>();
path.put(time[0],sourceMap);
}
Integer distance = sourceMap.get(time[1]);
if (distance == null || distance > time[2]){
sourceMap.put(time[1],time[2]);
}
}
//使用PriorityQueue获取绝对距离最短的节点,并计算其与邻居节点的绝对距离。
Map<Integer,Integer> distanceMap = new HashMap<>();
distanceMap.put(K,0);
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((i1,i2) -> {return i1[1] - i2[1];});
priorityQueue.offer(new int[]{K,0});
int max = -1;
while (! priorityQueue.isEmpty()){
int[] cur = priorityQueue.poll();
int node = cur[0];
int distance = cur[1];
if (distanceMap.containsKey(node) && distanceMap.get(node) < distance) continue;
Map<Integer,Integer> sourceMap = path.get(node);
if (sourceMap == null) continue;
for (Map.Entry<Integer,Integer> entry : sourceMap.entrySet()){
int absoluteDistance = distance + entry.getValue();
int targetNode = entry.getKey();
if (distanceMap.containsKey(targetNode) && distanceMap.get(targetNode) <= absoluteDistance) continue;
distanceMap.put(targetNode,absoluteDistance);
priorityQueue.offer(new int[]{targetNode,absoluteDistance});
}
}
for (int val : distanceMap.values()){
if (val > max){
max = val;
}
}
return distanceMap.size() == N ? max : -1;
}
}
② 使用floyd-warshall算法获得全部的最短路径,而后选择从节点K开始的路径,这样更容易(但效率更低)。时间复杂度为O(n^3),空间复杂度为O(n^2)。
1. 时间复杂度的对比
2.
3. 例如:
D0表示图的邻接矩阵的表示,D1由上面的公式求得,该公式与迪杰斯特拉算法同样。
P数组表示当前节点的前驱节点。
class Solution { //67ms
public int networkDelayTime(int[][] times, int N, int K) {
int maxDistance = 100 * 100;//两个节点之间的最短距离
int[][] distance = new int[N][N];
for (int i = 0;i < N;i ++){//初始化全部节点之间的距离为最远距离
Arrays.fill(distance[i],maxDistance);
}
//处理图的信息,将能够到达的点之间的路径距离存入表中
for (int[] time : times){
distance[time[0] - 1][time[1] - 1] = time[2];
}
for (int i = 0;i < N;i ++){
distance[i][i] = 0;
}
//使用弗洛伊德算法遍历图,更新节点的路径距离
for (int k = 0;k < N;k ++){
for (int i = 0;i < N;i ++){
for (int j = 0;j < N;j ++){
distance[i][j] = Math.min(distance[i][j],distance[i][k] + distance[k][j]);
}
}
}
//寻找节点K到最远节点的最短距离
int res = Integer.MIN_VALUE;
for (int i = 0;i < N;i ++){
if (distance[K - 1][i] >= maxDistance) return -1;//节点不可达
res = Math.max(res,distance[K - 1][i]);
}
return res;
}
}
③ 使用Bellman-Ford算法,时间复杂度为O(VE),其中V为图中节点数,E为图中边数;空间复杂度为O(V)。
Bellman-Ford算法从源点逐次绕过其余节点,以缩短到达终点的最短路径长度。
dist数组的递推公式:
class Solution { //74ms public int networkDelayTime(int[][] times, int N, int K) { int maxDistance = 100 * 100;//两个节点之间的最短距离 int[] distance = new int[N]; Arrays.fill(distance,maxDistance);//初始化全部节点之间的距离为最远距离 distance[K - 1] = 0;//第K个点做为起点 for (int i = 1;i < N;i ++) { for (int[] time : times) {//使用Bellman-Ford计算最短路径 int u = time[0] - 1; int v = time[1] - 1; int w = time[2]; distance[v] = Math.min(distance[v], distance[u] + w); } } int res = 0; for (int i = 0;i < distance.length;i ++){//寻找最远的路径 res = Math.max(res,distance[i]); } return res == maxDistance ? -1 : res; } }