WeightedUndirectedAList

final public class WeightedUndirectedAList<Vertex: Hashable> :
                AdjacencyList<Vertex>, WeightedUndirectedGraph

Implementation of a weighted undirected graph using an adjacency list.

The weights are stored separately from the edges, in a dictionary of dictionaries. Weights are Doubles.

  • Undocumented

    Declaration

    Swift

    final public class WeightedUndirectedAList<Vertex: Hashable> :
                    AdjacencyList<Vertex>, WeightedUndirectedGraph
  • Computed property which is an array of edges in the graph, represented as (Vertex, Vertex) tuples. Read-only.

    Declaration

    Swift

    public var edges: [(Vertex, Vertex)]
  • Computes the weight associated with the given edge.

    Throws

    GraphError.VertexNotPresent if either vertex not in the graph.

    Declaration

    Swift

    public func weight(from: Vertex, to: Vertex) throws -> Double?

    Parameters

    to

    The source of the desired edge.

    from

    The destination of the desired edge. Note: Since this is an undirected graph, the order of the parameters does not matter. weight(from, to) == weight(to, from)

    Return Value

    The weight associated with the given edge, a Double, or nil if the edge is not in the graph.

  • Adds a new edge of unspecified weight to the graph.

    Required to satisfy graph protocol - this is bad. Currently, this will add a zero-weight edge.

    Probably don’t use it when using a weighted adjacency list.

    Declaration

    Swift

    public func addEdge(from: Vertex, to: Vertex) throws
  • Adds a new weighted edge to the graph from one vertex to another.

    Changes the graph in-place to add the weighted edge.

    Throws

    GraphError.VertexNotPresent if either vertex does not exist in the graph.

    Declaration

    Swift

    public func addEdge(from: Vertex, to: Vertex, weight: Double) throws

    Parameters

    from

    The source of the edge to add

    to

    The destination of the edge to add

    weight

    The weight of the edge to add

  • Remove an edge between two given vertices in the graph.

    Throws

    GraphError.VertexNotPresent if those vertices don’t exist, and GraphError.EdgeNotPresent if the edge doesn’t exist.

    Declaration

    Swift

    public func removeEdge(from: Vertex, to: Vertex) throws

    Parameters

    from

    the source of the edge.

    to

    the destination of the edge to remove. These are interchangable in the undirected graph here.