BaseGraph

abstract class BaseGraph<T : Any>(initialNrOfNodes: Int, debugTimeUse: Boolean, isSparse: Boolean)

An abstract class that's used by the Graph, IntGraph and Grid classes for common functionality

Inheritors

Constructors

Link copied to clipboard
constructor(initialNrOfNodes: Int, debugTimeUse: Boolean, isSparse: Boolean)

Functions

Link copied to clipboard
abstract fun addEdge(node1: T, node2: T, weight: Double = 1.0)

Adds a directed edge from node1 to node2. See the implementing class for how undefined nodes are handled.

fun addEdge(node1: T, node2: T, weight: Int)

Overloaded function that calls addEdge with weight converted to a double.

Link copied to clipboard
abstract fun addNode(node: T)

Adds the given node to the graph

Link copied to clipboard

Executes the Floyd-Warshall algorithm on the graph to compute the shortest paths between all pairs of nodes.

Link copied to clipboard
fun bfs(startNode: T, target: T? = null, reset: Boolean = true, maxDistance: Double = Double.POSITIVE_INFINITY)

Overload of fun bfs(startNodes: List, target: T?, reset: Boolean, maxDistance: Double) that accepts a single starting node instead of a list of starting nodes

fun bfs(startNodes: List<T>, target: T? = null, reset: Boolean = true, maxDistance: Double = Double.POSITIVE_INFINITY)

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:

Link copied to clipboard

Removes all edges from the graph, including a rule defined by connectWithRule. Nodes and search results are kept.

Link copied to clipboard
fun connect(node1: T, node2: T)

Connects two nodes in the graph, by calling addEdge(node1,node2) and addEdge(node2, node1)

fun connect(node1: T, node2: T, weight: Double)

Connects two nodes in the graph, by calling addEdge(node1,node2, weight) and addEdge(node2, node1, weight)

fun connect(node1: T, node2: T, weight: Int)

Overloaded function that calls connect with weight converted to a double.

Link copied to clipboard
open fun connectWithRule(getNeighbours: (node: T) -> List<T>)

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

Link copied to clipboard
Link copied to clipboard
fun depth(): Int

Retrieves the depth of the graph from the most recent search operation

Link copied to clipboard
fun dfs(startNode: T, reset: Boolean = true)

Performs a Depth-First Search, which finds all nodes that's reachable from the starting node. It stores results that can be retrieved with the following functions:

Link copied to clipboard
fun distanceFromUtoV(u: T, v: T): Double

Retrieves the shortest distance between two nodes in the graph.

Link copied to clipboard
fun distanceTo(node: T): Double

Retrieves the distance to the specified node from the starting node of the most recent search operation (BFS, DFS, shortestPathsFrom).

Link copied to clipboard
fun edges(): List<Triple<T, T, Double>>

fun edges(node: T): List<Pair<Double, T>>

Retrieves a list of edges connected to the specified node.

Link copied to clipboard
fun finalPath(): List<T>?

Retrieves the shortest path from the start to target node (if a target was specified) during the most recent search operation (BFS, shortestPathsFrom). After topologicalSort(), it holds the topological ordering.

Link copied to clipboard
fun forEachNeighbour(node: T, action: (T) -> Unit)

Executes a given function on all the neighbours of a given node.

Link copied to clipboard

Checks if the target node was found during the most recent search operation (BFS, shortestPathsFrom).

Link copied to clipboard

Retrieves the node that is the farthest from the starting node in the most recent search operation (BFS, shortestPathsFrom).

Link copied to clipboard
fun getPath(target: T): List<T>?

Retrieves the path from the starting node to the specified target node based on the most recent search results.

Link copied to clipboard

Retrieves the maximum distance from the starting node to any other node after the most recent search operation (BFS, shortestPathsFrom).

Link copied to clipboard

Computes the Minimum Spanning Tree (MST) of the graph using Prim's algorithm.

Link copied to clipboard
fun neighbours(node: T): List<T>

Retrieves a list of the neighboring nodes of the specified node.

Link copied to clipboard
abstract fun nodes(): List<T>
Link copied to clipboard
fun nrOfEdges(): Int
Link copied to clipboard
fun nrOfPaths(startNode: T, targetNode: T, mod: Long = Long.MAX_VALUE): Long

Calculates the number of distinct paths from the starting node to the target node in the graph.

Link copied to clipboard
open fun print()

Prints the graph's adjacency list to the standard error stream.

Link copied to clipboard

Prints which nodes each node is connected to, to the standard error stream.

Link copied to clipboard

Clears the search results stored in the graph.

Link copied to clipboard
fun shortestPathsFrom(startNode: T, target: T? = null)

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:

Link copied to clipboard
fun size(): Int
Link copied to clipboard

Identifies groups where each node is reachable from every other node in the group.

Link copied to clipboard
open fun topologicalSort(): List<T>

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).

Link copied to clipboard
Link copied to clipboard
fun <T : Any> BaseGraph<T>.visualizeGraph(isBidirectional: Boolean = false, finalPath: List<T> = finalPath() ?: emptyList(), screenTitle: String = "Graph visualizer (Click space to pause and resume)", animationTicTimeOverride: Double? = null, closeOnEnd: Boolean = false, startPaused: Boolean = false)

Visualizes the graph with Bruno Silva's JavaFXSmartGraph library.