public class Intersection {
private static String intersect[]= new String[3];
public static void main(String args[])
{
// initialize Intersection
intersect[0]="A";
intersect[1]="B";
intersect[2]="C";
for (int i=0; i<intersect.length ;i++)
{System.out.println(intersect[i]);}
Intersection inter= new Intersection();
System.out.print(inter.getIndex("C"));
}
//get index of intersection
public int getIndex(String str)
{
for(int i=0;i<intersect.length;i++)
{
if(intersect[i].equalsIgnoreCase(str))
{
return i;
}
}
return -1;
}
}
public interface Graph {
//implement intersections and distance between them
public void add(Intersection start, Intersection end, int distance);
//get the shortest distance between two intersections
public int getDistance(Intersection start, Intersection end);
//store the list of intersection that lead to the given intersection.
public List <Intersection> getPredecessor(Intersection v);
That's how I implement the graph interface so far.
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;
public class Map implements Graph{
private final int[][]distances;
public Map(int intersect)
{
distances= new int[intersect][intersect];
}
//link two intersection by given distance
public void add(Intersection start, Intersection end, int distance)
{
distances[start.getIndex("A")][start.getIndex("B")]=distance;
}
public boolean isDestination(Intersection u){
}
public int getDistance(Intersection start, Intersection end)
{
return distances[start.getIndex("A")][end.getIndex("B")];
}
public List <Intersection> getPredecessor(Intersection v){
List<Intersection> list = new ArrayList<Intersection>();
for (int i = 0; i < distances.length; i++)
{
if (distances[i][v.getIndex("A")] > 0)
{
list.add( v );
}
}
return list;
}
}
There are two problems now. First one is I do not know how to continue to develop priority queue and second one is how to continue to implement Dijkstra's algorithm. I feel lost.
Your kind help is really appreciated