Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad95e30

Browse files
committedJun 18, 2016
removed a merge conflict
2 parents 4e672f8 + 037de82 commit ad95e30

File tree

157 files changed

+1275
-1231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+1275
-1231
lines changed
 

‎All-Pairs Shortest Paths/APSP/APSP/APSP.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import Foundation
99
import Graph
1010

1111
/**
12-
`APSPAlgorithm` is a protocol for encapsulating an All-Pairs Shortest Paths algorithm. It provides a single function `apply` that accepts a subclass of `AbstractGraph` and returns an object conforming to `APSPResult`.
12+
`APSPAlgorithm` is a protocol for encapsulating an All-Pairs Shortest Paths algorithm.
13+
It provides a single function `apply` that accepts a subclass of `AbstractGraph` and
14+
returns an object conforming to `APSPResult`.
1315
*/
1416
public protocol APSPAlgorithm {
1517

@@ -21,7 +23,8 @@ public protocol APSPAlgorithm {
2123
}
2224

2325
/**
24-
`APSPResult` is a protocol defining functions `distance` and `path`, allowing for opaque queries into the actual data structures that represent the APSP solution according to the algorithm used.
26+
`APSPResult` is a protocol defining functions `distance` and `path`, allowing for opaque
27+
queries into the actual data structures that represent the APSP solution according to the algorithm used.
2528
*/
2629
public protocol APSPResult {
2730

‎All-Pairs Shortest Paths/APSP/APSP/FloydWarshall.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
5151
}
5252

5353
/**
54-
For each iteration of `intermediateIdx`, perform the comparison for the dynamic algorith, checking for each pair of start/end vertices, whether a path taken through another vertex produces a shorter path.
54+
For each iteration of `intermediateIdx`, perform the comparison for the dynamic algorith,
55+
checking for each pair of start/end vertices, whether a path taken through another vertex
56+
produces a shorter path.
5557

5658
- complexity: `Θ(V^2)` time/space
57-
- returns: a tuple containing the next distance matrix with weights of currently known shortest paths and the corresponding predecessor matrix
59+
- returns: a tuple containing the next distance matrix with weights of currently known
60+
shortest paths and the corresponding predecessor matrix
5861
*/
59-
static private func nextStep<T>(intermediateIdx: Int, previousDistances: Distances, previousPredecessors: Predecessors, graph: AbstractGraph<T>) -> StepResult {
62+
static private func nextStep<T>(intermediateIdx: Int, previousDistances: Distances,
63+
previousPredecessors: Predecessors, graph: AbstractGraph<T>) -> StepResult {
6064

6165
let vertexCount = graph.vertices.count
6266
var nextDistances = Array(count: vertexCount, repeatedValue: Array(count: vertexCount, repeatedValue: Double.infinity))
@@ -85,9 +89,11 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
8589

8690
}
8791

88-
/**
89-
We need to map the graph's weight domain onto the one required by the algorithm: the graph stores either a weight as a `Double` or `nil` if no edge exists between two vertices, but the algorithm needs a lack of an edge represented as ∞ for the `min` comparison to work correctly.
90-
92+
/**
93+
We need to map the graph's weight domain onto the one required by the algorithm: the graph
94+
stores either a weight as a `Double` or `nil` if no edge exists between two vertices, but
95+
the algorithm needs a lack of an edge represented as ∞ for the `min` comparison to work correctly.
96+
9197
- complexity: `Θ(V^2)` time/space
9298
- returns: weighted adjacency matrix in form ready for processing with Floyd-Warshall
9399
*/
@@ -116,7 +122,7 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
116122

117123
/**
118124
Make the initial predecessor index matrix. Initially each value is equal to it's row index, it's "from" index when querying into it.
119-
125+
120126
- complexity: `Θ(V^2)` time/space
121127
*/
122128
static private func constructInitialPredecessorMatrix(distances: Distances) -> Predecessors {
@@ -139,17 +145,20 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
139145
}
140146

141147
/**
142-
`FloydWarshallResult` encapsulates the result of the computation, namely the minimized distance adjacency matrix, and the matrix of predecessor indices.
143-
144-
It conforms to the `APSPResult` procotol which provides methods to retrieve distances and paths between given pairs of start and end nodes.
148+
`FloydWarshallResult` encapsulates the result of the computation, namely the
149+
minimized distance adjacency matrix, and the matrix of predecessor indices.
150+
151+
It conforms to the `APSPResult` procotol which provides methods to retrieve
152+
distances and paths between given pairs of start and end nodes.
145153
*/
146154
public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
147155

148156
private var weights: Distances
149157
private var predecessors: Predecessors
150158

151159
/**
152-
- returns: the total weight of the path from a starting vertex to a destination. This value is the minimal connected weight between the two vertices, or `nil` if no path exists
160+
- returns: the total weight of the path from a starting vertex to a destination.
161+
This value is the minimal connected weight between the two vertices, or `nil` if no path exists
153162
- complexity: `Θ(1)` time/space
154163
*/
155164
public func distance(fromVertex from: Vertex<T>, toVertex to: Vertex<T>) -> Double? {
@@ -159,7 +168,8 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
159168
}
160169

161170
/**
162-
- returns: the reconstructed path from a starting vertex to a destination, as an array containing the data property of each vertex, or `nil` if no path exists
171+
- returns: the reconstructed path from a starting vertex to a destination,
172+
as an array containing the data property of each vertex, or `nil` if no path exists
163173
- complexity: `Θ(V)` time, `Θ(V^2)` space
164174
*/
165175
public func path(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, inGraph graph: AbstractGraph<T>) -> [T]? {
@@ -175,11 +185,13 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
175185
}
176186

177187
/**
178-
The recursive component to rebuilding the shortest path between two vertices using the predecessor matrix.
188+
The recursive component to rebuilding the shortest path between
189+
two vertices using the predecessor matrix.
179190

180191
- returns: the list of predecessors discovered so far
181192
*/
182-
private func recursePathFrom(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, path: [Vertex<T>], inGraph graph: AbstractGraph<T>) -> [Vertex<T>]? {
193+
private func recursePathFrom(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, path: [Vertex<T>],
194+
inGraph graph: AbstractGraph<T>) -> [Vertex<T>]? {
183195

184196
if from.index == to.index {
185197
return [ from, to ]
@@ -198,7 +210,6 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
198210
}
199211

200212
return nil
201-
202213
}
203214

204215
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.