Skip to content

Commit 67fe51d

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 8b5eda5 + 3313ddc commit 67fe51d

32 files changed

+467
-163
lines changed

.gitpod.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM gitpod/workspace-java-21:2024-11-26-08-43-19
1+
FROM gitpod/workspace-java-21:2025-02-10-10-54-28
22

33
ENV LLVM_SCRIPT="tmp_llvm.sh"
44

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<dependency>
126126
<groupId>com.puppycrawl.tools</groupId>
127127
<artifactId>checkstyle</artifactId>
128-
<version>10.21.1</version>
128+
<version>10.21.2</version>
129129
</dependency>
130130
</dependencies>
131131
</plugin>

src/main/java/com/thealgorithms/backtracking/ArrayCombination.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static void combine(List<List<Integer>> combinations, List<Integer> curr
4848
for (int i = start; i < n; i++) {
4949
current.add(i);
5050
combine(combinations, current, i + 1, n, k);
51-
current.removeLast(); // Backtrack
51+
current.remove(current.size() - 1); // Backtrack
5252
}
5353
}
5454
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.thealgorithms.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* This class implements a solution for the Constrained Shortest Path Problem (CSPP).
9+
* also known as Shortest Path Problem with Resource Constraints (SPPRC).
10+
* The goal is to find the shortest path between two nodes while ensuring that
11+
* the resource constraint is not exceeded.
12+
*
13+
* @author <a href="https://github.com/DenizAltunkapan">Deniz Altunkapan</a>
14+
*/
15+
public class ConstrainedShortestPath {
16+
17+
/**
18+
* Represents a graph using an adjacency list.
19+
* This graph is designed for the Constrained Shortest Path Problem (CSPP).
20+
*/
21+
public static class Graph {
22+
23+
private List<List<Edge>> adjacencyList;
24+
25+
public Graph(int numNodes) {
26+
adjacencyList = new ArrayList<>();
27+
for (int i = 0; i < numNodes; i++) {
28+
adjacencyList.add(new ArrayList<>());
29+
}
30+
}
31+
32+
/**
33+
* Adds an edge to the graph.
34+
* @param from the starting node
35+
* @param to the ending node
36+
* @param cost the cost of the edge
37+
* @param resource the resource required to traverse the edge
38+
*/
39+
public void addEdge(int from, int to, int cost, int resource) {
40+
adjacencyList.get(from).add(new Edge(from, to, cost, resource));
41+
}
42+
43+
/**
44+
* Gets the edges that are adjacent to a given node.
45+
* @param node the node to get the edges for
46+
* @return the list of edges adjacent to the node
47+
*/
48+
public List<Edge> getEdges(int node) {
49+
return adjacencyList.get(node);
50+
}
51+
52+
/**
53+
* Gets the number of nodes in the graph.
54+
* @return the number of nodes
55+
*/
56+
public int getNumNodes() {
57+
return adjacencyList.size();
58+
}
59+
60+
public record Edge(int from, int to, int cost, int resource) {
61+
}
62+
}
63+
64+
private Graph graph;
65+
private int maxResource;
66+
67+
/**
68+
* Constructs a CSPSolver with the given graph and maximum resource constraint.
69+
*
70+
* @param graph the graph representing the problem
71+
* @param maxResource the maximum allowable resource
72+
*/
73+
public ConstrainedShortestPath(Graph graph, int maxResource) {
74+
this.graph = graph;
75+
this.maxResource = maxResource;
76+
}
77+
78+
/**
79+
* Solves the CSP to find the shortest path from the start node to the target node
80+
* without exceeding the resource constraint.
81+
*
82+
* @param start the starting node
83+
* @param target the target node
84+
* @return the minimum cost to reach the target node within the resource constraint,
85+
* or -1 if no valid path exists
86+
*/
87+
public int solve(int start, int target) {
88+
int numNodes = graph.getNumNodes();
89+
int[][] dp = new int[maxResource + 1][numNodes];
90+
91+
// Initialize dp table with maximum values
92+
for (int i = 0; i <= maxResource; i++) {
93+
Arrays.fill(dp[i], Integer.MAX_VALUE);
94+
}
95+
dp[0][start] = 0;
96+
97+
// Dynamic Programming: Iterate over resources and nodes
98+
for (int r = 0; r <= maxResource; r++) {
99+
for (int u = 0; u < numNodes; u++) {
100+
if (dp[r][u] == Integer.MAX_VALUE) {
101+
continue;
102+
}
103+
for (Graph.Edge edge : graph.getEdges(u)) {
104+
int v = edge.to();
105+
int cost = edge.cost();
106+
int resource = edge.resource();
107+
108+
if (r + resource <= maxResource) {
109+
dp[r + resource][v] = Math.min(dp[r + resource][v], dp[r][u] + cost);
110+
}
111+
}
112+
}
113+
}
114+
115+
// Find the minimum cost to reach the target node
116+
int minCost = Integer.MAX_VALUE;
117+
for (int r = 0; r <= maxResource; r++) {
118+
minCost = Math.min(minCost, dp[r][target]);
119+
}
120+
121+
return minCost == Integer.MAX_VALUE ? -1 : minCost;
122+
}
123+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.thealgorithms.maths;
22

3-
import static com.thealgorithms.maths.PrimeCheck.isPrime;
3+
import static com.thealgorithms.maths.Prime.PrimeCheck.isPrime;
44

55
/**
66
* This is a representation of the unsolved problem of Goldbach's Projection, according to which every

src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java renamed to src/main/java/com/thealgorithms/maths/Prime/LiouvilleLambdaFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
/*
44
* Java program for liouville lambda function
@@ -24,7 +24,7 @@ private LiouvilleLambdaFunction() {
2424
* -1 when number has odd number of prime factors
2525
* @throws IllegalArgumentException when number is negative
2626
*/
27-
static int liouvilleLambda(int number) {
27+
public static int liouvilleLambda(int number) {
2828
if (number <= 0) {
2929
// throw exception when number is less than or is zero
3030
throw new IllegalArgumentException("Number must be greater than zero.");

src/main/java/com/thealgorithms/maths/MillerRabinPrimalityCheck.java renamed to src/main/java/com/thealgorithms/maths/Prime/MillerRabinPrimalityCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
import java.util.Random;
44

src/main/java/com/thealgorithms/maths/MobiusFunction.java renamed to src/main/java/com/thealgorithms/maths/Prime/MobiusFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
/*
44
* Java program for mobius function
@@ -25,7 +25,7 @@ private MobiusFunction() {
2525
* 0 when number has repeated prime factor
2626
* -1 when number has odd number of prime factors
2727
*/
28-
static int mobius(int number) {
28+
public static int mobius(int number) {
2929
if (number <= 0) {
3030
// throw exception when number is less than or is zero
3131
throw new IllegalArgumentException("Number must be greater than zero.");

src/main/java/com/thealgorithms/maths/PrimeCheck.java renamed to src/main/java/com/thealgorithms/maths/Prime/PrimeCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
import java.util.Scanner;
44

src/main/java/com/thealgorithms/maths/PrimeFactorization.java renamed to src/main/java/com/thealgorithms/maths/Prime/PrimeFactorization.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
package com.thealgorithms.maths.Prime;
22

33
/*
44
* Authors:

0 commit comments

Comments
 (0)