The Schulze method is a ranked-choice voting algorithm. It guarantees that a majority-preferred candidate wins when one exists. Preference cycles are resolved using transitive beatpaths: if A defeats B and B defeats C, then A indirectly defeats C.
https://en.wikipedia.org/wiki/Schulze_method
You need Swift 6.2 or later installed on your system. To install Swift, visit:
git clone https://github.com/uberbruns/SwiftSchulze.git
cd SwiftSchulzeYou can run the tool directly without building using:
swift run schulze --helpTo build an optimized release binary:
swift build -c releaseThe compiled binary will be located at:
.build/release/schulze
You can then copy it to a location in your PATH:
cp .build/release/schulze /usr/local/bin/If you want to use the Schulze algorithm in your Swift application, add this package as a dependency in your Package.swift:
dependencies: [
.package(url: "https://github.com/uberbruns/SwiftSchulze.git", from: "0.1.0")
]Then add SchulzeLibrary to your target dependencies:
.target(
name: "YourTarget",
dependencies: [
.product(name: "SchulzeLibrary", package: "SwiftSchulze")
]
)Import and use in your code:
import SchulzeLibrary
let rankings = [
["Alice", "Bob", "Charlie"],
["Bob", "Charlie", "Alice"],
["Charlie", "Alice", "Bob"]
]
let winner = Schulze.ranking(rankings: rankings)
print(winner)OVERVIEW: Tool for calculating the winner of an election by letting voters cast
their vote in form of a ranked list of candidates.
USAGE: schulze [--ranking <ranking> ...] [--format <format>] [--stdin] [--path <path>] [--directory <directory>]
OPTIONS:
-r, --ranking <ranking> A comma separated ranking of candidates casted by one
voter. Repeat this option for every participant.
-f, --format <format> The output format ('plain' or 'json'). (default:
plain)
-s, --stdin Takes a list of votes from standard input. Every line
should contain a comma separated ranking.
-p, --path <path> Use this option to provide a path to a file
containing the casted rankings. Every line should
contain the candidates ranked by one voter seperated
via comma.
-d, --directory <directory>
Use this option to provide a directory containing
.txt files. Every file should contain the candidates
ranked by one voter. One candidate per line.
-h, --help Show help information.
This project is released under the MIT license. See LICENSE for details.