Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Vector;
- enum MinimumCost {
- SUCCESS, FAIL
- }
- public class Path {
- public Path(ArrayList<Main.Edge> edges) {
- neighbourMatrix = new double[edges.size()][edges.size()];
- for (Main.Edge edge : edges) {
- neighbourMatrix[edge.X() - 1][edge.Y() - 1] = edge.Cost();
- }
- }
- /**
- *
- * @param currentVertex obecny wierzchołek sciezki
- * @param cost koszt drogi
- * @param previousCost Kosz poprzedniej drogi
- * @param usedVertices wierzchołki odwiedzone
- * @param k wierzchołek końcowy grafu
- ** @param currentVertex wierzchołek końcowy grafu
- *
- */
- private MinimumCost minimumPath(int currentVertex,
- int k,
- boolean[] usedVertices,
- double previousCost,
- double cost) {
- if (currentVertex == k) {
- pathsCost.add(cost);
- return MinimumCost.SUCCESS;
- }
- if (usedVertices[currentVertex] == true)
- return MinimumCost.FAIL;
- usedVertices[currentVertex] = true;
- for (int i = 0; i < neighbourMatrix[currentVertex].length; i++) {
- if (neighbourMatrix[currentVertex][i] != 0) {
- minimumPath(i,
- k,
- usedVertices.clone(),
- neighbourMatrix[currentVertex][i],
- cost + 1 / neighbourMatrix[currentVertex][i]
- + Math.abs(previousCost - neighbourMatrix[currentVertex][i]));
- }
- }
- return MinimumCost.SUCCESS;
- }
- /**
- *
- ** @param k wierzchołek końcowy grafu
- * @return zwraca minimalny kosz drogi
- *
- */
- public double minimumPath(int k) {
- pathsCost = new Vector<>();
- minimumPath(0, k - 1, new boolean[neighbourMatrix.length], 0.0, 0.0);
- double minCost = pathsCost.firstElement();
- for (Double Cost : pathsCost) {
- if (minCost > Cost) {
- minCost = Cost;
- }
- }
- return minCost;
- }
- private double[][] neighbourMatrix;
- private Vector<Double> pathsCost;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement