获得加权图在JGraphT中工作

时间:2013-11-27 12:48:09

标签: java graph jgrapht

为什么我得到这个程序的NULL指针错误,只要我看到一切看起来很好,我刚开始使用JGraphT并需要一些帮助。

上下文: 我需要能够在图表中添加航班,每个航班都是双向的,航班应按其价格加权。

错误:

run:
Please enter the number of flights: 
2
Please enter the flight destination for flight 1:
Edinburg
Please enter the flight destination for flight 2:
Heathrow
Enter the edges
Edinburg
Heathrow
Please enter a price for this edge:
25.99
Exception in thread "main" java.lang.NullPointerException
    at org.jgrapht.graph.AbstractBaseGraph.setEdgeWeight(Unknown Source)
    at graphapp.MyGraph.setEdgeWeight(MyGraph.java:25)
    at graphapp.GraphApp.main(GraphApp.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)

MyGraph.java(Class):

package graphapp;

import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.KruskalMinimumSpanningTree;

public class MyGraph {

    private final SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    static final double DEFAULT_EDGE_WEIGHT=19;
    //DefaultWeightedEdge > (DefaultWeightedEdge.class); 
    private DefaultWeightedEdge e1;

    public void addVertex(String name) {
        g.addVertex(name);
        //graph.addVertex(name);
    }

    public void addEdge(String v1, String v2) {
        g.addEdge(v1, v2);
        e1 = g.addEdge(v1, v2);
    }

    public void setEdgeWeight(String EDGE_WEIGHT) {
        g.setEdgeWeight(e1, Double.valueOf(EDGE_WEIGHT));          
    }

    public SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> getGraph() {
        return g;
    }

    /*public SimpleWeightedGraph<String,DefaultWeightedEdge> getGraph() {
        return graph;
    }*/

    public void getSpanningTree() {
        KruskalMinimumSpanningTree k = new KruskalMinimumSpanningTree(g);
        System.out.println(k.getEdgeSet().toString());
        //KruskalMinimumSpanningTree k1=new KruskalMinimumSpanningTree(graph);
        //System.out.println(k1.getEdgeSet().toString());   
    }

    public void getSpanningTreeCost() {
        KruskalMinimumSpanningTree k = new KruskalMinimumSpanningTree(g);
        System.out.println(k.getSpanningTreeCost());
    }
}

GraphApp(Main):

package graphapp;
import org.jgrapht.demo.*;
import java.util.Scanner;
import graphapp.*;
public class GraphApp{

    public static void main(String args[]) {

        int x;
        Scanner sc = new Scanner(System.in);
        MyGraph my = new MyGraph();
        System.out.println("Please enter the number of flights: ");
        int no_of_ver = sc.nextInt();

        for(int i=1;i <= no_of_ver;i++) {
            System.out.println("Please enter the flight destination for flight "+i+ ":");
            my.addVertex(sc.next());
        }

        do {
            System.out.println("Enter the edges");
            String e1 = sc.next();
            String e2 = sc.next();
            my.addEdge(e1, e2);

            System.out.println("Please enter a price for this edge:");
            my.setEdgeWeight(sc.next());
            System.out.println("Continue... Yes:1 ********** No:0");
            x=sc.nextInt();
        } while(x==1);

        System.out.println("Graph\n" + my.getGraph().toString());
        System.out.println("\n\n**********Spanning Tree*********");
        my.getSpanningTree();
        System.out.println("\nSpanning Tree Cost");
        my.getSpanningTreeCost();
    }
}

1 个答案:

答案 0 :(得分:5)

我的错误是这个方法:

public void addEdge(String v1, String v2) {
    g.addEdge(v1, v2);
    e1 =g.addEdge(v1, v2);
    System.out.println("Edge added: " + e1.toString());
}

我需要将其更改为:

public void addEdge(String v1, String v2) {
    e1 =g.addEdge(v1, v2);
    System.out.println("Edge added: " + e1.toString());
}