Skip to content

Commit e276f63

Browse files
authored
Merge branch 'master' into feature/two-sat
2 parents 9b47ea2 + 74ddea6 commit e276f63

File tree

17 files changed

+2256
-422
lines changed

17 files changed

+2256
-422
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
/**
10+
* An implementation of Dial's Algorithm for the single-source shortest path problem.
11+
* This algorithm is an optimization of Dijkstra's algorithm and is particularly
12+
* efficient for graphs with small, non-negative integer edge weights.
13+
*
14+
* It uses a bucket queue (implemented here as a List of HashSets) to store vertices,
15+
* where each bucket corresponds to a specific distance from the source. This is more
16+
* efficient than a standard priority queue when the range of edge weights is small.
17+
*
18+
* Time Complexity: O(E + W * V), where E is the number of edges, V is the number
19+
* of vertices, and W is the maximum weight of any edge.
20+
*
21+
* @see <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Dial's_algorithm">Wikipedia - Dial's Algorithm</a>
22+
*/
23+
public final class DialsAlgorithm {
24+
/**
25+
* Private constructor to prevent instantiation of this utility class.
26+
*/
27+
private DialsAlgorithm() {
28+
}
29+
/**
30+
* Represents an edge in the graph, connecting to a destination vertex with a given weight.
31+
*/
32+
public static class Edge {
33+
private final int destination;
34+
private final int weight;
35+
36+
public Edge(int destination, int weight) {
37+
this.destination = destination;
38+
this.weight = weight;
39+
}
40+
41+
public int getDestination() {
42+
return destination;
43+
}
44+
45+
public int getWeight() {
46+
return weight;
47+
}
48+
}
49+
/**
50+
* Finds the shortest paths from a source vertex to all other vertices in a weighted graph.
51+
*
52+
* @param graph The graph represented as an adjacency list.
53+
* @param source The source vertex to start from (0-indexed).
54+
* @param maxEdgeWeight The maximum weight of any single edge in the graph.
55+
* @return An array of integers where the value at each index `i` is the
56+
* shortest distance from the source to vertex `i`. Unreachable vertices
57+
* will have a value of Integer.MAX_VALUE.
58+
* @throws IllegalArgumentException if the source vertex is out of bounds.
59+
*/
60+
public static int[] run(List<List<Edge>> graph, int source, int maxEdgeWeight) {
61+
int numVertices = graph.size();
62+
if (source < 0 || source >= numVertices) {
63+
throw new IllegalArgumentException("Source vertex is out of bounds.");
64+
}
65+
66+
// Initialize distances array
67+
int[] distances = new int[numVertices];
68+
Arrays.fill(distances, Integer.MAX_VALUE);
69+
distances[source] = 0;
70+
71+
// The bucket queue. Size is determined by the max possible path length.
72+
int maxPathWeight = maxEdgeWeight * (numVertices > 0 ? numVertices - 1 : 0);
73+
List<Set<Integer>> buckets = new ArrayList<>(maxPathWeight + 1);
74+
for (int i = 0; i <= maxPathWeight; i++) {
75+
buckets.add(new HashSet<>());
76+
}
77+
78+
// Add the source vertex to the first bucket
79+
buckets.get(0).add(source);
80+
81+
// Process buckets in increasing order of distance
82+
for (int d = 0; d <= maxPathWeight; d++) {
83+
// Process all vertices in the current bucket
84+
while (!buckets.get(d).isEmpty()) {
85+
// Get and remove a vertex from the current bucket
86+
int u = buckets.get(d).iterator().next();
87+
buckets.get(d).remove(u);
88+
89+
// If we've found a shorter path already, skip
90+
if (d > distances[u]) {
91+
continue;
92+
}
93+
94+
// Relax all adjacent edges
95+
for (Edge edge : graph.get(u)) {
96+
int v = edge.getDestination();
97+
int weight = edge.getWeight();
98+
99+
// If a shorter path to v is found
100+
if (distances[u] != Integer.MAX_VALUE && distances[u] + weight < distances[v]) {
101+
// If v was already in a bucket, remove it from the old one
102+
if (distances[v] != Integer.MAX_VALUE) {
103+
buckets.get(distances[v]).remove(v);
104+
}
105+
// Update distance and move v to the new bucket
106+
distances[v] = distances[u] + weight;
107+
buckets.get(distances[v]).add(v);
108+
}
109+
}
110+
}
111+
}
112+
return distances;
113+
}
114+
}

src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,69 @@
33
/**
44
* Utility class for checking if a number is a Krishnamurthy number.
55
*
6-
* A Krishnamurthy number (also known as a Strong number) is a number whose sum of the factorials of its digits is equal to the number itself.
6+
* <p>
7+
* A Krishnamurthy number (also known as a Strong number or Factorion) is a
8+
* number
9+
* whose sum of the factorials of its digits is equal to the number itself.
10+
* </p>
711
*
8-
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
12+
* <p>
13+
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 +
14+
* 120 = 145.
15+
* </p>
16+
*
17+
* <p>
18+
* The only Krishnamurthy numbers in base 10 are: 1, 2, 145, and 40585.
19+
* </p>
20+
*
21+
* <p>
922
* <b>Example usage:</b>
23+
* </p>
24+
*
1025
* <pre>
1126
* boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
1227
* System.out.println(isKrishnamurthy); // Output: true
1328
*
1429
* isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
1530
* System.out.println(isKrishnamurthy); // Output: false
1631
* </pre>
32+
*
33+
* @see <a href="https://en.wikipedia.org/wiki/Factorion">Factorion
34+
* (Wikipedia)</a>
1735
*/
1836
public final class KrishnamurthyNumber {
1937

38+
// Pre-computed factorials for digits 0-9 to improve performance
39+
private static final int[] FACTORIALS = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
40+
2041
private KrishnamurthyNumber() {
2142
}
2243

2344
/**
2445
* Checks if a number is a Krishnamurthy number.
2546
*
26-
* @param n The number to check
47+
* <p>
48+
* A number is a Krishnamurthy number if the sum of the factorials of its digits
49+
* equals the number itself.
50+
* </p>
51+
*
52+
* @param n the number to check
2753
* @return true if the number is a Krishnamurthy number, false otherwise
2854
*/
2955
public static boolean isKrishnamurthy(int n) {
30-
int tmp = n;
31-
int s = 0;
32-
3356
if (n <= 0) {
3457
return false;
35-
} else {
36-
while (n != 0) {
37-
// initialising the variable fact that will store the factorials of the digits
38-
int fact = 1;
39-
// computing factorial of each digit
40-
for (int i = 1; i <= n % 10; i++) {
41-
fact = fact * i;
42-
}
43-
// computing the sum of the factorials
44-
s = s + fact;
45-
// discarding the digit for which factorial has been calculated
46-
n = n / 10;
47-
}
58+
}
4859

49-
// evaluating if sum of the factorials of the digits equals the number itself
50-
return tmp == s;
60+
int original = n;
61+
int sum = 0;
62+
63+
while (n != 0) {
64+
int digit = n % 10;
65+
sum = sum + FACTORIALS[digit];
66+
n = n / 10;
5167
}
68+
69+
return sum == original;
5270
}
5371
}

0 commit comments

Comments
 (0)