JGraphT - UndirectedGraph - addEdge方法不起作用

时间:2012-05-13 19:51:50

标签: java graph jgrapht

我有关于UndirectedGraph用法的问题。我的代码:

UndirectedGraph<String, DefaultEdge> graph =
            new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);

 String v1 = "v1";
 String v2 = "v2";
 String v3 = "v3";
 String v4 = "v4";

 graph.addVertex(v1);
 graph.addVertex(v2);
 graph.addVertex(v3);
 graph.addVertex(v4);

 graph.addEdge(v1, v2);
 graph.addEdge(v2, v3);
 graph.addEdge(v3, v4);
 graph.addEdge(v4, v1);

每个顶点都已成功添加到图表中。 Hovewer,addEdge方法无法正常工作。当我调试此代码时,首先addEdge行抛出class not found异常。

我希望,你会帮助我的。感谢。

1 个答案:

答案 0 :(得分:1)

使用jgrapht-0.8.3,以下工作符合预期:

import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;

public class GraphTest {

  public static void main(String[] args) {
    UndirectedGraph<String, DefaultEdge> graph =
      new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);

    String v1 = "v1";
    String v2 = "v2";
    String v3 = "v3";
    String v4 = "v4";

    graph.addVertex(v1);
    graph.addVertex(v2);
    graph.addVertex(v3);
    graph.addVertex(v4);

    graph.addEdge(v1, v2);
    graph.addEdge(v2, v3);
    graph.addEdge(v3, v4);
    graph.addEdge(v4, v1);

    if( graph.getEdge(v1, v2) != null) { System.out.println("SUCCESS"); }
    if (graph.getEdge(v2,v4) == null)  { System.out.println("SUCCESS"); }
  }
}

我得到了

  

SUCCESS

     

SUCCESS

你能澄清一下你遇到的问题吗?