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
trueif and only ifcount == 0.Declaration
Swift
public var isEmpty: Bool -
Returns
trueif every pair of distinct vertices is connected by a unique edge (one in each direction).Declaration
Swift
public var isComplete: Bool -
Returns
trueif 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
trueif the graph contains the given vertex.Declaration
Swift
public func containsVertex(_ vertex: Vertex) -> Bool -
Returns
trueif the graph contains an edge fromsourcetodestination. Subscript notation is preferred.Declaration
Swift
public func containsEdgeFrom(_ source: Vertex, to destination: Vertex) -> Bool -
Returns the edge connecting
sourcetodestinationif 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
sourcetodestination, if it exists.Declaration
Swift
public func pathFrom(_ source: Vertex, to destination: Vertex) -> [Vertex]?Return Value
An array containing at least two vertices (
sourceanddestination) 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.
View on GitHub
Graph Struct Reference