Graph

class Graph<Vertex>

A Graph is an unordered collection of vertices, along with the successor-predecessor relationships between them. From the Graph's viewpoint, the vertices are merely mementos that support equals and hashCode. Edges are not explicitly represented, but instead are a consequence of how the outEdges and inEdges are populated.

Graph is not synchronized.

Author

Mark van Gulik

Parameters

Vertex

The vertex type with which to parameterize the Graph.

Constructors

Link copied to clipboard
fun <Vertex> Graph(graph: Graph<Vertex>)

Construct a new graph with the same vertices and edges as the argument.

Link copied to clipboard
fun Graph()

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class GraphPreconditionFailure : RuntimeException

A GraphPreconditionFailure is thrown whenever a precondition of a graph manipulation operation does not hold. The preconditions are described in the JavaDoc comment for each operation.

Functions

Link copied to clipboard
fun addEdge(sourceVertex: Vertex, targetVertex: Vertex)

Add an edge to the graph from the source vertex to the target vertex. Fail if either vertex is not present in the graph or if it already contains an edge from the source vertex to the target vertex.

Link copied to clipboard
fun addVertex(vertex: Vertex)

Add a vertex to the graph. Fail if the vertex is already present in the graph. The vertex initially has no edges within this graph.

Link copied to clipboard
fun addVertices(vertices: Collection<Vertex>)

Add a collection of vertices to the graph. Fail if any vertex is already present in the graph, or if it occurs multiple times in the given collection. The vertices initially have no edges within this graph.

Link copied to clipboard
fun ancestryOfAll(seeds: Collection<Vertex>): Graph<Vertex>

Create a subgraph containing each of the provided vertices and all of their ancestors.

Link copied to clipboard
fun clear()

Remove all edges and vertices from the graph.

Link copied to clipboard
fun exciseVertex(vertex: Vertex)

If the given vertex is present in the graph, remove its incoming and outgoing edges, then the vertex itself. If the given vertex is not in the graph, do nothing.

Link copied to clipboard
fun excludeEdge(sourceVertex: Vertex, targetVertex: Vertex)

Remove an edge from the graph, from the source vertex to the target vertex. Fail if either vertex is not present in the graph. If there is no such edge in the graph then do nothing.

Link copied to clipboard
fun excludeEdgesFrom(sourceVertex: Vertex)

Remove all edges from the graph which originate at the given vertex. Fail if the vertex is not present in the graph.

Link copied to clipboard
fun excludeEdgesTo(targetVertex: Vertex)

Remove all edges from the graph which terminate at the given vertex. Fail if the vertex is not present in the graph.

Link copied to clipboard
fun excludeVertex(vertex: Vertex)

Remove a vertex from the graph, removing any connected edges. Do nothing if the vertex is not in the graph.

Link copied to clipboard
fun includeEdge(sourceVertex: Vertex, targetVertex: Vertex)

Add an edge to the graph from the source vertex to the target vertex. Fail if either vertex is not present in the graph. If the graph already contains an edge from the source vertex to the target vertex then do nothing.

Link copied to clipboard
fun includesEdge(sourceVertex: Vertex, targetVertex: Vertex): Boolean

Determine if the graph contains an edge from the source vertex to the target vertex. Fail if either vertex is not present in the graph.

Link copied to clipboard
fun includesVertex(vertex: Vertex): Boolean

Determine if the given vertex is in the graph.

Link copied to clipboard
fun includeVertex(vertex: Vertex)

Add a vertex to the graph if it's not already present. If the vertex is already present, do nothing. If the vertex is added it initially has no edges within this graph.

Link copied to clipboard
fun parallelVisit(visitAction: (Vertex, () -> Unit) -> Unit)

Visit the vertices in DAG order. The action is invoked for each vertex, also passing a completion action to invoke when that vertex visit is considered complete, allowing successors for which all predecessors have completed to be visited.

Link copied to clipboard
fun parallelVisitThen(visitAction: (Vertex, () -> Unit) -> Unit, afterTraversal: () -> Unit)

Visit the vertices in DAG order. The action is invoked for each vertex, also passing a completion action to invoke when that vertex visit is considered complete, allowing successors for which all predecessors have completed to be visited.

Link copied to clipboard
fun predecessorsOf(vertex: Vertex): Set<Vertex>

Answer the set of predecessors of the specified vertex. Fail if the vertex is not present in the graph.

Link copied to clipboard
fun removeEdge(sourceVertex: Vertex, targetVertex: Vertex)

Remove an edge from the graph, from the source vertex to the target vertex. Fail if either vertex is not present in the graph, or if there is no such edge in the graph.

Link copied to clipboard
fun removeVertex(vertex: Vertex)

Remove a vertex from the graph, failing if it has any connected edges. Fail if the vertex is not present in the graph.

Link copied to clipboard
fun successorsOf(vertex: Vertex): Set<Vertex>

Answer the set of successors of the specified vertex. Fail if the vertex is not present in the graph.

Link copied to clipboard
fun withoutRedundantEdges(spanningDag: Graph<Vertex>): Graph<Vertex>

Given an acyclic graph, produce a subgraph that excludes any edges for which there is another path connecting those edges in the original graph.

Properties

Link copied to clipboard
val dagWithoutRedundantEdges: Graph<Vertex>

A subgraph of an acyclic graph that excludes any edges for which there is another path connecting those edges in the original graph.

Link copied to clipboard
val firstCycle: List<Vertex>

The first cycle in this cyclic graph, as a List of vertices in the order in which they occur in the cycle.

Link copied to clipboard
val isCyclic: Boolean

true iff the graph is cyclic, otherwise false.

Link copied to clipboard
val isEmpty: Boolean

true if the graph is empty, false otherwise.

Link copied to clipboard
val reverse: Graph<Vertex>

A copy of this Graph with the same vertices, but with every edge having the reverse direction.

Link copied to clipboard
val roots: Set<Vertex>

The roots of the graph. These are the vertices with no incoming edges.

Link copied to clipboard
val size: Int

The number of edges in the graph.

Link copied to clipboard
val spanningDag: Graph<Vertex>

An acyclic subgraph of the receiver, containing all vertices, and a locally maximal subset of the edges (i.e., no additional edge of the original graph can be added without making the resulting graph acyclic).

Link copied to clipboard
val vertexCount: Int

The vertex count of the graph.

Link copied to clipboard
val vertices: Set<Vertex>

The graph's vertices.