在 jgrapht 中使用 predecessorListOf 时出错
Error when using predecessorListOf in jgrapht
我使用 jgrapht 库创建了一个有向图,添加了一些顶点和边。我无法使前身ListOf 方法在我的程序中工作。我做了一个非常简单的尝试找到问题但同样的问题,它说该功能不存在:
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;
}
}
有人知道我错过了什么吗?
与您的其他问题一样,JGraphT API 是答案:http://jgrapht.org/javadoc/org/jgrapht/Graphs.html
predecessorListOf()
函数是Graphs
class的静态函数。
这意味着您不能像您尝试的那样凭空调用 predecessorListOf()
。您必须包含 Graphs
class,这样处理就知道在哪里可以找到函数:
Graphs.predecessorListOf(directedGraph, fourthPoint);
我使用 jgrapht 库创建了一个有向图,添加了一些顶点和边。我无法使前身ListOf 方法在我的程序中工作。我做了一个非常简单的尝试找到问题但同样的问题,它说该功能不存在:
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;
}
}
有人知道我错过了什么吗?
与您的其他问题一样,JGraphT API 是答案:http://jgrapht.org/javadoc/org/jgrapht/Graphs.html
predecessorListOf()
函数是Graphs
class的静态函数。
这意味着您不能像您尝试的那样凭空调用 predecessorListOf()
。您必须包含 Graphs
class,这样处理就知道在哪里可以找到函数:
Graphs.predecessorListOf(directedGraph, fourthPoint);