forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- define an abstract class called AbstractGraph, with two concrete implementations: AdjacencyListGraph and AdjacencyMatrixGraph - improve some method naming, especially those dealing with creating (un)directed edges - make a framework target, and add xcodeproj and playground to a workspace to import the framework module so we don't need to duplicate the implementation in the playground - update README to reflect the new implementations/concepts consequently, the APSP implementation is updated to use the refactored Graph implementation, plus: - improvements to doc comments - remove redundant parameter in FloydWarshallResult.recursePathFrom - changed the test case ported from the book to reflect the exact names of the graph vertices
- Loading branch information
1 parent
2b58f9f
commit 495303b
Showing
33 changed files
with
1,434 additions
and
536 deletions.
There are no files selected for viewing
23 changes: 12 additions & 11 deletions
23
All-Pairs Shortest Paths/APSP/APSP.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,30 @@ | ||
//: Playground - noun: a place where people can play | ||
|
||
import Graph | ||
import APSP | ||
|
||
var graph = Graph<String>() | ||
var graph = AdjacencyListGraph<String>() | ||
|
||
let v1 = graph.createVertex("Montreal") | ||
let v2 = graph.createVertex("New York") | ||
let v3 = graph.createVertex("Boston") | ||
let v4 = graph.createVertex("Portland") | ||
let v5 = graph.createVertex("Portsmouth") | ||
|
||
graph.connect(v1, to: v2, withWeight: 3) | ||
graph.connect(v1, to: v5, withWeight: -4) | ||
graph.connect(v1, to: v3, withWeight: 8) | ||
graph.addDirectedEdge(v1, to: v2, withWeight: 3) | ||
graph.addDirectedEdge(v1, to: v5, withWeight: -4) | ||
graph.addDirectedEdge(v1, to: v3, withWeight: 8) | ||
|
||
graph.connect(v2, to: v4, withWeight: 1) | ||
graph.connect(v2, to: v5, withWeight: 7) | ||
graph.addDirectedEdge(v2, to: v4, withWeight: 1) | ||
graph.addDirectedEdge(v2, to: v5, withWeight: 7) | ||
|
||
graph.connect(v3, to: v2, withWeight: 4) | ||
graph.addDirectedEdge(v3, to: v2, withWeight: 4) | ||
|
||
graph.connect(v4, to: v1, withWeight: 2) | ||
graph.connect(v4, to: v3, withWeight: -5) | ||
graph.addDirectedEdge(v4, to: v1, withWeight: 2) | ||
graph.addDirectedEdge(v4, to: v3, withWeight: -5) | ||
|
||
graph.connect(v5, to: v4, withWeight: 6) | ||
graph.addDirectedEdge(v5, to: v4, withWeight: 6) | ||
|
||
let result = FloydWarshall<String>.apply(graph) | ||
|
||
let path = result.path(fromVertex: v1, toVertex: v4, inGraph: graph) | ||
let path = result.path(fromVertex: v1, toVertex: v4, inGraph: graph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.