Graph
A general graph class that represents nodes of any datatype.
Any new node is given an ID upon creation, which is used to build an adjacency list. The class maintains internal maps between ID's and nodes and vice versa.
Example usage:
val graph = Graph()
graph.addEdge("A", "B", 5.0)
graph.addEdge("A", "C", 2.0)
graph.addEdge("C", "B", 1.0)
graph.shortestPathsFrom("A", "B")
// NOTE: visualizeGraph() requires the smartgraph.css and smartgraph.properties files to be added to the root of your project.
graph.visualizeGraph() // Find the needed files here: https://github.com/Norskeaksel/GraphMateKTParameters
If true, the time taken by each graph algorithm is printed to the standard error stream. Defaults to false.
Functions
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.
Defines a rule for how each node is connected to its neighbors.
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).
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 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.