Hello,
I have a Graph of players. Each player has 2 neighbours, and there are circles in the graph. Each edge is of a different length (different weight for each edge).
Each player is an object of class Player.
I have to write a Java method called Route in class Player, that gets a destination player, and must find the shortest path to this destination.
I made a List to keep the founded path.
To find any path, I thought about an algorithm like this (pseudo code):
routeList.clear();
for (p : all players)
p.visited = false;
...
List<Player> Route(Player destination)
{
List<Player> tempList = new List<Player>;
tempList.clear();
if (destination player)
{
tempList.addfirst(this);
routeList.add(this);
return tempList;
}
if (!isVisited())
{
this.visited = true;
for (p : GetNeighbours)
{
tempList.addall(p.Route(destination));
if (!tempList.empty())
{
routeList.addfirst(p);
return tempList;
}
}
return tempList;
}
1) This algorithm for any path does not work properly for me. What is wrong?
2) Can you please help me with changing it and develop a java method that returns the SHORTEST path?
Thanks