-
Notifications
You must be signed in to change notification settings - Fork 505
Expand file tree
/
Copy pathpermutations.py
More file actions
255 lines (187 loc) · 6.12 KB
/
permutations.py
File metadata and controls
255 lines (187 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""
Author: ADWAITA JADHAV
Created On: 4th October 2025
Permutations Generator using Backtracking
Time Complexity: O(n! * n) where n is the length of the input
Space Complexity: O(n! * n)
Generate all possible permutations of a given list using backtracking.
"""
import inspect
def generate_permutations(arr):
"""
Generate all permutations of the given array using backtracking
:param arr: list of elements to permute
:return: list of all permutations
"""
if not arr:
return [[]]
result = []
def backtrack(current_perm, remaining):
"""Recursively generate permutations"""
if not remaining:
result.append(current_perm[:])
return
for i in range(len(remaining)):
# Choose
current_perm.append(remaining[i])
new_remaining = remaining[:i] + remaining[i+1:]
# Explore
backtrack(current_perm, new_remaining)
# Unchoose (backtrack)
current_perm.pop()
backtrack([], arr)
return result
def generate_permutations_iterative(arr):
"""
Generate all permutations using iterative approach with swapping
:param arr: list of elements to permute
:return: list of all permutations
"""
if not arr:
return [[]]
result = []
arr_copy = arr[:]
def generate(n):
if n == 1:
result.append(arr_copy[:])
return
for i in range(n):
generate(n - 1)
# If n is odd, swap first and last element
if n % 2 == 1:
arr_copy[0], arr_copy[n-1] = arr_copy[n-1], arr_copy[0]
# If n is even, swap ith and last element
else:
arr_copy[i], arr_copy[n-1] = arr_copy[n-1], arr_copy[i]
generate(len(arr))
return result
def generate_unique_permutations(arr):
"""
Generate all unique permutations of an array that may contain duplicates
:param arr: list of elements to permute (may contain duplicates)
:return: list of unique permutations
"""
if not arr:
return [[]]
result = []
arr_sorted = sorted(arr)
used = [False] * len(arr_sorted)
def backtrack(current_perm):
if len(current_perm) == len(arr_sorted):
result.append(current_perm[:])
return
for i in range(len(arr_sorted)):
# Skip used elements
if used[i]:
continue
# Skip duplicates: if current element is same as previous
# and previous is not used, skip current
if i > 0 and arr_sorted[i] == arr_sorted[i-1] and not used[i-1]:
continue
# Choose
used[i] = True
current_perm.append(arr_sorted[i])
# Explore
backtrack(current_perm)
# Unchoose
current_perm.pop()
used[i] = False
backtrack([])
return result
def generate_k_permutations(arr, k):
"""
Generate all k-length permutations of the given array
:param arr: list of elements
:param k: length of each permutation
:return: list of k-length permutations
"""
if k > len(arr) or k < 0:
return []
if k == 0:
return [[]]
result = []
def backtrack(current_perm, remaining):
if len(current_perm) == k:
result.append(current_perm[:])
return
for i in range(len(remaining)):
# Choose
current_perm.append(remaining[i])
new_remaining = remaining[:i] + remaining[i+1:]
# Explore
backtrack(current_perm, new_remaining)
# Unchoose
current_perm.pop()
backtrack([], arr)
return result
def count_permutations(n, r=None):
"""
Count the number of permutations of n items taken r at a time
:param n: total number of items
:param r: number of items to choose (defaults to n)
:return: number of permutations
"""
if r is None:
r = n
if r > n or r < 0:
return 0
if r == 0:
return 1
result = 1
for i in range(n, n - r, -1):
result *= i
return result
def is_permutation(arr1, arr2):
"""
Check if arr2 is a permutation of arr1
:param arr1: first array
:param arr2: second array
:return: True if arr2 is a permutation of arr1, False otherwise
"""
if len(arr1) != len(arr2):
return False
# Count frequency of each element
freq1 = {}
freq2 = {}
for item in arr1:
freq1[item] = freq1.get(item, 0) + 1
for item in arr2:
freq2[item] = freq2.get(item, 0) + 1
return freq1 == freq2
def next_permutation(arr):
"""
Generate the next lexicographically greater permutation
:param arr: current permutation
:return: next permutation or None if current is the last
"""
if not arr or len(arr) <= 1:
return None
arr_copy = arr[:]
# Find the largest index i such that arr[i] < arr[i + 1]
i = len(arr_copy) - 2
while i >= 0 and arr_copy[i] >= arr_copy[i + 1]:
i -= 1
# If no such index exists, this is the last permutation
if i == -1:
return None
# Find the largest index j such that arr[i] < arr[j]
j = len(arr_copy) - 1
while arr_copy[j] <= arr_copy[i]:
j -= 1
# Swap arr[i] and arr[j]
arr_copy[i], arr_copy[j] = arr_copy[j], arr_copy[i]
# Reverse the suffix starting at arr[i + 1]
arr_copy[i + 1:] = reversed(arr_copy[i + 1:])
return arr_copy
def time_complexities():
"""
Return information on time complexity
:return: string
"""
return "Best Case: O(n! * n), Average Case: O(n! * n), Worst Case: O(n! * n)"
def get_code():
"""
Easily retrieve the source code of the generate_permutations function
:return: source code
"""
return inspect.getsource(generate_permutations)