From 36ecbc75f829d2a921b90c3233afa9f0c804b5ef Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Tue, 14 May 2019 17:12:15 +0430 Subject: [PATCH 1/6] 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/6] 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 deb0cdcf35c646c3aefc6fbe58d64dddc6ffd4d8 Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Sat, 6 Jul 2019 12:28:59 +0430 Subject: [PATCH 3/6] Add all_subsequences to backtracking directory --- backtracking/all_subsequences.py | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 backtracking/all_subsequences.py diff --git a/backtracking/all_subsequences.py b/backtracking/all_subsequences.py new file mode 100644 index 000000000000..d868377234a8 --- /dev/null +++ b/backtracking/all_subsequences.py @@ -0,0 +1,42 @@ +''' + In this problem, we want to determine all possible subsequences + of the given sequence. We use backtracking to solve this problem. + + Time complexity: O(2^n), + where n denotes the length of the given sequence. +''' + + +def generate_all_subsequences(sequence): + create_state_space_tree(sequence, [], 0) + + +def create_state_space_tree(sequence, current_subsequence, index): + ''' + Creates a state space tree to iterate through each branch using DFS. + We know that each state has exactly two children. + It terminates when it reaches the end of the given sequence. + ''' + + if index == len(sequence): + print(current_subsequence) + return + + create_state_space_tree(sequence, current_subsequence, index + 1) + current_subsequence.append(sequence[index]) + create_state_space_tree(sequence, current_subsequence, index + 1) + current_subsequence.pop() + + +''' +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_subsequences(sequence) + +sequence = ["A", "B", "C"] +generate_all_subsequences(sequence) From c0cebeea0d7aaf961ab11517160ebedbd40f106a Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Sat, 6 Jul 2019 17:53:48 +0430 Subject: [PATCH 4/6] Fix time complexity --- backtracking/all_subsequences.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/all_subsequences.py b/backtracking/all_subsequences.py index d868377234a8..fdbc42ca679b 100644 --- a/backtracking/all_subsequences.py +++ b/backtracking/all_subsequences.py @@ -2,7 +2,7 @@ In this problem, we want to determine all possible subsequences of the given sequence. We use backtracking to solve this problem. - Time complexity: O(2^n), + Time complexity: O(2^n * n), where n denotes the length of the given sequence. ''' From d6815472097838eb85005367bface6df615251c6 Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Sat, 6 Jul 2019 17:54:44 +0430 Subject: [PATCH 5/6] Fix time complexity --- backtracking/all_permutations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 8c332970ba53..299b708fef4e 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -2,7 +2,7 @@ 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!), + Time complexity: O(n! * n), where n denotes the length of the given sequence. ''' From 9d2a2b370fba0ef8a333bbe584cce15feff677ca Mon Sep 17 00:00:00 2001 From: Andrii Siriak Date: Sat, 6 Jul 2019 18:25:10 +0300 Subject: [PATCH 6/6] Update backtracking/all_subsequences.py --- backtracking/all_subsequences.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/all_subsequences.py b/backtracking/all_subsequences.py index fdbc42ca679b..d868377234a8 100644 --- a/backtracking/all_subsequences.py +++ b/backtracking/all_subsequences.py @@ -2,7 +2,7 @@ In this problem, we want to determine all possible subsequences of the given sequence. We use backtracking to solve this problem. - Time complexity: O(2^n * n), + Time complexity: O(2^n), where n denotes the length of the given sequence. '''