Git Product home page Git Product logo

network-delay-time's Introduction

Network-Delay-Time

Dijkstra Algorithm Application to Get Network Delay Time

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.

Solution

  • Considering that we are looking for the longest path among all shortest paths from the source node, which is K, to all other nodes in the graph, using Dijkstra's algorithm seems a natural choice since it is made for solveing shortest path problems.
  • For a detailed explanation of Dijkstra's algorithm, check wiki page.
  • Here we used a implementation of heap based dijkstra algorithm.
  • Time complexity: O(ElogE) where E is the number of edges in the graph.
  • Space complexity: O(N + E). O(N) for the dist. O(E) for the heap and adjacency list.
class Solution {
   public int networkDelayTime(int[][] times, int N, int K) {
       int[] time = new int[N];
       Arrays.fill(time, Integer.MAX_VALUE);
       time[K - 1] = 0;
       PriorityQueue<int[]> pq = new PriorityQueue<>((a,b)->a[1] - b[1]);
       pq.offer(new int[]{K - 1, 0});
       while (!pq.isEmpty()) {
           int[] node = pq.poll();
           for (int[] cand : times) {
               if (cand[0] == node[0] + 1) {
                   if (node[1] + cand[2] < time[cand[1] - 1]) {
                       time[cand[1] - 1] = node[1] + cand[2];
                       pq.offer(new int[]{cand[1] - 1, time[cand[1]  -1]});
                   }
               }
           }
       }
       int max = 0;
       for (int i : time) {
           if (i == Integer.MAX_VALUE) {
              return -1;
           }
           max = Math.max (max, i);
       }
       return max;
   }
}

network-delay-time's People

Contributors

siqi-ontheway avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.