Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/main/java/com/thealgorithms/greedyalgorithms/Dijkstra.java
Original file line number Diff line number Diff line change
@@ -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<List<Edge>> adj, int src) {
long[] dist = new long[n];
Arrays.fill(dist, Long.MAX_VALUE);
dist[src] = 0L;

PriorityQueue<Node> 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;
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/thealgorithms/maths/HarmonicNumber.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading