![]() ![]() Consider the maximal substrings of Q containing only x and y. By the algorithm's choice, x is at least as frequent in Q as y. Suppose first that P does not end with y. Otherwise, let P' = Px and S = PQ and Q = yQ', where x and y are items and Q and Q' are sequences. Otherwise, let P be the (safe) prefix just before the step under consideration, let P' be the prefix just after, and let S be an optimal solution extending P. The only way that a greedy step introduces a defect is if only one item type remains, in which case there is only one way to continue, and that way is safe. It suffices to show inductively that each greedy step maintains safety. Clearly the empty prefix is safe, and if a safe prefix is a whole solution then that solution is optimal. We call a prefix (partial solution) safe if it extends to an optimal solution. ![]() This greedy algorithm returns optimal solutions, by the following logic. The others are symmetric each instance of the minority element prevents at most two defects out of a total of k1 + k2 - 1 possible. Heapq.heappush(heap, (minuscount1, key1))įor two item types, with counts k1 and k2, the optimal solution has k2 - k1 - 1 defects if k1 k2. Heapq.heappush(heap, (minuscount2, key2)) (See also Coady's implementation of this algorithm.) import collections The idea is to take the most frequent of the remaining item types unless it was just taken. This is along the lines of Thijser's currently incomplete pseudocode. Additional points to David for the correctness proof. and David also answered the bonus question (generate all permutations). Eisenstat, and provided functions that generate the best possible permutation flawlessly. UPD: Choosing a winner here was a tough choice, because many people posted excellent answers. This is the code I'm using to test the solutions: Since the lists are large, generating and filtering all permutations is not an option.īonus question: how to generate all such permutations efficiently? More formally, given a list a, generate a permutation p of it that minimizes the number of pairs p=p. How can I achieve the opposite task - shuffle the list so that equal elements are never (or as seldom as possible) adjacent?įor example, for the above list one of the possible solutions is p = Set j = last-2 and find first j such that a =.When we sort a list, like a = Įqual elements are always adjacent in the resulting list. So 6 is next larger and 2345(least using numbers other than 6) Find lexicogrpahically least way to extend the new aĬonsider example array state of for sorted Īfter 56432(treat as number) ->nothing larger than 6432(using 6,4,3,2) beginning with 5. ![]() Increase a by smallest feasible amount.Find largest j such that a can be increased.There are n! permutations at most and hasNextPermutation(.) runs in O(n) time complexity This is the asymptotically optimal way O(n*n!) of generating permutations after initial sorting. It was enough when I needed it, but it's no itertools.permutations by a long shot. ![]() This method is non-recursive, but it is slightly slower on my computer and xrange raises an error when n! is too large to be converted to a C long integer (n=13 for me). NewPermutation.append(available.pop(index)) This way the numbers 0 through n!-1 correspond to all possible permutations in lexicographic order. You have n choices for the first item, n-1 for the second, and only one for the last, so you can use the digits of a number in the factorial number system as the indices. I used an algorithm based on the factorial number system- For a list of length n, you can assemble each permutation item by item, selecting from the items left at each stage. Indices, indices = indices, indicesĪnd another, based on itertools.product: def permutations(iterable, r=None):įor indices in product(range(n), repeat=r): If len(elements) AB AC AD BA BC BD CA CB CD DA DB DC Use itertools.permutations from the standard library: import itertoolsĪdapted from here is a demonstration of how itertools.permutations might be implemented: def permutations(elements): ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |