This expression is specific to 6-cycles, and, as it involves several summations over elements of powers of the adjacency matrix, is rather cumbersome. Corrections are welcome.In, Chang and Fu derive an expression for c 6, the number of 6-cycles in a graph, by subtracting from the number of closed walks of length 6 all the closed walks that are not 6-cycles. Time complexity is O(n^2) by my count.Additional space complexity is O(n) (for the two auxiliary stacks), which is good, since that was a requirement of the problem.Reverse the main stack (more room for optimization, shuffling a lot of the same elements repeatedly).Repeat 1-2 until the main stack is sorted (but in reverse).Merge sort the auxiliary stacks back onto the main stack.Fill both auxiliary stacks (room for optimization here, possibly assigning to which stack based on some kind of pivot).A compelling example is that my implementation has worst case behavior if the main stack is already sorted. Lifting this self-imposed restriction would allow optimizations like determining the final order in advance and trying to minimize the number of moves required to achieve it. For my own sanity, I simplified this so that it was only possible to inspect the top element of each stack. There is a lot of room for improvement here. Note to OP: Thanks for not making this code-golf. # Test cases #įor cartesian_product in product(, repeat = elements ): # with P圜allGraph( output= GraphvizOutput() ): Reverse_exchange( 'c', 'b', 1 ) # Slightly more efficient While (len(b) > 0 and len(c) > 0 and len(a) > 0) and (sorted (b, reverse = True) 0 : # Move( 'a', 'b' ) # to proper position in b Reverse_exchange( 'b', 'c', sorted_copy_b.index( top_of_a ) ) # Sandwiching the top-most element Sorted_copy_b = sorted( copy_b, reverse = True ) # # Actual logic #Ĭopy_b = # Checking where the topmost element of a # Uncomment the following to view the workings step-by-step # effectively splitting the source stack into two # and from a to c so that the order is preserved # We first move the top elements to stack a # The remaining top elements are moved to the destination stack # keep is the number of elements to keep at the bottom of the source stack If d = sorted( copy, reverse = True ):ĭef reverse_exchange( source, destination, keep = 0 ): In other words, test your function/subroutine for # Passing the stacks to the nested functions by their names as a stringĬopy = # reference copy, read-only Test your function/subroutine for each permutation of the first 6 natural numbers. A single move is a pop from one stack which is pushed to another. Your function/subroutine can inspect any stack at any time, but it can only make a move by popping and pushing. However, if you for some reason cannot do so, you may simulate the mutable stacks, but remember that this is Tower of Hanoi sort there are only 3 pegs and only 1 peg may be unordered. Note: you must sort the main stack in place, no sorting onto another stack and calling that the answer. Your function/subroutine will return (or equivalent) the number of moves it made in sorting the stack. You are tasked with sorting the main stack in place, putting the largest integers underneath. These helper stacks have a unique property though: every element must be smaller than or the same size as the element underneath it. You are also given two more helper stacks. Write a function/subroutine to sort a list of integers, Tower of Hanoi style.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |