connectWithRule

open override fun connectWithRule(getNeighbours: (node: Any) -> List<Any>)

Defines a rule for how each node is connected to its neighbors.

This will configure edges and nodes to be lazily/dynamically added to the graph, based on the user defined function getNeighbours. This is useful when the nodes or edges of the graph are not known/defined in advance, or when the graph's state space is too large to be stored in memory effectively. The tradeoff is that searches (bfs, dfs) will be less performant per iteration, compared to if the graph was fully defined in advance.

DISCLAIMER: This will overwrite all existing edges (but not nodes) in the graph. If invoked from the Graph() class, it will make the graph be considered sparse and create new nodes during traversal. If invoked from the IntGraph() or Grid() class, it's on the user to make sure all reachable nodes are defined in the graph. Otherwise, the behavior will be undefined. If edges are added after connectWithRule is invoked, all nodes 'defined' in the graph will be connected according to the rule. Then the rule will be dropped and not considered in future searches.

Example usage (if invoked from a grid):

val grid = Grid(100,100, true)
grid.connectWithRule { t ->
grid.getStraightNeighbours(t) + grid.getDiagonalNeighbours(t)
}
grid.bfs(Tile(50,50))
grid.visualizeGrid()

Parameters

getNeighbours

A user defined function that takes a Node as input and returns a list of neighboring Nodes