Month: July 2018

Dijkstra’s Algorithm

Network Delay Time


public int networkDelayTime(int[][] times, int N, int K) {
        Map graph = new HashMap();
        for(int[] entry : times){
            if(!graph.containsKey(entry[0])){
                graph.put(entry[0], new ArrayList());
            }
            List neighbors = graph.get(entry[0]);
            neighbors.add(new Node(entry[1], entry[2]));
            graph.put(entry[0], neighbors);
        }

        PriorityQueue pq = new PriorityQueue(N, new Comparator(){
            public int compare(Node a, Node b){
                return a.dist - b.dist;
            }
        });
        Node start = new Node(K, 0);
        pq.offer(start);

        Map results = new HashMap();
        // Dijkstra's Algorithm
        while(!pq.isEmpty()){
            Node cur = pq.poll();
            if(results.containsKey(cur.index)){
                continue;
            }
            results.put(cur.index, cur.dist);
            if(graph.containsKey(cur.index)){
                List neighbors = graph.get(cur.index);
                for(Node nb : neighbors){
                    if(!results.containsKey(nb.index)){
                        nb.dist = cur.dist + nb.dist;
                        pq.offer(nb);
                    }
                }
            }                     
        }

        if(results.size() != N){
            return -1;
        }
        int ans = 0;
        for(int d : results.values()){
            ans = Math.max(ans, d);
        }
        return ans;
    }

    class Node {
        int index, dist;
        public Node(int index, int dist){
            this.index = index;
            this.dist = dist;
        }
    }
}