UniqueElementsGraph

open class UniqueElementsGraph<V, E> : Graph where V : Decodable, V : Encodable, V : Equatable, E : Equatable, E : Edge

An implementation Graph that ensures there are no pairs of equal vertices and no repeated edges.

  • Undocumented

    Declaration

    Swift

    public var vertices: [V]
  • Undocumented

    Declaration

    Swift

    public var edges: [[E]]
  • Undocumented

    Declaration

    Swift

    public init()
  • Init the Graph with vertices, but removes duplicates. O(n^2)

    Declaration

    Swift

    required public init(vertices: [V])
  • Add a vertex to the graph if no equal vertex already belongs to the Graph. O(n)

    Declaration

    Swift

    public func addVertex(_ v: V) -> Int

    Parameters

    v

    The vertex to be added.

    Return Value

    The index where the vertex was added, or the index of the equal vertex already belonging to the graph.

  • Add an edge to the graph. Only allow the edge to be added once

    Declaration

    Swift

    public func addEdge(_ e: E, directed: Bool = false)

    Parameters

    e

    The edge to add.

    directed

    If false, undirected edges are created. If true, a reversed edge is also created. Default is false.

Available where E == UnweightedEdge

  • Creates a new UniqueVerticesGraph that is the union of several UniqueVerticesGraphs.

    This operation is commutative in the sense that g1 ∪ g2 has the same vertices and edges than g2 ∪ g1. However, the indices of the vertices are not guaranteed to be conserved.

    This operation is O(k*n^2), where k is the number of graphs and n the number of vertices of the largest graph.

    Declaration

    Swift

    static func unionOf(_ graphs: [UniqueElementsGraph]) -> UniqueElementsGraph

    Parameters

    graphs

    Array of graphs to build the union from.

  • Undocumented

    Declaration

    Swift

    static func unionOf(_ graphs: UniqueElementsGraph...) -> UniqueElementsGraph
  • Initialize an UniqueElementsGraph consisting of path.

    The resulting graph has the vertices in path and an edge between each pair of consecutive vertices in path.

    If path is an empty array, the resulting graph is the empty graph. If path is an array with a single vertex, the resulting graph has that vertex and no edges.

    Declaration

    Swift

    public static func withPath(_ path: [V], directed: Bool = false) -> UniqueElementsGraph

    Parameters

    path

    An array of vertices representing a path.

    directed

    If false, undirected edges are created. If true, edges are directed from vertex i to vertex i+1 in path. Default is false.

  • Initialize an UniqueElementsGraph consisting of cycle.

    The resulting graph has the vertices in cycle and an edge between each pair of consecutive vertices in cycle, plus an edge between the last and the first vertices.

    If path is an empty array, the resulting graph is the empty graph. If path is an array with a single vertex, the resulting graph has the vertex and a single edge to itself if directed is true. If directed is false the resulting graph has the vertex and two edges to itself.

    Declaration

    Swift

    public static func withCycle(_ cycle: [V], directed: Bool = false) -> UniqueElementsGraph

    Parameters

    cycle

    An array of vertices representing a cycle.

    directed

    If false, undirected edges are created. If true, edges are directed from vertex i to vertex i+1 in cycle. Default is false.

  • Construct a UniqueElementsGraph by repeatedly applying a recursion function to a vertex and adding them to the graph.

    The recursion function is only called on a vertex when visited for the first time.

    Declaration

    Swift

    public static func fromRecursion(_ recursion: (V) -> [V], startingWith initialVertex: V) -> UniqueElementsGraph

    Parameters

    recursion

    A function that returns the neighbouring vertices for a given visited vertex.

    initialVertex

    The first vertex to which the recursion function is applied.

  • Construct a UniqueElementsGraph by repeatedly applying a recursion function to some elements and adding the corresponding vertex to the graph.

    The recursion function is only called on an element when visited for the first time.

    Declaration

    Swift

    public static func fromRecursion<T>(_ recursion: (T) -> [T], selectingVertex vertexFor: (T) -> V, startingWith initialElement: T) -> UniqueElementsGraph

    Parameters

    recursion

    A function that returns the neighbouring elements for a given visited element.

    vertexFor

    A function that returns the vertex that will be added to the graph for each visited element.

    initialElement

    The first element to which the recursion function is applied.

Available where V: Hashable, E == UnweightedEdge