Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Vector;
  3.  
  4.  
  5. enum MinimumCost {
  6. SUCCESS, FAIL
  7. }
  8.  
  9. public class Path {
  10. public Path(ArrayList<Main.Edge> edges) {
  11.  
  12. neighbourMatrix = new double[edges.size()][edges.size()];
  13.  
  14. for (Main.Edge edge : edges) {
  15. neighbourMatrix[edge.X() - 1][edge.Y() - 1] = edge.Cost();
  16. }
  17. }
  18.  
  19. /**
  20. *
  21. * @param currentVertex obecny wierzchołek sciezki
  22. * @param cost koszt drogi
  23. * @param previousCost Kosz poprzedniej drogi
  24. * @param usedVertices wierzchołki odwiedzone
  25. * @param k wierzchołek końcowy grafu
  26. ** @param currentVertex wierzchołek końcowy grafu
  27. *
  28. */
  29.  
  30. private MinimumCost minimumPath(int currentVertex,
  31. int k,
  32. boolean[] usedVertices,
  33. double previousCost,
  34. double cost) {
  35. if (currentVertex == k) {
  36. pathsCost.add(cost);
  37. return MinimumCost.SUCCESS;
  38. }
  39. if (usedVertices[currentVertex] == true)
  40. return MinimumCost.FAIL;
  41.  
  42. usedVertices[currentVertex] = true;
  43.  
  44. for (int i = 0; i < neighbourMatrix[currentVertex].length; i++) {
  45. if (neighbourMatrix[currentVertex][i] != 0) {
  46. minimumPath(i,
  47. k,
  48. usedVertices.clone(),
  49. neighbourMatrix[currentVertex][i],
  50. cost + 1 / neighbourMatrix[currentVertex][i]
  51. + Math.abs(previousCost - neighbourMatrix[currentVertex][i]));
  52. }
  53. }
  54. return MinimumCost.SUCCESS;
  55. }
  56.  
  57. /**
  58. *
  59. ** @param k wierzchołek końcowy grafu
  60. * @return zwraca minimalny kosz drogi
  61. *
  62. */
  63. public double minimumPath(int k) {
  64. pathsCost = new Vector<>();
  65. minimumPath(0, k - 1, new boolean[neighbourMatrix.length], 0.0, 0.0);
  66.  
  67. double minCost = pathsCost.firstElement();
  68.  
  69. for (Double Cost : pathsCost) {
  70. if (minCost > Cost) {
  71. minCost = Cost;
  72. }
  73. }
  74. return minCost;
  75. }
  76.  
  77. private double[][] neighbourMatrix;
  78. private Vector<Double> pathsCost;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement