Grid
A general graph class that represents a 2D grid structure of nodes.
It uses the data class: Tile(val x: Int, val y: Int, val data: Any? = null) to represent nodes of any datatype, where each node also has x and y coordinates.
The grid can be created in two ways:
By specifying a
widthandheight, optionally initializing it with tiles that hasdata=null.By passing a list of strings, where each string represents a row, and all strings must have the same length.
The Grid class supports the same algorithms as the Graph class. Additionally, it provides methods for connecting nodes in the grid without explicitly adding edges:
.connectGridDefault()connects each node to its neighbors in the up, down, left, and right directions, if they exist..connectWithRule(::yourCustomFunction)(or.connectWithRule { yourLambda }) allows custom connections, where the function takes aTileand returns aList<Tile>to connect to.
Example usage:
val grid = Grid(100,100)
grid.connectGridDefault()
grid.bfs(Tile(50,50))
grid.visualizeGrid()String constructor and custom connections example usage:
val stringList = listOf(
"0#4",
"123",
"234"
)
val grid = Grid(stringList)
grid.deleteNodesWithData('#')
grid.connectWithRule { t ->
grid.getStraightNeighbours(t).filter { it.x > t.x || it.y > t.y } // connect right and down
}
grid.bfs(Tile(0, 0))
grid.visualizeGrid()Parameters
The width of the grid (number of columns).
The height of the grid (number of rows).
If true, initializes the grid with empty tiles.
If true, prints the time use of algorithms to standard error. Defaults to false.
If true, search results are stored in maps instead of arrays. More performant when searches only reach a small part of a large grid. Defaults to false.
Constructors
Properties
Functions
Overloaded function that calls addEdge with weight converted to a double.
Adds a directed edge from node1 to node2. Tiles are matched by their (x, y) coordinates, so data is ignored.
Executes the Floyd-Warshall algorithm on the graph to compute the shortest paths between all pairs of nodes.
Overload of fun bfs(startNodes: List
Performs a Breadth-First Search, which finds the shortest path from the starting node to all other nodes, assuming the graph is unweighted (all edges have a weight of 1.0) It stores results that can be retrieved with the following functions:
Removes all edges from the graph, including a rule defined by connectWithRule. Nodes and search results are kept.
Connects two nodes in the graph, by calling addEdge(node1,node2) and addEdge(node2, node1)
Connects two nodes in the graph, by calling addEdge(node1,node2, weight) and addEdge(node2, node1, weight)
Overloaded function that calls connect with weight converted to a double.
Connects all nodes in the grid with their straight neighbours, i.e. top, down, left, right neighbours, if they exist within the grid boundaries and have not been deleted.
Defines a rule for how each node is connected to its neighbors.
Deletes the node at the specified (x, y) coordinates in the grid.
Deletes all nodes in the grid that have the specified data. Deleted nodes are not considered neighbours of nodes.
Retrieves the shortest distance between two nodes in the graph.
Retrieves the distance to the specified node from the starting node of the most recent search operation (BFS, DFS, shortestPathsFrom).
Returns the first tile in the grid (row-wise from left to right) that contains the requested data.
Executes a given function on all the neighbours of a given node.
Checks if the target node was found during the most recent search operation (BFS, shortestPathsFrom).
Retrieves the node that is the farthest from the starting node in the most recent search operation (BFS, shortestPathsFrom).
Retrieves all neighbors (both straight and diagonal) of the given tile.
Retrieves the diagonal neighbors of the given tile.
Retrieves the straight (orthogonal) neighbors of the given tile.
Retrieves the maximum distance from the starting node to any other node after the most recent search operation (BFS, shortestPathsFrom).
Computes the Minimum Spanning Tree (MST) of the graph using Prim's algorithm.
Retrieves a list of the neighboring nodes of the specified node.
Prints which nodes each node is connected to, to the standard error stream.
Clears the search results stored in the graph.
Performs Dijkstra's algorithm to find the shortest path from the starting node to all other nodes. It stores results that can be retrieved with the following functions:
Identifies groups where each node is reachable from every other node in the group.
Builds an order of nodes so that the first nodes has no outgoing edges, then nodes with edges pointing to these and so on, assuming the graph is a Directed Acyclic Graph (DAG). This is done by running a DFS from each node and ordering the nodes by descending depth (post-order).
Visualizes the graph with Bruno Silva's JavaFXSmartGraph library.
Visualizes the grid and optionally its traversal process using a graphical interface.
Retrieves the Tile node at the specified (x, y) coordinates, if it exists.