Skip to content

Commit

Permalink
Merge pull request kodecocodes#347 from pbodsk/apsp-swift3-migration
Browse files Browse the repository at this point in the history
Migrates All-Pairs Shortest Paths to Swift3 syntax
  • Loading branch information
vincentngo authored Jan 10, 2017
2 parents ee53a0d + 07dc70e commit f0c7f8a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install:

script:

# - xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests
- xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests
- xcodebuild test -project ./Array2D/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./AVL\ Tree/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Binary\ Search/Tests/Tests.xcodeproj -scheme Tests
Expand Down
13 changes: 12 additions & 1 deletion All-Pairs Shortest Paths/APSP/APSP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = "Swift Algorithm Club";
TargetAttributes = {
493D8DDF1CDD2A1C0089795A = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0820;
};
493D8DF01CDD5B960089795A = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -305,8 +307,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -350,8 +354,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -370,6 +376,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand All @@ -381,6 +388,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSPTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -392,6 +400,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSPTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -411,6 +420,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSP";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -432,6 +442,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSP";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion All-Pairs Shortest Paths/APSP/APSP/APSP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public protocol APSPAlgorithm {
associatedtype Q: Hashable
associatedtype P: APSPResult

static func apply(graph: AbstractGraph<Q>) -> P
static func apply(_ graph: AbstractGraph<Q>) -> P

}

