JGraphT图最短路径

时间:2016-11-04 22:29:06

标签: java graph jgrapht

我正在尝试计算航班行程系统最短路径的总成本,但它似乎是计算它所采用的路径数而不是长度/成本。我无法从Flight_Info对象中研究如何从每个旅程中获得费用。

这是我的代码:

import java.util.Scanner;
import org.jgrapht.graph.*;
import org.jgrapht.Graphs;
import org.jgrapht.alg.DijkstraShortestPath;

public class Flights2 {

public static SimpleDirectedWeightedGraph<String, Flight_Info> createGraph() {

    SimpleDirectedWeightedGraph<String, Flight_Info> airport = new SimpleDirectedWeightedGraph<String, Flight_Info>(
            Flight_Info.class);

    String Edinburgh = new String("Edinburgh");
    String Heathrow = new String("Heathrow");
    String Dubai = new String("Dubai");
    String Sydney = new String("Sydney");
    String KualaLumpur = new String("Kuala Lumpur");
    String Frankfurt = new String("Frankfurt");
    String Aukland = new String("Aukland");
    String RioDeJanerio = new String("Rio De Janerio");
    String NewYork = new String("New York");
    String Santiago = new String("Santiago");

    airport.addVertex(Edinburgh);
    airport.addVertex(Heathrow);
    airport.addVertex(Dubai);
    airport.addVertex(Sydney);
    airport.addVertex(KualaLumpur);
    airport.addVertex(Frankfurt);
    airport.addVertex(Aukland);
    airport.addVertex(RioDeJanerio);
    airport.addVertex(NewYork);
    airport.addVertex(Santiago);



    Flight_Info FL1001 = new Flight_Info("FL1001", 1530, 1630, 1, 80);
    airport.addEdge(Edinburgh, Heathrow, FL1001);
    Flight_Info FL1002 = new Flight_Info("FL1002", 1630, 1730, 1, 80);
    airport.addEdge(Heathrow, Edinburgh, FL1002);
    Flight_Info FL1003 = new Flight_Info("FL1003", 1630, 1730, 1, 80);
    airport.addEdge(Heathrow, Dubai, FL1003);
    Flight_Info FL1004 = new Flight_Info("FL1004", 1630, 1730, 1, 80);
    airport.addEdge(Dubai, Heathrow, FL1004);
    Flight_Info FL1005 = new Flight_Info("FL1005", 1630, 1730, 1, 80);
    airport.addEdge(Heathrow, Sydney, FL1005);
    Flight_Info FL1006 = new Flight_Info("FL1006", 1630, 1730, 1, 80);
    airport.addEdge(Sydney, Heathrow, FL1006);
    Flight_Info FL1007 = new Flight_Info("FL1007", 1630, 1730, 1, 80);
    airport.addEdge(Dubai, KualaLumpur, FL1007);
    Flight_Info FL1008 = new Flight_Info("FL1008", 1630, 1730, 1, 80);
    airport.addEdge(KualaLumpur, Dubai, FL1008);
    Flight_Info FL1009 = new Flight_Info("FL1009", 1630, 1730, 1, 80);
    airport.addEdge(Dubai, Edinburgh, FL1009);
    Flight_Info FL1010 = new Flight_Info("FL10010", 1630, 1730, 1, 80);
    airport.addEdge(Edinburgh, Dubai, FL1010);
    Flight_Info FL1011 = new Flight_Info("FL1011", 1630, 1730, 1, 80);
    airport.addEdge(KualaLumpur, Sydney, FL1011);
    Flight_Info FL1012 = new Flight_Info("FL1012", 1630, 1730, 1, 80);
    airport.addEdge(Sydney, KualaLumpur, FL1012);
    Flight_Info FL1013 = new Flight_Info("FL1013", 1630, 1730, 1, 80);
    airport.addEdge(Edinburgh, Frankfurt, FL1013);
    Flight_Info FL1014 = new Flight_Info("FL1014", 1630, 1730, 1, 80);
    airport.addEdge(Frankfurt, Edinburgh, FL1014);
    Flight_Info FL1015 = new Flight_Info("FL1015", 1630, 1730, 1, 80);
    airport.addEdge(Sydney, Aukland, FL1015);
    Flight_Info FL1016 = new Flight_Info("FL1016", 1630, 1730, 1, 80);
    airport.addEdge(Aukland, Sydney, FL1016);
    Flight_Info FL1017 = new Flight_Info("FL1017", 1630, 1730, 1, 80);
    airport.addEdge(RioDeJanerio, NewYork, FL1017);
    Flight_Info FL1018 = new Flight_Info("FL1018", 1630, 1730, 1, 80);
    airport.addEdge(NewYork, RioDeJanerio, FL1018);
    Flight_Info FL1019 = new Flight_Info("FL1019", 1630, 1730, 1, 80);
    airport.addEdge(Santiago, NewYork, FL1019);
    Flight_Info FL1020 = new Flight_Info("FL1020", 1630, 1730, 1, 80);
    airport.addEdge(NewYork, Santiago, FL1020);

    return airport;
}

public static void itinerary(SimpleDirectedWeightedGraph<String, Flight_Info> airport, String departure,
        String destination) {
    DijkstraShortestPath<String, Flight_Info> p = new DijkstraShortestPath<String, Flight_Info>(airport, departure,
            destination);




    System.out.println(p.getPathEdgeList());
    System.out.println("Cost of shortest (i.e cheapest) path = £" + p.getPathLength() );
}

public static void main(String args[]) {

    SimpleDirectedWeightedGraph<String, Flight_Info> airport = createGraph();
    System.out.println("The following airports are in use:" + airport.vertexSet());
    @SuppressWarnings("resource")
    Scanner s = new Scanner(System.in);
    System.out.println("Please enter the starting airport:");
    String departure = s.nextLine();
    System.out.println("Please enter the destination aiport:");
    String destination = s.nextLine();
    itinerary(airport, departure, destination);


}
  import org.jgrapht.graph.DefaultWeightedEdge;

public class Flight_Info extends DefaultWeightedEdge {

private String departure, destination, flightNumber;
private int departureTime, arrivalTime, duration, ticketPrice;

public Flight_Info() {

}

public Flight_Info(String flightNumber, int departureTime, int arrivalTime, int duration, int ticketPrice) {
    super();
    this.flightNumber = flightNumber;
    this.departureTime = departureTime;
    this.arrivalTime = arrivalTime;
    this.duration = duration;
    this.ticketPrice = ticketPrice;

}


public int getDuration(){
    return duration;
}

1 个答案:

答案 0 :(得分:0)

我只是猜测,但似乎你的Flight_Info类缺少票价的吸气剂。然后你正在读取边缘中计算出的最短路径的成本。我认为最好迭代最短路径的边缘并总结门票价格。

正如我所说:只是短暂地猜测...... =)

祝你好运

亚历