Skip to content

Commit c254522

Browse files
committed
Release 2.0.0
1 parent ade5899 commit c254522

6 files changed

Lines changed: 37 additions & 18 deletions

File tree

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0
1+
4.2

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
### 2.0.0
2+
- **This is an API breaking refactor**
3+
- This version requires Swift 4.2 (Xcode 10)
4+
- `Graph` is now a protocol instead of a class
5+
- `edgesToVertices()` is now a method on `Graph` instead of a free function
6+
- The `Edge` protocol has been significantly simplifieid
7+
- `UnweightedEdge` and `WeightedEdge` are now `Codable`
8+
- Subclasses of `Graph` `CodableUnweightedGraph` and `CodableWeightedGraph` provide serialization support to JSON and anything else `Codable` supports (thanks for the help, @yoiang)
9+
- Experimental subclass of `UnweightedGraph`, `UniqueElementsGraph` provides a union operation and guarantees no duplicate vertices & edges in a graph (thanks @ferranpujolcamins)
10+
- Cycle detector method that returns edges (thanks @ZevEisenberg)
11+
112
### 1.5.1
213
- Project reorganized to support testing on Linux, just run `swift test`
314
- `Package.swift` updated for Swift 4 package management style

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It includes copious in-source documentation, unit tests, as well as search funct
1717

1818
## Installation
1919

20-
SwiftGraph 2.0 (WIP on master) and above requires Swift 4.2 (Xcode 10). Use SwiftGraph 1.5.1 for Swift 4.1 (Xcode 9), SwiftGraph 1.4.1 for Swift 3 (Xcode 8), SwiftGraph 1.0.6 for Swift 2 (Xcode 7), and SwiftGraph 1.0.0 for Swift 1.2 (Xcode 6.3) support.
20+
SwiftGraph 2.0 and above requires Swift 4.2 (Xcode 10). Use SwiftGraph 1.5.1 for Swift 4.1 (Xcode 9), SwiftGraph 1.4.1 for Swift 3 (Xcode 8), SwiftGraph 1.0.6 for Swift 2 (Xcode 7), and SwiftGraph 1.0.0 for Swift 1.2 (Xcode 6.3) support. SwiftGraph runs fine and is tested on Linux.
2121

2222
### CocoaPods
2323

@@ -28,7 +28,7 @@ Use the CocoaPod `SwiftGraph`.
2828
Add the following to your `Cartfile`:
2929

3030
```
31-
github "davecom/SwiftGraph" ~> 1.5.1
31+
github "davecom/SwiftGraph" ~> 2.0.0
3232
```
3333

