Search code examples
javaprocessingjgraphtdigraphs

Error when using predecessorListOf in jgrapht


I created a digraph using the jgrapht libray, added some vertices and edges. I can't manage to make the predecessorListOf method work in my program. I made a very simple one to try to find the issue but same problem, it says that the function doesn't exist:

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

does anyone know what am I missing ?


Solution

  • Like your other questions, the JGraphT API is the answer: http://jgrapht.org/javadoc/org/jgrapht/Graphs.html

    The predecessorListOf() function is a static function of the Graphs class.

    That means you can't just call predecessorListOf() out of nowhere like you're trying to do. You have to include the Graphs class, that way Processing knows where to find the function:

    Graphs.predecessorListOf(directedGraph, fourthPoint);