Compare commits

...

5 Commits

Author SHA1 Message Date
hectr 824a7d8c9b Release 0.4.0 2019-08-11 19:39:23 +02:00
hectr e9ddc48972 Move Vector and Matrix2D to Idioms package 2019-08-11 19:39:02 +02:00
hectr e7ea5a92a8 Rename Matrix to Matrix2D 2019-07-15 00:29:26 +02:00
hectr 7a87ec23a7 Release 0.3.0 2019-07-14 23:35:50 +02:00
hectr 5369124c60 Update package to swift-tools 5.0 2019-07-14 23:35:02 +02:00
20 changed files with 60 additions and 294 deletions

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ElementaryCycles'
s.version = '0.2.0'
s.version = '0.4.0'
s.summary = 'Find all elementary cycles in a directed graph'
s.description = <<-DESC
Swift port of an algorythm by Donald B. Johnson to find all the cycles in a directed graph.
@ -14,5 +14,5 @@ Swift port of an algorythm by Donald B. Johnson to find all the cycles in a dire
s.source = { :git => 'https://github.com/hectr/swift-elementary-cycles.git', :tag => s.version.to_s }
s.source_files = 'Sources/ElementaryCycles/**/*'
s.dependency 'ElementaryCyclesSearch'
s.swift_version = '4.2'
s.swift_version = '5.0'
end

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ElementaryCyclesSearch'
s.version = '0.2.0'
s.version = '0.4.0'
s.summary = 'Elementary Circuits of a Directed Graph'
s.description = <<-DESC
The implementation is pretty much generic, all it needs is a adjacency-matrix of your graph and the objects of your nodes. Then you get back the sets of node-objects which build a cycle.
@ -13,5 +13,6 @@ The implementation is pretty much generic, all it needs is a adjacency-matrix of
s.osx.deployment_target = '10.13'
s.source = { :git => 'https://github.com/hectr/swift-elementary-cycles.git', :tag => s.version.to_s }
s.source_files = 'Sources/ElementaryCyclesSearch/**/*'
s.swift_version = '4.2'
s.dependency 'Idioms'
s.swift_version = '5.0'
end

16
Package.resolved Normal file
View File

@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "Idioms",
"repositoryURL": "https://github.com/hectr/swift-idioms.git",
"state": {
"branch": null,
"revision": "320c051b0f4775d2ccca6120ab673416300679f9",
"version": "1.4.0"
}
}
]
},
"version": 1
}

View File

