• Post Reply Bookmark Topic Watch Topic
  • New Topic

shortestPathCost using Dijkstra's algorithm  RSS feed

 
El Bel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I'm trying to find the shortest path cost in a graph (between 2 countries), using a hashmap + priority queue. Not sure exactly where it's going wrong. Any suggestions appreciated, thanks!

 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 9 and 18 you are comparing strings with '==' rather than equals -> you initialize most likely all values with Integer.MAX_VALUE.
You currently do not use the map (you most likely want to store only vertices in your queue rather than Entry<Integer,Vertex> and use the map to find the current minimum).
I guess you want to remove the now replaced entry rather than removing the (already removed) 'current' entry in line 35.
Your for-loop in line 45 iterates over the entries of the queue after the while loop has finished -> queue is always empty.
Additionally you are checking within this for-loop whether a vertex is equal to toCountry (= String) -> would always be false.

you should try to remove 'dead' loops, e.g. your outer for-loop in the while-loop does nothing except
 
El Bel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
El Bel wrote:Hi there,
I'm trying to find the shortest path cost in a graph (between 2 countries), using a hashmap + priority queue. Not sure exactly where it's going wrong. Any suggestions appreciated, thanks!

Edit:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!