DirectedGraph
public struct DirectedGraph<Vertex, Label> : Sendable where Vertex : Hashable, Vertex : Sendable, Label : Sendable
extension DirectedGraph: Equatable where Label: Equatable
extension DirectedGraph: Hashable where Label: Hashable
A finite, directed graph.
Note
UseDirectedGraph<V, NoLabel> rather than DirectedGraph<V, Void> to implement an
unlabeled graph. Unlike Void, NoLabel conforms to Equatable, allowing the graph itself
to be Equatable.
-
A collection with the outgoing edges of a vertex.
Declaration
Swift
public typealias OutgoingEdges = [Vertex : Label] -
An edge between two vertices.
See moreDeclaration
Swift
public struct Edge : Sendableextension DirectedGraph.Edge: Equatable where Label: Equatableextension DirectedGraph.Edge: Comparable where Vertex: Comparable, Label: Comparable -
The type of a table mapping a vertex to its outgoing edges.
Declaration
Swift
private typealias Out = [Vertex : OutgoingEdges] -
A table from a vertex to its outgoing edges.
Declaration
Swift
private var out: Out -
Creates an empty graph.
Declaration
Swift
public init() -
The vertices of the graph.
Declaration
Swift
public var vertices: some Collection<Vertex> { get } -
The edges of the graph.
Declaration
Swift
public var edges: some Collection<Edge> { get } -
Returns the vertices in
selfgathered in a breadth-first manner fromroot, applyingedgeFilterto edges.Declaration
Swift
public func bfs( from root: Vertex, edgeFilter: ((Label) -> Bool)? = nil ) -> BreadthFirstSequence<Vertex, Label> -
The type of action to perform after exploring a vertex.
See moreDeclaration
Swift
public enum ExplorationContinuation -
Explores the graph in a breadth-first manner starting from
start, considering the edges indicated byedgeFilter, callingactionfor each visited vertex.When calling
action, the vertex and its successors are passed as arguments. The return value ofactiondetermines how the exploration continues.Use this method when a simple exploration with
bfs(from:)is not sufficient, for example to skip parts of the graph or to stop the exploration early.Declaration
Swift
public func withBFS<C: Collection<Vertex>>( _ start: C, edgeFilter: ((Label) -> Bool)? = nil, _ action: (Vertex, [Vertex]) -> ExplorationContinuation ) -
Returns
trueiff there exists a path fromutovin the graph.Complexity
O(m) where m is the number of edges in the graph.Declaration
Swift
public func isReachable(_ v: Vertex, from u: Vertex) -> Bool -
Inserts
vinselfand returnstrueif it was not already present.Declaration
Swift
@discardableResult public mutating func insertVertex(_ v: Vertex) -> Bool -
Inserts an edge from
sourcetotarget, labeled bylabel.Complexity
O(1).
Declaration
Swift
@discardableResult public mutating func insertEdge( from source: Vertex, to target: Vertex, labeledBy label: Label ) -> (inserted: Bool, labelAfterInsert: Label)Return Value
(true, label)if there was no edge betweensourceandtarget. Otherwise,(false, currentLabel), wherecurrentLabelis label of the existing edge. -
Removes the edge from
sourcetotarget.Complexity
O(1).
Declaration
Swift
@discardableResult public mutating func removeEdge(from source: Vertex, to target: Vertex) -> Label?Return Value
The label of the removed edge, or
nilif there was no edge to remove. -
Accesses the label on the edge from
sourcetotarget.Declaration
Swift
public subscript(from source: Vertex, to target: Vertex) -> Label? { get set }Return Value
If there exists a edge from from
sourcetotarget, the label of that edge. Otherwise,nil. -
Accesses the outgoing edges of
source.Complexity
O(1).Declaration
Swift
public subscript(from source: Vertex) -> OutgoingEdges { get set }
-
Inserts an edge from
sourcetotarget, labeled bylabel.Complexity
O(1).
Declaration
Swift
@discardableResult public mutating func insertEdge(from source: Vertex, to target: Vertex) -> BoolReturn Value
trueif there was no edge betweensourceandtarget. Otherwise,false.
-
Declaration
Swift
public static func == (l: `Self`, r: `Self`) -> Bool
-
Declaration
Swift
public func hash(into hasher: inout Hasher)
View on GitHub