addEdge

fun addEdge(node1: T, node2: T, weight: Number? = null)

Adds an edge between two nodes in the graph, and creates the nodes if they don't exist.

If the graph is unweighted, the edge is added without a weight. If a weight is provided for an unweighted graph, an error is thrown. If the graph is weighted, a weight must be provided; otherwise, an error is thrown.

Parameters

node1

The starting node of the edge.

node2

The ending node of the edge.

weight

The weight of the edge (required for weighted graphs). Defaults to null.

Throws

IllegalStateException

If a weight is provided for an unweighted graph or if no weight is provided for a weighted graph.