AVStrategy
TheAVStrategy class implements the Alternative Vote (AV) system, also known as Instant Runoff Voting (IRV). Voters rank candidates in order of preference, and if no candidate achieves an absolute majority, the lowest-ranked candidate is iteratively eliminated with votes redistributed until a winner emerges.
Algorithm
AV uses an iterative elimination process:- Calculate majority threshold:
floor(total_votes / 2) + 1 - Count first preferences: Tally each ballot’s current highest-ranked active candidate
- Check for majority winner: If any candidate has votes ≥ majority, they win
- Elimination round: If no majority winner:
- Eliminate the candidate with the fewest votes
- Redistribute their ballots to the next available preference
- Return to step 2
- Repeat until a candidate achieves majority or only one candidate remains
Interface
calculateResults
Calculates election results using the Alternative Vote mechanism with iterative elimination.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 in the decisive round. Eliminated candidates show their count when eliminated.
Percentage of total ballots based on final count (0-100)
True if this candidate won the election (achieved majority or was last remaining)
Example
Multi-Round Example
validateBallot
Validates that a ballot is properly formed for AV 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
Example
Vote Redistribution
When a candidate is eliminated during counting:- All ballots currently assigned to the eliminated candidate are redistributed
- Each ballot transfers to its next-ranked active candidate
- If the next preference is also eliminated, continue down the ranking
- Ballots with no remaining active preferences are exhausted (do not count)
Majority Calculation
The majority threshold is calculated as:Exhausted Ballots
Ballots become exhausted when:- All ranked candidates have been eliminated
- The ballot did not rank any remaining candidates
Implementation Location
Source:~/workspace/source/src/services/strategies/AVStrategy.ts:6-93