STVStrategy
TheSTVStrategy class implements a simplified Single Transferable Vote (STV) system for single-winner elections. STV allows voters to rank candidates in order of preference, and uses vote redistribution to ensure the winner has broad support.
Algorithm
This implementation uses a simplified STV algorithm for single-winner scenarios:- Calculate quota: Uses the Droop quota:
floor(total_votes / 2) + 1 - Count first preferences: Tally each ballot’s first-choice candidate
- Check for quota winner: If any candidate meets or exceeds the quota, they win immediately
- Elimination round: If no candidate meets the quota:
- Eliminate the candidate with the fewest votes
- Redistribute their votes to the next preference on each ballot
- The candidate with the most votes after redistribution wins
This is a simplified single-round elimination. Full STV implementations may perform multiple elimination rounds.
Interface
calculateResults
Calculates election results using the STV mechanism with vote redistribution.Array of all ballots cast in the election. Each ballot contains a ranked list of candidate preferences.
Array of all candidates participating in the election.
Ordered array of vote results, sorted by final vote count (descending). Each result contains:
Unique identifier of the candidate
Display name of the candidate
Final vote count after redistribution. For eliminated candidates, this will be 0.
Percentage of total votes based on final count (0-100)
True if this candidate won the election
Example
validateBallot
Validates that a ballot is properly formed for STV voting.The ballot to validate
Number of candidates in the election (not used in current validation)
Returns
true if the ballot contains valid ranked preferences without duplicates, false otherwise.Validation Rules
- The ballot must have a preferences array
- The preferences array must not be empty
- The preferences array must not contain duplicate candidate IDs
- Voters may rank as few or as many candidates as they wish (partial rankings allowed)
Example
Vote Redistribution
When a candidate is eliminated, their votes are redistributed:- For each ballot that ranked the eliminated candidate
- Move to the next preference on that ballot
- If the next preference is also eliminated, continue to the subsequent preference
- If no valid preferences remain, the vote is exhausted
Droop Quota
The quota required to win is calculated as:Implementation Location
Source:~/workspace/source/src/services/strategies/STVStrategy.ts:6-88