Package-level declarations

Types

Link copied to clipboard
abstract class AbstractSearchStrategy<T, W, S>(val initialState: S) : SearchStrategy<T, W, S>

A base SearchStrategy taking care of the common bookkeeping of a graph traversal (the "fringe" of edges still to explore, and driving search as a lazy Sequence), so that concrete strategies (see BreadthFirst and DepthFirst) only need to implement selectNextVisit, deciding, at each step, which edge in the fringe to visit next and how to grow the fringe from there.

Link copied to clipboard
class BreadthFirst<T, W>(maxDepth: Int = -1) : AbstractSearchStrategy<T, W, Int>

A SearchStrategy visiting a Graph breadth-first, i.e. level by level: all nodes at depth 0 (the source), then all nodes at depth 1, and so on. Each yielded Visit.state is the depth (an Int, starting at 0) at which the corresponding node was reached.

Link copied to clipboard
class DepthFirst<T, W>(maxDepth: Int = -1, postOrder: Boolean = false) : AbstractSearchStrategy<T, W, Int>

A SearchStrategy visiting a Graph depth-first: from the source, it follows one outgoing edge as deep as possible before backtracking to explore the next one. Each yielded Visit.state is the depth (an Int, starting at 0) at which the corresponding node was reached.

Link copied to clipboard
interface Edge<T, W>

A directed, optionally-weighted edge of a Graph, going from source to destination; an unweighted edge (as used e.g. by Graph.build<T, Nothing> { ... }) simply carries a null weight. Build one via Edge.of, or the edgeOf top-level shorthand. Two Edges connecting the same source and destination with the same weight are ==-equal, since the default implementation returned by Edge.of is a data class; a bidirectional connection is modelled as two separate Edges (see MutableGraph.connect).

Link copied to clipboard
interface Graph<T, W> : Iterable<Edge<T, W>>

An immutable, directed, optionally-weighted graph over nodes carrying a payload of type T, with edges carrying an optional weight of type W. Iterating a Graph (it implements Iterable) yields its edges.

Link copied to clipboard
interface MutableGraph<T, W> : Graph<T, W>

A mutable variant of Graph: add/remove (and their operator aliases plusAssign/minusAssign) change this instance in place, unlike the copy-on-write Graph.plus/Graph.minus. This is the type to reach for when building up a graph incrementally, or when applying many changes in a row without paying the cost of a defensive copy for each of them (as Graph.plus/Graph.minus would); see Graph.build and MutableGraph.build to construct one via a builder block:

Link copied to clipboard
interface Node<T>

A node of a Graph, wrapping a payload value of type T. A Node is a distinct, identity-less wrapper around value (two Nodes built from ==-equal values are themselves ==-equal, since the default implementation returned by Node.of is a data class), so that the same graph can, e.g., use plain Strings or numbers as node payloads without those values having to implement any graph-specific interface. Build one via Node.of, or the nodeOf top-level shorthand.

Link copied to clipboard
interface SearchStrategy<T, W, S>

A pluggable graph-traversal algorithm, generic over the kind of state it threads through the traversal (e.g. the current depth, for BreadthFirst/DepthFirst). Rather than Graph baking in a fixed set of traversal orders, it delegates to a SearchStrategy via Graph.asSequence/Graph.asIterable, so new orders (e.g. a custom best-first search) can be added without touching Graph itself. See AbstractSearchStrategy for a base class handling the traversal's bookkeeping (the fringe of edges still to explore), leaving concrete strategies to only decide which edge to explore next.

Link copied to clipboard
interface Visit<T, S>

One step of a graph traversal, as produced by a SearchStrategy: the node reached by the traversal, paired with the traversal's state at the time it was reached (e.g. the current depth for it.unibo.tuprolog.utils.graphs.BreadthFirst/it.unibo.tuprolog.utils.graphs.DepthFirst, both of which use an Int state). Graph.asSequence (and Graph.asIterable) yield a stream of Visits, one per traversed node. Build one via Visit.of, or the visitOf top-level shorthand.

Properties

Link copied to clipboard

Whether this graph contains no directed cycles, checked by repeatedly stripping away leaf nodes (nodes with no outgoing edges) from a working copy until either no nodes are left (the original graph is acyclic) or no leaf remains despite nodes still being present (there is a cycle among them). This makes a mutable copy of the whole graph and is therefore O(nodes + edges), not free to call repeatedly on a large graph.

Link copied to clipboard
val <T, W> Graph<T, W>.isTree: Boolean

Whether this graph is a tree/forest, i.e. every node has at most one incoming edge. Note that this alone does not rule out cycles (see isAcyclic for that); e.g. a single node with a self-loop satisfies this check.

Functions

Link copied to clipboard
fun <T, W> Graph<T, W>.copy(f: MutableGraph<T, W>.() -> Unit): Graph<T, W>

Returns a new, immutable Graph, obtained by applying the in-place changes performed by f to a mutable copy of this graph. Handy to apply several changes to an (otherwise immutable) Graph at once, without paying the cost of a defensive copy for each individual change.

Link copied to clipboard
fun <T, W> edgeOf(node1: Node<T>, node2: Node<T>, weight: W? = null): Edge<T, W>

Shorthand for Edge.of.

Link copied to clipboard
fun <T, W> Graph<T, W>.filter(p: (Edge<T, W>) -> Boolean): Graph<T, W>

Returns a new Graph, keeping only the edges of this graph that satisfy p.

Link copied to clipboard
fun <T, W> Graph<T, W>.isLeaf(node: Node<T>): Boolean

Whether node has no outgoing edges in this graph, i.e. outdegree(node) == 0.

Link copied to clipboard
fun <T1, W1, T2, W2> Graph<T1, W1>.map(f: (Edge<T1, W1>) -> Edge<T2, W2>): Graph<T2, W2>

Returns a new Graph, obtained by transforming every edge of this graph via f (which may change the type of the payload/weight, or entirely rewire an edge's endpoints).

Link copied to clipboard
fun <T> nodeOf(value: T): Node<T>

Shorthand for Node.of.

Link copied to clipboard
fun <T, S> visitOf(state: S, node: Node<T>): Visit<T, S>

Shorthand for Visit.of.