Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package butlergraph;
- /**
- *
- * @author Bernard Bailey
- */
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- public class ButlerGraph extends ReadOnlyGraph{
- private int size = 7;
- private String[] names = {"Boyers", "Butler", "Cranberry",
- "Karns City", "Prospect", "Saxonburg", "Slippery Rock"};
- private double distances[][] = {
- {0.0},
- {18.3, 0.0},
- {42.8, 20.2, 0.0},
- {15.3, 15.3, 36.7, 0.0},
- {19.4, 9.0, 19.3, 21.5, 0.0},
- {30.0, 9.5, 19.2, 21.2, 20.3, 0.0},
- {10.8, 17.1, 31.4, 20.4, 12.2, 28.7, 0.0}
- };
- @Override
- public int getSize() {
- return size;
- }
- @Override
- public String getName(int i) {
- return names[i];
- }
- @Override
- public double getDistance(int from, int to) {
- if (to <= from)
- return distances[from][to];
- else
- return distances[to][from];
- }
- public static void main(String[] args) {
- ButlerGraph g = new ButlerGraph();
- DecimalFormat oneplace = new DecimalFormat("0.0");
- for (int i = 0; i < g.getSize()-1; i++)
- for (int j = 0; j < g.getSize()-1; j++)
- {
- System.out.print("Distance from " + g.getName(i));
- System.out.print(" to " + g.getName(j) + " is ");
- System.out.print(oneplace.format(g.getDistance(i, j)));
- System.out.println(" miles.");
- FindShortestPath cPath = new FindShortestPath();
- // find the shortest path recursively
- ArrayList<Integer> currentPath = new ArrayList<Integer>();
- cPath.findShortestDistance(1, 0, currentPath);
- // Extract shortest path from class
- System.out.println("The shortest Distance is " + cPath.shortestDistance);
- System.out.println("Shortest Path is " + cPath.bestPath);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment