Grid
The Grid class is a specialized graph class.
It uses the data class: Tile(val x: Int, val y: Int, var 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..connectGrid(::yourCustomFunction)(or.connectGrid { yourLambda }) allows custom connections, where the function takes aTileand returns aList<Tile>to connect to.
Example usage:
val grid = Grid(100,100,true)
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.connectGrid { t ->
grid.getStraightNeighbours(t).filter { it.x >= t.x || it.y > t.y } // connect right and down
}
grid.bfs(Tile(0, 0, 'S'))
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.
Indicates whether the grid uses weighted or unweighted edges.
Constructors
Properties
Functions
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:
Connects all nodes in the grid with their neighbors, using a user-defined function to determine the neighbors.
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 are not deleted.
Retrieves a list of all visited nodes on the order they were visited during the last search operation (DFS, BFS, Dijkstra).
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, Dijkstra).
Executes the Floyd-Warshall algorithm on the graph to compute the shortest paths between all pairs of nodes.
Checks if the target node was found during the most recent search operation (BFS, Dijkstra).
Retrieves the node that is the farthest from the starting node in the most recent search operation (BFS, Dijkstra).
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 of the most recent search operation (BFS, Dijkstra).
Computes the Minimum Spanning Tree (MST) of the graph using Prim's algorithm.
Retrieves a list the neighboring nodes of the specified node.
Removes the edge(s) between two nodes in the graph.
Clears the search results stored in the graph.
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).
Retrieves a (unordered) list of all visited nodes during any non-reset search operation (DFS, BFS, Dijkstra).
Visualizes the graph with Bruno Silva's JavaFXSmartGraph library.
Visualizes the grid and optionally its traversal process using a graphical interface.