|
| 1 | +package com.thealgorithms.backtracking; |
| 2 | + |
| 3 | +// Problem Statement: Find the shortest path from a starting node to all other nodes in a weighted graph. |
| 4 | + |
| 5 | +// Example: |
| 6 | +// 7 |
| 7 | +// A -------- B |
| 8 | +// | | |
| 9 | +// 3| |2 |
| 10 | +// | | |
| 11 | +// C -------- D |
| 12 | +// 1 |
| 13 | +// Starting Node: A |
| 14 | +// Result: |
| 15 | +// Shortest paths: |
| 16 | + |
| 17 | +// A -> A: 0 |
| 18 | +// A -> B: 7 |
| 19 | +// A -> C: 3 |
| 20 | +// A -> D: 4 (A -> C -> D) |
| 21 | + |
| 22 | +import java.util.*; |
| 23 | + |
| 24 | +// Class representing an Edge in the graph |
| 25 | +class Edge { |
| 26 | + private final int destination; |
| 27 | + private final int weight; |
| 28 | + |
| 29 | + public Edge(int destination, int weight) { |
| 30 | + this.destination = destination; |
| 31 | + this.weight = weight; |
| 32 | + } |
| 33 | + |
| 34 | + public int getDestination() { |
| 35 | + return destination; |
| 36 | + } |
| 37 | + |
| 38 | + public int getWeight() { |
| 39 | + return weight; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Class representing a Graph |
| 44 | +class Graph { |
| 45 | + private final Map<Integer, List<Edge>> adjList; |
| 46 | + |
| 47 | + public Graph() { |
| 48 | + this.adjList = new HashMap<>(); |
| 49 | + } |
| 50 | + |
| 51 | + // Add edge between two nodes with a given weight |
| 52 | + public void addEdge(int source, int destination, int weight) { |
| 53 | + adjList.computeIfAbsent(source, k -> new ArrayList<>()).add(new Edge(destination, weight)); |
| 54 | + adjList.computeIfAbsent(destination, k -> new ArrayList<>()).add(new Edge(source, weight)); // undirected graph |
| 55 | + } |
| 56 | + |
| 57 | + public List<Edge> getEdges(int node) { |
| 58 | + return adjList.getOrDefault(node, new ArrayList<>()); |
| 59 | + } |
| 60 | + |
| 61 | + public Set<Integer> getAllNodes() { |
| 62 | + return adjList.keySet(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Class representing a Node (for use in the PriorityQueue) |
| 67 | +class Node { |
| 68 | + private final int node; |
| 69 | + private final int distance; |
| 70 | + |
| 71 | + public Node(int node, int distance) { |
| 72 | + this.node = node; |
| 73 | + this.distance = distance; |
| 74 | + } |
| 75 | + |
| 76 | + public int getNode() { |
| 77 | + return node; |
| 78 | + } |
| 79 | + |
| 80 | + public int getDistance() { |
| 81 | + return distance; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Dijkstra's Algorithm class |
| 86 | +public class Dijkstra { |
| 87 | + |
| 88 | + // method to find shortest paths from a given start node |
| 89 | + public Map<Integer, Integer> findShortestPaths(Graph graph, int startNode) { |
| 90 | + // priority Queue to select the minimum distance node |
| 91 | + PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(Node::getDistance)); |
| 92 | + Map<Integer, Integer> distances = new HashMap<>(); |
| 93 | + Map<Integer, Boolean> visited = new HashMap<>(); |
| 94 | + |
| 95 | + // Initialize distances with infinity |
| 96 | + for (int node : graph.getAllNodes()) { |
| 97 | + distances.put(node, Integer.MAX_VALUE); |
| 98 | + visited.put(node, false); |
| 99 | + } |
| 100 | + |
| 101 | + // Starting point |
| 102 | + distances.put(startNode, 0); |
| 103 | + pq.add(new Node(startNode, 0)); |
| 104 | + |
| 105 | + while (!pq.isEmpty()) { |
| 106 | + Node currentNode = pq.poll(); |
| 107 | + int current = currentNode.getNode(); |
| 108 | + |
| 109 | + if (visited.get(current)) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + visited.put(current, true); |
| 113 | + |
| 114 | + // Update the distances to the neighboring nodes |
| 115 | + for (Edge edge : graph.getEdges(current)) { |
| 116 | + int neighbor = edge.getDestination(); |
| 117 | + int newDist = distances.get(current) + edge.getWeight(); |
| 118 | + |
| 119 | + if (newDist < distances.get(neighbor)) { |
| 120 | + distances.put(neighbor, newDist); |
| 121 | + pq.add(new Node(neighbor, newDist)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return distances; |
| 126 | + } |
| 127 | +} |
0 commit comments