Guest User

Untitled

a guest
Oct 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class indivisualvertex implements Comparable<indivisualvertex> {
  2.  
  3. int data; //data associated with the vertex
  4. double distanceFromSource; //will store the distances from the source vertex
  5. int previousVertexOnShortestPathFromSource;//will store the previous vertex on the path
  6. neighbours adjacentPointsList; // neighbouring vertices stored in a linked list
  7.  
  8.  
  9.  
  10. //for sorting the vertices in the priority queue according to their distances from the source
  11. public int compareTo(indivisualvertex other) {
  12. if (this.equals(other)) {
  13. return 0;
  14. } else if (this.distanceFromSource > other.distanceFromSource) {
  15. return 1;
  16. } else {
  17. return -1;
  18. }
  19.  
  20. }
  21. }
  22.  
  23. private void relax(indivisualvertex indx) {
  24.  
  25. //traverse the neighbouring edges
  26. for (neighbours nbr = indx.adjacentPointsList; nbr != null; nbr = nbr.next){
  27.  
  28. //condition where a vertex is unvisited and also provides a shorter path
  29. if (visited[nbr.pointer_of_vertex] == false && vertices[nbr.pointer_of_vertex].distanceFromSource > vertices[nbr.from].distanceFromSource + nbr.weight) {
  30. pq.remove(vertices[nbr.pointer_of_vertex]);
  31. vertices[nbr.pointer_of_vertex].distanceFromSource = vertices[nbr.from].distanceFromSource + nbr.weight;
  32. vertices[nbr.pointer_of_vertex].previousVertexOnShortestPathFromSource = nbr.from;
  33. pq.add(vertices[nbr.pointer_of_vertex]);
  34. }
  35. }
  36. }
Add Comment
Please, Sign In to add comment