Select Page
Latest 0.2.0 https://github.com/hectr/swift-elementary-cycles BSD-2 ios 10.0, osx 10.13

# ElementaryCycles

Swift port of an algorythm used to find all the cycles in a directed graph:

This is an implementation of an algorithm by Donald B. Johnson to find all elementary cycles in a directed graph (Donald B. Johnson: Finding All the Elementary Circuits of a Directed Graph. SIAM Journal on Computing. Volumne 4, Nr. 1 (1975), pp. 77-84).

Original Java implementation: http://normalisiert.de

## Usage

### ElementaryCycles

``````import ElementaryCycles

// dictionary pairs represent node connections from the keys to their values
let graph = ["A": ["B", "C"], "B": ["A"], "C": ["D"], "D": ["C"]]

let cycles = ElementaryCycles.find(graph: graph, sort: { \$0 < \$1 })

// nodes order is determined by sort parameter
print(cycles) // [["A", "B"], ["C", "D"]]``````
• Input: Directed graph
``````                ┌─────────┐
∨         │
┌───┐     ┌───┐     ┌───┐
┌> │ A │ ──> │ C │ ──> │ D │
│  └───┘     └───┘     └───┘
│    │
│    │
│    ∨
│  ┌───┐
└─ │ B │
└───┘``````
• Output: Elementary circuits
`````` ┌───┐     ┌───┐
│ A │ ──> │ B │
└───┘     └───┘
┌───┐     ┌───┐
│ C │ ──> │ D │
└───┘     └───┘``````

### ElementaryCyclesSearch

Alternatively, you can use ElementaryCyclesSearch library directly, which contains the actual algorythm implementation:

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.

Original Java implemnetation: http://normalisiert.de

``````import ElementaryCyclesSearch

let nodes = Vector(["A", "B", "C", "D"])
let matrix = AdjacencyMatrix(4, 4) { matrix in
matrix[0][1] = true
matrix[0][2] = true
matrix[1][0] = true
matrix[2][3] = true
matrix[3][2] = true
}
let cycles = getElementaryCycles(adjacencyMatrix: matrix, graphNodes: nodes)``````
• Input: Node objects
A B C D
A B C D
A `false` `true` `false` `false`
B `true` `false` `false` `false`
C `true` `false` `false` `true`
D `false` `false` `true` `false`
• Output: Elementary circuits
`````` ┌───┐     ┌───┐
│ A │ ──> │ B │
└───┘     └───┘
┌───┐     ┌───┐
│ C │ ──> │ D │
└───┘     └───┘``````

Original work Copyright (c) 2012, Frank Meyer

Swift port 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.

### Latest podspec

```{
"name": "ElementaryCyclesSearch",
"version": "0.2.0",
"summary": "Elementary Circuits of a Directed Graph",
"description": "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.",
"homepage": "https://github.com/hectr/swift-elementary-cycles",
"type": "BSD-2",
},
"authors": "Heu0300ctor Marqueu0300s",
"platforms": {
"ios": "10.0",
"osx": "10.13"
},
"source": {
"git": "https://github.com/hectr/swift-elementary-cycles.git",
"tag": "0.2.0"
},
"source_files": "Sources/ElementaryCyclesSearch/**/*",
"swift_version": "4.2"
}```