BABJ

ButlerGraph for Traveling Salesman!!

Nov 8th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package butlergraph;
  7.  
  8. /**
  9. *
  10. * @author Bernard Bailey
  11. */
  12. import java.text.DecimalFormat;
  13. import java.util.ArrayList;
  14.  
  15. public class ButlerGraph extends ReadOnlyGraph{
  16. private int size = 7;
  17. private String[] names = {"Boyers", "Butler", "Cranberry",
  18. "Karns City", "Prospect", "Saxonburg", "Slippery Rock"};
  19. private double distances[][] = {
  20. {0.0},
  21. {18.3, 0.0},
  22. {42.8, 20.2, 0.0},
  23. {15.3, 15.3, 36.7, 0.0},
  24. {19.4, 9.0, 19.3, 21.5, 0.0},
  25. {30.0, 9.5, 19.2, 21.2, 20.3, 0.0},
  26. {10.8, 17.1, 31.4, 20.4, 12.2, 28.7, 0.0}
  27. };
  28.  
  29.  
  30.  
  31.  
  32. @Override
  33. public int getSize() {
  34. return size;
  35. }
  36.  
  37. @Override
  38. public String getName(int i) {
  39. return names[i];
  40. }
  41.  
  42. @Override
  43. public double getDistance(int from, int to) {
  44. if (to <= from)
  45. return distances[from][to];
  46. else
  47. return distances[to][from];
  48. }
  49.  
  50. public static void main(String[] args) {
  51. ButlerGraph g = new ButlerGraph();
  52. DecimalFormat oneplace = new DecimalFormat("0.0");
  53. for (int i = 0; i < g.getSize()-1; i++)
  54. for (int j = 0; j < g.getSize()-1; j++)
  55. {
  56. System.out.print("Distance from " + g.getName(i));
  57. System.out.print(" to " + g.getName(j) + " is ");
  58. System.out.print(oneplace.format(g.getDistance(i, j)));
  59. System.out.println(" miles.");
  60. FindShortestPath cPath = new FindShortestPath();
  61. // find the shortest path recursively
  62. ArrayList<Integer> currentPath = new ArrayList<Integer>();
  63. cPath.findShortestDistance(1, 0, currentPath);
  64. // Extract shortest path from class
  65. System.out.println("The shortest Distance is " + cPath.shortestDistance);
  66. System.out.println("Shortest Path is " + cPath.bestPath);
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment