Reputation: 13
I was trying to make a route from a starting point(green) to a finishing point(red) as shown in the photo below.
I've made one random algorithm to make the route,
also a list with connected vertices to each vertex. like,, [a] connected to points(c,d)
path = list of points(x,y),
#path =10
s_point = get starting point(x,y)
end_Point = get end point(x,y)
for(int i=0;i<=#paths;i++){
path.add(s_point)
checked_points.add(s_point)
next_point = s_point
while(next_point.not_equals(end_point)){
_point[] = get points connected to (next_point)
while(_point[random()].not checked){
path.add(_point[random()])
next_point = _point[random()]
}
}
}
get_use_of_the_shortest_path()
so, the question is, how to replace the random method, to a method that should guide the path directly to the end point (considering the different paths available)??? or any information regarding how to make a route algorithms.
Upvotes: 1
Views: 184
Reputation: 374
Considering the paths do not have weights on them (like google maps for example which has information regarding traffic and speed limits on their roads) i would recommend BFS algorithm which finds the route with the least edges
Here is a great implementation in Java of BFS as written by Aakash Hasija:
import java.io.*;
import java.util.*;
// This class represents a directed graph using adjacency list
// representation
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency Lists
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
// Function to add an edge into the graph
void addEdge(int v,int w)
{
adj[v].add(w);
}
// prints BFS traversal from a given source s
void BFS(int s)
{
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[V];
// Create a queue for BFS
LinkedList<Integer> queue = new LinkedList<Integer>();
// Mark the current node as visited and enqueue it
visited[s]=true;
queue.add(s);
while (queue.size() != 0)
{
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
// Driver method to
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");
g.BFS(2);
}
}
// This code is contributed by Aakash Hasija
Upvotes: 1
Reputation: 979
You could use the Manhattan distance from the goal as a heuristic to guide the path to the goal. Whatever value is closer is chosen.
Upvotes: 0