Tower of Hanoi Sort
Java - optimal solution (1080544 moves)
This solution builds a shortest path tree from the target and backwards, then traverses the path from the initial state to the target. Lots of room for speed improvement, but it still completes all 55986 problems in about a minute.
Assuming the algorithm is correctly implemented, this should be the theoretically best solution.
import java.util.*;
public class HanoiSort {
public static void main(String[] args) {
int sumNumMoves = 0;
for (int size = 1; size <= 6; ++size) {
Collection<List<Integer>> initMainStacks = generateInitMainStacks(Collections.<Integer>emptyList(), size);
for (List<Integer> initMainStack : initMainStacks) {
sumNumMoves += solve(initMainStack);
}
}
System.out.println(sumNumMoves);
}
/*
* Recursively create initial main stacks
*/
private static Collection<List<Integer>> generateInitMainStacks(List<Integer> mainStack, int remainingSize) {
Collection<List<Integer>> initMainStacks;
if (remainingSize > 0) {
initMainStacks = new ArrayList<>();
for (int number = 1; number <= 6; ++number) {
List<Integer> nextMainStack = new ArrayList<>(mainStack);
nextMainStack.add(number);
initMainStacks.addAll(generateInitMainStacks(nextMainStack, remainingSize - 1));
}
} else {
List<Integer> initMainStack = new ArrayList<>(mainStack);
initMainStacks = Collections.singleton(initMainStack);
}
return initMainStacks;
}
private static final List<Integer> EMPTY_STACK = Collections.emptyList();
/*
* Create a shortest path tree, starting from the target state (sorted main stack). Break when the initial state
* is found, since there can be no shorter path. This is akin to building a chess endgame tablebase.
*
* Traverse the path from initial state to the target state to count the number of moves.
*/
private static int solve(List<Integer> initMainStack) {
List<List<Integer>> initState = Arrays.asList(new ArrayList<>(initMainStack), EMPTY_STACK, EMPTY_STACK);
List<Integer> targetMainStack = new ArrayList<>(initMainStack);
Collections.sort(targetMainStack);
List<List<Integer>> targetState = Arrays.asList(new ArrayList<>(targetMainStack), EMPTY_STACK, EMPTY_STACK);
Map<List<List<Integer>>,List<List<Integer>>> tablebase = new HashMap<>();
Deque<List<List<Integer>>> workQueue = new ArrayDeque<>();
tablebase.put(targetState, null);
workQueue.add(targetState);
while (!tablebase.containsKey(initState)) {
assert !workQueue.isEmpty() : initState.toString();
List<List<Integer>> state = workQueue.removeFirst();
Collection<List<List<Integer>>> prevStates = calcPrevStates(state);
for (List<List<Integer>> prevState : prevStates) {
if (!tablebase.containsKey(prevState)) {
tablebase.put(prevState, state);
workQueue.add(prevState);
}
}
}
int numMoves = 0;
List<List<Integer>> state = tablebase.get(initState);
while (state != null) {
++numMoves;
state = tablebase.get(state);
}
return numMoves;
}
/*
* Given a state, calculate all possible previous states
*/
private static Collection<List<List<Integer>>> calcPrevStates(List<List<Integer>> state) {
Collection<List<List<Integer>>> prevStates = new ArrayList<>();
for (int fromStackNo = 0; fromStackNo < 3; ++fromStackNo) {
List<Integer> fromStack = state.get(fromStackNo);
if (!fromStack.isEmpty()) {
int number = fromStack.get(0);
for (int toStackNo = 0; toStackNo < 3; ++toStackNo) {
if (toStackNo != fromStackNo) {
List<Integer> toStack = state.get(toStackNo);
if ((toStackNo == 0) || toStack.isEmpty() || (toStack.get(0) >= number)) {
List<List<Integer>> prevState = new ArrayList<>(state);
List<Integer> prevFromStack = new ArrayList<>(fromStack);
prevFromStack.remove(0);
prevState.set(fromStackNo, prevFromStack);
List<Integer> prevToStack = new ArrayList<>(toStack);
prevToStack.add(0, number);
prevState.set(toStackNo, prevToStack);
prevStates.add(prevState);
}
}
}
}
}
return prevStates;
}
}
Python, 3983838 3912258 moves over 55986 inputs
This is very inefficient.
I'll add the total number of moves after the OP clarifies whether they are for all of those cases or for specific other cases.
from itertools import product # Required for testing
import time # Required if you want to see it in action.
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
def main( l ):
################### Data ###################
global a , b , c , d , copy , total_moves
total_moves = 0
a = [ x for x in l ] # Input stack, order doesn't matter, we'll try to empty this one
b = [] # Usable stack, restricted by order, we'll try to get the final sorted order here
c = [] # Usable stack, restricted by order, but we'll try to keep it as empty as possible
d = { 'a':a , 'b':b , 'c':c } # Passing the stacks to the nested functions by their names as a string
copy = [ x for x in a ] # reference copy, read-only
################### Functions ###################
def is_correct( stack ):
if d[ stack ] == sorted( copy , reverse = True ):
return True
else:
return False
def reverse_exchange( source , destination , keep = 0 ):
#
# keep is the number of elements to keep at the bottom of the source stack
# The remaining top elements are moved to the destination stack
# We first move the top elements to stack a
# and from a to c so that the order is preserved
# effectively splitting the source stack into two
#
i = 0
while len( d[ source ] ) > keep :
move( source , 'a' )
i += 1
else:
while i > 0:
move( 'a' , destination )
i -= 1
# def validate( source , destination ):
# #
# # Validates the give move
# #
# if len( d[ source ] ) == 0:
# return False
# if destination == 'a' or len( d[ destination ] ) == 0:
# return True
# else:
# if d[ destination ][ len( d[ destination ] ) - 1 ] >= d[ source ][ len( d[ source ] ) - 1 ]:
# return True
# else:
# return False
def move( source , destination ):
global total_moves
# if validate( source , destination ):
d[ destination ].append( d[ source ].pop() )
total_moves += 1
# Uncomment the following to view the workings step-by-step
# print '\n'
# print a
# print b
# print c
# print '\n'
# time.sleep(0.1)
return True
# else:
# return False
################### Actual logic ###################
while ( not is_correct( 'a' ) ):
copy_b = [x for x in b ] # Checking where the topmost element of a
top_of_a = a[ len(a) - 1 ] # should be inserted
copy_b.append( top_of_a ) #
sorted_copy_b = sorted( copy_b , reverse = True ) #
reverse_exchange( 'b' , 'c' , sorted_copy_b.index( top_of_a ) ) # Sandwiching the top-most element
move( 'a' , 'b' ) # to proper position in b
while (len(b) > 0 and len(c) > 0 and len(a) > 0) and (sorted (b , reverse = True)[0] <= a[len(a) - 1] <= c[0]): # # Optimization
move( 'a' , 'b' ) # #
reverse_exchange( 'c' , 'b' ) # b is always sorted, c is always empty
if is_correct( 'b' ): # Just moving b to a
while ( not is_correct( 'a' ) ): # The entire program focuses on "insertion sorting"
reverse_exchange( 'b' , 'c' , 1 ) # elements of a onto b while keeping c empty
move( 'b' , 'a' ) #
if len(c) > 0 : #
reverse_exchange( 'c' , 'b' , 1 ) # Slightly more efficient
move('c' , 'a' ) #
return total_moves
# with PyCallGraph( output= GraphvizOutput() ):
################### Test cases #############
i = 0
for elements in xrange( 1 , 7 ):
for cartesian_product in product( [ 1 , 2 , 3 , 4 , 5 , 6 ] , repeat = elements ):
input_list = [ int( y ) for y in cartesian_product ]
i += main(input_list)
print i
print i
Explanation
What, comments not good enough for you?
Note to OP: Thanks for not making this code-golf.
C - 2547172 for 55986 inputs
There is a lot of room for improvement here. For my own sanity, I simplified this so that it was only possible to inspect the top element of each stack. 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. A compelling example is that my implementation has worst case behavior if the main stack is already sorted.
Algorithm:
- Fill both auxiliary stacks (room for optimization here, possibly assigning to which stack based on some kind of pivot).
- Merge sort the auxiliary stacks back onto the main stack.
- Repeat 1-2 until the main stack is sorted (but in reverse).
- Reverse the main stack (more room for optimization, shuffling a lot of the same elements repeatedly).
Analysis:
- Additional space complexity is O(n) (for the two auxiliary stacks), which is good, since that was a requirement of the problem.
- Time complexity is O(n^2) by my count. Corrections are welcome.
#include <assert.h>
#include <stdio.h>
#define SIZE 6
int s0[SIZE + 1];
int s1[SIZE + 1];
int s2[SIZE + 1];
int
count(int *stack)
{
return stack[0];
}
int
top(int *stack)
{
return stack[stack[0]];
}
void
push(int *stack, int value)
{
assert(count(stack) < SIZE && "stack overflow");
assert((stack == s0 || count(stack) == 0 || value <= top(stack)) && "stack order violated");
stack[++stack[0]] = value;
}
int
pop(int *stack)
{
int result = stack[stack[0]];
assert(count(stack) > 0 && "stack underflow");
stack[stack[0]] = 0;
stack[0]--;
return result;
}
int permutations;
void
permute(int len, int range, void (*cb)(void))
{
int i;
if(len == 0)
{
permutations++;
cb();
return;
}
for(i = 1; i <= range; i++)
{
push(s0, i);
permute(len - 1, range, cb);
pop(s0);
}
}
void
print(void)
{
int i;
for(i = 1; i <= count(s0); i++)
{
printf("%d ", s0[i]);
}
printf("\n");
}
int save[SIZE + 1];
void
copy(int *src, int *dst)
{
int i;
for(i = 0; i <= SIZE; i++)
{
dst[i] = src[i];
}
}
int total;
void
move(int *src, int *dst)
{
total++;
push(dst, pop(src));
}
void
merge(void)
{
while(1)
{
if(count(s1) == 0 && count(s2) == 0)
{
break;
}
else if(count(s1) == 0 || (count(s2) > 0 && top(s2) < top(s1)))
{
move(s2, s0);
}
else
{
move(s1, s0);
}
}
}
void
reverse(void)
{
while(1)
{
while(count(s2) == 0 || top(s0) == top(s2))
{
move(s0, s2);
}
if(count(s0) == 0 || top(s2) < top(s0))
{
while(count(s2) > 0)
{
move(s2, s0);
}
break;
}
while(count(s0) > 0 && (count(s1) == 0 || top(s0) <= top(s1)))
{
move(s0, s1);
}
while(count(s2) > 0)
{
move(s2, s0);
}
merge();
}
}
void
sort(void)
{
while(1)
{
if(count(s0) == 0)
{
merge();
reverse();
break;
}
else if(count(s1) == 0 || top(s1) >= top(s0))
{
move(s0, s1);
}
else if(count(s2) == 0 || top(s2) >= top(s0))
{
move(s0, s2);
}
else
{
merge();
}
}
}
void
helper(void)
{
copy(s0, save);
sort();
copy(save, s0);
}
int
main(void)
{
permute(1, 6, helper);
permute(2, 6, helper);
permute(3, 6, helper);
permute(4, 6, helper);
permute(5, 6, helper);
permute(6, 6, helper);
printf("%d\n", permutations);
printf("%d\n", total);
return 0;
}