job tessio job tessio Interview Question
job tessios job tessiosTeam: engineer
Country: United States
Interview Type: Written Test
A BFS is probably not the fastest for this case. Use A* to get the paths.
Also, your complexity for the TSP is very wrong. Unless I'm mistaken,TSP is a factorial computation so finding the optimal ordering of checkpoints is going to be quite difficult to compute ( O(18!) which will be 6,402,373,705,728,000 different paths). You're better off using some stochastic algorithm like an evolutionary algorithm or an ACO to get the checkpoint ordering.
This is in reply to zortlord's comment. If I am not mistaken, the worst case complexity of A* is O(E), the same as of the BFS. It is true that A* may be faster on average for 2 points, but (a) we have to find the distance between 1 and many points, and (b) the path finding itself is not bottleneck here, the TSP is.
Also, the complexity of Held-Karp algorithm solving TSP is indeed O(n^2 2^n). O(n!) is the complexity of the most straightforward approach and Held-Karp is much faster.
could not get output,,,,
class orienting
{
public:
void main();
//height of map
const int mapHeight =6;
//width of map
const int mapWidth = 8;
// representation of map
char map[mapHeight][mapWidth + 1] =
{
"########",
"#@....G#",
"##.##@##",
"#..@..S#",
"#@.....#",
"########",
};
const char closeblock = '#';
const char openblock= '.';
const char checkpoint = '@';
class orienting::main(start.x,start.y)
{
public:
int X;
int Y;
orienting(int x = 0, int y = 0) { X = x, Y = y; }
orienting(const orientinggame &orientinggame)
{
X = orienting.X;// orienting position of the move
Y = orienting.Y;//orienting position of the move
}
};
orienting.start(4,7);
orienting.goal(2, 7);
void displaymap()
{
for (int Y = 0; Y < mapHeight; Y++)
{
for(int X=0;x<mapWidth;X++)
{
cout<<"The shortest path from start S(2,7) to goal G(4,7) is"<<map[Y][X]);
}
}
printf("\n");
}
bool Solve(int X, int Y)
{
// stop the searching progress in another direction
map[Y][X] =closeblock;
//progressive update of blocked positions that user cannot pass
displaymap();
sleep(50);
// move to the position if it is shortest path
map[y][x]=openblock;
displaymap();
// Make the move (if it's wrong, we will backtrack later.
map[Y][X] = checkpoint;
// for progressive update of map
displaymap();
Sleep(50);
// Check if we have reached our goal.
if (X == goal.X && Y == goal.Y)
{
return true;
}
// Recursively searching for our goal.
if (X > 0 && map[Y][X - 1] ==openblock && Solve(X - 1, Y))
{
return true;
}
if (X < mapWidth && map[Y][X + 1] ==openblock && Solve(X + 1, Y))
{
return true;
}
if (Y > 0 && map[Y - 1][X] == openblock && Solve(X, Y - 1))
{
return true;
}
if (Y < mapHeight && map[Y + 1][X] ==openblock && Solve(X, Y + 1))
{
return true;
}
// Otherwise we need to backtrack and find another solution.
map[Y][X] =openblock;
// for progressive update
display();
Sleep(50);
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
orienting o;
o.main(orienting.start,&orienting.goal);
if (Solve(start.X, start.Y))
{
displaymap();
}
else
{
printf("map does not exists\n");
}
return 0;
}
I would suggest to use BFS to find distance from start to every checkpoint, from every checkpoint to the end, and then between every pair of checkpoints. The complexity is O(x*n) , where x = 18, n = 10000.
- Maxim November 07, 2014Then it become travelling salesman problem with 18 towns with complexity O(x^2*2^x) which is roughly 3*10^8, quite feasible.
Can we do better?