diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/Dijkstra.java b/src/main/java/com/thealgorithms/greedyalgorithms/Dijkstra.java new file mode 100644 index 000000000000..e4891df43ef5 --- /dev/null +++ b/src/main/java/com/thealgorithms/greedyalgorithms/Dijkstra.java @@ -0,0 +1,71 @@ +package com.thealgorithms.greedyalgorithms; + +import java.util.*; + +/** + * Dijkstra's shortest path algorithm for non-negative weighted graphs. + * + * Provides a method to compute shortest distances from a source node to all + * other nodes using an adjacency list representation. + */ +public final class Dijkstra { + private Dijkstra() {} + + /** + * Compute shortest distances from source to all vertices. + * + * @param n number of vertices (vertices are 0..n-1) + * @param adj adjacency list where adj[u] is a list of (v, weight) pairs + * @param src source vertex + * @return array of distances where dist[i] is shortest distance or Long.MAX_VALUE if unreachable + */ + public static long[] shortestPaths(int n, List> adj, int src) { + long[] dist = new long[n]; + Arrays.fill(dist, Long.MAX_VALUE); + dist[src] = 0L; + + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingLong(a -> a.dist)); + pq.add(new Node(src, 0L)); + + boolean[] visited = new boolean[n]; + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + int u = cur.id; + if (visited[u]) continue; + visited[u] = true; + + for (Edge e : adj.get(u)) { + int v = e.to; + long w = e.weight; + if (dist[u] != Long.MAX_VALUE && dist[u] + w < dist[v]) { + dist[v] = dist[u] + w; + pq.add(new Node(v, dist[v])); + } + } + } + + return dist; + } + + /** Simple edge class for adjacency lists */ + public static class Edge { + public final int to; + public final long weight; + + public Edge(int to, long weight) { + this.to = to; + this.weight = weight; + } + } + + private static class Node { + final int id; + final long dist; + + Node(int id, long dist) { + this.id = id; + this.dist = dist; + } + } +} diff --git a/src/main/java/com/thealgorithms/maths/HarmonicNumber.java b/src/main/java/com/thealgorithms/maths/HarmonicNumber.java new file mode 100644 index 000000000000..05f6aeac6a33 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/HarmonicNumber.java @@ -0,0 +1,36 @@ +package com.thealgorithms.maths; + +/** + * A class for calculating harmonic numbers. + * The harmonic series is the divergent infinite series: 1 + 1/2 + 1/3 + 1/4 + ... + */ +public final class HarmonicNumber { + + private HarmonicNumber() {} + + /** + * Calculates the nth harmonic number. + * The harmonic series is the sum: 1 + 1/2 + 1/3 + ... + 1/n + * This series appears in various mathematical and practical applications. + * + * For example: + * H(1) = 1 + * H(2) = 1 + 1/2 = 1.5 + * H(3) = 1 + 1/2 + 1/3 ≈ 1.833 + * H(4) = 1 + 1/2 + 1/3 + 1/4 ≈ 2.083 + * + * @param numTerms the number of terms to sum (n) + * @return the nth harmonic number + * @throws IllegalArgumentException if numTerms is less than 1 + */ + public static double of(int numTerms) { + if (numTerms < 1) { + throw new IllegalArgumentException("Number of terms must be positive"); + } + double harmonicSum = 0.0; + for (int termIndex = 1; termIndex <= numTerms; termIndex++) { + harmonicSum += 1.0 / termIndex; + } + return harmonicSum; + } +}