BidirectionalAdjacencyList
public struct BidirectionalAdjacencyList<
Vertex: DefaultInitializable,
Edge: DefaultInitializable,
RawId: BinaryInteger & IdIndexable
>: DirectedAdjacencyListProtocol, SearchDefaultsGraph where RawId.Stride: SignedInteger
extension BidirectionalAdjacencyList: BidirectionalGraph
A general purpose, bidirectional adjacency list graph.
BidirectionalAdjacencyList implements a bidirectional graph. Additionally, parallel edges are supported.
BidirectionalAdjacencyList also allows storing arbitrary additional data with each vertex and
edge. If you select a zero-sized type (such as Empty
), all overhead is optimized away by the
Swift compiler.
Note: because tuples cannot yet conform to protocols, we have to use a separate type (
Empty
) instead ofVoid
.
Operations that do not modify the graph structure occur in O(1) time. Additional operations that
run in O(1) (amortized) time include: adding a new edge, and adding a new vertex. Operations that
remove either vertices or edges invalidate existing VertexId
s and EdgeId
s. Adding new
vertices or edges do not invalidate previously retrieved ids.
BidirectionalAdjacencyList is parameterized by the RawId
which can be carefully tuned to save memory.
A good default is Int32
, unless you are trying to represent more than 2^32 vertices.
-
The name of a vertex in this graph.
Note:
VertexId
‘s are not stable across some graph mutation operations.Declaration
Swift
public typealias VertexId = RawId
-
The name of an edge in this graph.
Note:
EdgeId
‘s are not stable across some graph mutation operations.Declaration
Swift
public typealias EdgeId = _AdjacencyList_DirectedEdgeId<RawId>
-
A collection of all
VertexId
‘s inself
.Declaration
Swift
public typealias VertexCollection = Range<RawId>
-
Data structure storing per-edge information.
Declaration
Swift
public typealias _EdgeData = _AdjacencyList_BidirectionalPerEdge<VertexId, Edge>
-
Data structure storing per-vertex data.
Declaration
Swift
public typealias _VertexData = _AdjacencyList_BidirectionalPerVertex<Vertex, _EdgeData>
-
The collection of all edges in
self
.Declaration
Swift
public typealias EdgeCollection = _AdjacencyList_DirectedEdgeCollection<_Storage>
-
The collection of all edges whose origin is a given vertex.
Declaration
Swift
public typealias VertexEdgeCollection = _AdjacencyList_DirectedVertexEdgeCollection<_EdgeData>
-
The parallel projection of
self
.Declaration
Swift
public typealias ParallelProjection = _DirectedAdjacencyList_ParallelProjection<_VertexData>
-
The graph’s storage!
Declaration
Swift
public var _storage: _Storage
-
Initialize an empty BidirectionalAdjacencyList.
Declaration
Swift
public init()
-
All
VertexId
‘s inself
.Declaration
Swift
public var vertices: Range<RawId> { get }
-
All edges originating from
vertex
.Declaration
Swift
public func edges(from vertex: VertexId) -> VertexEdgeCollection
-
The number of edges originating from
vertex
.Declaration
Swift
public func outDegree(of vertex: VertexId) -> Int
-
Accesses the arbitrary data associated with
vertex
.Declaration
Swift
public subscript(vertex vertex: VertexId) -> Vertex { get set }
-
Accesses the arbitrary data associated with
edge
.Declaration
Swift
public subscript(edge edge: EdgeId) -> Edge { get set }
-
A reasonable color map implementation.
Declaration
Swift
public typealias DefaultColorMap = TablePropertyMap<`Self`, VertexId, VertexColor>
-
Makes a default color map where every vertex is set to
color
.Declaration
Swift
public func makeDefaultColorMap(repeating color: VertexColor) -> DefaultColorMap
-
Makes a default int map for every vertex.
Declaration
Swift
public func makeDefaultVertexIntMap(repeating value: Int) -> TablePropertyMap<`Self`, VertexId, Int>
-
Makes a default vertex property map mapping vertices.
Declaration
Swift
public func makeDefaultVertexVertexMap(repeating vertex: VertexId) -> TablePropertyMap<`Self`, VertexId, VertexId>
-
Hints
self
to reserve space for a total ofvertexCount
vertices.Declaration
Swift
public mutating func reserveCapacity(vertexCount: Int)
-
Removes all edges from
u
tov
.If there are parallel edges, it removes all edges.
Precondition
u
andv
are vertices inself
.Complexity
worst case O(|E|)Declaration
Return Value
true if one or more edges were removed; false otherwise.
-
Removes
edge
.Declaration
Swift
public mutating func remove(_ edge: EdgeId)
-
Removes all edges that
shouldBeRemoved
.Declaration
Swift
public mutating func removeEdges(where shouldBeRemoved: (EdgeId, `Self`) -> Bool)
-
Remove all out edges of
vertex
that satisfy the given predicate.Complexity
O(|E|)Declaration
-
Removes all edges from
vertex
.Complexity
O(|E|)Declaration
Swift
public mutating func clear(vertex: VertexId)
-
Removes
vertex
from the graph.Complexity
O(|E| + |V|)Declaration
Swift
public mutating func remove(_ vertex: VertexId)
-
Adds a new vertex with associated
vertexProperty
, returning its identifier.Complexity
O(1) (amortized)Declaration
Swift
public mutating func addVertex(storing vertexProperty: Vertex) -> VertexId
-
Adds a new edge from
source
todestination
and associatededgeProperty
, returning its identifier.Complexity
O(1) (amortized)Declaration
-
Declaration
Swift
public typealias VertexInEdgeCollection = [EdgeId]
-
Returns the collection of all edges whose destination is
vertex
.Declaration
Swift
public func edges(to vertex: VertexId) -> VertexInEdgeCollection
-
Returns the number of edges whose destination is
vertex
.Declaration
Swift
public func inDegree(of vertex: VertexId) -> Int