AdjacencyMatrix

public class AdjacencyMatrix<Vertex: Hashable>

Adjacency matrices for general graphs.

The AdjacencyMatrix class is only capable of keeping track of the size and shape of the matrix. To use adjacency matrices as graphs, use its subclasses, UndirectedAMatrix and DirectedAMatrix.

  • Initializer that takes a collection of vertices, and starts with no edges.

    Complexity

    O(vertices.count²)

    Declaration

    Swift

    required public init<V: CollectionType
                    where V.Generator.Element == Vertex> (vertices: V)

    Parameters

    vertices

    A collection of vertices to include initially.

  • Empty initializer.

    This initializer is not recommended for large graphs, because adding vertices to an existing graph is an expensive operation.

    Complexity

    O(1)

    Declaration

    Swift

    required public init()
  • A computed array of all the vertices in the graph.

    Complexity

    O(V)

    Declaration

    Swift

    final public var vertices: [Vertex]
  • Adds a new vertex to the graph with no edges connected to it.

    Changes the graph in-place to add the vertex. Note that this is a very slow operation on adjacency matrices; it is better to create the matrix with all needed vertices.

    Throws

    GraphError.VertexAlreadyPresent if vertex is already in the graph.

    Complexity

    O(V²)

    Declaration

    Swift

    final public func addVertex(vertex: Vertex) throws

    Parameters

    vertex

    The vertex to add.

  • Removes a vertex and all edges connected to it.

    Note that this is a very slow operation on adjacency matrices.

    Throws

    GraphError.VertexNotPresent if the vertex to be deleted is not in the graph.

    Complexity

    O(V²)

    Declaration

    Swift

    final public func removeVertex(vertex: Vertex) throws

    Parameters

    vertex

    The vertex to remove.