Skip to content

Commit 78dce9d

Browse files
committed
Added detailed time and space complexity analysis to enhance clarity and help readers understand the algorithm's performance.
1 parent af1d9d1 commit 78dce9d

17 files changed

+203
-13
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* <p>This implementation is intended for educational purposes.</p>
3333
*
3434
* @see <a href="https://en.wikipedia.org/wiki/Depth-first_search">Depth First Search</a>
35+
3536
*/
3637

3738
@SuppressWarnings({"rawtypes", "unchecked"})

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ private ArrayCombination() {
1818
* @param k The desired length of each combination.
1919
* @return A list containing all combinations of length k.
2020
* @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
21+
*
22+
* Complexity Analysis
23+
* Time Complexity: O(C(n, k) * k)
24+
* Space Complexity: O(k)
2125
*/
26+
2227
public static List<List<Integer>> combination(int n, int k) {
2328
if (k < 0 || k > n) {
2429
throw new IllegalArgumentException("Invalid input: 0 ≤ k ≤ n is required.");

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88

99
/**
1010
* Finds all combinations of a given array using backtracking algorithm * @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
11+
*
12+
* Complexity Analysis
13+
*
14+
* Time Complexity:
15+
* O(C(N, n) * n log n), where N is array size and n is combination length.
16+
* The log n factor comes from TreeSet operations (add/remove).
17+
*
18+
* Space Complexity:
19+
* O(C(N, n) * n) for storing all combinations +
20+
* O(n) auxiliary space for recursion and current set.
1121
*/
22+
1223
public final class Combination {
1324
private Combination() {
1425
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@
44
import java.util.Arrays;
55
import java.util.List;
66

7-
/** Backtracking: pick/not-pick with reuse of candidates. */
7+
/**
8+
* Backtracking: pick/not-pick with reuse of candidates.
9+
*
10+
* Complexity Analysis
11+
*
12+
* Time Complexity:
13+
* O(N^(T / min)), where N is number of candidates,
14+
* T is target, and min is the smallest candidate value.
15+
* In practice, it is O(P * k), where P is number of valid combinations
16+
* and k is average combination length.
17+
*
18+
* Space Complexity:
19+
* O(T / min) for recursion stack and current combination +
20+
* O(P * (T / min)) for storing all results.
21+
*/
22+
823
public final class CombinationSum {
924
private CombinationSum() {
1025
throw new UnsupportedOperationException("Utility class");

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,32 @@
88
* A class to solve a crossword puzzle using backtracking.
99
* Example:
1010
* Input:
11-
* puzzle = {
12-
* {' ', ' ', ' '},
13-
* {' ', ' ', ' '},
14-
* {' ', ' ', ' '}
15-
* }
16-
* words = List.of("cat", "dog")
11+
* puzzle = {
12+
* {' ', ' ', ' '},
13+
* {' ', ' ', ' '},
14+
* {' ', ' ', ' '}
15+
* }
16+
* words = List.of("cat", "dog")
1717
*
1818
* Output:
19-
* {
20-
* {'c', 'a', 't'},
21-
* {' ', ' ', ' '},
22-
* {'d', 'o', 'g'}
23-
* }
19+
* {
20+
* {'c', 'a', 't'},
21+
* {' ', ' ', ' '},
22+
* {'d', 'o', 'g'}
23+
* }
24+
*
25+
* Complexity Analysis
26+
*
27+
* Time Complexity:
28+
* O((2W)^W * L), where W is number of words and L is average word length.
29+
* In the worst case, this can be approximated as O(W! * 2^W * L)
30+
* due to trying all permutations of words with two placement directions.
31+
*
32+
* Space Complexity:
33+
* O(W) for recursion stack +
34+
* O(R * C) for the puzzle grid.
2435
*/
36+
2537
public final class CrosswordSolver {
2638
private CrosswordSolver() {
2739
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ private FloodFill() {
1414
* @param image The image to be filled
1515
* @param x The x coordinate of which color is to be obtained
1616
* @param y The y coordinate of which color is to be obtained
17+
*
18+
* Complexity Analysis:
19+
*
20+
* Time Complexity:
21+
* O(R * C), where R is number of rows and C is number of columns.
22+
* Each cell is visited at most once.
23+
*
24+
* Space Complexity:
25+
* O(R * C) in the worst case due to recursion stack.
1726
*/
1827

1928
public static int getPixel(final int[][] image, final int x, final int y) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.backtracking;
22

3+
import java.sql.Time;
34
import java.util.ArrayList;
45
import java.util.Comparator;
56
import java.util.List;
@@ -66,6 +67,18 @@ public static void resetBoard() {
6667
* @param count The current move number
6768
* @return True if a solution is found, False otherwise
6869
*/
70+
71+
/**
72+
* Time Complexity:
73+
*
74+
* Worst Case: O(8^(N^2)) due to backtracking.
75+
* Practical Complexity: Significantly reduced to near O(N^2)
76+
* using Warnsdorff’s heuristic and pruning (orphan detection).
77+
*
78+
* Space Complexity:
79+
* O(N^2) for the board and recursion stack.
80+
*/
81+
6982
static boolean solve(int row, int column, int count) {
7083
if (count > total) {
7184
return true;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ private MColoring() {
3535
* @param m The maximum number of allowed colors.
3636
* @return true if the graph can be colored using M colors, false otherwise.
3737
*/
38+
39+
/**
40+
* Time Complexity:
41+
* O(V + E), where V is number of vertices and E is number of edges.
42+
* Each node and edge is processed once during BFS traversal.
43+
*
44+
* Space Complexity:
45+
* O(V + E) for storing the graph and BFS queue.
46+
*/
47+
3848
static boolean isColoringPossible(ArrayList<Node> nodes, int n, int m) {
3949

4050
// Visited array keeps track of whether each node has been processed.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public static int[][] solveMazeUsingSecondStrategy(int[][] map) {
5050
* @param j The current y-coordinate of the ball (column index)
5151
* @return True if a path is found to (6,5), otherwise false
5252
*/
53+
54+
/**
55+
* Complexity Analysis
56+
*
57+
* Time Complexity:
58+
* O(R * C), where R is number of rows and C is number of columns.
59+
* Each cell is visited at most once.
60+
*
61+
* Space Complexity:
62+
* O(R * C) in the worst case due to recursion stack.
63+
*/
64+
5365
private static boolean setWay(int[][] map, int i, int j) {
5466
if (map[6][5] == 2) {
5567
return true;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public static void placeQueens(final int queens) {
6464
* @param columns: columns[i] = rowId where queen is placed in ith column.
6565
* @param columnIndex: This is the column in which queen is being placed
6666
*/
67+
68+
/**
69+
* Complexity Analysis:
70+
*
71+
* Time Complexity:
72+
* O(N! * N), where N is the number of queens.
73+
* The factorial comes from placing queens column by column,
74+
* and O(N) is for checking validity at each step.
75+
*
76+
* Space Complexity:
77+
* O(N) for recursion stack and column tracking +
78+
* O(S * N^2) for storing all valid solutions,
79+
* where S is the number of solutions.
80+
*/
81+
6782
private static void getSolution(int boardSize, List<List<String>> solutions, int[] columns, int columnIndex) {
6883
if (columnIndex == boardSize) {
6984
// this means that all queens have been placed

0 commit comments

Comments
 (0)