3434
### Swift Package Manager (SPM)
@@ -88,7 +88,7 @@ var nameDistance: [String: Int?] = distanceArrayToVertexDict(distances: distance
8888
let temp = nameDistance["San Francisco"]
8989
// path between New York and San Francisco
9090
let path: [WeightedEdge<Int>] = pathDictToPath(from: cityGraph.indexOfVertex("New York")!, to: cityGraph.indexOfVertex("San Francisco")!, pathDict: pathDict)
91-
let stops: [String] = edgesToVertices(edges: path, graph: cityGraph)
91+
let stops: [String] = cightGraph.edgesToVertices(edges: path)
9292
```
9393
The shortest paths are found between various vertices in the graph using Dijkstra's algorithm.
9494
```swift
@@ -118,17 +118,14 @@ There is a large amount of documentation in the source code using the latest App
118118
### Edges
119119
Edges connect the vertices in your graph to one another.
120120

121-
* `Edge` (Protocol) - A protocol that all edges in a graph must conform to. An edge is a connection between two vertices in the graph. The vertices are specified by their index in the graph which is an integer. Further, an edge knows if it's directed, or weighted. An edge can create a reversed version of itself.
121+
* `Edge` (Protocol) - A protocol that all edges in a graph must conform to. An edge is a connection between two vertices in the graph. The vertices are specified by their index in the graph which is an integer.
122122
* `UnweightedEdge` - This is a concrete implementation of `Edge` for unweighted graphs.
123-
* `WeightedEdge` - A subclass of `UnweightedEdge` that adds weights. Weights are a generic type - they can be anything that implements `Comparable` and `Summable`. `Summable` is anything that implements the `+` operator. To add `Summable` support to a data type that already has the plus operator, simply write something like (support in SwiftGraph is already included for `Int`, `Float`, `Double`, and `String`):
124-
```swift
125-
extension Int: Summable {}
126-
```
123+
* `WeightedEdge` - A subclass of `UnweightedEdge` that adds weights. Weights are a generic type - they can be anything that implements `Comparable`, `Numeric` and `Codable`. Typical weight types are `Int` and `Float`.
127124

128125
### Graphs
129126
Graphs are the data structures at the heart of SwiftGraph. All vertices are assigned an integer index when they are inserted into a graph and it's generally faster to refer to them by their index than by the vertex's actual object.
130127

131-
Graphs implement the standard Swift protocols `SequenceType` (for iterating through all vertices) and `CollectionType` (for grabbing a vertex by its index through a subscript). For instance, the following example prints all vertices in a Graph on separate lines:
128+
Graphs implement the standard Swift protocols `Collection` (for iterating through all vertices and for grabbing a vertex by its index through a subscript). For instance, the following example prints all vertices in a Graph on separate lines:
132129
```swift
133130
for v in g { // g is a Graph<String>
134131
print(v)
@@ -141,7 +138,7 @@ print(g[23]) // g is a Graph<String>
141138

142139
Note: At this time, graphs are *not* thread-safe. However, once a graph is constructed, if you will only be doing lookups and searches through it (no removals of vertices/edges and no additions of vertices/edges) then you should be able to do that from multiple threads. A fully thread-safe graph implementation is a possible future direction.
143140

144-
* `Graph` - This is the base class for all graphs. Generally, you should use one of its canonical subclasses, `UnweightedGraph` or `WeightedGraph`, because they offer more functionality. The vertices in a `Graph` (defined as a generic at graph creation time) can be of any type that conforms to `Equatable`. `Graph` has methods for:
141+
* `Graph` (Protocol) - This is the base protocol for all graphs. Generally, you should use one of its canonical class implementations, `UnweightedGraph` or `WeightedGraph`, instead of rolling your own adopter, because they offer significant built-in functionality. The vertices in a `Graph` (defined as a generic at graph creation time) can be of any type that conforms to `Equatable`. `Graph` has methods for:
145142
* Adding a vertex
146143
* Getting the index of a vertex
147144
* Finding the neighbors of an index/vertex
@@ -151,8 +148,10 @@ Note: At this time, graphs are *not* thread-safe. However, once a graph is const
151148
* Adding an edge
152149
* Removing all edges between two indexes/vertices
153150
* Removing a particular vertex (all other edge relationships are automatically updated at the same time (because the indices of their connections changes) so this is slow - O(v + e) where v is the number of vertices and e is the number of edges)
154-
* `UnweightedGraph` - A subclass of `Graph` that adds convenience methods for adding and removing edges of type `UnweightedEdge`.
155-
* `WeightedGraph` - A subclass of `Graph` that adds convenience methods for adding and removing edges of type `WeightedEdge`. `WeightedGraph` also adds a method for returning a list of tuples containing all of the neighbor vertices of an index along with their respective weights.
151+
* `UnweightedGraph` - A generic class implementation of `Graph` that adds convenience methods for adding and removing edges of type `UnweightedEdge`. `UnweightedGraph` is generic over the type of the vertices.
152+
* `WeightedGraph` - A generic class implementation of `Graph` that adds convenience methods for adding and removing edges of type `WeightedEdge`. `WeightedGraph` also adds a method for returning a list of tuples containing all of the neighbor vertices of an index along with their respective weights. `WeightedGraph` is generic over the types of the vertices and its weights.
153+
* `CodableUnweightedGraph` and `CodableWeightedGraph` - Subclasses of `UnweightedGraph` and `WeightedGraph` that add support for the `Codable` protocol. Their vertex and weight types must also be `Codable`. This allows serialization of graphs to formats like JSON.
154+
* `UniqueElementsGraph` - an experimental subclass of `UnweightedGraph` with support for union operations that ensures all vertices and edges in a graph are unique.
156155

157156
### Search
158157
Search methods are defined in extensions of `Graph` and `WeightedGraph` in `Search.swift`.
@@ -172,12 +171,14 @@ An extension to `WeightedGraph` in `MST.swift` can find a minimum-spanning tree
172171
An extension to `Graph` in `Cycles.swift` finds all of the cycles in a graph.
173172
* `detectCycles()` - Uses an algorithm developed by Liu/Wang to find all of the cycles in a graph. Optionally, this method can take one parameter, `upToLength`, that specifies a length at which to stop searching for cycles. For instance, if `upToLength` is 3, `detectCycles()` will find all of the 1 vertex cycles (self-cycles, vertices with edges to themselves), and 3 vertex cycles (connection to another vertex and back again, present in all undirected graphs with more than 1 vertex). There is no such thing as a 2 vertex cycle.
174173

175-
## Authorship & License
176-
SwiftGraph is written by David Kopec and released under the Apache License (see `LICENSE`). You can find my email address on my GitHub profile page. I encourage you to submit pull requests and open issues here on GitHub.
174+
## Authorship, License, & Contributors
175+
SwiftGraph is written by David Kopec and released under the Apache License (see `LICENSE`). You can find my email address on my GitHub profile page. I encourage you to submit pull requests and open issues here on GitHub.
176+
177+
I would like to thank all of the contributors who have helped improve SwiftGraph over the years, and have kept me motivated. Contributing to SwiftGraph, in-line with the Apache license, means also releasing your contribution under the same license as the original project. However, the Apache license is permissive, and you are free to include SwiftGraph in a commercial, closed source product as long as you give it & its author credit (in fact SwiftGraph has already found its way into several products). See `LICENSE` for details.
177178

178179
## Future Direction
179180
Future directions for this project to take could include:
180181
* More utility functions
181-
* A thread safe subclass of `Graph`
182+
* A thread safe implementation of `Graph`
182183
* More extensive performance testing
183-
* Integration with Swift serialization (`Codable` support)
184+
* GraphML Support

SwiftGraph.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'SwiftGraph'
3-
s.version = '1.5.1'
3+
s.version = '2.0.0'
44
s.license = { :type => "Apache License, Version 2.0", :file => "LICENSE" }
55
s.summary = 'A Graph Data Structure in Pure Swift'
66
s.homepage = 'https://github.com/davecom/SwiftGraph'

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ XCTMain([
1212
testCase(UniqueElementsGraphTests.allTests),
1313
testCase(UniqueElementsGraphInitTests.allTests),
1414
testCase(UnionTests.allTests),
15+
testCase(SwiftGraphCodableTests.allTests),
1516
])

Tests/SwiftGraphTests/SwiftGraphCodableTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,10 @@ extension SwiftGraphCodableTests {
234234
self.validateDijkstra1(cityGraph: g2)
235235
// XCTAssertEqual(g, self.cityGraph())
236236
}
237+
238+
static var allTests = [
239+
("testEncodable", testEncodable),
240+
("testDecodable", testDecodable),
241+
("testComplexWeightedEncodableDecodable", testComplexWeightedEncodableDecodable)
242+
]
237243
}

0 commit comments

Comments
 (0)