Schulze method

''Note that this is a copy of the Wikipedia definition of the Schulze method as at 24 July 2016. It is mirrored here to ensure its integrity and accuracy as of the time and date it was accepted as the standard definition during the 2016 Pirate Party National Congress''

The Schulze method is a voting system developed in 1997 by Markus Schulze that selects a single winner using votes that express preferences. The method can also be used to create a sorted list of winners. The Schulze method is also known as Schwartz Sequential Dropping (SSD), Cloneproof Schwartz Sequential Dropping (CSSD), the Beatpath Method, Beatpath Winner, Path Voting, and Path Winner.

The Schulze method is a Condorcet method, which means the following: if there is a candidate who is preferred by a majority over every other candidate in pairwise comparisons, then this candidate will be the winner when the Schulze method is applied.

The output of the Schulze method (defined below) gives an ordering of candidates. Therefore, if several positions are available, the method can be used for this purpose without modification, by letting the k top-ranked candidates win the k available seats. Furthermore, for proportional representation elections, a single transferable vote variant has been proposed.

Ballot
The input for the Schulze method is the same as for other ranked single-winner election methods: each voter must furnish an ordered preference list on candidates where ties are allowed (a strict weak order).

One typical way for voters to specify their preferences on a ballot is as follows. Each ballot lists all the candidates, and each voter ranks this list in order of preference using numbers: the voter places a '1' beside the most preferred candidate(s), a '2' beside the second-most preferred, and so forth. Each voter may optionally:
 * give the same preference to more than one candidate. This indicates that this voter is indifferent between these candidates.
 * use non-consecutive numbers to express preferences. This has no impact on the result of the elections, since only the order in which the candidates are ranked by the voter matters, and not the absolute numbers of the preferences.
 * keep candidates unranked. When a voter doesn't rank all candidates, then this is interpreted as if this voter (i) strictly prefers all ranked to all unranked candidates, and (ii) is indifferent among all unranked candidates.

Computation
Let $$d[V,W]$$ be the number of voters who prefer candidate $$V$$ to candidate $$W$$.

A path from candidate $$X$$ to candidate $$Y$$ of strength $$p$$ is a sequence of candidates $$C(1),\cdots,C(n)$$ with the following properties:
 * 1) $$C(1) = X$$ and $$C(n) = Y$$.
 * 2) For all $$i = 1,\cdots,(n-1): d[C(i),C(i+1)] > d[C(i+1),C(i)]$$.
 * 3) For all $$i = 1,\cdots,(n-1): d[C(i),C(i+1)] \ge p$$.

Let $$p[A,B]$$, the strength of the strongest path from candidate $$A$$ to candidate $$B$$, be the maximum value such that there is a path from candidate $$A$$ to candidate $$B$$ of that strength. If there is no path from candidate $$A$$ to candidate $$B$$ at all, then $$p[A,B] = 0$$.

Candidate $$D$$ is better than candidate $$E$$ if and only if $$p[D,E] > p[E,D]$$.

Candidate $$D$$ is a potential winner if and only if $$p[D,E] \ge p[E,D]$$ for every other candidate $$E$$.

It can be proven that $$p[X,Y] > p[Y,X]$$ and $$p[Y,Z] > p[Z,Y]$$ together imply $$p[X,Z] > p[Z,X]$$. Therefore, it is guaranteed (1) that the above definition of "better" really defines a transitive relation and (2) that there is always at least one candidate $$D$$ with $$p[D,E] \ge p[E,D]$$ for every other candidate $$E$$.

Implementation
The only difficult step in implementing the Schulze method is computing the strongest path strengths. However, this is a well-known problem in graph theory sometimes called the widest path problem. One simple way to compute the strengths therefore is a variant of the Floyd–Warshall algorithm. The following pseudocode illustrates the algorithm.


 * 1) Input: d[i,j], the number of voters who prefer candidate i to candidate j.
 * 2) Output: p[i,j], the strength of the strongest path from candidate i to candidate j.

for i from 1 to C  for j from 1 to C      if (i ≠ j) then if (d[i,j] > d[j,i]) then p[i,j] := d[i,j] else p[i,j] := 0

for i from 1 to C  for j from 1 to C      if (i ≠ j) then for k from 1 to C           if (i ≠ k and j ≠ k) then p[j,k] := max ( p[j,k], min ( p[j,i], p[i,k] ) )

This algorithm is P (complexity)|efficient, and has time complexity|running time O(C3 where C is the number of candidates.

Ties and alternative implementations
When allowing users to have ties in their preferences, the outcome of the Schulze method naturally depends on how these ties are interpreted in defining d[*,*]. Two natural choices are that d[A, B] represents either the number of voters who strictly prefer A to B (A>B), or the margin of (voters with A>B) minus (voters with B>A). But no matter how the ds are defined, the Schulze ranking has no cycles, and assuming the ds are unique it has no ties.

Although ties in the Schulze ranking are unlikely, they are possible. Schulze's original paper proposed breaking ties in accordance with a voter selected at random, and iterating as needed.

An alternative, slower, way to describe the winner of the Schulze method is the following procedure:
 * 1) draw a complete directed graph with all candidates, and all possible edges between candidates
 * 2) iteratively [a] delete all candidates not in the Schwartz set (i.e. any candidate which cannot reach all others) and [b] delete the weakest link
 * 3) the winner is the last non-deleted candidate.