what is wrong with my djikstra algorithm implementation . it is not passing all the test cases?

I’m working on implementing Dijkstra’s algorithm in Java to find the shortest paths in a graph. While I’ve written the code below, I’m encountering issues with its functionality. Here’s the code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import java.util.*;
class Graph{
Map<Integer,List<Edge>>adjacencyList;
public Graph(){
adjacencyList=new HashMap<>();
}
public void addEdge(int source,int destination,int weight){
adjacencyList.putIfAbsent(source,new ArrayList<>());
adjacencyList.putIfAbsent(destination,new ArrayList<>());
adjacencyList.get(source).add(new Edge(destination,weight));
adjacencyList.get(destination).add(new Edge(source,weight));
}
public Map<Integer,Integer> dij(int start){
PriorityQueue<Edge> pq=new PriorityQueue<>(Comparator.comparingInt(edge->edge.weight));
Map<Integer,Integer> distances=new HashMap<>();
for(Integer node: adjacencyList.keySet()){
distances.put(node,Integer.MAX_VALUE);
}
distances.put(start,0);
pq.add(new Edge(start, 0));
while(!pq.isEmpty()){
Edge curr=pq.poll();
int curnode=curr.node;
int currDist=curr.weight;
for( Edge neighbour:adjacencyList.getOrDefault(curnode, new ArrayList<>()) ){
int newDist=distances.get(curnode)+neighbour.weight;
if (currDist > distances.get(curnode)) {
continue;
}
if(newDist<distances.get(neighbour.node)){
distances.put(neighbour.node, newDist);
pq.add(new Edge(neighbour.node, newDist));
}
}
}
return distances;
}
}
class Edge{
int node;
int weight;
public Edge(int node, int weight) {
this.node = node;
this.weight = weight;
}
}
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int V = s.nextInt();
int E = s.nextInt();
Graph graph=new Graph();
for(int i=0;i<E;i++){
int ei=s.nextInt();
int ej=s.nextInt();
int w=s.nextInt();
graph.addEdge(ei, ej, w);
}
int start=0;
Map<Integer,Integer> ans=graph.dij(start);
for(Map.Entry entry:ans.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
</code>
<code>import java.util.*; class Graph{ Map<Integer,List<Edge>>adjacencyList; public Graph(){ adjacencyList=new HashMap<>(); } public void addEdge(int source,int destination,int weight){ adjacencyList.putIfAbsent(source,new ArrayList<>()); adjacencyList.putIfAbsent(destination,new ArrayList<>()); adjacencyList.get(source).add(new Edge(destination,weight)); adjacencyList.get(destination).add(new Edge(source,weight)); } public Map<Integer,Integer> dij(int start){ PriorityQueue<Edge> pq=new PriorityQueue<>(Comparator.comparingInt(edge->edge.weight)); Map<Integer,Integer> distances=new HashMap<>(); for(Integer node: adjacencyList.keySet()){ distances.put(node,Integer.MAX_VALUE); } distances.put(start,0); pq.add(new Edge(start, 0)); while(!pq.isEmpty()){ Edge curr=pq.poll(); int curnode=curr.node; int currDist=curr.weight; for( Edge neighbour:adjacencyList.getOrDefault(curnode, new ArrayList<>()) ){ int newDist=distances.get(curnode)+neighbour.weight; if (currDist > distances.get(curnode)) { continue; } if(newDist<distances.get(neighbour.node)){ distances.put(neighbour.node, newDist); pq.add(new Edge(neighbour.node, newDist)); } } } return distances; } } class Edge{ int node; int weight; public Edge(int node, int weight) { this.node = node; this.weight = weight; } } public class Solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); int V = s.nextInt(); int E = s.nextInt(); Graph graph=new Graph(); for(int i=0;i<E;i++){ int ei=s.nextInt(); int ej=s.nextInt(); int w=s.nextInt(); graph.addEdge(ei, ej, w); } int start=0; Map<Integer,Integer> ans=graph.dij(start); for(Map.Entry entry:ans.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue()); } } } </code>
import java.util.*;
class Graph{
    Map<Integer,List<Edge>>adjacencyList;

    public Graph(){
adjacencyList=new HashMap<>();
    }
    public void addEdge(int source,int destination,int weight){
        adjacencyList.putIfAbsent(source,new ArrayList<>());
        adjacencyList.putIfAbsent(destination,new ArrayList<>());
        adjacencyList.get(source).add(new Edge(destination,weight));
adjacencyList.get(destination).add(new Edge(source,weight));
    }

    public Map<Integer,Integer> dij(int start){

        PriorityQueue<Edge> pq=new PriorityQueue<>(Comparator.comparingInt(edge->edge.weight));
        Map<Integer,Integer> distances=new HashMap<>();

        for(Integer node: adjacencyList.keySet()){
distances.put(node,Integer.MAX_VALUE);
        }
        distances.put(start,0);
        pq.add(new Edge(start, 0));

while(!pq.isEmpty()){

    Edge curr=pq.poll();
 int curnode=curr.node;
 int currDist=curr.weight;

for( Edge neighbour:adjacencyList.getOrDefault(curnode, new ArrayList<>()) ){

    int newDist=distances.get(curnode)+neighbour.weight;
if (currDist > distances.get(curnode)) {
    continue;
}

    if(newDist<distances.get(neighbour.node)){
 distances.put(neighbour.node, newDist);
 pq.add(new Edge(neighbour.node, newDist));
    }


}




}
return distances; 
    }
}

class Edge{
    int node;
    int weight;

    
        public Edge(int node, int weight) {
            this.node = node;
            this.weight = weight;
        }
}

       
        
    

public class Solution {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in); 
        int V = s.nextInt();
        int E = s.nextInt();
        Graph graph=new Graph();
        for(int i=0;i<E;i++){
            int ei=s.nextInt();
            int ej=s.nextInt();
            int w=s.nextInt();
 graph.addEdge(ei, ej, w);
        }
int start=0;

Map<Integer,Integer> ans=graph.dij(start);

for(Map.Entry entry:ans.entrySet()){
    System.out.println(entry.getKey()+" "+entry.getValue());
}

        }

        
    }

**In this implementation, I expect to compute the shortest distances from a specified start node to all other nodes in the graph. However, when I run the code, I’m not getting the expected results
need your help

**

New contributor

Shubham Shandilya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật