i am working in Dijkstra code.which i want to get the vertexes from user by using Array list.but there is a problem in computepath method i can't solve it.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("insert the number of Vertexes:");
int nv=input.nextInt();//nv = number of vertexes
Integer tmp;
HashMap<String , Vertex> h = new HashMap<String , Vertex>();
ArrayList <Vertex > a = new ArrayList<Vertex>();
for(Integer i = 0 ; i < nv ; i++){
String str;
str = "v"+Integer.toString(i);
Vertex buff = new Vertex(str);
//a[i] = new Vertex(str);
a.add(buff);
h.put(str, a.get(i));
str = "" ;
}
for(int j = 0 ; j < nv ; j++){
System.out.println("enter the number of adjacencies of v"+j);
int na=input.nextInt();//na = number of adjacencies
for(int i = 0 ; i < na ; i++){
String tmp2 = "";
double wi = 0;
System.out.println("enter the "+(i+1)+" th adjacence of v"+j+": ");
String buff = input.nextLine();
tmp2 = input.nextLine();
System.out.println("enter the weight of this adjacencies:");
wi = input.nextDouble();
a.get(j).adjacencies = new edge []{
new edge(h.get(tmp2) , wi)
};
}
}
computePaths(a.get(0));
System.out.println("pleas enter a vertex:");
int x=input.nextInt();
compute path method is this
public static void computePaths(Vertex source)
{
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
// Visit each edge exiting u
for (edge e : u.adjacencies)
{
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU ;
v.previous = u;
vertexQueue.add(v);
}
}
}}