diff --git a/DIRECTORY.md b/DIRECTORY.md index 477a6ebe..7c98f202 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1,133 +1,148 @@ - ## Association Algorithms - * [Apriori](https://github.com/TheAlgorithms/R/blob/HEAD/association_algorithms/apriori.r) + +- [Apriori](https://github.com/TheAlgorithms/R/blob/HEAD/association_algorithms/apriori.r) ## Classification Algorithms - * [Decision Tree](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/decision_tree.r) - * [Gradient Boosting Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/gradient_boosting_algorithms.r) - * [Knn](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/knn.r) - * [Light Gbm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/light_gbm.r) - * [Logistic Regression](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression.r) - * [Logistic Regression 2](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression_2.r) - * [Naive Bayes](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/naive_bayes.r) - * [Random Forest](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/random_forest.r) - * [Svm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/svm.r) - * [Xgboost](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/xgboost.r) + +- [Decision Tree](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/decision_tree.r) +- [Gradient Boosting Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/gradient_boosting_algorithms.r) +- [Knn](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/knn.r) +- [Light Gbm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/light_gbm.r) +- [Logistic Regression](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression.r) +- [Logistic Regression 2](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression_2.r) +- [Naive Bayes](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/naive_bayes.r) +- [Random Forest](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/random_forest.r) +- [Svm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/svm.r) +- [Xgboost](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/xgboost.r) ## Clustering Algorithms - * [Dbscan Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/dbscan_clustering.r) - * [Gmm](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/gmm.r) - * [Heirarchical Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/heirarchical_clustering.r) - * [K Means](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/k_means.r) - * [Kmeans Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_clustering.r) - * [Kmeans Raw R](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_raw_r.r) - * [Pam](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/pam.r) + +- [Dbscan Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/dbscan_clustering.r) +- [Gmm](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/gmm.r) +- [Heirarchical Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/heirarchical_clustering.r) +- [K Means](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/k_means.r) +- [Kmeans Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_clustering.r) +- [Kmeans Raw R](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_raw_r.r) +- [Pam](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/pam.r) ## Data Manipulation - * [Label Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/label_encode.r) - * [One Hot Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/one_hot_encode.r) - * [Shorten.Vector](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/shorten.vector.r) + +- [Label Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/label_encode.r) +- [One Hot Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/one_hot_encode.r) +- [Shorten.Vector](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/shorten.vector.r) ## Data Preprocessing - * [Data Normalization Standardization](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_normalization_standardization.r) - * [Data Processing](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_processing.r) - * [Dimensionality Reduction Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/dimensionality_reduction_algorithms.r) - * [K Folds](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/k_folds.r) - * [Lasso](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/lasso.r) + +- [Data Normalization Standardization](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_normalization_standardization.r) +- [Data Processing](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_processing.r) +- [Dimensionality Reduction Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/dimensionality_reduction_algorithms.r) +- [K Folds](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/k_folds.r) +- [Lasso](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/lasso.r) ## Data Structures - * [Binary Search Tree](https://github.com/TheAlgorithms/R/blob/HEAD/data_structures/binary_search_tree.r) + +- [Binary Search Tree](https://github.com/TheAlgorithms/R/blob/HEAD/data_structures/binary_search_tree.r) ## Dynamic Programming - * 0 - * 0 - * [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/0/1_knapsack_problem.r) - * [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/1_knapsack_problem.r) - * [Coin Change](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/coin_change.r) - * [Longest Common Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_common_subsequence.r) - * [Longest Increasing Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_increasing_subsequence.r) + +- 0 + - 0 + - [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/0/1_knapsack_problem.r) + - [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/1_knapsack_problem.r) +- [Coin Change](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/coin_change.r) +- [Minimum Path Sum](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/minimum_path_sum.r) +- [Longest Common Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_common_subsequence.r) +- [Longest Increasing Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_increasing_subsequence.r) +- [Unique Paths](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/unique_paths.r) ## Graph Algorithms - * [Breadth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/breadth_first_search.r) - * [Depth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/depth_first_search.r) - * [Dijkstra Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/dijkstra_shortest_path.r) - * [Bellman-Ford Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/bellman_ford_shortest_path.r) + +- [Breadth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/breadth_first_search.r) +- [Depth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/depth_first_search.r) +- [Dijkstra Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/dijkstra_shortest_path.r) +- [Bellman-Ford Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/bellman_ford_shortest_path.r) ## Mathematics - * [Amicable Numbers](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/amicable_numbers.r) - * [Armstrong Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/armstrong_number.r) - * [Bisection Method](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/bisection_method.r) - * [Euclidean Distance](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/euclidean_distance.r) - * [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/extended_euclidean_algorithm.r) - * [Factorial](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/factorial.r) - * [Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/fibonacci.r) - * [First N Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/first_n_fibonacci.r) - * [Greatest Common Divisor](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/greatest_common_divisor.r) - * [Josephus Problem](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/josephus_problem.r) - * [Least Common Multiple](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/least_common_multiple.r) - * [Perfect Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_number.r) - * [Perfect Square](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_square.r) - * [Permutation Calculation](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/permutation_calculation.r) - * [Pi Monte Carlo](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/pi_monte_carlo.r) - * [Prime](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/prime.r) - * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/sieve_of_eratosthenes.r) + +- [Amicable Numbers](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/amicable_numbers.r) +- [Armstrong Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/armstrong_number.r) +- [Bisection Method](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/bisection_method.r) +- [Euclidean Distance](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/euclidean_distance.r) +- [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/extended_euclidean_algorithm.r) +- [Factorial](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/factorial.r) +- [Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/fibonacci.r) +- [First N Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/first_n_fibonacci.r) +- [Greatest Common Divisor](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/greatest_common_divisor.r) +- [Josephus Problem](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/josephus_problem.r) +- [Least Common Multiple](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/least_common_multiple.r) +- [Perfect Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_number.r) +- [Perfect Square](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_square.r) +- [Permutation Calculation](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/permutation_calculation.r) +- [Pi Monte Carlo](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/pi_monte_carlo.r) +- [Prime](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/prime.r) +- [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/sieve_of_eratosthenes.r) ## Quantitative Finance - * [Kalman Filter](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/kalman_filter.r) - * [Markowitz Portfolio Optimization](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/markowitz_portfolio_optimization.r) - * [Monte Carlo Simulation](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/monte_carlo_simulation.r) + +- [Kalman Filter](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/kalman_filter.r) +- [Markowitz Portfolio Optimization](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/markowitz_portfolio_optimization.r) +- [Monte Carlo Simulation](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/monte_carlo_simulation.r) ## Regression Algorithms - * [Ann](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/ann.r) - * [Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linear_regression.r) - * [Linearregressionrawr](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linearregressionrawr.r) - * [Multiple Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/multiple_linear_regression.r) + +- [Ann](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/ann.r) +- [Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linear_regression.r) +- [Linearregressionrawr](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linearregressionrawr.r) +- [Multiple Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/multiple_linear_regression.r) ## Searches - * [Binary Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/binary_search.r) - * [Linear Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/linear_search.r) - * [Rabin.Karp.String.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/rabin.karp.string.search.r) - * [Z.Algorithm.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/z.algorithm.search.r) + +- [Binary Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/binary_search.r) +- [Linear Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/linear_search.r) +- [Rabin.Karp.String.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/rabin.karp.string.search.r) +- [Z.Algorithm.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/z.algorithm.search.r) ## Sorting Algorithms - * [Binary Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/binary_insertion_sort.r) - * [Bubble Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bubble_sort.r) - * [Bucket Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bucket_sort.r) - * [Cocktail Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cocktail_sort.r) - * [Comb Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/comb_sort.r) - * [Counting Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/counting_sort.r) - * [Cycle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cycle_sort.r) - * [Gnome Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/gnome_sort.r) - * [Heap Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/heap_sort.r) - * [Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/insertion_sort.r) - * [Merge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/merge_sort.r) - * [Odd Even Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/odd_even_sort.r) - * [Pancake Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/pancake_sort.r) - * [Patience Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/patience_sort.r) - * [Quick Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/quick_sort.r) - * [Radix Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/radix_sort.r) - * [Selection Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/selection_sort.r) - * [Shell Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/shell_sort.r) - * [Stooge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/stooge_sort.r) - * [Strand Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/strand_sort.r) - * [Tim Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/tim_sort.r) - * [Topological Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/topological_sort.r) - * [Wiggle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/wiggle_sort.r) + +- [Binary Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/binary_insertion_sort.r) +- [Bubble Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bubble_sort.r) +- [Bucket Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bucket_sort.r) +- [Cocktail Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cocktail_sort.r) +- [Comb Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/comb_sort.r) +- [Counting Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/counting_sort.r) +- [Cycle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cycle_sort.r) +- [Gnome Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/gnome_sort.r) +- [Heap Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/heap_sort.r) +- [Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/insertion_sort.r) +- [Merge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/merge_sort.r) +- [Odd Even Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/odd_even_sort.r) +- [Pancake Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/pancake_sort.r) +- [Patience Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/patience_sort.r) +- [Quick Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/quick_sort.r) +- [Radix Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/radix_sort.r) +- [Selection Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/selection_sort.r) +- [Shell Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/shell_sort.r) +- [Stooge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/stooge_sort.r) +- [Strand Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/strand_sort.r) +- [Tim Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/tim_sort.r) +- [Topological Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/topological_sort.r) +- [Wiggle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/wiggle_sort.r) ## String Manipulation - * [Burrows](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/burrows.r) - * [Findpalindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/findpalindrome.r) - * [Hamming Distance](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/hamming_distance.r) - * [Is.Anagram](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.anagram.r) - * [Is.Lower](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.lower.r) - * [Is.Uppercase](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.uppercase.r) - * [Levenshtein](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/levenshtein.r) - * [Longest.Palindromic.Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.palindromic.subsequence.r) - * [Longest.Substring.No.Repeat](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.substring.no.repeat.r) - * [Manacher.Longest.Palindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/manacher.longest.palindrome.r) - * [Maskwords](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/maskwords.r) - * [Min.Palindromic.Insert](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/min.palindromic.insert.r) - * [Minimum.Window.Substring](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/minimum.window.substring.r) - * [Rearrangeways](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/rearrangeways.r) - * [Shortest.Common.Supersequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/shortest.common.supersequence.r) - * [Unique.Letters.Count](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/unique.letters.count.r) + +- [Burrows](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/burrows.r) +- [Findpalindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/findpalindrome.r) +- [Hamming Distance](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/hamming_distance.r) +- [Is.Anagram](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.anagram.r) +- [Is.Lower](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.lower.r) +- [Is.Uppercase](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.uppercase.r) +- [Levenshtein](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/levenshtein.r) +- [Longest.Palindromic.Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.palindromic.subsequence.r) +- [Longest.Substring.No.Repeat](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.substring.no.repeat.r) +- [Manacher.Longest.Palindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/manacher.longest.palindrome.r) +- [Maskwords](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/maskwords.r) +- [Min.Palindromic.Insert](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/min.palindromic.insert.r) +- [Minimum.Window.Substring](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/minimum.window.substring.r) +- [Rearrangeways](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/rearrangeways.r) +- [Shortest.Common.Supersequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/shortest.common.supersequence.r) +- [Unique.Letters.Count](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/unique.letters.count.r) diff --git a/dynamic_programming/unique_paths.r b/dynamic_programming/unique_paths.r new file mode 100644 index 00000000..38421870 --- /dev/null +++ b/dynamic_programming/unique_paths.r @@ -0,0 +1,227 @@ +# Unique Paths Problem (Grid Paths) +# +# Count the number of unique paths from the top-left corner (1,1) to the +# bottom-right corner (m,n) in an m x n grid when you can only move either +# right or down at any point in time. +# +# This is a classic dynamic programming problem. Below are three approaches: +# 1) Tabulation DP (O(m*n) time, O(m*n) space) +# 2) Space-optimized DP using a 1D array (O(m*n) time, O(n) space) +# 3) Top-down recursion with memoization (O(m*n) time, O(m*n) space) +# +# Additionally, we provide a variant that supports obstacles on the grid. +# +# Complexity +# Time complexity: O(m * n) +# Space complexity: O(m * n) for the standard/memo DP; O(n) for the optimized version +# +# Applications +# - Robot/grid movement counting +# - Combinatorics and path counting +# - Subproblems in DP interviews and contests + +# ------------------------------ +# 1) Tabulation DP (returns DP table for illustration) +# ------------------------------ +unique_paths_dp <- function(m, n) { + #' Count unique paths using 2D DP table + #' @param m: number of rows (positive integer) + #' @param n: number of columns (positive integer) + #' @return: list(count = paths count, dp_table = m x n integer matrix) + + if (m <= 0 || n <= 0) { + return(list(count = 0L, dp_table = matrix(0L, nrow = max(1, m), ncol = max(1, n)))) + } + + dp <- matrix(0L, nrow = m, ncol = n) + + # Base cases: first row and first column have exactly one path + dp[1, ] <- 1L + dp[, 1] <- 1L + + # Fill the rest: dp[i,j] = dp[i-1,j] + dp[i,j-1] + if (m > 1 && n > 1) { + for (i in 2:m) { + for (j in 2:n) { + dp[i, j] <- dp[i - 1, j] + dp[i, j - 1] + } + } + } + + list(count = dp[m, n], dp_table = dp) +} + +# ------------------------------ +# 2) Space-optimized DP using 1D array +# ------------------------------ +unique_paths_optimized <- function(m, n) { + #' Count unique paths using 1D DP array (O(n) space) + #' @param m: number of rows + #' @param n: number of columns + #' @return: integer count of unique paths + + if (m <= 0 || n <= 0) return(0L) + + # Ensure we iterate over the longer dimension in the outer loop only if desired. + # Here, we fix a 1D array of size n (columns) and iterate rows. + dp <- rep.int(1L, n) + if (m == 1L || n == 1L) return(1L) + + for (i in 2:m) { + for (j in 2:n) { + dp[j] <- dp[j] + dp[j - 1] + } + } + dp[n] +} + +# ------------------------------ +# 3) Top-down recursion with memoization (mirrors the user's Java approach) +# ------------------------------ +unique_paths_memo <- function(m, n) { + #' Count unique paths using recursion + memoization + #' @param m: number of rows + #' @param n: number of columns + #' @return: integer count of unique paths + + if (m <= 0 || n <= 0) return(0L) + + memo <- matrix(-1L, nrow = m, ncol = n) + + helper <- function(i, j) { + # i, j are 1-based indices + if (i > m || j > n) return(0L) + if (i == m && j == n) return(1L) + if (memo[i, j] != -1L) return(memo[i, j]) + + memo[i, j] <<- helper(i, j + 1L) + helper(i + 1L, j) + memo[i, j] + } + + helper(1L, 1L) +} + +# ------------------------------ +# Variant: Unique Paths with Obstacles +# Grid values: 0 = free cell, 1 = obstacle +# ------------------------------ +unique_paths_with_obstacles <- function(grid) { + #' Count unique paths with obstacles using DP + #' @param grid: m x n integer matrix (0 free, 1 obstacle) + #' @return: list(count = paths count, dp_table = m x n integer matrix) + + m <- nrow(grid) + n <- ncol(grid) + if (length(grid) == 0 || m == 0 || n == 0) { + return(list(count = 0L, dp_table = matrix(0L, nrow = 1, ncol = 1))) + } + + dp <- matrix(0L, nrow = m, ncol = n) + + # If start or end is blocked, no paths + if (grid[1, 1] == 1L || grid[m, n] == 1L) { + return(list(count = 0L, dp_table = dp)) + } + + dp[1, 1] <- 1L + + # First row + if (n > 1) { + for (j in 2:n) { + dp[1, j] <- if (grid[1, j] == 1L) 0L else dp[1, j - 1] + } + } + + # First column + if (m > 1) { + for (i in 2:m) { + dp[i, 1] <- if (grid[i, 1] == 1L) 0L else dp[i - 1, 1] + } + } + + # Rest cells + if (m > 1 && n > 1) { + for (i in 2:m) { + for (j in 2:n) { + if (grid[i, j] == 1L) { + dp[i, j] <- 0L + } else { + dp[i, j] <- dp[i - 1, j] + dp[i, j - 1] + } + } + } + } + + list(count = dp[m, n], dp_table = dp) +} + +# ------------------------------ +# Helpers for display +# ------------------------------ +print_unique_paths_dp <- function(dp_table) { + m <- nrow(dp_table) + n <- ncol(dp_table) + cat("DP Table for Unique Paths (values are number of paths to reach each cell):\n") + for (i in 1:m) { + cat(" ") + for (j in 1:n) { + cat(sprintf("%6d ", dp_table[i, j])) + } + cat("\n") + } + cat("\n") +} + +# =========================== +# Example Usage & Testing +# =========================== +cat("=== Unique Paths (Dynamic Programming) ===\n\n") + +# Test 1: Basic cases +cat("Test 1: Basic cases\n") +res_3x7 <- unique_paths_dp(3, 7) +cat("Grid 3x7 -> unique paths:", res_3x7$count, "\n") +print_unique_paths_dp(res_3x7$dp_table) + +res_3x7_opt <- unique_paths_optimized(3, 7) +cat("Optimized 3x7 -> unique paths:", res_3x7_opt, " | match:", res_3x7_opt == res_3x7$count, "\n\n") + +# Test 2: Edge cases +cat("Test 2: Edge cases\n") +cat("1x1 ->", unique_paths_optimized(1, 1), "\n") +cat("1x5 ->", unique_paths_optimized(1, 5), "\n") +cat("5x1 ->", unique_paths_optimized(5, 1), "\n\n") + +# Test 3: Memoized version parity +cat("Test 3: Memoized version parity\n") +memo_3x7 <- unique_paths_memo(3, 7) +cat("Memo 3x7 ->", memo_3x7, " | match:", memo_3x7 == res_3x7$count, "\n\n") + +# Test 4: Obstacles variant +cat("Test 4: Obstacles variant\n") +grid_obs <- matrix(c( + 0, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 0, 0 +), nrow = 3, byrow = TRUE) + +obs_res <- unique_paths_with_obstacles(grid_obs) +cat("Unique paths with obstacles:", obs_res$count, "\n") +print_unique_paths_dp(obs_res$dp_table) + +# Test 5: Quick performance comparison (optional) +if (requireNamespace("microbenchmark", quietly = TRUE)) { + cat("Test 5: Performance (microbenchmark)\n") + library(microbenchmark) + mb <- microbenchmark( + dp = unique_paths_dp(10, 10)$count, + opt = unique_paths_optimized(10, 10), + memo = unique_paths_memo(10, 10), + times = 50L + ) + print(summary(mb)) + cat("Results agree:", unique_paths_dp(10, 10)$count == unique_paths_optimized(10, 10) && + unique_paths_optimized(10, 10) == unique_paths_memo(10, 10), "\n\n") +} else { + cat("microbenchmark not installed; skipping performance test.\n\n") +}