From 2f86af8736effc15f1f0a5be014f554679296319 Mon Sep 17 00:00:00 2001 From: JACOB JAMES K Date: Thu, 15 Apr 2021 00:24:00 +0530 Subject: [PATCH] Added Java Implementation of Dijkstras Algorithm for Single Source Shortest Path (#187) * Added Java Implementation of Dijkstras Algorithm for Single Source Shortest Path * Added changes to Dijkstras Algorithm Implementation according to review * Corrected spelling mistakes --- graphs/README.md | 4 ++ graphs/java/Dijkstras.java | 133 +++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 graphs/java/Dijkstras.java diff --git a/graphs/README.md b/graphs/README.md index b0806cc6..510a1ccc 100644 --- a/graphs/README.md +++ b/graphs/README.md @@ -5,3 +5,7 @@ 1. [Kruskal Algorithm](c-or-cpp/kruskal-algorithm.cpp) 2. [Bellman Ford Algorithm](c-or-cpp/bellman-ford.cpp) 3. [Prim's Algorithm](c-or-cpp/Prim's-algorithm.c) + +### Java + +1. [Dijkstras Algorithm](java/Dijkstras.java) diff --git a/graphs/java/Dijkstras.java b/graphs/java/Dijkstras.java new file mode 100644 index 00000000..9ac9fb16 --- /dev/null +++ b/graphs/java/Dijkstras.java @@ -0,0 +1,133 @@ +import java.util.*; + +class AdjListNode +{ + int dest; + int weight; + AdjListNode(int dest,int weight) + { + this.dest=dest; + this.weight=weight; + } +} + +class NodeComparator implements Comparator +{ + @Override + public int compare(AdjListNode node1, AdjListNode node2) + { + if (node1.weight < node2.weight) + return -1; + if (node1.weight > node2.weight) + return 1; + return 0; + } +} + +class Dijkstras +{ + public int[] dijkstras(List> adj_list,int source) + { + int[] distance = new int[adj_list.size()]; + Set completed = new HashSet(); + for(int i=0;i pq = new PriorityQueue<>(adj_list.size(),new NodeComparator()); + AdjListNode source_node = new AdjListNode(source,0); + for(int i=0;i (distance[current.dest]+n.weight)) + { + distance[n.dest] = distance[current.dest]+n.weight; + pq.add(new AdjListNode(n.dest, distance[n.dest])); + } + } + } + } + return distance; + } + + public static void main(String args[]) + { + /* + SAMPLE INPUT AND OUTPUT + ___________________________ + + Input: + __________ + Please enter the number of nodes N. Vertices will be [0,N-1] + 6 + Please Enter the number of Edges + 9 + Please enter each edge in the sequence + 0 1 1 + 0 2 5 + 1 2 2 + 1 4 1 + 1 3 2 + 2 4 2 + 3 5 1 + 3 4 3 + 4 5 2 + Please enter source vertex + 0 + + Output: + ___________ + Distances from source 0 + Node0--->0 + Node1--->1 + Node2--->3 + Node3--->3 + Node4--->2 + Node5--->4 + */ + + List> adj_list = new ArrayList>(); + System.out.println("Please enter the number of nodes N. Vertices will be [0,N-1]"); + Scanner sc = new Scanner(System.in); + int v = sc.nextInt(); + for(int i=0;i()); + + System.out.println("Please enter the number of edges"); + int e = sc.nextInt(); + System.out.println("Please enter each edge in the sequence "); + // Sample Data: 0 2 5 (edge from 0 to 2 with weight 5) + for(int i=0;i"+"infinity"); + else + System.out.println("Node"+i+"--->"+distances[i]); + } + } +}