0

我对 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);

每个顶点都已成功添加到图形中。但是, addEdge 方法无法正常工作。当我调试这段代码时,首先 addEdge 行抛出类未找到异常。

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

4

1 回答 1

1

With jgrapht-0.8.3, the following works as expected:

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"); }
  }
}

I get

SUCCESS

SUCCESS

Can you clarify what problem you're having?

于 2012-05-13T20:57:07.167 回答