diff --git a/Cargo.toml b/Cargo.toml index 91ec0ead1ea..6f9b0139623 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -145,6 +145,7 @@ unwrap_in_result = { level = "allow", priority = 1 } unwrap_used = { level = "allow", priority = 1 } use_debug = { level = "allow", priority = 1 } wildcard_enum_match_arm = { level = "allow", priority = 1 } +renamed_function_params = { level = "allow", priority = 1 } # nursery-lints: branches_sharing_code = { level = "allow", priority = 1 } cognitive_complexity = { level = "allow", priority = 1 } @@ -160,5 +161,9 @@ redundant_clone = { level = "allow", priority = 1 } suboptimal_flops = { level = "allow", priority = 1 } suspicious_operation_groupings = { level = "allow", priority = 1 } use_self = { level = "allow", priority = 1 } +while_float = { level = "allow", priority = 1 } +needless_pass_by_ref_mut = { level = "allow", priority = 1 } # cargo-lints: cargo_common_metadata = { level = "allow", priority = 1 } +# style-lints: +doc_lazy_continuation = { level = "allow", priority = 1 } diff --git a/DIRECTORY.md b/DIRECTORY.md index 8d7f93f97b3..64b274abb9b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -10,6 +10,7 @@ * [Parentheses Generator](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/parentheses_generator.rs) * [Permutations](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/permutations.rs) * [Rat In Maze](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/rat_in_maze.rs) + * [Subset Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/subset_sum.rs) * [Sudoku](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/sudoku.rs) * Big Integer * [Fast Factorial](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/fast_factorial.rs) @@ -62,7 +63,6 @@ * [Graph](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/graph.rs) * [Hash Table](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/hash_table.rs) * [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/heap.rs) - * [Infix To Postfix](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/infix_to_postfix.rs) * [Lazy Segment Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/lazy_segment_tree.rs) * [Linked List](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/linked_list.rs) * [Postfix Evaluation](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/postfix_evaluation.rs) @@ -155,6 +155,7 @@ * [K Means](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/k_means.rs) * [Linear Regression](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/linear_regression.rs) * Loss Function + * [Average Margin Ranking Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/average_margin_ranking_loss.rs) * [Hinge Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/hinge_loss.rs) * [Huber Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/huber_loss.rs) * [Kl Divergence Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/kl_divergence_loss.rs) @@ -201,6 +202,7 @@ * [Geometric Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/geometric_series.rs) * [Greatest Common Divisor](https://github.com/TheAlgorithms/Rust/blob/master/src/math/greatest_common_divisor.rs) * [Huber Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/math/huber_loss.rs) + * [Infix To Postfix](https://github.com/TheAlgorithms/Rust/blob/master/src/math/infix_to_postfix.rs) * [Interest](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interest.rs) * [Interpolation](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interpolation.rs) * [Interquartile Range](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interquartile_range.rs) diff --git a/src/backtracking/mod.rs b/src/backtracking/mod.rs index ea7b134b058..182c6fbbc01 100644 --- a/src/backtracking/mod.rs +++ b/src/backtracking/mod.rs @@ -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; @@ -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; diff --git a/src/backtracking/subset_sum.rs b/src/backtracking/subset_sum.rs new file mode 100644 index 00000000000..3e69b380b58 --- /dev/null +++ b/src/backtracking/subset_sum.rs @@ -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) +} + +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), + test_even_inputs_odd_target: (&[2, 4, 6, 2, 8, -2, 10, 12, -24, 8, 12, 18], 3, false), + } +} diff --git a/src/big_integer/fast_factorial.rs b/src/big_integer/fast_factorial.rs index f498bb101be..80652073e17 100644 --- a/src/big_integer/fast_factorial.rs +++ b/src/big_integer/fast_factorial.rs @@ -30,21 +30,19 @@ pub fn fast_factorial(n: usize) -> BigUint { // get list of primes that will be factors of n! let primes = sieve_of_eratosthenes(n); - let mut p_indeces = BTreeMap::new(); - // Map the primes with their index - primes.into_iter().for_each(|p| { - p_indeces.insert(p, index(p, n)); - }); + let p_indices = primes + .into_iter() + .map(|p| (p, index(p, n))) + .collect::>(); - let max_bits = p_indeces.get(&2).unwrap().next_power_of_two().ilog2() + 1; + let max_bits = p_indices.get(&2).unwrap().next_power_of_two().ilog2() + 1; // Create a Vec of 1's - let mut a = Vec::with_capacity(max_bits as usize); - a.resize(max_bits as usize, BigUint::one()); + let mut a = vec![BigUint::one(); max_bits as usize]; // For every prime p, multiply a[i] by p if the ith bit of p's index is 1 - for (p, i) in p_indeces { + for (p, i) in p_indices { let mut bit = 1usize; while bit.ilog2() < max_bits { if (bit & i) > 0 { diff --git a/src/data_structures/infix_to_postfix.rs b/src/data_structures/infix_to_postfix.rs deleted file mode 100755 index 8d1ca6e7922..00000000000 --- a/src/data_structures/infix_to_postfix.rs +++ /dev/null @@ -1,72 +0,0 @@ -// Function to convert infix expression to postfix expression -pub fn infix_to_postfix(infix: &str) -> String { - let mut postfix = String::new(); - let mut stack: Vec = Vec::new(); - - // Define the precedence of operators - let precedence = |op: char| -> i32 { - match op { - '+' | '-' => 1, - '*' | '/' => 2, - '^' => 3, - _ => 0, - } - }; - - for token in infix.chars() { - match token { - c if c.is_alphanumeric() => { - postfix.push(c); - } - '(' => { - stack.push('('); - } - ')' => { - while let Some(top) = stack.pop() { - if top == '(' { - break; - } - postfix.push(top); - } - } - '+' | '-' | '*' | '/' | '^' => { - while let Some(top) = stack.last() { - if *top == '(' || precedence(*top) < precedence(token) { - break; - } - postfix.push(stack.pop().unwrap()); - } - stack.push(token); - } - _ => {} - } - } - - while let Some(top) = stack.pop() { - if top == '(' { - // If there are unmatched parentheses, it's an error. - return "Error: Unmatched parentheses".to_string(); - } - postfix.push(top); - } - - postfix -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_infix_to_postfix() { - assert_eq!(infix_to_postfix("a-b+c-d*e"), "ab-c+de*-".to_string()); - assert_eq!( - infix_to_postfix("a*(b+c)+d/(e+f)"), - "abc+*def+/+".to_string() - ); - assert_eq!( - infix_to_postfix("(a-b+c)*(d+e*f)"), - "ab-c+def*+*".to_string() - ); - } -} diff --git a/src/data_structures/mod.rs b/src/data_structures/mod.rs index 660ba8f0608..7c0ddf827e0 100644 --- a/src/data_structures/mod.rs +++ b/src/data_structures/mod.rs @@ -6,7 +6,6 @@ mod floyds_algorithm; pub mod graph; mod hash_table; mod heap; -mod infix_to_postfix; mod lazy_segment_tree; mod linked_list; mod postfix_evaluation; @@ -31,7 +30,6 @@ pub use self::graph::DirectedGraph; pub use self::graph::UndirectedGraph; pub use self::hash_table::HashTable; pub use self::heap::Heap; -pub use self::infix_to_postfix::infix_to_postfix; pub use self::lazy_segment_tree::LazySegmentTree; pub use self::linked_list::LinkedList; pub use self::postfix_evaluation::evaluate_postfix; diff --git a/src/dynamic_programming/coin_change.rs b/src/dynamic_programming/coin_change.rs index 84e4ab26323..2bfd573a9c0 100644 --- a/src/dynamic_programming/coin_change.rs +++ b/src/dynamic_programming/coin_change.rs @@ -1,70 +1,94 @@ -/// Coin change via Dynamic Programming +//! This module provides a solution to the coin change problem using dynamic programming. +//! The `coin_change` function calculates the fewest number of coins required to make up +//! a given amount using a specified set of coin denominations. +//! +//! The implementation leverages dynamic programming to build up solutions for smaller +//! amounts and combines them to solve for larger amounts. It ensures optimal substructure +//! and overlapping subproblems are efficiently utilized to achieve the solution. -/// coin_change(coins, amount) returns the fewest number of coins that need to make up that amount. -/// If that amount of money cannot be made up by any combination of the coins, return `None`. +//! # Complexity +//! - Time complexity: O(amount * coins.length) +//! - Space complexity: O(amount) + +/// Returns the fewest number of coins needed to make up the given amount using the provided coin denominations. +/// If the amount cannot be made up by any combination of the coins, returns `None`. +/// +/// # Arguments +/// * `coins` - A slice of coin denominations. +/// * `amount` - The total amount of money to be made up. +/// +/// # Returns +/// * `Option` - The minimum number of coins required to make up the amount, or `None` if it's not possible. /// -/// # Arguments: -/// * `coins` - coins of different denominations -/// * `amount` - a total amount of money be made up. /// # Complexity -/// - time complexity: O(amount * coins.length), -/// - space complexity: O(amount), +/// * Time complexity: O(amount * coins.length) +/// * Space complexity: O(amount) pub fn coin_change(coins: &[usize], amount: usize) -> Option { - let mut dp = vec![None; amount + 1]; - dp[0] = Some(0); + let mut min_coins = vec![None; amount + 1]; + min_coins[0] = Some(0); - // Assume dp[i] is the fewest number of coins making up amount i, - // then for every coin in coins, dp[i] = min(dp[i - coin] + 1). - for i in 0..=amount { - for &coin in coins { - if i >= coin { - dp[i] = match dp[i - coin] { - Some(prev_coins) => match dp[i] { - Some(curr_coins) => Some(curr_coins.min(prev_coins + 1)), - None => Some(prev_coins + 1), - }, - None => dp[i], - }; - } - } - } + (0..=amount).for_each(|curr_amount| { + coins + .iter() + .filter(|&&coin| curr_amount >= coin) + .for_each(|&coin| { + if let Some(prev_min_coins) = min_coins[curr_amount - coin] { + min_coins[curr_amount] = Some( + min_coins[curr_amount].map_or(prev_min_coins + 1, |curr_min_coins| { + curr_min_coins.min(prev_min_coins + 1) + }), + ); + } + }); + }); - dp[amount] + min_coins[amount] } #[cfg(test)] mod tests { use super::*; - #[test] - fn basic() { - // 11 = 5 * 2 + 1 * 1 - let coins = vec![1, 2, 5]; - assert_eq!(Some(3), coin_change(&coins, 11)); - - // 119 = 11 * 10 + 7 * 1 + 2 * 1 - let coins = vec![2, 3, 5, 7, 11]; - assert_eq!(Some(12), coin_change(&coins, 119)); - } - - #[test] - fn coins_empty() { - let coins = vec![]; - assert_eq!(None, coin_change(&coins, 1)); - } - - #[test] - fn amount_zero() { - let coins = vec![1, 2, 3]; - assert_eq!(Some(0), coin_change(&coins, 0)); + macro_rules! coin_change_tests { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (coins, amount, expected) = $test_case; + assert_eq!(expected, coin_change(&coins, amount)); + } + )* + } } - #[test] - fn fail_change() { - // 3 can't be change by 2. - let coins = vec![2]; - assert_eq!(None, coin_change(&coins, 3)); - let coins = vec![10, 20, 50, 100]; - assert_eq!(None, coin_change(&coins, 5)); + coin_change_tests! { + test_basic_case: (vec![1, 2, 5], 11, Some(3)), + test_multiple_denominations: (vec![2, 3, 5, 7, 11], 119, Some(12)), + test_empty_coins: (vec![], 1, None), + test_zero_amount: (vec![1, 2, 3], 0, Some(0)), + test_no_solution_small_coin: (vec![2], 3, None), + test_no_solution_large_coin: (vec![10, 20, 50, 100], 5, None), + test_single_coin_large_amount: (vec![1], 100, Some(100)), + test_large_amount_multiple_coins: (vec![1, 2, 5], 10000, Some(2000)), + test_no_combination_possible: (vec![3, 7], 5, None), + test_exact_combination: (vec![1, 3, 4], 6, Some(2)), + test_large_denomination_multiple_coins: (vec![10, 50, 100], 1000, Some(10)), + test_small_amount_not_possible: (vec![5, 10], 1, None), + test_non_divisible_amount: (vec![2], 3, None), + test_all_multiples: (vec![1, 2, 4, 8], 15, Some(4)), + test_large_amount_mixed_coins: (vec![1, 5, 10, 25], 999, Some(45)), + test_prime_coins_and_amount: (vec![2, 3, 5, 7], 17, Some(3)), + test_coins_larger_than_amount: (vec![5, 10, 20], 1, None), + test_repeating_denominations: (vec![1, 1, 1, 5], 8, Some(4)), + test_non_standard_denominations: (vec![1, 4, 6, 9], 15, Some(2)), + test_very_large_denominations: (vec![1000, 2000, 5000], 1, None), + test_large_amount_performance: (vec![1, 5, 10, 25, 50, 100, 200, 500], 9999, Some(29)), + test_powers_of_two: (vec![1, 2, 4, 8, 16, 32, 64], 127, Some(7)), + test_fibonacci_sequence: (vec![1, 2, 3, 5, 8, 13, 21, 34], 55, Some(2)), + test_mixed_small_large: (vec![1, 100, 1000, 10000], 11001, Some(3)), + test_impossible_combinations: (vec![2, 4, 6, 8], 7, None), + test_greedy_approach_does_not_work: (vec![1, 12, 20], 24, Some(2)), + test_zero_denominations_no_solution: (vec![0], 1, None), + test_zero_denominations_solution: (vec![0], 0, Some(0)), } } diff --git a/src/dynamic_programming/longest_common_subsequence.rs b/src/dynamic_programming/longest_common_subsequence.rs index a92ad50e26e..58f82714f93 100644 --- a/src/dynamic_programming/longest_common_subsequence.rs +++ b/src/dynamic_programming/longest_common_subsequence.rs @@ -1,73 +1,116 @@ -/// Longest common subsequence via Dynamic Programming +//! This module implements the Longest Common Subsequence (LCS) algorithm. +//! The LCS problem is finding the longest subsequence common to two sequences. +//! It differs from the problem of finding common substrings: unlike substrings, subsequences +//! are not required to occupy consecutive positions within the original sequences. +//! This implementation handles Unicode strings efficiently and correctly, ensuring +//! that multi-byte characters are managed properly. -/// longest_common_subsequence(a, b) returns the longest common subsequence -/// between the strings a and b. -pub fn longest_common_subsequence(a: &str, b: &str) -> String { - let a: Vec<_> = a.chars().collect(); - let b: Vec<_> = b.chars().collect(); - let (na, nb) = (a.len(), b.len()); +/// Computes the longest common subsequence of two input strings. +/// +/// The longest common subsequence (LCS) of two strings is the longest sequence that can +/// be derived from both strings by deleting some elements without changing the order of +/// the remaining elements. +/// +/// ## Note +/// The function may return different LCSs for the same pair of strings depending on the +/// order of the inputs and the nature of the sequences. This is due to the way the dynamic +/// programming algorithm resolves ties when multiple common subsequences of the same length +/// exist. The order of the input strings can influence the specific path taken through the +/// DP table, resulting in different valid LCS outputs. +/// +/// For example: +/// `longest_common_subsequence("hello, world!", "world, hello!")` returns `"hello!"` +/// but +/// `longest_common_subsequence("world, hello!", "hello, world!")` returns `"world!"` +/// +/// This difference arises because the dynamic programming table is filled differently based +/// on the input order, leading to different tie-breaking decisions and thus different LCS results. +pub fn longest_common_subsequence(first_seq: &str, second_seq: &str) -> String { + let first_seq_chars = first_seq.chars().collect::>(); + let second_seq_chars = second_seq.chars().collect::>(); - // solutions[i][j] is the length of the longest common subsequence - // between a[0..i-1] and b[0..j-1] - let mut solutions = vec![vec![0; nb + 1]; na + 1]; + let lcs_lengths = initialize_lcs_lengths(&first_seq_chars, &second_seq_chars); + let lcs_chars = reconstruct_lcs(&first_seq_chars, &second_seq_chars, &lcs_lengths); - for (i, ci) in a.iter().enumerate() { - for (j, cj) in b.iter().enumerate() { - // if ci == cj, there is a new common character; - // otherwise, take the best of the two solutions - // at (i-1,j) and (i,j-1) - solutions[i + 1][j + 1] = if ci == cj { - solutions[i][j] + 1 + lcs_chars.into_iter().collect() +} + +fn initialize_lcs_lengths(first_seq_chars: &[char], second_seq_chars: &[char]) -> Vec> { + let first_seq_len = first_seq_chars.len(); + let second_seq_len = second_seq_chars.len(); + + let mut lcs_lengths = vec![vec![0; second_seq_len + 1]; first_seq_len + 1]; + + // Populate the LCS lengths table + (1..=first_seq_len).for_each(|i| { + (1..=second_seq_len).for_each(|j| { + lcs_lengths[i][j] = if first_seq_chars[i - 1] == second_seq_chars[j - 1] { + lcs_lengths[i - 1][j - 1] + 1 } else { - solutions[i][j + 1].max(solutions[i + 1][j]) - } - } - } + lcs_lengths[i - 1][j].max(lcs_lengths[i][j - 1]) + }; + }); + }); - // reconstitute the solution string from the lengths - let mut result: Vec = Vec::new(); - let (mut i, mut j) = (na, nb); + lcs_lengths +} + +fn reconstruct_lcs( + first_seq_chars: &[char], + second_seq_chars: &[char], + lcs_lengths: &[Vec], +) -> Vec { + let mut lcs_chars = Vec::new(); + let mut i = first_seq_chars.len(); + let mut j = second_seq_chars.len(); while i > 0 && j > 0 { - if a[i - 1] == b[j - 1] { - result.push(a[i - 1]); + if first_seq_chars[i - 1] == second_seq_chars[j - 1] { + lcs_chars.push(first_seq_chars[i - 1]); i -= 1; j -= 1; - } else if solutions[i - 1][j] > solutions[i][j - 1] { + } else if lcs_lengths[i - 1][j] >= lcs_lengths[i][j - 1] { i -= 1; } else { j -= 1; } } - result.reverse(); - result.iter().collect() + lcs_chars.reverse(); + lcs_chars } #[cfg(test)] mod tests { - use super::longest_common_subsequence; - - #[test] - fn test_longest_common_subsequence() { - // empty case - assert_eq!(&longest_common_subsequence("", ""), ""); - assert_eq!(&longest_common_subsequence("", "abcd"), ""); - assert_eq!(&longest_common_subsequence("abcd", ""), ""); + use super::*; - // simple cases - assert_eq!(&longest_common_subsequence("abcd", "c"), "c"); - assert_eq!(&longest_common_subsequence("abcd", "d"), "d"); - assert_eq!(&longest_common_subsequence("abcd", "e"), ""); - assert_eq!(&longest_common_subsequence("abcdefghi", "acegi"), "acegi"); - - // less simple cases - assert_eq!(&longest_common_subsequence("abcdgh", "aedfhr"), "adh"); - assert_eq!(&longest_common_subsequence("aggtab", "gxtxayb"), "gtab"); + macro_rules! longest_common_subsequence_tests { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (first_seq, second_seq, expected_lcs) = $test_case; + assert_eq!(longest_common_subsequence(&first_seq, &second_seq), expected_lcs); + } + )* + }; + } - // unicode - assert_eq!( - &longest_common_subsequence("你好,世界", "再见世界"), - "世界" - ); + longest_common_subsequence_tests! { + empty_case: ("", "", ""), + one_empty: ("", "abcd", ""), + identical_strings: ("abcd", "abcd", "abcd"), + completely_different: ("abcd", "efgh", ""), + single_character: ("a", "a", "a"), + different_length: ("abcd", "abc", "abc"), + special_characters: ("$#%&", "#@!%", "#%"), + long_strings: ("abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", + "bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgha", + "bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh"), + unicode_characters: ("你好,世界", "再见,世界", ",世界"), + spaces_and_punctuation_0: ("hello, world!", "world, hello!", "hello!"), + spaces_and_punctuation_1: ("hello, world!", "world, hello!", "hello!"), // longest_common_subsequence is not symmetric + random_case_1: ("abcdef", "xbcxxxe", "bce"), + random_case_2: ("xyz", "abc", ""), + random_case_3: ("abracadabra", "avadakedavra", "aaadara"), } } diff --git a/src/dynamic_programming/matrix_chain_multiply.rs b/src/dynamic_programming/matrix_chain_multiply.rs index 83f519a9d21..410aec741e5 100644 --- a/src/dynamic_programming/matrix_chain_multiply.rs +++ b/src/dynamic_programming/matrix_chain_multiply.rs @@ -1,76 +1,91 @@ -// matrix_chain_multiply finds the minimum number of multiplications to perform a chain of matrix -// multiplications. The input matrices represents the dimensions of matrices. For example [1,2,3,4] -// represents matrices of dimension (1x2), (2x3), and (3x4) -// -// Lets say we are given [4, 3, 2, 1]. If we naively multiply left to right, we get: -// -// (4*3*2) + (4*2*1) = 20 -// -// We can reduce the multiplications by reordering the matrix multiplications: -// -// (3*2*1) + (4*3*1) = 18 -// -// We solve this problem with dynamic programming and tabulation. table[i][j] holds the optimal -// number of multiplications in range matrices[i..j] (inclusive). Note this means that table[i][i] -// and table[i][i+1] are always zero, since those represent a single vector/matrix and do not -// require any multiplications. -// -// For any i, j, and k = i+1, i+2, ..., j-1: -// -// table[i][j] = min(table[i][k] + table[k][j] + matrices[i] * matrices[k] * matrices[j]) -// -// table[i][k] holds the optimal solution to matrices[i..k] -// -// table[k][j] holds the optimal solution to matrices[k..j] -// -// matrices[i] * matrices[k] * matrices[j] computes the number of multiplications to join the two -// matrices together. -// -// Runs in O(n^3) time and O(n^2) space. +//! This module implements a dynamic programming solution to find the minimum +//! number of multiplications needed to multiply a chain of matrices with given dimensions. +//! +//! The algorithm uses a dynamic programming approach with tabulation to calculate the minimum +//! number of multiplications required for matrix chain multiplication. +//! +//! # Time Complexity +//! +//! The algorithm runs in O(n^3) time complexity and O(n^2) space complexity, where n is the +//! number of matrices. -pub fn matrix_chain_multiply(matrices: Vec) -> u32 { - let n = matrices.len(); - if n <= 2 { - // No multiplications required. - return 0; +/// Custom error types for matrix chain multiplication +#[derive(Debug, PartialEq)] +pub enum MatrixChainMultiplicationError { + EmptyDimensions, + InsufficientDimensions, +} + +/// Calculates the minimum number of scalar multiplications required to multiply a chain +/// of matrices with given dimensions. +/// +/// # Arguments +/// +/// * `dimensions`: A vector where each element represents the dimensions of consecutive matrices +/// in the chain. For example, [1, 2, 3, 4] represents matrices of dimensions (1x2), (2x3), and (3x4). +/// +/// # Returns +/// +/// The minimum number of scalar multiplications needed to compute the product of the matrices +/// in the optimal order. +/// +/// # Errors +/// +/// Returns an error if the input is invalid (i.e., empty or length less than 2). +pub fn matrix_chain_multiply( + dimensions: Vec, +) -> Result { + if dimensions.is_empty() { + return Err(MatrixChainMultiplicationError::EmptyDimensions); } - let mut table = vec![vec![0; n]; n]; - for length in 2..n { - for i in 0..n - length { - let j = i + length; - table[i][j] = u32::MAX; - for k in i + 1..j { - let multiplications = - table[i][k] + table[k][j] + matrices[i] * matrices[k] * matrices[j]; - if multiplications < table[i][j] { - table[i][j] = multiplications; - } - } - } + if dimensions.len() == 1 { + return Err(MatrixChainMultiplicationError::InsufficientDimensions); } - table[0][n - 1] + let mut min_operations = vec![vec![0; dimensions.len()]; dimensions.len()]; + + (2..dimensions.len()).for_each(|chain_len| { + (0..dimensions.len() - chain_len).for_each(|start| { + let end = start + chain_len; + min_operations[start][end] = (start + 1..end) + .map(|split| { + min_operations[start][split] + + min_operations[split][end] + + dimensions[start] * dimensions[split] * dimensions[end] + }) + .min() + .unwrap_or(usize::MAX); + }); + }); + + Ok(min_operations[0][dimensions.len() - 1]) } #[cfg(test)] mod tests { use super::*; - #[test] - fn basic() { - assert_eq!(matrix_chain_multiply(vec![1, 2, 3, 4]), 18); - assert_eq!(matrix_chain_multiply(vec![4, 3, 2, 1]), 18); - assert_eq!(matrix_chain_multiply(vec![40, 20, 30, 10, 30]), 26000); - assert_eq!(matrix_chain_multiply(vec![1, 2, 3, 4, 3]), 30); - assert_eq!(matrix_chain_multiply(vec![1, 2, 3, 4, 3]), 30); - assert_eq!(matrix_chain_multiply(vec![4, 10, 3, 12, 20, 7]), 1344); + macro_rules! test_cases { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (input, expected) = $test_case; + assert_eq!(matrix_chain_multiply(input.clone()), expected); + assert_eq!(matrix_chain_multiply(input.into_iter().rev().collect()), expected); + } + )* + }; } - #[test] - fn zero() { - assert_eq!(matrix_chain_multiply(vec![]), 0); - assert_eq!(matrix_chain_multiply(vec![10]), 0); - assert_eq!(matrix_chain_multiply(vec![10, 20]), 0); + test_cases! { + basic_chain_of_matrices: (vec![1, 2, 3, 4], Ok(18)), + chain_of_large_matrices: (vec![40, 20, 30, 10, 30], Ok(26000)), + long_chain_of_matrices: (vec![1, 2, 3, 4, 3, 5, 7, 6, 10], Ok(182)), + complex_chain_of_matrices: (vec![4, 10, 3, 12, 20, 7], Ok(1344)), + empty_dimensions_input: (vec![], Err(MatrixChainMultiplicationError::EmptyDimensions)), + single_dimensions_input: (vec![10], Err(MatrixChainMultiplicationError::InsufficientDimensions)), + single_matrix_input: (vec![10, 20], Ok(0)), } } diff --git a/src/dynamic_programming/rod_cutting.rs b/src/dynamic_programming/rod_cutting.rs index 015e26d46a2..e56d482fdf7 100644 --- a/src/dynamic_programming/rod_cutting.rs +++ b/src/dynamic_programming/rod_cutting.rs @@ -1,55 +1,65 @@ -//! Solves the rod-cutting problem +//! This module provides functions for solving the rod-cutting problem using dynamic programming. use std::cmp::max; -/// `rod_cut(p)` returns the maximum possible profit if a rod of length `n` = `p.len()` -/// is cut into up to `n` pieces, where the profit gained from each piece of length -/// `l` is determined by `p[l - 1]` and the total profit is the sum of the profit -/// gained from each piece. +/// Calculates the maximum possible profit from cutting a rod into pieces of varying lengths. /// -/// # Arguments -/// - `p` - profit for rods of length 1 to n inclusive +/// Returns the maximum profit achievable by cutting a rod into pieces such that the profit from each +/// piece is determined by its length and predefined prices. /// /// # Complexity -/// - time complexity: O(n^2), -/// - space complexity: O(n^2), +/// - Time complexity: `O(n^2)` +/// - Space complexity: `O(n)` /// -/// where n is the length of `p`. -pub fn rod_cut(p: &[usize]) -> usize { - let n = p.len(); - // f is the dynamic programming table - let mut f = vec![0; n]; - - for i in 0..n { - let mut max_price = p[i]; - for j in 1..=i { - max_price = max(max_price, p[j - 1] + f[i - j]); - } - f[i] = max_price; +/// where `n` is the number of different rod lengths considered. +pub fn rod_cut(prices: &[usize]) -> usize { + if prices.is_empty() { + return 0; } - // accomodate for input with length zero - if n != 0 { - f[n - 1] - } else { - 0 - } + (1..=prices.len()).fold(vec![0; prices.len() + 1], |mut max_profit, rod_length| { + max_profit[rod_length] = (1..=rod_length) + .map(|cut_position| prices[cut_position - 1] + max_profit[rod_length - cut_position]) + .fold(prices[rod_length - 1], |max_price, current_price| { + max(max_price, current_price) + }); + max_profit + })[prices.len()] } #[cfg(test)] mod tests { - use super::rod_cut; + use super::*; + + macro_rules! rod_cut_tests { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (input, expected_output) = $test_case; + assert_eq!(expected_output, rod_cut(input)); + } + )* + }; + } - #[test] - fn test_rod_cut() { - assert_eq!(0, rod_cut(&[])); - assert_eq!(15, rod_cut(&[5, 8, 2])); - assert_eq!(10, rod_cut(&[1, 5, 8, 9])); - assert_eq!(25, rod_cut(&[5, 8, 2, 1, 7])); - assert_eq!(87, rod_cut(&[0, 0, 0, 0, 0, 87])); - assert_eq!(49, rod_cut(&[7, 6, 5, 4, 3, 2, 1])); - assert_eq!(22, rod_cut(&[1, 5, 8, 9, 10, 17, 17, 20])); - assert_eq!(60, rod_cut(&[6, 4, 8, 2, 5, 8, 2, 3, 7, 11])); - assert_eq!(30, rod_cut(&[1, 5, 8, 9, 10, 17, 17, 20, 24, 30])); - assert_eq!(12, rod_cut(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])); + rod_cut_tests! { + test_empty_prices: (&[], 0), + test_example_with_three_prices: (&[5, 8, 2], 15), + test_example_with_four_prices: (&[1, 5, 8, 9], 10), + test_example_with_five_prices: (&[5, 8, 2, 1, 7], 25), + test_all_zeros_except_last: (&[0, 0, 0, 0, 0, 87], 87), + test_descending_prices: (&[7, 6, 5, 4, 3, 2, 1], 49), + test_varied_prices: (&[1, 5, 8, 9, 10, 17, 17, 20], 22), + test_complex_prices: (&[6, 4, 8, 2, 5, 8, 2, 3, 7, 11], 60), + test_increasing_prices: (&[1, 5, 8, 9, 10, 17, 17, 20, 24, 30], 30), + test_large_range_prices: (&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 12), + test_single_length_price: (&[5], 5), + test_zero_length_price: (&[0], 0), + test_repeated_prices: (&[5, 5, 5, 5], 20), + test_no_profit: (&[0, 0, 0, 0], 0), + test_large_input: (&[1; 1000], 1000), + test_all_zero_input: (&[0; 100], 0), + test_very_large_prices: (&[1000000, 2000000, 3000000], 3000000), + test_greedy_does_not_work: (&[2, 5, 7, 8], 10), } } diff --git a/src/machine_learning/loss_function/average_margin_ranking_loss.rs b/src/machine_learning/loss_function/average_margin_ranking_loss.rs new file mode 100644 index 00000000000..505bf2a94a7 --- /dev/null +++ b/src/machine_learning/loss_function/average_margin_ranking_loss.rs @@ -0,0 +1,113 @@ +/// Marginal Ranking +/// +/// The 'average_margin_ranking_loss' function calculates the Margin Ranking loss, which is a +/// loss function used for ranking problems in machine learning. +/// +/// ## Formula +/// +/// For a pair of values `x_first` and `x_second`, `margin`, and `y_true`, +/// the Margin Ranking loss is calculated as: +/// +/// - loss = `max(0, -y_true * (x_first - x_second) + margin)`. +/// +/// It returns the average loss by dividing the `total_loss` by total no. of +/// elements. +/// +/// Pytorch implementation: +/// https://pytorch.org/docs/stable/generated/torch.nn.MarginRankingLoss.html +/// https://gombru.github.io/2019/04/03/ranking_loss/ +/// https://vinija.ai/concepts/loss/#pairwise-ranking-loss +/// + +pub fn average_margin_ranking_loss( + x_first: &[f64], + x_second: &[f64], + margin: f64, + y_true: f64, +) -> Result { + check_input(x_first, x_second, margin, y_true)?; + + let total_loss: f64 = x_first + .iter() + .zip(x_second.iter()) + .map(|(f, s)| (margin - y_true * (f - s)).max(0.0)) + .sum(); + Ok(total_loss / (x_first.len() as f64)) +} + +fn check_input( + x_first: &[f64], + x_second: &[f64], + margin: f64, + y_true: f64, +) -> Result<(), MarginalRankingLossError> { + if x_first.len() != x_second.len() { + return Err(MarginalRankingLossError::InputsHaveDifferentLength); + } + if x_first.is_empty() { + return Err(MarginalRankingLossError::EmptyInputs); + } + if margin < 0.0 { + return Err(MarginalRankingLossError::NegativeMargin); + } + if y_true != 1.0 && y_true != -1.0 { + return Err(MarginalRankingLossError::InvalidValues); + } + + Ok(()) +} + +#[derive(Debug, PartialEq, Eq)] +pub enum MarginalRankingLossError { + InputsHaveDifferentLength, + EmptyInputs, + InvalidValues, + NegativeMargin, +} + +#[cfg(test)] +mod tests { + use super::*; + + macro_rules! test_with_wrong_inputs { + ($($name:ident: $inputs:expr,)*) => { + $( + #[test] + fn $name() { + let (vec_a, vec_b, margin, y_true, expected) = $inputs; + assert_eq!(average_margin_ranking_loss(&vec_a, &vec_b, margin, y_true), expected); + assert_eq!(average_margin_ranking_loss(&vec_b, &vec_a, margin, y_true), expected); + } + )* + } + } + + test_with_wrong_inputs! { + invalid_length0: (vec![1.0, 2.0, 3.0], vec![2.0, 3.0], 1.0, 1.0, Err(MarginalRankingLossError::InputsHaveDifferentLength)), + invalid_length1: (vec![1.0, 2.0], vec![2.0, 3.0, 4.0], 1.0, 1.0, Err(MarginalRankingLossError::InputsHaveDifferentLength)), + invalid_length2: (vec![], vec![1.0, 2.0, 3.0], 1.0, 1.0, Err(MarginalRankingLossError::InputsHaveDifferentLength)), + invalid_length3: (vec![1.0, 2.0, 3.0], vec![], 1.0, 1.0, Err(MarginalRankingLossError::InputsHaveDifferentLength)), + invalid_values: (vec![1.0, 2.0, 3.0], vec![2.0, 3.0, 4.0], -1.0, 1.0, Err(MarginalRankingLossError::NegativeMargin)), + invalid_y_true: (vec![1.0, 2.0, 3.0], vec![2.0, 3.0, 4.0], 1.0, 2.0, Err(MarginalRankingLossError::InvalidValues)), + empty_inputs: (vec![], vec![], 1.0, 1.0, Err(MarginalRankingLossError::EmptyInputs)), + } + + macro_rules! test_average_margin_ranking_loss { + ($($name:ident: $inputs:expr,)*) => { + $( + #[test] + fn $name() { + let (x_first, x_second, margin, y_true, expected) = $inputs; + assert_eq!(average_margin_ranking_loss(&x_first, &x_second, margin, y_true), Ok(expected)); + } + )* + } + } + + test_average_margin_ranking_loss! { + set_0: (vec![1.0, 2.0, 3.0], vec![2.0, 3.0, 4.0], 1.0, -1.0, 0.0), + set_1: (vec![1.0, 2.0, 3.0], vec![2.0, 3.0, 4.0], 1.0, 1.0, 2.0), + set_2: (vec![1.0, 2.0, 3.0], vec![1.0, 2.0, 3.0], 0.0, 1.0, 0.0), + set_3: (vec![4.0, 5.0, 6.0], vec![1.0, 2.0, 3.0], 1.0, -1.0, 4.0), + } +} diff --git a/src/machine_learning/loss_function/mod.rs b/src/machine_learning/loss_function/mod.rs index 0637d63e1ef..95686eb8c20 100644 --- a/src/machine_learning/loss_function/mod.rs +++ b/src/machine_learning/loss_function/mod.rs @@ -1,3 +1,4 @@ +mod average_margin_ranking_loss; mod hinge_loss; mod huber_loss; mod kl_divergence_loss; @@ -5,6 +6,7 @@ mod mean_absolute_error_loss; mod mean_squared_error_loss; mod negative_log_likelihood; +pub use self::average_margin_ranking_loss::average_margin_ranking_loss; pub use self::hinge_loss::hng_loss; pub use self::huber_loss::huber_loss; pub use self::kl_divergence_loss::kld_loss; diff --git a/src/machine_learning/mod.rs b/src/machine_learning/mod.rs index c9344a508ef..c77fd65116b 100644 --- a/src/machine_learning/mod.rs +++ b/src/machine_learning/mod.rs @@ -7,6 +7,7 @@ mod optimization; pub use self::cholesky::cholesky; pub use self::k_means::k_means; pub use self::linear_regression::linear_regression; +pub use self::loss_function::average_margin_ranking_loss; pub use self::loss_function::hng_loss; pub use self::loss_function::huber_loss; pub use self::loss_function::kld_loss; diff --git a/src/math/infix_to_postfix.rs b/src/math/infix_to_postfix.rs new file mode 100644 index 00000000000..123c792779d --- /dev/null +++ b/src/math/infix_to_postfix.rs @@ -0,0 +1,94 @@ +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum InfixToPostfixError { + UnknownCharacter(char), + UnmatchedParent, +} + +/// Function to convert [infix expression](https://en.wikipedia.org/wiki/Infix_notation) to [postfix expression](https://en.wikipedia.org/wiki/Reverse_Polish_notation) +pub fn infix_to_postfix(infix: &str) -> Result { + let mut postfix = String::new(); + let mut stack: Vec = Vec::new(); + + // Define the precedence of operators + let precedence = |op: char| -> u8 { + match op { + '+' | '-' => 1, + '*' | '/' => 2, + '^' => 3, + _ => 0, + } + }; + + for token in infix.chars() { + match token { + c if c.is_alphanumeric() => { + postfix.push(c); + } + '(' => { + stack.push('('); + } + ')' => { + while let Some(top) = stack.pop() { + if top == '(' { + break; + } + postfix.push(top); + } + } + '+' | '-' | '*' | '/' | '^' => { + while let Some(top) = stack.last() { + if *top == '(' || precedence(*top) < precedence(token) { + break; + } + postfix.push(stack.pop().unwrap()); + } + stack.push(token); + } + other => return Err(InfixToPostfixError::UnknownCharacter(other)), + } + } + + while let Some(top) = stack.pop() { + if top == '(' { + return Err(InfixToPostfixError::UnmatchedParent); + } + + postfix.push(top); + } + + Ok(postfix) +} + +#[cfg(test)] +mod tests { + use super::*; + + macro_rules! test_infix_to_postfix { + ($($name:ident: $inputs:expr,)*) => { + $( + #[test] + fn $name() { + let (infix, expected) = $inputs; + assert_eq!(infix_to_postfix(infix), expected) + } + )* + } + } + + test_infix_to_postfix! { + single_symbol: ("x", Ok(String::from("x"))), + simple_sum: ("x+y", Ok(String::from("xy+"))), + multiply_sum_left: ("x*(y+z)", Ok(String::from("xyz+*"))), + multiply_sum_right: ("(x+y)*z", Ok(String::from("xy+z*"))), + multiply_two_sums: ("(a+b)*(c+d)", Ok(String::from("ab+cd+*"))), + product_and_power: ("a*b^c", Ok(String::from("abc^*"))), + power_and_product: ("a^b*c", Ok(String::from("ab^c*"))), + product_of_powers: ("(a*b)^c", Ok(String::from("ab*c^"))), + product_in_exponent: ("a^(b*c)", Ok(String::from("abc*^"))), + regular_0: ("a-b+c-d*e", Ok(String::from("ab-c+de*-"))), + regular_1: ("a*(b+c)+d/(e+f)", Ok(String::from("abc+*def+/+"))), + regular_2: ("(a-b+c)*(d+e*f)", Ok(String::from("ab-c+def*+*"))), + unknown_character: ("(a-b)*#", Err(InfixToPostfixError::UnknownCharacter('#'))), + unmatched_paren: ("((a-b)", Err(InfixToPostfixError::UnmatchedParent)), + } +} diff --git a/src/math/mod.rs b/src/math/mod.rs index 0e225808e6a..b23e2f1faa7 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -34,6 +34,7 @@ mod gcd_of_n_numbers; mod geometric_series; mod greatest_common_divisor; mod huber_loss; +mod infix_to_postfix; mod interest; mod interpolation; mod interquartile_range; @@ -122,6 +123,7 @@ pub use self::greatest_common_divisor::{ greatest_common_divisor_stein, }; pub use self::huber_loss::huber_loss; +pub use self::infix_to_postfix::infix_to_postfix; pub use self::interest::{compound_interest, simple_interest}; pub use self::interpolation::{lagrange_polynomial_interpolation, linear_interpolation}; pub use self::interquartile_range::interquartile_range;