Graph
public protocol Graph : Collection, CustomStringConvertible, Decodable, Encodable
The protocol for all graphs. You should generally use one of its two canonical class implementations, UnweightedGraph and WeightedGraph
-
Undocumented
Declaration
Swift
associatedtype V : Decodable, Encodable, Equatable -
Undocumented
Declaration
Swift
associatedtype E : Equatable, Edge -
Undocumented
Declaration
Swift
var vertices: [V] { get set } -
Undocumented
Declaration
Swift
var edges: [[E]] { get set } -
Undocumented
Declaration
Swift
init(vertices: [V]) -
addEdge(_:directed:)Default implementationUndocumented
Default Implementation
Add an edge to the graph.
Declaration
Swift
func addEdge(_ e: E, directed: Bool)
-
detectCycles(upToLength:)Extension methodFind all of the cycles in a
Graph, expressed as vertices.Declaration
Swift
func detectCycles(upToLength maxK: Int = Int.max) -> [[V]]Parameters
upToLengthDoes the caller only want to detect cycles up to a certain length?
Return Value
a list of lists of vertices in cycles
-
PathExtension methodUndocumented
Declaration
Swift
typealias Path = [E] -
PathTupleExtension methodUndocumented
Declaration
Swift
typealias PathTuple = (start: Int, path: Path) -
detectCyclesAsEdges(upToLength:)Extension methodFind all of the cycles in a
Graph, expressed as edges.Declaration
Swift
func detectCyclesAsEdges(upToLength maxK: Int = Int.max) -> [[E]]Parameters
upToLengthDoes the caller only want to detect cycles up to a certain length?
Return Value
a list of lists of edges in cycles
-
vertexCountExtension methodHow many vertices are in the graph?
Declaration
Swift
public var vertexCount: Int { get } -
edgeCountExtension methodHow many edges are in the graph?
Declaration
Swift
public var edgeCount: Int { get } -
edgeList()Extension methodReturns a list of all the edges, undirected edges are only appended once.
Declaration
Swift
public func edgeList() -> [E] -
vertexAtIndex(_:)Extension methodGet a vertex by its index.
Declaration
Swift
public func vertexAtIndex(_ index: Int) -> VParameters
indexThe index of the vertex.
Return Value
The vertex at i.
-
indexOfVertex(_:)Extension methodFind the first occurence of a vertex if it exists.
Declaration
Swift
public func indexOfVertex(_ vertex: V) -> Int?Parameters
vertexThe vertex you are looking for.
Return Value
The index of the vertex. Return nil if it can’t find it.
-
neighborsForIndex(_:)Extension methodFind all of the neighbors of a vertex at a given index.
Declaration
Swift
public func neighborsForIndex(_ index: Int) -> [V]Parameters
indexThe index for the vertex to find the neighbors of.
Return Value
An array of the neighbor vertices.
-
neighborsForVertex(_:)Extension methodFind all of the neighbors of a given Vertex.
Parameters
vertexThe vertex to find the neighbors of.
Return Value
An optional array of the neighbor vertices.
-
edgesForIndex(_:)Extension methodFind all of the edges of a vertex at a given index.
Declaration
Swift
public func edgesForIndex(_ index: Int) -> [E]Parameters
indexThe index for the vertex to find the children of.
-
edgesForVertex(_:)Extension methodFind all of the edges of a given vertex.
Parameters
vertexThe vertex to find the edges of.
-
vertexInGraph(vertex:)Extension methodFind the first occurence of a vertex.
Declaration
Swift
public func vertexInGraph(vertex: V) -> BoolParameters
vertexThe vertex you are looking for.
-
addVertex(_:)Extension methodAdd a vertex to the graph.
Declaration
Swift
public mutating func addVertex(_ v: V) -> IntParameters
vThe vertex to be added.
Return Value
The index where the vertex was added.
-
removeAllEdges(from:to:bidirectional:)Extension methodRemoves all edges in both directions between vertices at indexes from & to.
Declaration
Swift
public mutating func removeAllEdges(from: Int, to: Int, bidirectional: Bool = true)Parameters
fromThe starting vertex’s index.
toThe ending vertex’s index.
bidirectionalRemove edges coming back (to -> from)
-
removeAllEdges(from:to:bidirectional:)Extension methodRemoves all edges in both directions between two vertices.
Parameters
fromThe starting vertex.
toThe ending vertex.
bidirectionalRemove edges coming back (to -> from)
-
removeEdge(_:)Extension methodRemove the first edge found to be equal to e
Declaration
Swift
public mutating func removeEdge(_ e: E)Parameters
eThe edge to remove.
-
removeVertexAtIndex(_:)Extension methodRemoves a vertex at a specified index, all of the edges attached to it, and renumbers the indexes of the rest of the edges.
Declaration
Swift
public mutating func removeVertexAtIndex(_ index: Int)Parameters
indexThe index of the vertex.
-
removeVertex(_:)Extension methodRemoves the first occurence of a vertex, all of the edges attached to it, and renumbers the indexes of the rest of the edges.
Declaration
Swift
public mutating func removeVertex(_ vertex: V)Parameters
vertexThe vertex to be removed..
-
edgeExists(_:)Extension methodCheck whether an edge is in the graph or not.
Declaration
Swift
public func edgeExists(_ edge: E) -> BoolParameters
edgeThe edge to find in the graph.
Return Value
True if the edge exists, and false otherwise.
-
descriptionExtension methodDeclaration
Swift
public var description: String { get }
-
startIndexExtension methodDeclaration
Swift
public var startIndex: Int { get } -
endIndexExtension methodDeclaration
Swift
public var endIndex: Int { get } -
index(after:)Extension methodDeclaration
Swift
public func index(after i: Int) -> Int -
subscript(_:)Extension methodThe same as vertexAtIndex() - returns the vertex at index
Declaration
Swift
public subscript(i: Int) -> V { get }Parameters
indexThe index of vertex to return.
Return Value
The vertex at index.
-
dfs(fromIndex:goalTest:visitOrder:reducer:)Extension methodPerform a computation over the graph visiting the vertices using a depth-first algorithm.
The vertices of the graph are visited one time at most.
Declaration
Parameters
initalVertexIndexThe index of the vertex that will be visited first.
goalTestReturns true if a given vertex index is a goal.
visitOrderA closure that orders an array of edges. For each visited vertex, the array of its outgoing edges will be passed to this closure and the neighbours will be visited in the order of the resulting array.
reducerA closure that is fed with each visited edge. The input parameter is the edge from the previously visited vertex to the currently visited vertex. If the return value is false, the neighbours of the currently visited vertex won’t be visited.
Return Value
The index of the first vertex found to satisfy goalTest or nil if no vertex is found.
-
traverseDfs(fromIndex:goalTest:visitOrder:reducer:)Extension methodPerform a computation over the graph visiting the vertices using a depth-first algorithm.
The vertices of the graph can be visited more than once. This means that the algorithm is not guaranteed to terminate if tha graph has cycles, it dependes on what goalTest and reducer are used.
Declaration
Parameters
initalVertexIndexThe index of the vertex that will be visited first.
goalTestReturns true if a given vertex index is a goal.
visitOrderA closure that orders an array of edges. For each visited vertex, the array of its outgoing edges will be passed to this closure and the neighbours will be visited in the order of the resulting array.
reducerA closure that is fed with each visited edge. The input parameter is the edge from the previously visited vertex to the currently visited vertex. If the return value is false, the neighbours of the currently visited vertex won’t be visited.
Return Value
The index of the first vertex found to satisfy goalTest or nil if no vertex is found.
-
dfs(fromIndex:goalTest:)Extension methodFind a route from a vertex to the first that satisfies goalTest() using a depth-first search.
Parameters
fromIndexThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
dfs(from:goalTest:)Extension methodFind a route from a vertex to the first that satisfies goalTest() using a depth-first search.
Parameters
fromThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
dfs(fromIndex:toIndex:)Extension methodFind a route from one vertex to another using a depth-first search.
Declaration
Swift
func dfs(fromIndex: Int, toIndex: Int) -> [E]Parameters
fromIndexThe index of the starting vertex.
toIndexThe index of the ending vertex.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
dfs(from:to:)Extension methodFind a route from one vertex to another using a depth-first search.
Parameters
fromThe starting vertex.
toThe ending vertex.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
findAllDfs(fromIndex:goalTest:)Extension methodFind path routes from a vertex to all others the function goalTest() returns true for using a depth-first search.
Parameters
fromIndexThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be found
-
findAllDfs(from:goalTest:)Extension methodFind path routes from a vertex to all others the function goalTest() returns true for using a depth-first search.
Parameters
fromThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be founding the entire route, or an empty array if no route could be found
-
bfs(fromIndex:goalTest:visitOrder:reducer:)Extension methodPerform a computation over the graph visiting the vertices using a breadth-first algorithm.
The vertices of the graph are visited one time at most.
Declaration
Parameters
initalVertexIndexThe index of the vertex that will be visited first.
goalTestReturns true if a given vertex index is a goal.
visitOrderA closure that orders an array of edges. For each visited vertex, the array of its outgoing edges will be passed to this closure and the neighbours will be visited in the order of the resulting array.
reducerA closure that is fed with each visited edge. The input parameter is the edge from the previously visited vertex to the currently visited vertex. If the return value is false, the neighbours of the currently visited vertex won’t be visited.
Return Value
The index of the first vertex found to satisfy goalTest or nil if no vertex is found.
-
traverseBfs(fromIndex:goalTest:visitOrder:reducer:)Extension methodPerform a computation over the graph visiting the vertices using a breadth-first algorithm.
The vertices of the graph can be visited more than once. This means that the algorithm is not guaranteed to terminate if tha graph has cycles, it dependes on what goalTest and reducer are used.
Declaration
Parameters
initalVertexIndexThe index of the vertex that will be visited first.
goalTestReturns true if a given vertex index is a goal.
visitOrderA closure that orders an array of edges. For each visited vertex, the array of its outgoing edges will be passed to this closure and the neighbours will be visited in the order of the resulting array.
reducerA closure that is fed with each visited edge. The input parameter is the edge from the previously visited vertex to the currently visited vertex. If the return value is false, the neighbours of the currently visited vertex won’t be visited.
Return Value
The index of the first vertex found to satisfy goalTest or nil if no vertex is found.
-
bfs(fromIndex:goalTest:)Extension methodFind a route from a vertex to the first that satisfies goalTest() using a breadth-first search.
Parameters
fromIndexThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
bfs(from:goalTest:)Extension methodFind a route from a vertex to the first that satisfies goalTest() using a breadth-first search.
Parameters
fromThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
bfs(fromIndex:toIndex:)Extension methodFind a route from one vertex to another using a breadth-first search.
Declaration
Swift
func bfs(fromIndex: Int, toIndex: Int) -> [E]Parameters
fromIndexThe index of the starting vertex.
toIndexThe index of the ending vertex.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
bfs(from:to:)Extension methodFind a route from one vertex to another using a breadth-first search.
Parameters
fromThe starting vertex.
toThe ending vertex.
Return Value
An array of Edges containing the entire route, or an empty array if no route could be found
-
findAllBfs(fromIndex:goalTest:)Extension methodFind path routes from a vertex to all others the function goalTest() returns true for using a breadth-first search.
Parameters
fromIndexThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be found
-
findAllBfs(from:goalTest:)Extension methodFind path routes from a vertex to all others the function goalTest() returns true for using a breadth-first search.
Parameters
fromThe index of the starting vertex.
goalTestReturns true if a given vertex is a goal.
Return Value
An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be founding the entire route, or an empty array if no route could be found
-
edgesToVertices(edges:)Extension methodUndocumented
-
topologicalSort()Extension methodTopologically sorts a
GraphO(n)Declaration
Swift
func topologicalSort() -> [V]?Return Value
the sorted vertices, or nil if the graph cannot be sorted due to not being a DAG
-
isDAGExtension methodIs the
Grapha directed-acyclic graph (DAG)? O(n) Finds the answer based on the result of a topological sort.Declaration
Swift
var isDAG: Bool { get }
-
withPath(_:directed:)Extension methodInitialize an UnweightedGraph 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) -> SelfParameters
pathAn array of vertices representing a path.
directedIf false, undirected edges are created. If true, edges are directed from vertex i to vertex i+1 in path. Default is false.
-
withCycle(_:directed:)Extension methodInitialize an UnweightedGraph 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 cycle is an empty array, the resulting graph is the empty graph. If cycle 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) -> SelfParameters
cycleAn array of vertices representing a cycle.
directedIf false, undirected edges are created. If true, edges are directed from vertex i to vertex i+1 in cycle. Default is false.
-
addEdge(fromIndex:toIndex:directed:)Extension methodThis is a convenience method that adds an unweighted edge.
Declaration
Swift
public func addEdge(fromIndex: Int, toIndex: Int, directed: Bool = false)Parameters
fromThe starting vertex’s index.
toThe ending vertex’s index.
directedIs the edge directed? (default
false) -
addEdge(from:to:directed:)Extension methodThis is a convenience method that adds an unweighted, undirected edge between the first occurence of two vertices. It takes O(n) time.
Parameters
fromThe starting vertex.
toThe ending vertex.
directedIs the edge directed? (default
false) -
edgeExists(fromIndex:toIndex:)Extension methodCheck whether there is an edge from one vertex to another vertex.
Declaration
Swift
public func edgeExists(fromIndex: Int, toIndex: Int) -> BoolParameters
fromThe index of the starting vertex of the edge.
toThe index of the ending vertex of the edge.
Return Value
True if there is an edge from the starting vertex to the ending vertex.
-
edgeExists(from:to:)Extension methodCheck whether there is an edge from one vertex to another vertex.
Note this will look at the first occurence of each vertex. Also returns false if either of the supplied vertices cannot be found in the graph.
Parameters
fromThe starting vertex of the edge.
toThe ending vertex of the edge.
Return Value
True if there is an edge from the starting vertex to the ending vertex.
-
WExtension methodUndocumented
Declaration
Swift
public typealias W = E.Weight -
neighborsForIndexWithWeights(_:)Extension methodFind all of the neighbors of a vertex at a given index.
Parameters
indexThe index for the vertex to find the neighbors of.
Return Value
An array of tuples including the vertices as the first element and the weights as the second element.
-
addEdge(fromIndex:toIndex:weight:directed:)Extension methodThis is a convenience method that adds a weighted edge.
Declaration
Swift
public func addEdge(fromIndex: Int, toIndex: Int, weight: W, directed: Bool = false)Parameters
fromThe starting vertex’s index.
toThe ending vertex’s index.
directedIs the edge directed? (default false)
weightthe Weight of the edge to add.
-
addEdge(from:to:weight:directed:)Extension methodThis is a convenience method that adds a weighted edge between the first occurence of two vertices. It takes O(n) time.
Parameters
fromThe starting vertex.
toThe ending vertex.
directedIs the edge directed? (default false)
weightthe Weight of the edge to add.
-
edgeExists(fromIndex:toIndex:withWeight:)Extension methodCheck whether there is an edge from one vertex to another vertex with a specific weight.
Declaration
Swift
public func edgeExists(fromIndex: Int, toIndex: Int, withWeight weight: W) -> BoolParameters
fromThe index of the starting vertex of the edge.
toThe index of the ending vertex of the edge.
Return Value
True if there is an edge from the starting vertex to the ending vertex.
-
edgeExists(from:to:withWeight:)Extension methodCheck whether there is an edge from one vertex to another vertex with a specific weight.
Note this will look at the first occurence of each vertex. Also returns false if either of the supplied vertices cannot be found in the graph.
Parameters
fromThe starting vertex of the edge.
toThe ending vertex of the edge.
Return Value
True if there is an edge from the starting vertex to the ending vertex.
-
edgeExists(fromIndex:toIndex:)Extension methodCheck whether there is an edge from one vertex to another vertex.
Declaration
Swift
public func edgeExists(fromIndex: Int, toIndex: Int) -> BoolParameters
fromThe index of the starting vertex of the edge.
toThe index of the ending vertex of the edge.
Return Value
True if there is an edge from the starting vertex to the ending vertex.
-
edgeExists(from:to:)Extension methodCheck whether there is an edge from one vertex to another vertex.
Note this will look at the first occurence of each vertex. Also returns false if either of the supplied vertices cannot be found in the graph.
Parameters
fromThe starting vertex of the edge.
toThe ending vertex of the edge.
Return Value
True if there is an edge from the starting vertex to the ending vertex.
-
weights(from:to:)Extension methodReturns all the weights associated to the edges between two vertex indices.
Declaration
Swift
public func weights(from: Int, to: Int) -> [W]Parameters
fromThe starting vertex index
toThe ending vertex index
Return Value
An array with all the weights associated to edges between the provided indexes.
-
weights(from:to:)Extension methodReturns all the weights associated to the edges between two vertices.
Parameters
fromThe starting vertex
toThe ending vertex
Return Value
An array with all the weights associated to edges between the provided vertices.
View on GitHub
Graph Protocol Reference