Expand Down
26 changes: 13 additions & 13 deletions All-Pairs Shortest Paths/APSP/APSP/FloydWarshall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private typealias StepResult = (distances: Distances, predecessors: Predecessors

- note: In all complexity bounds, `V` is the number of vertices in the graph, and `E` is the number of edges.
*/
public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
public struct FloydWarshall<T>: APSPAlgorithm where T: Hashable {

public typealias Q = T
public typealias P = FloydWarshallResult<T>
Expand All @@ -29,7 +29,7 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
- complexity: `Θ(V^3)` time, `Θ(V^2)` space
- returns a `FloydWarshallResult` struct which can be queried for shortest paths and their total weights
*/
public static func apply<T>(graph: AbstractGraph<T>) -> FloydWarshallResult<T> {
public static func apply<T>(_ graph: AbstractGraph<T>) -> FloydWarshallResult<T> {

var previousDistance = constructInitialDistanceMatrix(graph)
var previousPredecessor = constructInitialPredecessorMatrix(previousDistance)
Expand Down Expand Up @@ -59,12 +59,12 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
- returns: a tuple containing the next distance matrix with weights of currently known
shortest paths and the corresponding predecessor matrix
*/
static private func nextStep<T>(intermediateIdx: Int, previousDistances: Distances,
static fileprivate func nextStep<T>(_ intermediateIdx: Int, previousDistances: Distances,
previousPredecessors: Predecessors, graph: AbstractGraph<T>) -> StepResult {

let vertexCount = graph.vertices.count
var nextDistances = Array(count: vertexCount, repeatedValue: Array(count: vertexCount, repeatedValue: Double.infinity))
var nextPredecessors = Array(count: vertexCount, repeatedValue: Array<Int?>(count: vertexCount, repeatedValue: nil))
var nextDistances = Array(repeating: Array(repeating: Double.infinity, count: vertexCount), count: vertexCount)
var nextPredecessors = Array(repeating: Array<Int?>(repeating: nil, count: vertexCount), count: vertexCount)

for fromIdx in 0 ..< vertexCount {
for toIndex in 0 ..< vertexCount {
Expand Down Expand Up @@ -97,12 +97,12 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
- complexity: `Θ(V^2)` time/space
- returns: weighted adjacency matrix in form ready for processing with Floyd-Warshall
*/
static private func constructInitialDistanceMatrix<T>(graph: AbstractGraph<T>) -> Distances {
static fileprivate func constructInitialDistanceMatrix<T>(_ graph: AbstractGraph<T>) -> Distances {

let vertices = graph.vertices

let vertexCount = graph.vertices.count
var distances = Array(count: vertexCount, repeatedValue: Array(count: vertexCount, repeatedValue: Double.infinity))
var distances = Array(repeating: Array(repeating: Double.infinity, count: vertexCount), count: vertexCount)

for row in vertices {
for col in vertices {
Expand All @@ -125,10 +125,10 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {

- complexity: `Θ(V^2)` time/space
*/
static private func constructInitialPredecessorMatrix(distances: Distances) -> Predecessors {
static fileprivate func constructInitialPredecessorMatrix(_ distances: Distances) -> Predecessors {

let vertexCount = distances.count
var predecessors = Array(count: vertexCount, repeatedValue: Array<Int?>(count: vertexCount, repeatedValue: nil))
var predecessors = Array(repeating: Array<Int?>(repeating: nil, count: vertexCount), count: vertexCount)

for fromIdx in 0 ..< vertexCount {
for toIdx in 0 ..< vertexCount {
Expand All @@ -151,10 +151,10 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
It conforms to the `APSPResult` procotol which provides methods to retrieve
distances and paths between given pairs of start and end nodes.
*/
public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
public struct FloydWarshallResult<T>: APSPResult where T: Hashable {

private var weights: Distances
private var predecessors: Predecessors
fileprivate var weights: Distances
fileprivate var predecessors: Predecessors

/**
- returns: the total weight of the path from a starting vertex to a destination.
Expand Down Expand Up @@ -190,7 +190,7 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {

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

if from.index == to.index {
Expand Down
11 changes: 6 additions & 5 deletions All-Pairs Shortest Paths/APSP/APSP/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import Foundation


/**
Print a matrix, optionally specifying only the cells to display with the triplet (i, j, k) -> matrix[i][j], matrix[i][k], matrix[k][j]
*/
func printMatrix(matrix: [[Double]], i: Int = -1, j: Int = -1, k: Int = -1) {
func printMatrix(_ matrix: [[Double]], i: Int = -1, j: Int = -1, k: Int = -1) {

if i >= 0 {
print(" k: \(k); i: \(i); j: \(j)\n")
Expand All @@ -31,12 +32,12 @@ func printMatrix(matrix: [[Double]], i: Int = -1, j: Int = -1, k: Int = -1) {
}
grid.append(row)
}
print((grid as NSArray).componentsJoinedByString("\n"))
print((grid as NSArray).componentsJoined(by: "\n"))
print(" =======================")

}

func printIntMatrix(matrix: [[Int?]]) {
func printIntMatrix(_ matrix: [[Int?]]) {

var grid = [String]()

Expand All @@ -46,14 +47,14 @@ func printIntMatrix(matrix: [[Int?]]) {
for y in 0..<n {
if let value = matrix[x][y] {
let valueString = NSString(format: "%i", value)
row += "\(matrix[x][y] >= 0 ? " " : "")\(valueString) "
row += "\(value >= 0 ? " " : "")\(valueString) "
} else {
row += " ø "
}
}
grid.append(row)
}
print((grid as NSArray).componentsJoinedByString("\n"))
print((grid as NSArray).componentsJoined(by: "\n"))
print(" =======================")

}
6 changes: 3 additions & 3 deletions All-Pairs Shortest Paths/APSP/APSPTests/APSPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import APSP
import Graph
import XCTest

struct TestCase<T where T: Hashable> {
struct TestCase<T> where T: Hashable {

var from: Vertex<T>
var to: Vertex<T>
Expand Down Expand Up @@ -71,7 +71,7 @@ class APSPTests: XCTestCase {

for testCase: TestCase<Int> in cases {
if let computedPath = result.path(fromVertex: testCase.from, toVertex: testCase.to, inGraph: graph),
computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
let computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
XCTAssert(computedDistance == testCase.expectedDistance, "expected distance \(testCase.expectedDistance) but got \(computedDistance)")
XCTAssert(computedPath == testCase.expectedPath, "expected path \(testCase.expectedPath) but got \(computedPath)")
}
Expand Down Expand Up @@ -111,7 +111,7 @@ class APSPTests: XCTestCase {

for testCase: TestCase<Int> in cases {
if let computedPath = result.path(fromVertex: testCase.from, toVertex: testCase.to, inGraph: graph),
computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
let computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
XCTAssert(computedDistance == testCase.expectedDistance, "expected distance \(testCase.expectedDistance) but got \(computedDistance)")
XCTAssert(computedPath == testCase.expectedPath, "expected path \(testCase.expectedPath) but got \(computedPath)")
}
Expand Down

0 comments on commit f0c7f8a

Please sign in to comment.