# 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`.

• ``` init() ```

Creates an empty graph.

#### Declaration

Swift

``public init()``
• ``` count ```

Number of vertices stored in the graph.

#### Declaration

Swift

``public var count: Int``
• ``` isEmpty ```

Returns `true` if and only if `count == 0`.

#### Declaration

Swift

``public var isEmpty: Bool``
• ``` isComplete ```

Returns `true` if every pair of distinct vertices is connected by a unique edge (one in each direction).

#### Declaration

Swift

``public var isComplete: Bool``
• ``` isAcyclic ```

Returns `true` if there’s not a path starting and ending at the same vertex.

#### Declaration

Swift

``public var isAcyclic: Bool``
• ``` vertices ```

Returns the vertices stored in the graph.

#### Declaration

Swift

``public var vertices: Set<Vertex>``
• ``` edges ```

Returns the edges stored in the graph.

#### Declaration

Swift

``public var edges: [Edge]``
• ``` containsVertex(_:) ```

Returns `true` if the graph contains the given vertex.

#### Declaration

Swift

``public func containsVertex(_ vertex: Vertex) -> Bool``
• ``` containsEdgeFrom(_:to:) ```

Returns `true` if the graph contains an edge from `source` to `destination`. Subscript notation is preferred.

#### Declaration

Swift

``public func containsEdgeFrom(_ source: Vertex, to destination: Vertex) -> Bool``
• ``` edgeFrom(_:to:) ```

Returns the edge connecting `source` to `destination` if it exists. Subscript notation is preferred.

#### Declaration

Swift

``public func edgeFrom(_ source: Vertex, to destination: Vertex) -> Edge?``
• ``` neighbors(_:) ```

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>``
• ``` pathFrom(_:to:) ```

Returns an array of vertices representing a path from `source` to `destination`, if it exists.

#### Declaration

Swift

``public func pathFrom(_ source: Vertex, to destination: Vertex) -> [Vertex]?``

#### Return Value

An array containing at least two vertices (`source` and `destination`) or nil.

• ``` generateAt(_:order:) ```

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>``
• ``` insertVertex(_:) ```

Inserts the given vertex to the graph if it doesn’t exist.

#### Declaration

Swift

``public mutating func insertVertex(_ vertex: Vertex)``
• ``` removeVertex(_:) ```

Removes and returns the given vertex from the graph if it was present.

#### Declaration

Swift

``public mutating func removeVertex(_ vertex : Vertex) -> Vertex?``
• ``` insertEdge(_:from:to:) ```

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)``
• ``` removeEdgeFrom(_:to:) ```

Removes and returns the edge connecting the given vertices if it exists.

#### Declaration

Swift

``public mutating func removeEdgeFrom(_ source: Vertex, to destination: Vertex) -> Edge?``
• ``` removeAll(keepingCapacity:) ```

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)``
• ``` makeIterator() ```

Provides for-in loop functionality.

#### Declaration

Swift

``public func makeIterator() -> AnyIterator<Vertex>``

#### Return Value

A generator over the vertices.