在jgrapht中使用predecessorListOf时出错

时间:2015-08-01 16:58:52

标签: java processing jgrapht digraphs

我使用jgrapht libray创建了一个有向图,添加了一些顶点和边。我无法让我的程序中的predecessorListOf方法工作。我做了一个非常简单的尝试找到问题,但同样的问题,它说该功能不存在:

import java.util.*; 
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
import org.jgrapht.alg.*;
import org.jgrapht.demo.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.alg.*;
import org.jgrapht.experimental.dag.*;


public static DirectedGraph<Point, DefaultEdge> directedGraph = new DefaultDirectedGraph<Point, DefaultEdge>(DefaultEdge.class);
public static Point firstPoint = new Point(2, 7);
public static Point secondPoint = new Point(2, 8);
public static Point thirdPoint = new Point(2, 9);
public static Point fourthPoint = new Point(2, 4);


void setup ()  {
  directedGraph.addVertex(firstPoint);
  directedGraph.addVertex(secondPoint);
  directedGraph.addVertex(thirdPoint);
  directedGraph.addVertex(fourthPoint);
  directedGraph.addEdge(firstPoint, secondPoint);
  directedGraph.addEdge(secondPoint, thirdPoint);
  directedGraph.addEdge(thirdPoint, fourthPoint);

  System.out.println(predecessorListOf(directedGraph, fourthPoint));
}

// --------------------------------------------------------------
public static ArrayList<Point> pointList = new ArrayList<Point>();
public static class Point {

  public int x;
  public int y;

  public  Point(int x, int y) 
  {

    this.x = x;
    this.y = y;
  }
  @Override
    public String toString() {
    return ("[x="+x+" y="+y+"]");
  }

  @Override
    public int hashCode() {
    int hash = 7;
    hash = 71 * hash + this.x;
    hash = 71 * hash + this.y;
    return hash;
  }



  @Override
    public boolean equals(Object other) 
  {
    if (this == other)
      return true;

    if (!(other instanceof Point))
      return false;

    Point otherPoint = (Point) other;
    return otherPoint.x == x && otherPoint.y == y;
  }
}

有谁知道我错过了什么?

1 个答案:

答案 0 :(得分:0)

与其他问题一样,JGraphT API就是答案:http://jgrapht.org/javadoc/org/jgrapht/Graphs.html

predecessorListOf()函数是Graphs类的静态函数。

这意味着你不能像你想要的那样单独调用predecessorListOf()。您必须包含Graphs类,处理器知道在哪里找到函数:

Graphs.predecessorListOf(directedGraph, fourthPoint);