@ -1,4 +1,4 @@
// swift-tools-version:4.2
// swift-tools-version:5.0
import PackageDescription
@ -16,6 +16,7 @@ let package = Package(
targets: ["ElementaryCyclesSearchExample"]),
],
dependencies: [
.package(url: "https://github.com/hectr/swift-idioms.git", from: "1.4.0"),
],
targets: [
.target(
@ -23,7 +24,7 @@ let package = Package(
dependencies: ["ElementaryCyclesSearch"]),
.target(
name: "ElementaryCyclesSearch",
dependencies: []),
dependencies: ["Idioms"]),
.target(
name: "ElementaryCycles",
dependencies: ["ElementaryCyclesSearch"]),

View File

@ -12,9 +12,10 @@
*/
import Swift
import Idioms
import ElementaryCyclesSearch
extension Matrix where Element == Bool {
extension Matrix2D where Element == Bool {
enum Error: Swift.Error {
case indexNotFound(node: AnyHashable, nodes: [AnyHashable])
}
@ -24,7 +25,7 @@ extension Matrix where Element == Bool {
for (offset, node) in nodes.enumerated() {
if let adjacentNodes = adjacencyDictionary[node] {
for adjacentNode in adjacentNodes {
guard let adjacentIndex = nodes.index(of: adjacentNode) else {
guard let adjacentIndex = nodes.firstIndex(of: adjacentNode) else {
throw Error.indexNotFound(node: adjacentNode, nodes: nodes)
}
matrix[offset][adjacentIndex] = true

View File

@ -12,9 +12,10 @@
*/
import Swift
import Idioms
import ElementaryCyclesSearch
extension Matrix where Element == Bool {
extension Matrix2D where Element == Bool {
static func getNodes<Node: Hashable>(graph: [Node: [Node]], sort: ((Node, Node) -> Bool)?) -> [Node] {
var nodes = [Node]()
for (node, adjacentNodes) in graph {

View File

@ -12,6 +12,7 @@
*/
import Swift
import Idioms
import ElementaryCyclesSearch
public struct ElementaryCycles {

View File

@ -15,6 +15,7 @@
*/
import Swift
import Idioms
/**
* Calculates the adjacency-list for a given adjacency-matrix.
@ -24,7 +25,7 @@ import Swift
* @version 1.0, 26.08.2006
*
*/
extension Matrix where Element == Int {
extension Matrix2D where Element == Int {
/**
* Calculates a adjacency-list for a given array of an adjacency-matrix.
*
@ -35,8 +36,8 @@ extension Matrix where Element == Int {
* adjacency, the second dimension represents the indicies of those nodes,
* that are direct successornodes of the node.
*/
static func getAdjacencyList(adjacencyMatrix: AdjacencyMatrix) -> Matrix<Int> {
let list = Matrix<Int>(adjacencyMatrix.reservedLength)
static func getAdjacencyList(adjacencyMatrix: AdjacencyMatrix) -> Matrix2D<Int> {
let list = Matrix2D<Int>(adjacencyMatrix.reservedLength)
for i in 0 ..< adjacencyMatrix.reservedLength {
var v = [Int]()

View File

@ -15,5 +15,6 @@
*/
import Swift
import Idioms
typealias AdjacencyList = Matrix<Int>
typealias AdjacencyList = Matrix2D<Int>

View File

@ -15,6 +15,7 @@
*/
import Swift
import Idioms
/**
* This is a helpclass for the search of all elementary cycles in a graph

View File

@ -15,10 +15,11 @@
*/
import Swift
import Idioms
public typealias AdjacencyMatrix = Matrix<Bool>
public typealias AdjacencyMatrix = Matrix2D<Bool>
extension Matrix where Element == Bool {
extension Matrix2D where Element == Bool {
public convenience init(_ reservedRows: Int, _ reservedColumns: Int = 0, builder: (AdjacencyMatrix) -> Void) {
self.init(reservedRows, reservedColumns)
builder(self)

View File

@ -15,6 +15,7 @@
*/
import Swift
import Idioms
/**
* Searchs all elementary cycles in a given directed graph. The implementation
@ -54,7 +55,7 @@ public class ElementaryCyclesSearch<Node> {
private var blocked: Vector<Bool>
/** B-Lists, used by the algorithm of Johnson */
private var B: Matrix<Int>
private var B: Matrix2D<Int>
/** Stack for nodes, used by the algorithm of Johnson */
private var stack: [Int]
@ -88,7 +89,7 @@ public class ElementaryCyclesSearch<Node> {
self.adjacencyList = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjacencyMatrix)
cycles = [[Node]]()
blocked = Vector<Bool>(adjacencyList.reservedLength)
B = Matrix<Int>(adjacencyList.reservedLength)
B = Matrix2D<Int>(adjacencyList.reservedLength)
stack = [Int]()
}
@ -168,7 +169,7 @@ public class ElementaryCyclesSearch<Node> {
}
}
if let index = stack.index(of: v) {
if let index = stack.firstIndex(of: v) {
stack.remove(at: index)
}
return f

View File

@ -1,41 +0,0 @@
/*
(BSD-2 license)
Copyright (c) 2018, Hèctor Marquès
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import Swift
public struct MatrixIterator<Element>: IteratorProtocol {
private let matrix: Matrix<Element>
private var currentIndex = -1
private var currentIterator: VectorIterator<Element>?
fileprivate init(matrix: Matrix<Element>) {
self.matrix = matrix
}
mutating public func next() -> Element? {
if let element = currentIterator?.next() {
return element
}
currentIndex += 1
guard matrix.reservedLength > currentIndex else { return nil }
let vector = matrix[currentIndex]
currentIterator = vector?.makeIterator()
return next()
}
}
extension Matrix: Sequence {
public func makeIterator() -> MatrixIterator<Element> {
return MatrixIterator(matrix: self)
}
}

View File

@ -1,71 +0,0 @@
/*
(BSD-2 license)
Copyright (c) 2018, Hèctor Marquès
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import Swift
public final class Matrix<Element> {
private var vector: Vector<Vector<Element>>
public subscript(_ row: Int) -> Vector<Element>! {
get {
return vector[row]
}
set(newValue) {
vector[row] = newValue
}
}
public init(_ reservedRows: Int, _ reservedColumns: Int = 0) {
self.vector = Vector<Vector<Element>>(reservedRows)
for i in 0 ..< reservedLength {
vector[i] = Vector<Element>(reservedColumns)
}
}
public convenience init(rows: [[Element]]) {
var reservedColumns = 0
for row in rows {
reservedColumns = Swift.max(reservedColumns, row.count)
}
self.init(rows.count, reservedColumns)
for (offset, row) in rows.enumerated() {
self[offset] = Vector(array: row, reservedLength: reservedColumns)
}
}
public var reservedLength: Int {
return vector.reservedLength
}
}
extension Matrix: CustomDebugStringConvertible {
public var debugDescription: String {
return vector.debugDescription
}
}
extension Matrix: CustomStringConvertible {
public var description: String {
return vector.description
}
}
extension Matrix: Equatable where Element: Equatable {
public static func == (lhs: Matrix<Element>, rhs: Matrix<Element>) -> Bool {
guard lhs.reservedLength == rhs.reservedLength else { return false }
for index in 0 ..< lhs.reservedLength {
guard lhs[index] == rhs[index] else { return false }
}
return true
}
}

View File

@ -1,36 +0,0 @@
/*
(BSD-2 license)
Copyright (c) 2018, Hèctor Marquès
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import Swift
public struct VectorIterator<Element>: IteratorProtocol {
private let vector: Vector<Element>
private var currentIndex = -1
fileprivate init(vector: Vector<Element>) {
self.vector = vector
}
mutating public func next() -> Element? {
currentIndex += 1
guard vector.size > currentIndex else { return nil }
guard let element = vector[currentIndex] else { return next() }
return element
}
}
extension Vector: Sequence {
public func makeIterator() -> VectorIterator<Element> {
return VectorIterator(vector: self)
}
}

View File

@ -1,115 +0,0 @@
/*
(BSD-2 license)
Copyright (c) 2018, Hèctor Marquès
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import Swift
public final class Vector<Element> {
private var array: [Element?] {
didSet {
reservedLength = Swift.max(reservedLength, array.count)
}
}
public private(set) var reservedLength: Int {
didSet {
assert(reservedLength >= size)
}
}
public subscript(_ index: Int) -> Element? {
get {
guard array.count > index || index >= reservedLength else { return nil }
return array[index]
}
set(newValue) {
while index >= array.count && index < reservedLength {
array.append(nil)
}
array[index] = newValue
}
}
public init(array: [Element], reservedLength: Int? = nil) {
self.array = array
self.reservedLength = Swift.max(reservedLength ?? 0, array.count)
}
public init(_ reservedLength: Int = 0) {
self.array = [Element?]()
self.reservedLength = reservedLength
}
public var size: Int {
return array.count
}
public func get(_ index: Int) -> Element {
guard let element = self[index] else { fatalError("\(#function): index (\(index)) out of bounds (\(reservedLength))") }
return element
}
public func add(_ newElement: Element) {
array.append(newElement)
}
@discardableResult
public func remove(at index: Int) -> Element? {
var removedObject: Element?
if index < array.count {
removedObject = array[index]
array.remove(at: index)
} else if index >= reservedLength {
preconditionFailure("Index out of range: \(index) is beyond count (\(array.count) and reservedLength \(reservedLength)")
}
reservedLength -= 1
return removedObject
}
}
extension Vector where Element: Equatable {
func contains(_ element: Element) -> Bool{
return array.contains(element)
}
@discardableResult
func remove(element: Element) -> Bool {
if let index = array.index(of: element) {
array.remove(at: -index.distance(to: 0))
return true
}
return false
}
}
extension Vector: CustomDebugStringConvertible {
public var debugDescription: String {
return array.debugDescription
}
}
extension Vector: CustomStringConvertible {
public var description: String {
return array.description
}
}
extension Vector: Equatable where Element: Equatable {
public static func == (lhs: Vector<Element>, rhs: Vector<Element>) -> Bool {
guard lhs.reservedLength == rhs.reservedLength else { return false }
guard lhs.size == rhs.size else { return false }
for index in 0 ..< lhs.size {
guard lhs[index] == rhs[index] else { return false }
}
return true
}
}

View File

@ -1,4 +1,5 @@
import XCTest
import Idioms
@testable import ElementaryCyclesSearch
final class AdjacencyListTests: XCTestCase {
@ -16,7 +17,7 @@ final class AdjacencyListTests: XCTestCase {
}
}
var sut: ((AdjacencyMatrix) -> Matrix<Int>)!
var sut: ((AdjacencyMatrix) -> Matrix2D<Int>)!
override func setUp() {
super.setUp()

View File

@ -1,4 +1,5 @@
import XCTest
import Idioms
@testable import ElementaryCyclesSearch
final class AdjacencyMatrixTests: XCTestCase {
@ -22,9 +23,9 @@ final class AdjacencyMatrixTests: XCTestCase {
func testConvenienceInit() {
let rows = [["a"],[],["b", "c"]]
let matrix = Matrix<String>(rows: rows)
let expectedMatrix: Matrix<String> = {
let matrix = Matrix<String>(3, 2)
let matrix = Matrix2D<String>(rows: rows)
let expectedMatrix: Matrix2D<String> = {
let matrix = Matrix2D<String>(3, 2)
matrix[0][0] = "a"
matrix[2][0] = "b"
matrix[2][1] = "c"
@ -34,7 +35,7 @@ final class AdjacencyMatrixTests: XCTestCase {
}
func testSequence() {
let matrix = Matrix<Int>(4, 4)
let matrix = Matrix2D<Int>(4, 4)
matrix[0][1] = 1
matrix[1][2] = 2
matrix[3][0] = 3

View File

@ -1,4 +1,5 @@
import XCTest
import Idioms
@testable import ElementaryCyclesSearch
final class StrongConnectedComponentsTests: XCTestCase {
@ -7,7 +8,7 @@ final class StrongConnectedComponentsTests: XCTestCase {
func test1() {
let adjMatrix = Matrix(10, 10) { matrix in
let adjMatrix = Matrix2D(10, 10) { matrix in
matrix[0][1] = true
matrix[1][2] = true
matrix[2][0] = true; matrix[2][6] = true
@ -20,7 +21,7 @@ final class StrongConnectedComponentsTests: XCTestCase {
matrix[6][1] = true
}
let adjacencyList: Matrix<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
let adjacencyList: Matrix2D<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
let sccs = StrongConnectedComponents(adjacencyList: adjacencyList)
var description = ""
@ -91,7 +92,7 @@ i: 9
}
func test2() {
let adjMatrix: AdjacencyMatrix = Matrix(10, 10) { matrix in
let adjMatrix: AdjacencyMatrix = Matrix2D(10, 10) { matrix in
matrix[0][1] = true
matrix[1][2] = true
matrix[2][0] = true
@ -106,7 +107,7 @@ i: 9
matrix[9][6] = true
}
let adjacencyList: Matrix<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
let adjacencyList: Matrix2D<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
let sccs = StrongConnectedComponents(adjacencyList: adjacencyList)
var description = ""

View File

@ -31,10 +31,10 @@ final class AdjacencyMatrixShould: XCTestCase {
"IT": ["FR"]]
let nodes = AdjacencyMatrix.getNodes(graph: graph, sort: nil)
let adjacencyMatrix = try! AdjacencyMatrix.getAdjacencyMatrix(nodes: nodes, adjacencyDictionary: graph)
let es = nodes.index(of: "ES")!
let pt = nodes.index(of: "PT")!
let fr = nodes.index(of: "FR")!
let it = nodes.index(of: "IT")!
let es = nodes.firstIndex(of: "ES")!
let pt = nodes.firstIndex(of: "PT")!
let fr = nodes.firstIndex(of: "FR")!
let it = nodes.firstIndex(of: "IT")!
XCTAssertTrue(adjacencyMatrix[es][pt] ?? adjacencyMatrix[pt][es] ?? false)
XCTAssertTrue(adjacencyMatrix[es][fr] ?? adjacencyMatrix[fr][es] ?? false)
XCTAssertTrue(adjacencyMatrix[fr][it] ?? adjacencyMatrix[it][fr] ?? false)