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
VertexIdofedge.Declaration
Swift
func source(of edge: EdgeId) -> VertexId -
Returns the source
VertexIdofedge.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 -
BFSCallbackExtension methodA hook to observe events that occur during depth first search.
Declaration
Swift
public typealias BFSCallback = (BFSEvent<Self>, inout Self) throws -> Void -
BFSCallbackWithWorkListExtension 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
selfusingvertexVisitationStateto keep track of search progress;callbackis invoked at key events during the search.Precondition
vertexVisitationStatemust be initialized for everyVertexIdinGraphto be.white. (Note: this precondition is not checked.)Precondition
startVerticesis 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
selfstarting fromstartVertices;callbackis invoked at key events during the search.Precondition
startVerticesis 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 -
DFSCallbackExtension 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
selfdepth-first starting atsource, usingvertexVisitationStateto keep track of visited vertices, invokingcallbackat key events of the search.Note
this is a mutating method because thevertexVisitationStateorvisitormay store data within the graph itself.Precondition
VertexVisitationStatehas 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 -
DijkstraSearchCallbackExtension 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
selfusing the supplied property maps;callbackis 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>>
-
degreeDistributionExtension 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
neighborhoodassumingselfis an undirected graph.Precondition
(Not checked)vertexdoes not contain an edge to itself.Declaration
Swift
public func undirectedClusteringCoefficient(of vertex: VertexId) -> Double
-
undirectedAverageClusteringCoefficientExtension methodThe unweighted average clustering coefficient of
self.Note: this is only valid if
selfmodels an undirected graph.Declaration
Swift
public var undirectedAverageClusteringCoefficient: Double { get }
-
pathLengths(from:Extension method) Returns summaries about the shortest paths from
vertexinself.The greatest distance between
vertexand any other vertex inselfis known as the eccentricity. The average path length corresponds to the typical number of hops fromvertexto 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
vertexinself.The greatest distance between
vertexand any other vertex inselfis known as the eccentricity. The average path length corresponds to the typical number of hops fromvertexto 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
-
distanceMetricsExtension 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:averageClusteringCoefficientanddegreeDistribution.)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.isEmptyDeclaration
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
vertexand itsknearest 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
knearest neighbors ofvertexand their computed similarities. -
addKNearestNeighborEdges(k:Extension methodsimilarityBetween: ) Adds edges between all vertices and their
knearest 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
selfstarting fromstartVertices;callbackis invoked at key events during the search.Precondition
startVerticesis 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
selfstarting fromstartVertex;callbackis 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
selfstarting fromstartVertexterminating onceendVertexhas been encountered;callbackis 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
selfdepth-first starting atsource, invokingcallbackat key events during the search.Note
this is a mutating method because thecallbackmay 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
startVertexto every other vertex using Dijkstra’s search algorithm using edge weights fromedgeLengths;callbackis 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
startVertexinselfusingedgeLengthsstarting fromstartVertexand terminating onceendVertexhas been encountered;callbackis 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
startVertextogoalVertexand their distances fromstartVertexusingedgeLengthsto determine the cost of traversing an edge;callbackis 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
startVertexto every other vertex using Dijkstra’s search algorithm using edge weights fromedgeLengths;callbackis 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
startVertexinselfusingedgeLengthsstarting fromstartVertexand terminating onceendVertexhas been encountered;callbackis 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
startVertextogoalVertexand their distances fromstartVertexusingedgeLengthsto determine the cost of traversing an edge;callbackis 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
-
hasParallelEdgesExtension methodtrueiff there exist at least two edges inselfsuch that both the source and destination are identical.Complexity
O(|V| + |E|)Declaration
Swift
public var hasParallelEdges: Bool { get }
-
hasSelfEdgeExtension methodtrueiff 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.VertexIdReturn Value
the number of strong components in
self. -
strongComponents(components:Extension method) Returns the number of strongly connected components of
selfand 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: FixedWidthIntegerReturn 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() -> IntReturn Value
the number of strong components in
self. -
isStronglyConnectedExtension methodTrue if
selfis 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 ) throwsParameters
reverseSinkthis 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]
View on GitHub
IncidenceGraph Protocol Reference