IncidenceGraph
public protocol IncidenceGraph : GraphProtocol
A graph that allows retrieval of edges from each vertex.
-
The collection of edges originating from a given vertex.
Declaration
Swift
associatedtype VertexEdgeCollection : Collection where Self.EdgeId == Self.VertexEdgeCollection.Element
-
Returns the collection of edges whose source is
vertex
.Declaration
Swift
func edges(from vertex: VertexId) -> VertexEdgeCollection
-
Returns the source
VertexId
ofedge
.Declaration
Swift
func source(of edge: EdgeId) -> VertexId
-
Returns the source
VertexId
ofedge
.Declaration
Swift
func destination(of edge: EdgeId) -> VertexId
-
outDegree(of:
Default implementation) Returns the number of edges whose source is
vertex
.Default Implementation
Returns the number of edges whose source is
vertex
.Declaration
Swift
func outDegree(of vertex: VertexId) -> Int
-
localClusteringCoefficient(of:
Extension method) Returns the local clustering coefficient of the
neighborhood
.Declaration
Swift
public func localClusteringCoefficient<C: Collection>(of neighborhood: C) -> Double where C.Element == VertexId
-
BFSCallback
Extension methodA hook to observe events that occur during depth first search.
Declaration
Swift
public typealias BFSCallback = (BFSEvent<Self>, inout Self) throws -> Void
-
BFSCallbackWithWorkList
Extension methodA hook to (1) observe events that occur during depth first search, and (2) to optionally modify the work list of vertices.
Declaration
Swift
public typealias BFSCallbackWithWorkList<WorkList: Queue> = (BFSEvent<Self>, inout Self, inout WorkList) throws -> Void
-
Runs breadth first search on
self
usingvertexVisitationState
to keep track of search progress;callback
is invoked at key events during the search.Precondition
vertexVisitationState
must be initialized for everyVertexId
inGraph
to be.white
. (Note: this precondition is not checked.)Precondition
startVertices
is non-empty.Declaration
Swift
public mutating func breadthFirstSearch< VertexVisitationState: PropertyMap, WorkList: Queue, StartVertices: Collection >( startingAt startVertices: StartVertices, workList: inout WorkList, vertexVisitationState: inout VertexVisitationState, callback: BFSCallbackWithWorkList<WorkList> ) rethrows where VertexVisitationState.Graph == Self, VertexVisitationState.Key == VertexId, VertexVisitationState.Value == VertexColor, WorkList.Element == VertexId, StartVertices.Element == VertexId
-
breadthFirstSearch(startingAt:
Extension methodvertexVisitationState: callback: ) Runs breadth first search on
self
starting fromstartVertices
;callback
is invoked at key events during the search.Precondition
startVertices
is non-empty.Declaration
Swift
public mutating func breadthFirstSearch< StartVertices: Collection, VertexVisitationState: PropertyMap >( startingAt startVertices: StartVertices, vertexVisitationState: inout VertexVisitationState, callback: BFSCallback ) rethrows where StartVertices.Element == VertexId, VertexVisitationState.Graph == Self, VertexVisitationState.Key == VertexId, VertexVisitationState.Value == VertexColor
-
DFSCallback
Extension methodA hook to observe events that occur during depth first search.
Declaration
Swift
public typealias DFSCallback = (DFSEvent<Self>, inout Self) throws -> Void
-
depthFirstSearch(startingAt:
Extension methodvertexVisitationState: callback: ) Expores
self
depth-first starting atsource
, usingvertexVisitationState
to keep track of visited vertices, invokingcallback
at key events of the search.Note
this is a mutating method because thevertexVisitationState
orvisitor
may store data within the graph itself.Precondition
VertexVisitationState
has been initialized for every vertex to.white
.Declaration
Swift
public mutating func depthFirstSearch< VertexVisitationState: PropertyMap >( startingAt source: VertexId, vertexVisitationState: inout VertexVisitationState, callback: DFSCallback ) rethrows where VertexVisitationState.Graph == Self, VertexVisitationState.Key == VertexId, VertexVisitationState.Value == VertexColor
-
DijkstraSearchCallback
Extension methodA hook to observe events that occur during Dijkstra’s search.
Declaration
Swift
public typealias DijkstraSearchCallback = (DijkstraSearchEvent<Self>, inout Self) throws -> Void
-
dijkstraSearch(startingAt:
Extension methodvertexVisitationState: distancesToVertex: edgeLengths: workList: workListIndex: effectivelyInfinite: callback: ) Executes Dijkstra’s graph search algorithm in
self
using the supplied property maps;callback
is called at key events during the search.Declaration
Swift
public mutating func dijkstraSearch< Distance: Comparable & AdditiveArithmetic, EdgeLengths: PropertyMap, DistancesToVertex: PropertyMap, VertexVisitationState: PropertyMap, WorkList: RandomAccessCollection & RangeReplaceableCollection & MutableCollection, WorkListIndex: PriorityQueueIndexer & IndexProtocol >( startingAt startVertex: VertexId, vertexVisitationState: inout VertexVisitationState, distancesToVertex: inout DistancesToVertex, edgeLengths: EdgeLengths, workList: WorkList, workListIndex: WorkListIndex, effectivelyInfinite: Distance, callback: DijkstraSearchCallback ) rethrows where EdgeLengths.Graph == Self, EdgeLengths.Key == EdgeId, EdgeLengths.Value == Distance, DistancesToVertex.Graph == Self, DistancesToVertex.Key == VertexId, DistancesToVertex.Value == Distance, VertexVisitationState.Graph == Self, VertexVisitationState.Key == VertexId, VertexVisitationState.Value == VertexColor, WorkList.Element == PriorityQueueElement<Distance, VertexId>, WorkListIndex.Key == VertexId, WorkListIndex.Value == WorkList.Index
-
excludingSelfLoops()
Extension methodReturns a graph that contains no edges whose source and destination is the same.
Declaration
Swift
public func excludingSelfLoops() -> EdgeFilterGraph<Self, ExcludeSelfEdges<Self>>
-
degreeDistribution
Extension methodComputes the distribution of degrees of all vertices in
self
.Complexity
O(|V| + |E|)Declaration
Swift
public var degreeDistribution: DegreeDistribution { get }
-
undirectedClusteringCoefficient(of:
Extension method) Returns the local clustering coefficient of the
neighborhood
assumingself
is an undirected graph.Precondition
(Not checked)vertex
does not contain an edge to itself.Declaration
Swift
public func undirectedClusteringCoefficient(of vertex: VertexId) -> Double
-
undirectedAverageClusteringCoefficient
Extension methodThe unweighted average clustering coefficient of
self
.Note: this is only valid if
self
models an undirected graph.Declaration
Swift
public var undirectedAverageClusteringCoefficient: Double { get }
-
pathLengths(from:
Extension method) Returns summaries about the shortest paths from
vertex
inself
.The greatest distance between
vertex
and any other vertex inself
is known as the eccentricity. The average path length corresponds to the typical number of hops fromvertex
to reach an arbitrary other connected vertex.Because graphs can sometimes be disconnected, the number of vertices encountered is also returned.
See also
IncidenceGraph.distanceMetrics
.Complexity
O(|V| + |E|)Declaration
Swift
public mutating func pathLengths(from vertex: VertexId) -> ( eccentricity: Int, averagePathLength: Double, totalPathLengths: Int, verticesEncountered: Int )
-
pathLengths(from:
Extension methodvertexDistances: ) Returns summaries about the shortest paths from
vertex
inself
.The greatest distance between
vertex
and any other vertex inself
is known as the eccentricity. The average path length corresponds to the typical number of hops fromvertex
to reach an arbitrary other connected vertex.Because graphs can sometimes be disconnected, the number of vertices encountered is also returned.
See also
IncidenceGraph.distanceMetrics
.Complexity
O(|V| + |E|)Declaration
Swift
public mutating func pathLengths<VertexDistances: ExternalPropertyMap>( from vertex: VertexId, vertexDistances: inout VertexDistances ) -> (eccentricity: Int, averagePathLength: Double, totalPathLengths: Int, verticesEncountered: Int) where VertexDistances.Graph == Self, VertexDistances.Key == VertexId, VertexDistances.Value == Int
-
distanceMetrics
Extension methodReturns distance metrics and representative vertices for
self
.The average path length is the mean number of steps along the shortest paths for all possible pairs of vertices in
self
. It is a measure of efficiency of information or mass transport on a network, and is a robust measure of network topology. (See also:averageClusteringCoefficient
anddegreeDistribution
.)The eccentricity of a vertex is the greatest distance between the vertex and any other vertex in
self
.A graph’s diameter is the maximum number of edges to traverse the shortest path between any two arbitrary connected vertices. Equivalently, it is the maximum eccentricity.
A graph’s radius is the maximum distance to traverse the shortest path from a central vertex that is identified as having the minimum eccentricity.
The diameter and radius of a graph can give information about how connected the network is, and how efficiently information can flow through it. The returned value ignores the logically infinite distance between disconnected vertices.
The computed metrics are valid for both directed and undirected graphs.
Complexity
O(|V| * (|V| + |E|))Precondition
!self.vertices.isEmpty
Declaration
Swift
public var distanceMetrics: (averagePathLength: Double, diameter: Int, radius: Int, centralVertex: VertexId, centralVertexCount: Int, peripheralVertex: VertexId, peripheralVertexCount: Int) { mutating get }
-
addKNearestNeighborEdges(vertex:
Extension methodk: similarityBetween: ) Adds edges between
vertex
and itsk
nearest neighbors, as determined bysimilarityBetween
.Complexity
O(|V| * O(similarityBetween)), where |V| is the number of vertices inself
.Declaration
Swift
@discardableResult public mutating func addKNearestNeighborEdges<Similarity: Comparable>( vertex: VertexId, k: Int, similarityBetween: (VertexId, VertexId, inout Self) -> Similarity) -> [(EdgeId, Similarity)]
Return Value
A collection of the
k
nearest neighbors ofvertex
and their computed similarities. -
addKNearestNeighborEdges(k:
Extension methodsimilarityBetween: ) Adds edges between all vertices and their
k
nearest neighbors, as determined bysimilarityBetween
.Complexity
O(|V|^2 * O(similarityBetween))Declaration
Swift
public mutating func addKNearestNeighborEdges<Similarity: Comparable>( k: Int, similarityBetween: (VertexId, VertexId, inout Self) -> Similarity )
-
breadthFirstSearch(startingAt:
Extension methodcallback: ) Runs breadth first search on
self
starting fromstartVertices
;callback
is invoked at key events during the search.Precondition
startVertices
is non-empty.Declaration
Swift
public mutating func breadthFirstSearch< StartVertices: Collection >( startingAt startVertices: StartVertices, callback: BFSCallback ) rethrows where StartVertices.Element == VertexId
-
breadthFirstSearch(startingAt:
Extension methodcallback: ) Runs breadth first search on
self
starting fromstartVertex
;callback
is invoked at key events during the search.Declaration
Swift
public mutating func breadthFirstSearch( startingAt startVertex: VertexId, callback: BFSCallback ) rethrows
-
breadthFirstSearch(startingAt:
Extension methodendingAt: callback: ) Runs breadth first search on
self
starting fromstartVertex
terminating onceendVertex
has been encountered;callback
is invoked at key events during the search.Declaration
Swift
public mutating func breadthFirstSearch( startingAt startVertex: VertexId, endingAt endVertex: VertexId, callback: BFSCallback ) rethrows
-
depthFirstTraversal(vertexVisitationState:
Extension methodcallback: ) Runs depth first search repeatedly until all vertices have been visited.
Declaration
Swift
public mutating func depthFirstTraversal< VertexVisitationState: PropertyMap >( vertexVisitationState: inout VertexVisitationState, callback: DFSCallback ) rethrows where VertexVisitationState.Graph == Self, VertexVisitationState.Key == VertexId, VertexVisitationState.Value == VertexColor
-
depthFirstSearch(startingAt:
Extension methodcallback: ) Expores
self
depth-first starting atsource
, invokingcallback
at key events during the search.Note
this is a mutating method because thecallback
may modify the graph.Declaration
Swift
public mutating func depthFirstSearch( startingAt source: VertexId, callback: DFSCallback ) rethrows
-
depthFirstTraversal(callback:
Extension method) Runs depth first search repeatedly until all vertices have been visited.
Declaration
Swift
public mutating func depthFirstTraversal( callback: DFSCallback ) rethrows
-
dijkstraSearch(startingAt:
Extension methodedgeLengths: callback: ) Returns the distances from
startVertex
to every other vertex using Dijkstra’s search algorithm using edge weights fromedgeLengths
;callback
is called at key events during the search.Declaration
Swift
public mutating func dijkstraSearch< Distance: GraphDistanceMeasure, EdgeLengths: PropertyMap >( startingAt startVertex: VertexId, edgeLengths: EdgeLengths, callback: DijkstraSearchCallback ) rethrows -> TablePropertyMap<Self, VertexId, Distance> where EdgeLengths.Graph == Self, EdgeLengths.Key == EdgeId, EdgeLengths.Value == Distance
-
dijkstraSearch(startingAt:
Extension methodendingAt: edgeLengths: callback: ) Returns distances from
startVertex
inself
usingedgeLengths
starting fromstartVertex
and terminating onceendVertex
has been encountered;callback
is invoked at key events during the search.Declaration
Swift
public mutating func dijkstraSearch< Distance: GraphDistanceMeasure, EdgeLengths: PropertyMap >( startingAt startVertex: VertexId, endingAt goalVertex: VertexId, edgeLengths: EdgeLengths, callback: DijkstraSearchCallback ) rethrows -> TablePropertyMap<Self, VertexId, Distance> where EdgeLengths.Graph == Self, EdgeLengths.Key == EdgeId, EdgeLengths.Value == Distance
-
Returns the vertices along the path from
startVertex
togoalVertex
and their distances fromstartVertex
usingedgeLengths
to determine the cost of traversing an edge;callback
is called regularly during search execution.Declaration
Swift
public mutating func dijkstraShortestPath< Distance: GraphDistanceMeasure, EdgeLengths: PropertyMap >( from startVertex: VertexId, to goalVertex: VertexId, edgeLengths: EdgeLengths, effectivelyInfinite: Distance, callback: DijkstraSearchCallback ) rethrows -> [(VertexId, Distance)] where EdgeLengths.Graph == Self, EdgeLengths.Key == EdgeId, EdgeLengths.Value == Distance
-
dijkstraSearch(startingAt:
Extension methodedgeLengths: callback: ) Returns the distances from
startVertex
to every other vertex using Dijkstra’s search algorithm using edge weights fromedgeLengths
;callback
is called at key events during the search.Declaration
Swift
public mutating func dijkstraSearch< Distance: GraphDistanceMeasure, EdgeLengths: PropertyMap >( startingAt startVertex: VertexId, edgeLengths: EdgeLengths, callback: DijkstraSearchCallback ) rethrows -> DictionaryPropertyMap<Self, VertexId, Distance> where EdgeLengths.Graph == Self, EdgeLengths.Key == EdgeId, EdgeLengths.Value == Distance
-
dijkstraSearch(startingAt:
Extension methodendingAt: edgeLengths: callback: ) Returns distances from
startVertex
inself
usingedgeLengths
starting fromstartVertex
and terminating onceendVertex
has been encountered;callback
is invoked at key events during the search.Declaration
Swift
public mutating func dijkstraSearch< Distance: GraphDistanceMeasure, EdgeLengths: PropertyMap >( startingAt startVertex: VertexId, endingAt goalVertex: VertexId, edgeLengths: EdgeLengths, callback: DijkstraSearchCallback ) rethrows -> DictionaryPropertyMap<Self, VertexId, Distance> where EdgeLengths.Graph == Self, EdgeLengths.Key == EdgeId, EdgeLengths.Value == Distance
-
Returns the vertices along the path from
startVertex
togoalVertex
and their distances fromstartVertex
usingedgeLengths
to determine the cost of traversing an edge;callback
is called regularly during search execution.Declaration
Swift
public mutating func dijkstraShortestPath< Distance: GraphDistanceMeasure, EdgeLengths: PropertyMap >( from startVertex: VertexId, to goalVertex: VertexId, edgeLengths: EdgeLengths, effectivelyInfinite: Distance, callback: DijkstraSearchCallback ) rethrows -> [(VertexId, Distance)] where EdgeLengths.Graph == Self, EdgeLengths.Key == EdgeId, EdgeLengths.Value == Distance
-
hasParallelEdges
Extension methodtrue
iff there exist at least two edges inself
such that both the source and destination are identical.Complexity
O(|V| + |E|)Declaration
Swift
public var hasParallelEdges: Bool { get }
-
hasSelfEdge
Extension methodtrue
iff there exists at least one edge whose source and destination is the same vertex.Complexity
O(|V| + |E|)Declaration
Swift
public var hasSelfEdge: Bool { get }
-
uniquingUndirectedEdges()
Extension methodReturns a graph where undirected edges are de-duplicated so they only appear once when traversing all incidences for all vertices.
This transformation is especially useful when copying from an undirected representation to another undirected representation. Example:
let s = UndirectedStarGraph(n: 5) let l = SimpleUndirectedAdjacencyList(s.uniquingUndirectedEdges()) // s and l now represent the same logical graph.
Declaration
Swift
public func uniquingUndirectedEdges() -> EdgeFilterGraph<Self, UniqueUndirectedEdges<Self>>
-
strongComponents(components:
Extension methoddiscoverTime: roots: ) Computes the strongly connected components of
self
.A strongly connected component is a subset of vertices within a directed graph such that every vertex is reachable from every other vertex in the subset.
Declaration
Swift
public mutating func strongComponents< ComponentType: FixedWidthInteger, TimeType: FixedWidthInteger, Components: PropertyMap, DiscoverTime: PropertyMap, Roots: PropertyMap >( components: inout Components, discoverTime: inout DiscoverTime, roots: inout Roots ) -> ComponentType where Components.Graph == Self, Components.Key == VertexId, Components.Value == ComponentType, DiscoverTime.Graph == Self, DiscoverTime.Key == VertexId, DiscoverTime.Value == TimeType, Roots.Graph == Self, Roots.Key == VertexId, Roots.Value == Self.VertexId
Return Value
the number of strong components in
self
. -
strongComponents(components:
Extension method) Returns the number of strongly connected components of
self
and stores the per-vertex mapping to components intocomponents
.A strongly connected component is a subset of vertices within a directed graph such that every vertex is reachable from every other vertex in the subset. The computed numbering of strong components corresponds to a reverse topological sort of the graph of strong components.
Declaration
Swift
public mutating func strongComponents<Components: PropertyMap>( components: inout Components ) -> Components.Value where Components.Graph == Self, Components.Key == VertexId, Components.Value: FixedWidthInteger
Return Value
the number of strong components in
self
. -
strongComponents()
Extension methodReturns the strongly connected components of
self
.A strongly connected component is a subset of vertices within a directed graph such that every vertex is reachable from every other vertex in the subset.
Declaration
Swift
public mutating func strongComponents() -> (components: DefaultVertexIntMap, componentCount: Int)
Return Value
a table mapping each vertex to a number corresponding to a strong component, and the number of strong components in
self
. -
strongComponentsCount()
Extension methodReturns the number of strongly connected components of
self
.A strongly connected component is a subset of vertices within a directed graph such that every vertex is reachable from every other vertex in the subset.
Declaration
Swift
public mutating func strongComponentsCount() -> Int
Return Value
the number of strong components in
self
. -
isStronglyConnected
Extension methodTrue if
self
is strongly connected, false otherwise.Declaration
Swift
public var isStronglyConnected: Bool { mutating get }
-
topologicalSort(reverseSink:
Extension method) Computes a topological sort of
self
.Throws
if a cycle is detected.Declaration
Swift
public mutating func topologicalSort( reverseSink: (VertexId) -> Void ) throws
Parameters
reverseSink
this function will be called once for every vertex in reverse topological sort order.
-
topologicalSort()
Extension methodComputes a topological sort of
self
.A topological sort means that for every 0 <= i < j < vertexCount, there does not exist an edge from
returnValue[j]
toreturnValue[i]
(i.e. backwards in the array).Throws
if a cycle is detected.Declaration
Swift
public mutating func topologicalSort() throws -> [VertexId]