WeightedDirectedAList

final public class WeightedDirectedAList<Vertex: Hashable> :
                AdjacencyList<Vertex>, WeightedDirectedGraph

A weighted directed Adjacency List implementation of weighted, directed graphs. Similar to the WeightedUndirected adjacency list, but with asymmetric edges.

Weights are stored separately from edges, in a nested dictionary of type [Vertex: [Vertex: Double]].

  • Undocumented

    Declaration

    Swift

    final public class WeightedDirectedAList<Vertex: Hashable> :
                    AdjacencyList<Vertex>, WeightedDirectedGraph
  • A 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, by looking it up in the weight table.

    Like other methods on directed graphs, the order of the parameters is important. weight(from, to) may exist while weight(to, from) is nil.

    Throws

    GraphError.VertexNotPresent if either vertex doesn’t exist.

    Declaration

    Swift

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

    Parameters

    from

    the source vertex of the desired edge.

    to

    the destination vertex of the desired edge.

    Return Value

    The weight of the edge (from -> to), a Double, or nil if the edge does not exist in the graph.

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

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

    Probably don’t use it with weighted adjacency lists.

    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 doesn’t exist.

    Declaration

    Swift

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

    Parameters

    from

    The source of the edge to add.

    to

    The source of the edge to add.

    weight

    The weight of the egde to add, a double.

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

    Throws

    GraphError.VertexNotPresent if those vertices don't exist, andGraphError.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 remove.

    to

    The destination of the edge to remove. Not interchangable. This removes (from -> to) but doesn’t touch the edge (to -> from).