From 36ecbc75f829d2a921b90c3233afa9f0c804b5ef Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Tue, 14 May 2019 17:12:15 +0430 Subject: [PATCH 1/4] Fix typo --- strings/manacher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/manacher.py b/strings/manacher.py index 9a44b19ba77a..e73e173b43e0 100644 --- a/strings/manacher.py +++ b/strings/manacher.py @@ -1,4 +1,4 @@ -# calculate palindromic length from center with incresmenting difference +# calculate palindromic length from center with incrementing difference def palindromic_length( center, diff, string): if center-diff == -1 or center+diff == len(string) or string[center-diff] != string[center+diff] : return 0 From ab9a6070bf7f032f12ea9e32abcc63d38611060c Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Sat, 6 Jul 2019 03:33:58 +0430 Subject: [PATCH 2/4] Add all_permutations algorithm to backtracking directory --- backtracking/all_permutations.py | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 backtracking/all_permutations.py diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py new file mode 100644 index 000000000000..8c332970ba53 --- /dev/null +++ b/backtracking/all_permutations.py @@ -0,0 +1,45 @@ +''' + In this problem, we want to determine all possible permutations + of the given sequence. We use backtracking to solve this problem. + + Time complexity: O(n!), + where n denotes the length of the given sequence. +''' + + +def generate_all_permutations(sequence): + create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) + + +def create_state_space_tree(sequence, current_sequence, index, index_used): + ''' + Creates a state space tree to iterate through each branch using DFS. + We know that each state has exactly len(sequence) - index children. + It terminates when it reaches the end of the given sequence. + ''' + + if index == len(sequence): + print(current_sequence) + return + + for i in range(len(sequence)): + if not index_used[i]: + current_sequence.append(sequence[i]) + index_used[i] = True + create_state_space_tree(sequence, current_sequence, index + 1, index_used) + current_sequence.pop() + index_used[i] = False + + +''' +remove the comment to take an input from the user + +print("Enter the elements") +sequence = list(map(int, input().split())) +''' + +sequence = [3, 1, 2, 4] +generate_all_permutations(sequence) + +sequence = ["A", "B", "C"] +generate_all_permutations(sequence) From c476210ae39850f6a5948ffa4d6a239cf533f068 Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Sat, 6 Jul 2019 18:16:04 +0430 Subject: [PATCH 3/4] Update backtracking and D&C algorithms in README Update backtracking and divide_and_conquer algorithms in README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a609dc077a77..a28475791432 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ We're on [Gitter](https://gitter.im/TheAlgorithms)! Please join us. - [N Queens](./backtracking/n_queens.py) - [Sum Of Subsets](./backtracking/sum_of_subsets.py) +- [All Subsequences](./backtracking/all_subsequences.py) +- [All Permutations](./backtracking/all_permutations.py) ## Ciphers @@ -220,7 +222,6 @@ We're on [Gitter](https://gitter.im/TheAlgorithms)! Please join us. ## Divide And Conquer - [Max Subarray Sum](./divide_and_conquer/max_subarray_sum.py) -- [Max Sub Array Sum](./divide_and_conquer/max_sub_array_sum.py) - [Closest Pair Of Points](./divide_and_conquer/closest_pair_of_points.py) ## Strings From 0b758499f535e253606a537be1e4491f0ea9edeb Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Sat, 6 Jul 2019 18:19:01 +0430 Subject: [PATCH 4/4] Remove the duplicated file --- divide_and_conquer/max_sub_array_sum.py | 72 ------------------------- 1 file changed, 72 deletions(-) delete mode 100644 divide_and_conquer/max_sub_array_sum.py diff --git a/divide_and_conquer/max_sub_array_sum.py b/divide_and_conquer/max_sub_array_sum.py deleted file mode 100644 index 531a45abca6f..000000000000 --- a/divide_and_conquer/max_sub_array_sum.py +++ /dev/null @@ -1,72 +0,0 @@ -""" -Given a array of length n, max_sub_array_sum() finds the maximum of sum of contiguous sub-array using divide and conquer method. - -Time complexity : O(n log n) - -Ref : INTRODUCTION TO ALGORITHMS THIRD EDITION (section : 4, sub-section : 4.1, page : 70) - -""" - - -def max_sum_from_start(array): - """ This function finds the maximum contiguous sum of array from 0 index - - Parameters : - array (list[int]) : given array - - Returns : - max_sum (int) : maximum contiguous sum of array from 0 index - - """ - array_sum = 0 - max_sum = float("-inf") - for num in array: - array_sum += num - if array_sum > max_sum: - max_sum = array_sum - return max_sum - - -def max_cross_array_sum(array, left, mid, right): - """ This function finds the maximum contiguous sum of left and right arrays - - Parameters : - array, left, mid, right (list[int], int, int, int) - - Returns : - (int) : maximum of sum of contiguous sum of left and right arrays - - """ - - max_sum_of_left = max_sum_from_start(array[left:mid+1][::-1]) - max_sum_of_right = max_sum_from_start(array[mid+1: right+1]) - return max_sum_of_left + max_sum_of_right - - -def max_sub_array_sum(array, left, right): - """ This function finds the maximum of sum of contiguous sub-array using divide and conquer method - - Parameters : - array, left, right (list[int], int, int) : given array, current left index and current right index - - Returns : - int : maximum of sum of contiguous sub-array - - """ - - # base case: array has only one element - if left == right: - return array[right] - - # Recursion - mid = (left + right) // 2 - left_half_sum = max_sub_array_sum(array, left, mid) - right_half_sum = max_sub_array_sum(array, mid + 1, right) - cross_sum = max_cross_array_sum(array, left, mid, right) - return max(left_half_sum, right_half_sum, cross_sum) - - -array = [-2, -5, 6, -2, -3, 1, 5, -6] -array_length = len(array) -print("Maximum sum of contiguous subarray:", max_sub_array_sum(array, 0, array_length - 1)) -