Graph
public struct Graph<Vertex: Hashable, Edge>
A simple directed graph: that is, one with no loops (edges
connected at both ends to the same vertex) and no more than one edge
in the same direction between any two different vertices.
All vertices in the graph are unique. Edges are not allowed to be generic types.
The preferred way to acess and insert vertices or edges is using subscript notation. Example:
graph["NY", "Boston"] = distance
Conforms to Sequence
.
-
Creates an empty graph.
Declaration
Swift
public init()
-
Number of vertices stored in the graph.
Declaration
Swift
public var count: Int
-
Returns
true
if and only ifcount == 0
.Declaration
Swift
public var isEmpty: Bool
-
Returns
true
if every pair of distinct vertices is connected by a unique edge (one in each direction).Declaration
Swift
public var isComplete: Bool
-
Returns
true
if there’s not a path starting and ending at the same vertex.Declaration
Swift
public var isAcyclic: Bool
-
Returns the vertices stored in the graph.
Declaration
Swift
public var vertices: Set<Vertex>
-
Returns the edges stored in the graph.
Declaration
Swift
public var edges: [Edge]
-
Returns
true
if the graph contains the given vertex.Declaration
Swift
public func containsVertex(_ vertex: Vertex) -> Bool
-
Returns
true
if the graph contains an edge fromsource
todestination
. Subscript notation is preferred.Declaration
Swift
public func containsEdgeFrom(_ source: Vertex, to destination: Vertex) -> Bool
-
Returns the edge connecting
source
todestination
if it exists. Subscript notation is preferred.Declaration
Swift
public func edgeFrom(_ source: Vertex, to destination: Vertex) -> Edge?
-
Returns the set of direct successors of the given vertex. An empty set is returned if the vertex does not exist.
Declaration
Swift
public func neighbors(_ source: Vertex) -> Set<Vertex>
-
Returns an array of vertices representing a path from
source
todestination
, if it exists.Declaration
Swift
public func pathFrom(_ source: Vertex, to destination: Vertex) -> [Vertex]?
Return Value
An array containing at least two vertices (
source
anddestination
) or nil. -
Returns a generator iterating over the vertices in the specified order starting at the given vertex. Valid options are .DepthFirst and .BreadthFirst.
The generator never returns the same vertex more than once.
Declaration
Swift
public func generateAt(_ source: Vertex, order: GraphTraversalOrder) -> AnyIterator<Vertex>
-
Inserts the given vertex to the graph if it doesn’t exist.
Declaration
Swift
public mutating func insertVertex(_ vertex: Vertex)
-
Removes and returns the given vertex from the graph if it was present.
Declaration
Swift
public mutating func removeVertex(_ vertex : Vertex) -> Vertex?
-
Connects two vertices with the given edge. If an edge already exists, it is replaced. Subscript notation is preferred.
Declaration
Swift
public mutating func insertEdge(_ edge: Edge, from source: Vertex, to destination: Vertex)
-
Removes and returns the edge connecting the given vertices if it exists.
Declaration
Swift
public mutating func removeEdgeFrom(_ source: Vertex, to destination: Vertex) -> Edge?
-
Removes all vertices and edges from the graph, and by default clears the underlying storage buffer.
Declaration
Swift
public mutating func removeAll(keepingCapacity keep: Bool = false)
-
Provides for-in loop functionality.
Declaration
Swift
public func makeIterator() -> AnyIterator<Vertex>
Return Value
A generator over the vertices.