2016-07-08

Dijkstra's algorithm : graph Shortest Path implementation using java

Input Format

The first line contains T, denoting the number of test cases.
First line of each test case has two integers N, denoting the number of nodes in the graph and M, denoting the number of edges in the graph.

The next M  lines each consist of three space-separated integers x,y,r , where x and y denote the two nodes between which the undirected edge exists, r denotes the length of edge between these corresponding nodes.

The last line has an integer , S denoting the starting position.


Example:
1
4 4
1 2 24
1 4 20
3 1 3
4 3 12
1




Output Format

For each of the T  test cases, print a single line consisting N-1  space separated integers denoting the shortest distance of  N-1 nodes other than S from starting position S in increasing order of their labels.

For unreachable nodes, print -1.

Example:

24 3 15



Explanation

The straight line is a weighted edge, denoting length of edge between the corresponding nodes.
The nodes S,B,C and D denote the obvious node 1,2,3 and 4 in the test case.
The shortest paths followed for the three nodes B,C and D are as follows :

S->B - Shortest Path Value : 24

S->C - Shortest Path Value : 3

S->C->D - Shortest Path Value : 15


Program:


  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;

  6. class Node implements Comparable<Node>{
  7.     int val, cost;
  8.     Node(int val, int cost){
  9.         this.val = val; this.cost = cost;
  10.     }
  11.     
  12.     public int compareTo(Node x){
  13.         return Integer.compare(this.cost, x.cost);
  14.     }
  15. }

  16. public class Solution {

  17.     public static void main(String[] args) {
  18.         Scanner sc = new Scanner(System.in);
  19.         int t = sc.nextInt();
  20.         while(t-- > 0){
  21.             int n = sc.nextInt(), m = sc.nextInt();
  22.             ArrayList<ArrayList<Node>> adj = new ArrayList<ArrayList<Node>>(n+1);
  23.             for(int i=0; i<n+1; i++)adj.add(new ArrayList<Node>(n+1));
  24.         
  25.             while(m-- > 0){
  26.                 int x = sc.nextInt(), y = sc.nextInt(), cost = sc.nextInt();
  27.                 adj.get(x).add(new Node(y, cost));
  28.                 adj.get(y).add(new Node(x, cost));
  29.             }
  30.             int s = sc.nextInt();
  31.             djikstra(s, adj, n);
  32.         }
  33.     }
  34.     
  35.     static void djikstra(int s, ArrayList<ArrayList<Node>> adj, int n){
  36.         int[] dist = new int[n+1];
  37.         Arrays.fill(dist, Integer.MAX_VALUE);
  38.         dist[s] = 0;
  39.         PriorityQueue<Node> pq = new PriorityQueue<Node>();
  40.         pq.add(new Node(s, 0));
  41.         while(pq.size() > 0){
  42.             Node curr = pq.peek(); pq.remove();
  43.             int currN = curr.val;
  44.             Iterator<Node> it = adj.get(currN).iterator();
  45.             while(it.hasNext()){
  46.                 Node temp = it.next();
  47.                 if(dist[temp.val] > dist[currN] + temp.cost){
  48.                     pq.add(new Node(temp.val, dist[currN]+temp.cost));
  49.                     dist[temp.val] = dist[currN] + temp.cost;
  50.                 }
  51.             }
  52.         }
  53.         
  54.         for(int i=1; i<dist.length; i++){
  55.             if(i!=s){
  56.                 System.out.print(((dist[i] == Integer.MAX_VALUE)?-1:dist[i]) + " ");
  57.             }
  58.         }
  59.         System.out.println();
  60.     }
  61. }


No comments:

Post a Comment