@@ -17,7 +17,7 @@ private typealias StepResult = (distances: Distances, predecessors: Predecessors
17
17
18
18
- note: In all complexity bounds, `V` is the number of vertices in the graph, and `E` is the number of edges.
19
19
*/
20
- public struct FloydWarshall < T where T: Hashable > : APSPAlgorithm {
20
+ public struct FloydWarshall < T> : APSPAlgorithm where T: Hashable {
21
21
22
22
public typealias Q = T
23
23
public typealias P = FloydWarshallResult < T >
@@ -29,7 +29,7 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
29
29
- complexity: `Θ(V^3)` time, `Θ(V^2)` space
30
30
- returns a `FloydWarshallResult` struct which can be queried for shortest paths and their total weights
31
31
*/
32
- public static func apply< T> ( graph: AbstractGraph < T > ) -> FloydWarshallResult < T > {
32
+ public static func apply< T> ( _ graph: AbstractGraph < T > ) -> FloydWarshallResult < T > {
33
33
34
34
var previousDistance = constructInitialDistanceMatrix ( graph)
35
35
var previousPredecessor = constructInitialPredecessorMatrix ( previousDistance)
@@ -59,12 +59,12 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
59
59
- returns: a tuple containing the next distance matrix with weights of currently known
60
60
shortest paths and the corresponding predecessor matrix
61
61
*/
62
- static private func nextStep< T> ( intermediateIdx: Int , previousDistances: Distances ,
62
+ static fileprivate func nextStep< T> ( _ intermediateIdx: Int , previousDistances: Distances ,
63
63
previousPredecessors: Predecessors , graph: AbstractGraph < T > ) -> StepResult {
64
64
65
65
let vertexCount = graph. vertices. count
66
- var nextDistances = Array ( count : vertexCount , repeatedValue : Array ( count: vertexCount, repeatedValue : Double . infinity ) )
67
- var nextPredecessors = Array ( count : vertexCount , repeatedValue : Array < Int ? > ( count: vertexCount, repeatedValue : nil ) )
66
+ var nextDistances = Array ( repeating : Array ( repeating : Double . infinity , count: vertexCount) , count : vertexCount )
67
+ var nextPredecessors = Array ( repeating : Array < Int ? > ( repeating : nil , count: vertexCount) , count : vertexCount )
68
68
69
69
for fromIdx in 0 ..< vertexCount {
70
70
for toIndex in 0 ..< vertexCount {
@@ -97,12 +97,12 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
97
97
- complexity: `Θ(V^2)` time/space
98
98
- returns: weighted adjacency matrix in form ready for processing with Floyd-Warshall
99
99
*/
100
- static private func constructInitialDistanceMatrix< T> ( graph: AbstractGraph < T > ) -> Distances {
100
+ static fileprivate func constructInitialDistanceMatrix< T> ( _ graph: AbstractGraph < T > ) -> Distances {
101
101
102
102
let vertices = graph. vertices
103
103
104
104
let vertexCount = graph. vertices. count
105
- var distances = Array ( count : vertexCount , repeatedValue : Array ( count: vertexCount, repeatedValue : Double . infinity ) )
105
+ var distances = Array ( repeating : Array ( repeating : Double . infinity , count: vertexCount) , count : vertexCount )
106
106
107
107
for row in vertices {
108
108
for col in vertices {
@@ -125,10 +125,10 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
125
125
126
126
- complexity: `Θ(V^2)` time/space
127
127
*/
128
- static private func constructInitialPredecessorMatrix( distances: Distances ) -> Predecessors {
128
+ static fileprivate func constructInitialPredecessorMatrix( _ distances: Distances ) -> Predecessors {
129
129
130
130
let vertexCount = distances. count
131
- var predecessors = Array ( count : vertexCount , repeatedValue : Array < Int ? > ( count: vertexCount, repeatedValue : nil ) )
131
+ var predecessors = Array ( repeating : Array < Int ? > ( repeating : nil , count: vertexCount) , count : vertexCount )
132
132
133
133
for fromIdx in 0 ..< vertexCount {
134
134
for toIdx in 0 ..< vertexCount {
@@ -151,10 +151,10 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
151
151
It conforms to the `APSPResult` procotol which provides methods to retrieve
152
152
distances and paths between given pairs of start and end nodes.
153
153
*/
154
- public struct FloydWarshallResult < T where T: Hashable > : APSPResult {
154
+ public struct FloydWarshallResult < T> : APSPResult where T: Hashable {
155
155
156
- private var weights : Distances
157
- private var predecessors : Predecessors
156
+ fileprivate var weights : Distances
157
+ fileprivate var predecessors : Predecessors
158
158
159
159
/**
160
160
- returns: the total weight of the path from a starting vertex to a destination.
@@ -190,7 +190,7 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
190
190
191
191
- returns: the list of predecessors discovered so far
192
192
*/
193
- private func recursePathFrom( fromVertex from: Vertex < T > , toVertex to: Vertex < T > , path: [ Vertex < T > ] ,
193
+ fileprivate func recursePathFrom( fromVertex from: Vertex < T > , toVertex to: Vertex < T > , path: [ Vertex < T > ] ,
194
194
inGraph graph: AbstractGraph < T > ) -> [ Vertex < T > ] ? {
195
195
196
196
if from. index == to. index {
0 commit comments