Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. package Lab9;
  2. import java.util.*;
  3.  
  4. public class DijksrtAlgo{
  5.  
  6. public static void main(String[] args){
  7. Scanner myscanner = new Scanner(System.in);
  8. int size=myscanner.nextInt();
  9. int[][] array = new int[size][size];
  10.  
  11. for(int i=0;i<size;i++){ //load up the data into array
  12.  
  13. for(int j=0;j<size;j++){
  14.  
  15. array[i][j]=myscanner.nextInt();
  16.  
  17. }
  18. }
  19.  
  20. int furthestdistance=0; //keep track of the longest path found so far
  21. String furthestroute="";
  22.  
  23. for(int i=0;i<size;i++){ //loop through all possible starting positions
  24.  
  25. int visited=0; //how many vertices have been visited so far?
  26. Vertex[] vertices = new Vertex[size]; //initiate the vertex objects
  27.  
  28. for(int j=0;j<size;j++){
  29. vertices[j]=new Vertex(""+(char)(97+i));
  30. }
  31. vertices[i].visited=true; //we visit the starting position
  32. visited++; //we've visited one vertex so far
  33. vertices[i].distance=0; //set this vertex as starting point
  34.  
  35. for(int j=0;j<size;j++){ //update distances from starting point
  36.  
  37. if(array[j][i]>0){ //only update those that can be reached from here
  38.  
  39. vertices[j].distance=array[j][i];
  40.  
  41. }
  42. }
  43.  
  44. while(visited<size){ //while not all vertices visited
  45. int minvalue=Integer.MAX_VALUE; //set to infinity as default-there must be something shorter
  46. int minvertex=-1; //a default which will crash if it is not superseded
  47. for(int j=0;j<size;j++){ //find the next vertex to visit
  48. if(vertices[j].visited==false){ //if it has not been visited
  49. if(vertices[j].distance<minvalue){
  50. minvalue=vertices[j].distance;
  51. minvertex=j; //we are choosing the closest vertex to the starting position which has not been visited yet
  52. }
  53. }
  54. }
  55. vertices[minvertex].visited=true; //we have now visited it
  56. visited++;
  57.  
  58. for(int j=0;j<size;j++){ //update distances from this new point
  59.  
  60. if(array[j][minvertex]>0&&vertices[j].visited==false){ //if vertex is connected and not visited
  61.  
  62. if(vertices[j].distance>vertices[minvertex].distance+array[j][minvertex]){
  63.  
  64. vertices[j].distance=vertices[minvertex].distance+array[j][minvertex];
  65. //update if a new shorter route to this vertex has been found
  66. vertices[j].route=minvertex; //track the path taken
  67. }
  68. }
  69. }
  70. }
  71. for(int x=0;x<size;x++){ //check for a new record distance
  72. if(vertices[x].distance>furthestdistance){ //go through all distances in the graph
  73. furthestdistance=vertices[x].distance;
  74. int visiting=x;
  75. String solution=""; //build up the path taken to get this particular record beating distance
  76. while(visiting>=0){ //the starting position has route of -1
  77. solution=vertices[visiting].label+""+solution;
  78. visiting=vertices[visiting].route; //step back along the route generating this distance
  79. }
  80. solution=vertices[i].label+""+solution; //this is the starting position
  81. furthestroute=solution; //keep track of this record path
  82. }
  83. }
  84. }
  85. System.out.println(furthestdistance); //print out the results
  86. // System.out.println(furthestroute);
  87. }
  88. }
  89. class Vertex{ //everything you need for a vertex
  90. public boolean visited=false; //has it been visited?
  91. int distance=Integer.MAX_VALUE; //the shortest route from starting position to this vertex
  92. int route=-1; //keep track of the last step taken to reach this vertex for the shortest path - what vertex did it come from?
  93. String label; //name of the vertex
  94.  
  95. public Vertex (String labelin){ //give the vertex a name when instantiated
  96. label=labelin;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement