743 Network Delay Time

1. Question

There areNnetwork nodes, labelled1toN.

Giventimes, a list of travel times as directed edgestimes[i] = (u, v, w), whereuis the source node,vis the target node, andwis the time it takes for a signal to travel from source to target.

Now, we send a signal from a certain nodeK. How long will it take for all nodes to receive the signal? If it is impossible, return-1.

Note:

  1. Nwill be in the range[1, 100].

  2. Kwill be in the range[1, N].

  3. The length oftimeswill be in the range[1, 6000].

  4. All edgestimes[i] = (u, v, w)will have1 <= u, v <= Nand1 <= w <= 100.

2. Implementation

(1) BFS

class Solution {
    public int networkDelayTime(int[][] times, int N, int K) {
        int[][] edge = new int[N + 1][N + 1];
        for (int[] e : edge) {
            Arrays.fill(e, Integer.MAX_VALUE);
        }

        for (int[] time : times) {
            edge[time[0]][time[1]] = time[2];
        }



        int[] delay = new int[N + 1];
        Arrays.fill(delay, Integer.MAX_VALUE);
        delay[K] = 0;

        Queue<Integer> queue = new LinkedList<>();
        queue.add(K);

        while (!queue.isEmpty()) {
            int size = queue.size();
            Set<Integer> set = new HashSet<>();

            for (int i = 0; i < size; i++) {
                int curNode = queue.remove();

                for (int nextNode = 1; nextNode <= N; nextNode++) {
                    if (edge[curNode][nextNode] != Integer.MAX_VALUE 
                        && delay[curNode] + edge[curNode][nextNode] < delay[nextNode]) {
                        if (!set.contains(nextNode)) {
                            set.add(nextNode);
                            queue.add(nextNode);
                        }
                        delay[nextNode] = delay[curNode] + edge[curNode][nextNode];
                    }
                }
            }
        }

        int res = 0;
        for (int i = 1; i <= N; i++) {
            res = Math.max(res, delay[i]);
        }
        return res == Integer.MAX_VALUE ? -1 : res;
    }
}

(2) Dijkstra's Algorithm

(3) Dijkstra's Algorithm堆优化

3. Time & Space Complexity

BFS: 时间复杂度O(N ^ 2), 空间复杂度O(N ^ 2)

Dijkstra's Algorithm: 时间复杂度O(N ^ 2 + E), E是times的长度,空间复杂度O(N + E)

Dijkstra's Algorithm堆优化: 时间复杂度O(NlogN + E), 空间复杂度O(N + E)

Last updated

Was this helpful?