Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement subset sum using backtracking #765

Merged
merged 7 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions src/backtracking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod n_queens;
mod parentheses_generator;
mod permutations;
mod rat_in_maze;
mod subset_sum;
mod sudoku;

pub use all_combination_of_size_k::generate_all_combinations;
Expand All @@ -16,4 +17,5 @@ pub use n_queens::n_queens_solver;
pub use parentheses_generator::generate_parentheses;
pub use permutations::permute;
pub use rat_in_maze::find_path_in_maze;
pub use subset_sum::has_subset_with_sum;
pub use sudoku::sudoku_solver;
55 changes: 55 additions & 0 deletions src/backtracking/subset_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! This module provides functionality to check if there exists a subset of a given set of integers
//! that sums to a target value. The implementation uses a recursive backtracking approach.

/// Checks if there exists a subset of the given set that sums to the target value.
pub fn has_subset_with_sum(set: &[isize], target: isize) -> bool {
backtrack(set, set.len(), target)
}

/// Recursive helper function to check for a subset sum using backtracking.
vil02 marked this conversation as resolved.
Show resolved Hide resolved
fn backtrack(set: &[isize], remaining_items: usize, target: isize) -> bool {
// Found a subset with the required sum
if target == 0 {
return true;
}
// No more elements to process
if remaining_items == 0 {
return false;
}
// Check if we can find a subset including or excluding the last element
backtrack(set, remaining_items - 1, target)
|| backtrack(set, remaining_items - 1, target - set[remaining_items - 1])
}

#[cfg(test)]
mod tests {
use super::*;

macro_rules! has_subset_with_sum_tests {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (set, target, expected) = $test_case;
assert_eq!(has_subset_with_sum(set, target), expected);
}
)*
}
}

has_subset_with_sum_tests! {
test_small_set_with_sum: (&[3, 34, 4, 12, 5, 2], 9, true),
test_small_set_without_sum: (&[3, 34, 4, 12, 5, 2], 30, false),
test_consecutive_set_with_sum: (&[1, 2, 3, 4, 5, 6], 10, true),
test_consecutive_set_without_sum: (&[1, 2, 3, 4, 5, 6], 22, false),
test_large_set_with_sum: (&[5, 10, 12, 13, 15, 18, -1, 10, 50, -2, 3, 4], 30, true),
test_empty_set: (&[], 0, true),
test_empty_set_with_nonzero_sum: (&[], 10, false),
test_single_element_equal_to_sum: (&[10], 10, true),
test_single_element_not_equal_to_sum: (&[5], 10, false),
test_negative_set_with_sum: (&[-7, -3, -2, 5, 8], 0, true),
test_negative_sum: (&[1, 2, 3, 4, 5], -1, false),
test_negative_sum_with_negatives: (&[-7, -3, -2, 5, 8], -4, true),
test_negative_sum_with_negatives_no_solution: (&[-7, -3, -2, 5, 8], -14, false),
vil02 marked this conversation as resolved.
Show resolved Hide resolved
}
}