So I’ve been working on a problem, and the basic premise is that given a grid of any size, I need to calculate the number of “tours”. A tour being a run that starts at the top left point (which I use the point x=1, y=1 for) and ends at the bottom left point (x=1 y=max, whatever the max for ‘y’ is). In addition to this, it has to touch every other point along the way, and can only visit any point in the grid once.
The way I have it written below, it runs in ~42-45 seconds, but I’d like to get it to run in 30 seconds or less, if possible. So, my question to you, what can I change or take out that will make it run faster?
I’ve tried making everything not static and instantiating the class (which added a few seconds to my run time).
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.Date;
public class CodingPuzzle
{
public static List<Point> lAllPoints = new ArrayList<Point>();
public static int iMaxX;
public static int iMaxY;
public static int iCompletePaths = 0;
public static void depthFirstSearch(Point current, Stack<Point> result)
{
if (result.contains(current))
return;
result.push(current);
if (current.x == 1 && current.y == iMaxY && result.size() == iMaxX*iMaxY)
{
// This is a complete path
iCompletePaths++;
}
for (Point p: getPossibleMoves(current))
{
depthFirstSearch(p, result);
}
// No path was found
result.pop();
return;
}
public static List<Point> getPossibleMoves (Point fromPoint)
{
int iCurrentPointIndex = lAllPoints.indexOf(fromPoint);
List<Point> lPossibleMoves = new ArrayList<Point>();
if (fromPoint.x == 1 && fromPoint.y == 1)
{
// Top left point
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + iMaxY));
}
else if (fromPoint.x == 1 && fromPoint.y == iMaxY)
{
// Bottom left point. Should always be the last point. No valid moves.
// If a path gets here before the end it shouldn't need to continue.
}
else if (fromPoint.x == iMaxX && fromPoint.y == 1)
{
// Top right point
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - iMaxY));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + 1));
}
else if (fromPoint.x == iMaxX && fromPoint.y == iMaxY)
{
// Bottom right point
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - iMaxY));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - 1));
}
else if (fromPoint.x == 1 && fromPoint.y != iMaxY)
{
// Any other point on the left side
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + iMaxY));
}
else if (fromPoint.x == iMaxX)
{
// Any other point on the right side
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - iMaxY));
}
else if (fromPoint.y == 1)
{
// Any other point on the top
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - iMaxY));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + iMaxY));
}
else if (fromPoint.y == iMaxY && fromPoint.x != 1)
{
// Any other point on the bottom
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - iMaxY));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + iMaxY));
}
else
{
// Any other point not on an edge.
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - 1));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex - iMaxY));
lPossibleMoves.add(lAllPoints.get(iCurrentPointIndex + iMaxY));
}
return lPossibleMoves;
}
public static void setUpGrid(int x, int y)
{
iMaxX = x;
iMaxY = y;
for (int i = 1; i <= x; i++)
for (int j = 1; j <= y; j++)
lAllPoints.add(new Point(i, j));
}
public static void main(String[] args)
{
Date start = new Date(System.currentTimeMillis());
setUpGrid(10, 4);
Stack<Point> sCurrentPoints = new Stack<Point>();
depthFirstSearch(lAllPoints.get(0), sCurrentPoints);
Date end = new Date(System.currentTimeMillis());
long total = end.getTime() - start.getTime();
System.out.println(iCompletePaths + " paths found in " + total/1000 + " seconds.");
}
What others say. It will help you to become a better programmer.
Other than that, here’s the quick win you’re looking for – switch from
Stackto Deque, namely ArrayDeque. This single change made it 44 % faster on myJava 7 -servermachine.