Class DefaultGraph

java.lang.Object
com.github.yellowstonegames.path.Graph<com.github.yellowstonegames.grid.Coord>
com.github.yellowstonegames.path.UndirectedGraph<com.github.yellowstonegames.grid.Coord>
com.github.yellowstonegames.path.DefaultGraph
All Implemented Interfaces:
com.github.yellowstonegames.core.ISerializersNeeded, Externalizable, Serializable

public class DefaultGraph extends UndirectedGraph<com.github.yellowstonegames.grid.Coord> implements com.github.yellowstonegames.core.ISerializersNeeded
A default setting for an UndirectedGraph of Coord vertices where all connections have cost 1. This should be initialized with a 2D (rectangular) char array using the map convention where '#' is a wall and anything else is passable.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
     
    int
     

    Fields inherited from class UndirectedGraph

    algorithms

    Fields inherited from class Graph

    edgeSet, vertexMap
  • Constructor Summary

    Constructors
    Constructor
    Description
    No-op no-arg constructor, present for serialization; if you use this you must call init(char[][]) or init(char[][], boolean) before using the DefaultGraph.
    DefaultGraph(char[][] map)
    Builds a DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable.
    DefaultGraph(char[][] map, boolean eightWay)
    Builds a DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable.
  • Method Summary

    Modifier and Type
    Method
    Description
    Graph<com.github.yellowstonegames.grid.Coord>
    breadthFirstSearch(com.github.yellowstonegames.grid.Coord coord)
    Perform a breadth first search starting from the specified vertex.
    Graph<com.github.yellowstonegames.grid.Coord>
    breadthFirstSearch(com.github.yellowstonegames.grid.Coord coord, int maxVertices, int maxDepth)
    Perform a breadth first search starting from the specified vertex.
    boolean
    Checks whether there are any cycles in the graph using depth first searches.
    Graph<com.github.yellowstonegames.grid.Coord>
    depthFirstSearch(com.github.yellowstonegames.grid.Coord coord)
    Perform a depth first search starting from the specified vertex.
    Graph<com.github.yellowstonegames.grid.Coord>
    depthFirstSearch(com.github.yellowstonegames.grid.Coord coord, int maxVertices, int maxDepth)
    Perform a depth first search starting from the specified vertex.
    boolean
     
    double
    findMinimumDistance(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target)
    Find the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.
    Graph<com.github.yellowstonegames.grid.Coord>
    Find a minimum weight spanning tree using Kruskal's algorithm.
    com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord>
    findShortestPath(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target)
    Find the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.
    boolean
    findShortestPath(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target, com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> path, Heuristic<com.github.yellowstonegames.grid.Coord> heuristic)
    Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue.
    com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord>
    findShortestPath(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target, Heuristic<com.github.yellowstonegames.grid.Coord> heuristic)
    Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue.
    Gets a List of Class instances that must each be registered with a serialization library before this object can be successfully serialized or deserialized.
    int
     
    void
    init(char[][] map)
    Re-initializes a DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable.
    void
    init(char[][] map, boolean eightWay)
    Re-initializes this DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable.
    void
    Meant for deserialization using Fory.
    char[]
    Creates a 1D char array (which can be passed to String.valueOf(char[])) filled with a grid made of the vertices in this Graph and their estimated costs, if this has done an estimate.
     
    void
    Meant for serialization using Fory.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • width

      public int width
    • height

      public int height
  • Constructor Details

    • DefaultGraph

      public DefaultGraph()
      No-op no-arg constructor, present for serialization; if you use this you must call init(char[][]) or init(char[][], boolean) before using the DefaultGraph.
    • DefaultGraph

      public DefaultGraph(char[][] map)
      Builds a DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable. This only builds connections along cardinal directions.
      Parameters:
      map - a 2D char array where '#' represents an inaccessible area (such as a wall) and anything else is walkable
    • DefaultGraph

      public DefaultGraph(char[][] map, boolean eightWay)
      Builds a DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable. If eightWay is true, this builds connections along diagonals as well as along cardinals, but if eightWay is false, it only builds connections along cardinal directions.
      Parameters:
      map - a 2D char array where '#' represents an inaccessible area (such as a wall) and anything else is walkable
      eightWay - if true, this will build connections on diagonals as well as cardinal directions; if false, this will only use cardinal connections
  • Method Details

    • init

      public void init(char[][] map)
      Re-initializes a DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable. This only builds connections along cardinal directions.
      Parameters:
      map - a 2D char array where '#' represents an inaccessible area (such as a wall) and anything else is walkable
    • init

      public void init(char[][] map, boolean eightWay)
      Re-initializes this DefaultGraph from a 2D char array that uses '#' to represent any kind of inaccessible cell, with all other chars treated as walkable. If eightWay is true, this builds connections along diagonals as well as along cardinals, but if eightWay is false, it only builds connections along cardinal directions.
      Parameters:
      map - a 2D char array where '#' represents an inaccessible area (such as a wall) and anything else is walkable
      eightWay - if true, this will build connections on diagonals as well as cardinal directions; if false, this will only use cardinal connections
    • findMinimumWeightSpanningTree

      public Graph<com.github.yellowstonegames.grid.Coord> findMinimumWeightSpanningTree()
      Find a minimum weight spanning tree using Kruskal's algorithm.
      Returns:
      a Graph object containing a minimum weight spanning tree (if this graph is connected - in general a minimum weight spanning forest)
    • findShortestPath

      public com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> findShortestPath(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target)
      Find the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.
      Parameters:
      start - the starting vertex
      target - the target vertex
      Returns:
      a list of vertices from start to target containing the ordered vertices of a shortest path, including both the start and target vertices
    • findShortestPath

      public com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> findShortestPath(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target, Heuristic<com.github.yellowstonegames.grid.Coord> heuristic)
      Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue.
      Parameters:
      start - the starting vertex
      target - the target vertex
      heuristic - typically predefined in Heuristic, this determines how the optimal path will be estimated
      Returns:
      a list of vertices from start to target containing the ordered vertices of a shortest path, including both the start and target vertices
    • findShortestPath

      public boolean findShortestPath(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target, com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> path, Heuristic<com.github.yellowstonegames.grid.Coord> heuristic)
      Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue. Fills path with a list of vertices from start to target containing the ordered vertices of a shortest path, including both the start and target vertices.
      Parameters:
      start - the starting vertex
      target - the target vertex
      path - the list of vertices to which the path vertices should be added
      Returns:
      true if a path was found, or false if no path could be found
    • findMinimumDistance

      public double findMinimumDistance(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target)
      Find the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.
      Parameters:
      start - the starting vertex
      target - the target vertex
      Returns:
      the sum of the weights in a shortest path from the starting vertex to the target vertex
    • breadthFirstSearch

      public Graph<com.github.yellowstonegames.grid.Coord> breadthFirstSearch(com.github.yellowstonegames.grid.Coord coord, int maxVertices, int maxDepth)
      Perform a breadth first search starting from the specified vertex.
      Parameters:
      coord - the vertex at which to start the search
      maxVertices - the maximum number of vertices to process before terminating the search
      maxDepth - the maximum edge distance (the number of edges in a shortest path between vertices) a vertex should have to be considered for processing. If a vertex has a distance larger than the maxDepth, it will not be added to the returned graph
      Returns:
      a Graph object containing all the processed vertices, and the edges from which each vertex was encountered. The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be reflected in the iteration order of the collections returned by Graph.getVertices() and Graph.getEdges().
    • breadthFirstSearch

      public Graph<com.github.yellowstonegames.grid.Coord> breadthFirstSearch(com.github.yellowstonegames.grid.Coord coord)
      Perform a breadth first search starting from the specified vertex.
      Parameters:
      coord - the vertex at which to start the search
      Returns:
      a Graph object containing all the processed vertices (all the vertices in this graph), and the edges from which each vertex was encountered. The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be reflected in the iteration order of the collections returned by Graph.getVertices() and Graph.getEdges().
    • depthFirstSearch

      public Graph<com.github.yellowstonegames.grid.Coord> depthFirstSearch(com.github.yellowstonegames.grid.Coord coord, int maxVertices, int maxDepth)
      Perform a depth first search starting from the specified vertex.
      Parameters:
      coord - the vertex at which to start the search
      maxVertices - the maximum number of vertices to process before terminating the search
      maxDepth - the maximum edge distance (the number of edges in a shortest path between vertices) a vertex should have to be considered for processing. If a vertex has a distance larger than the maxDepth, it will not be added to the returned graph
      Returns:
      a Graph object containing all the processed vertices, and the edges from which each vertex was encountered. The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be reflected in the iteration order of the collections returned by Graph.getVertices() and Graph.getEdges().
    • depthFirstSearch

      public Graph<com.github.yellowstonegames.grid.Coord> depthFirstSearch(com.github.yellowstonegames.grid.Coord coord)
      Perform a depth first search starting from the specified vertex.
      Parameters:
      coord - the vertex at which to start the search
      Returns:
      a Graph object containing all the processed vertices (all the vertices in this graph), and the edges from which each vertex was encountered. The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be reflected in the iteration order of the collections returned by Graph.getVertices() and Graph.getEdges().
    • containsCycle

      public boolean containsCycle()
      Checks whether there are any cycles in the graph using depth first searches.
      Returns:
      true if the graph contains a cycle, false otherwise
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Meant for serialization using Fory. If a class overrides this with different behavior, readExternal(ObjectInput) must also be overridden to match that behavior.
      Specified by:
      writeExternal in interface Externalizable
      Overrides:
      writeExternal in class Graph<com.github.yellowstonegames.grid.Coord>
      Parameters:
      out - the stream to write the object to
      Throws:
      IOException - Includes any I/O exceptions that may occur
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Meant for deserialization using Fory. If a class overrides this with different behavior, writeExternal(ObjectOutput) must also be overridden to match that behavior.
      Specified by:
      readExternal in interface Externalizable
      Overrides:
      readExternal in class Graph<com.github.yellowstonegames.grid.Coord>
      Parameters:
      in - the stream to read data from in order to restore the object
      Throws:
      IOException - if I/O errors occur
      ClassNotFoundException - If the class for an object being restored cannot be found.
    • show

      public char[] show()
      Creates a 1D char array (which can be passed to String.valueOf(char[])) filled with a grid made of the vertices in this Graph and their estimated costs, if this has done an estimate. Each estimate is rounded to the nearest int and only printed if it is 4 digits or less; otherwise this puts '####' in the grid cell. This is a building-block for toString() implementations that may have debugging uses as well.
      Returns:
      a 1D char array containing newline-separated rows of space-separated grid cells that contain estimated costs or '####' for unexplored
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Graph<com.github.yellowstonegames.grid.Coord>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Graph<com.github.yellowstonegames.grid.Coord>
    • toString

      public String toString()
      Overrides:
      toString in class UndirectedGraph<com.github.yellowstonegames.grid.Coord>
    • getSerializersNeeded

      public List<Class<?>> getSerializersNeeded()
      Gets a List of Class instances that must each be registered with a serialization library before this object can be successfully serialized or deserialized. This is GwtIncompatible; none of the serialization libraries this is meant for have any support for GWT.
      Specified by:
      getSerializersNeeded in interface com.github.yellowstonegames.core.ISerializersNeeded
      Returns:
      a List of Class instances that must each be registered with a serialization library