1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use super::errors::TallyError;
use super::RankedCandidate;
use super::RankedWinners;
use hashbrown::HashMap;
use num_traits::cast::NumCast;
use num_traits::Num;
use petgraph::algo::tarjan_scc;
use petgraph::graph::NodeIndex;
use petgraph::Graph;
use std::convert::TryInto;
use std::hash::Hash;
use std::ops::AddAssign;
pub type DefaultCondorcetTally<T> = CondorcetTally<T, u64>;
pub struct CondorcetTally<T, C = u64>
where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast,
{
pub(crate) running_total: HashMap<(usize, usize), C>,
pub(crate) num_winners: usize,
pub(crate) candidates: HashMap<T, usize>,
check_votes: bool,
}
impl<T, C> CondorcetTally<T, C>
where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast,
{
pub fn new(num_winners: usize) -> Self {
CondorcetTally {
running_total: HashMap::new(),
num_winners: num_winners,
candidates: HashMap::new(),
check_votes: true,
}
}
pub fn with_candidates(num_winners: usize, candidates: Vec<T>) -> Self {
let mut tally = CondorcetTally {
running_total: HashMap::with_capacity(candidates.len() ^ 2),
num_winners: num_winners,
candidates: HashMap::with_capacity(candidates.len()),
check_votes: true,
};
tally.add_candidates(candidates);
tally
}
pub fn unchecked(mut self) -> Self {
self.check_votes = false;
self
}
pub fn add_candidate(&mut self, candidate: T) {
let candidate_id = self.candidates.len();
self.candidates.insert(candidate, candidate_id);
}
pub fn add_candidates(&mut self, mut candidates: Vec<T>) {
for candidate in candidates.drain(..) {
self.add_candidate(candidate)
}
}
pub fn add(&mut self, vote: &[T]) -> Result<(), TallyError> {
self.add_weighted(vote, C::one())
}
pub fn add_weighted(&mut self, vote: &[T], weight: C) -> Result<(), TallyError> {
if self.check_votes {
self.check_vote(vote)?;
}
let selection = self.unranked_mapped_candidates(&vote);
self.add_ranked_candidate_ids(selection, weight);
Ok(())
}
pub fn ranked_add(&mut self, vote: &[(T, u32)]) -> Result<(), TallyError> {
self.ranked_add_weighted(vote, C::one())
}
pub fn ranked_add_weighted(&mut self, vote: &[(T, u32)], weight: C) -> Result<(), TallyError> {
if self.check_votes {
self.check_ranked_vote(vote)?;
}
let selection = self.ranked_mapped_candidates(&vote);
self.add_ranked_candidate_ids(selection, weight);
Ok(())
}
fn add_ranked_candidate_ids(&mut self, selection: Vec<(usize, u32)>, weight: C) {
for (i, (candidate_1, rank_1)) in selection.iter().enumerate() {
let mut j = i + 1;
while let Some((candidate_2, rank_2)) = selection.get(j) {
if rank_1 < rank_2 {
*self.running_total.entry((*candidate_1, *candidate_2)).or_insert(C::zero()) += weight;
}
if rank_2 < rank_1 {
*self.running_total.entry((*candidate_2, *candidate_1)).or_insert(C::zero()) += weight;
}
j += 1;
}
}
}
pub fn totals(&self) -> Vec<((T, T), C)> {
let mut totals = Vec::<((T, T), C)>::with_capacity(self.running_total.len());
let mut candidates = HashMap::<usize, T>::with_capacity(self.candidates.len());
for (candidate, i) in self.candidates.iter() {
candidates.insert(*i, candidate.clone());
}
for ((candidate1, candidate2), count) in self.running_total.iter() {
let candidate1 = candidates.get(candidate1).unwrap().clone();
let candidate2 = candidates.get(candidate2).unwrap().clone();
totals.push(((candidate1, candidate2), *count));
}
totals
}
pub fn ranked(&self) -> Vec<RankedCandidate<T>> {
let graph = self.build_graph();
let smith_sets = tarjan_scc(&graph);
let mut candidates = HashMap::<usize, T>::with_capacity(self.candidates.len());
for (candidate, i) in self.candidates.iter() {
candidates.insert(*i, candidate.clone());
}
let mut ranked = Vec::<RankedCandidate<T>>::with_capacity(self.candidates.len());
for (rank, smith_set) in smith_sets.iter().enumerate() {
for graph_id in smith_set.iter() {
let candidate = graph.node_weight(*graph_id).unwrap();
ranked.push(RankedCandidate {
candidate: candidate.clone(),
rank,
});
}
}
ranked
}
pub fn winners(&self) -> RankedWinners<T> {
RankedWinners::from_ranked(self.ranked(), self.num_winners)
}
pub fn build_graph(&self) -> Graph<T, (C, C)> {
let mut graph = Graph::<T, (C, C)>::with_capacity(self.candidates.len(), self.candidates.len() ^ 2);
let mut graph_ids = HashMap::<usize, NodeIndex>::new();
for (candidate, candidate_id) in self.candidates.iter() {
graph_ids.insert(*candidate_id, graph.add_node(candidate.clone()));
}
let zero = C::zero();
for ((candidate_1, candidate_2), votecount_1) in self.running_total.iter() {
let votecount_2 = self.running_total.get(&(*candidate_2, *candidate_1)).unwrap_or(&zero);
if votecount_1 >= votecount_2 {
let candidate_1_id = graph_ids.get(candidate_1).unwrap();
let candidate_2_id = graph_ids.get(candidate_2).unwrap();
graph.add_edge(*candidate_2_id, *candidate_1_id, (*votecount_1, *votecount_2));
}
}
graph
}
pub fn candidates(&self) -> Vec<T> {
self.candidates.iter().map(|(k, _v)| k.clone()).collect()
}
pub fn check_vote(&self, vote: &[T]) -> Result<(), TallyError> {
for candidate in vote {
if self.candidates.get(candidate).is_none() {
return Err(TallyError::UnknownCandidate);
}
}
crate::util::check_duplicates_transitive_vote(vote)?;
Ok(())
}
pub fn check_ranked_vote(&self, vote: &[(T, u32)]) -> Result<(), TallyError> {
for (candidate, _rank) in vote {
if self.candidates.get(candidate).is_none() {
return Err(TallyError::UnknownCandidate);
}
}
crate::util::check_duplicates_ranked_vote(vote)?;
Ok(())
}
fn unranked_mapped_candidates(&mut self, selection: &[T]) -> Vec<(usize, u32)> {
let mut mapped = Vec::<(usize, u32)>::new();
for (candidate, candidate_id) in self.candidates.iter() {
let index = selection.iter().position(|ref r| *r == candidate);
let rank = match index {
Some(i) => i,
None => selection.len(),
};
mapped.push((*candidate_id, rank.try_into().unwrap()));
}
mapped
}
fn ranked_mapped_candidates(&mut self, selection: &[(T, u32)]) -> Vec<(usize, u32)> {
let mut mapped = Vec::<(usize, u32)>::new();
let mut trailing_candidates = Vec::<usize>::new();
let mut max_rank = 0;
for (candidate, candidate_id) in self.candidates.iter() {
let ranked_candidate = selection.iter().find(|ref r| &(r.0) == candidate);
match ranked_candidate {
Some(rc) => {
max_rank = std::cmp::max(max_rank, rc.1);
mapped.push((*candidate_id, rc.1));
}
None => trailing_candidates.push(*candidate_id),
};
}
for candidate_id in trailing_candidates {
mapped.push((candidate_id, max_rank + 1));
}
mapped
}
}
#[cfg(test)]
mod tests {
use super::*;
use maplit::hashset;
use std::collections::HashSet;
use std::iter::FromIterator;
#[test]
fn condorcet_basic() -> Result<(), TallyError> {
let mut tally = DefaultCondorcetTally::with_candidates(2, vec!["Alice", "Bob", "Carol"]);
tally.add(&vec!["Alice", "Bob", "Carol"])?;
tally.add(&vec!["Alice", "Bob", "Carol"])?;
tally.add(&vec!["Alice", "Bob", "Carol"])?;
let totals = tally.totals();
let totals = HashSet::from_iter(totals.iter().cloned());
assert_eq!(
totals,
hashset![(("Alice", "Bob"), 3), (("Bob", "Carol"), 3), (("Alice", "Carol"), 3)]
);
let winners = tally.winners();
assert_eq!(winners.into_vec(), vec! {("Alice", 0), ("Bob", 1)});
let mut tally = DefaultCondorcetTally::with_candidates(2, vec!["Alice", "Bob", "Carol"]);
tally.add(&vec!["Alice", "Bob", "Carol"])?;
tally.add(&vec!["Bob", "Carol", "Alice"])?;
tally.add(&vec!["Carol", "Alice", "Bob"])?;
let winners = tally.winners();
assert_eq!(winners.is_empty(), false);
assert_eq!(winners.check_overflow(), true);
assert_eq!(winners.all().len(), 3);
assert_eq!(winners.overflow().unwrap().len(), 3);
assert_eq!(winners.rank(&"Alice").unwrap(), 0);
assert_eq!(winners.rank(&"Bob").unwrap(), 0);
assert_eq!(winners.rank(&"Carol").unwrap(), 0);
Ok(())
}
#[test]
fn condorcet_wikipedia() -> Result<(), TallyError> {
let mut tally = DefaultCondorcetTally::with_candidates(4, vec!["Memphis", "Nashville", "Chattanooga", "Knoxville"]);
tally.add_weighted(&vec!["Memphis", "Nashville", "Chattanooga", "Knoxville"], 42)?;
tally.add_weighted(&vec!["Nashville", "Chattanooga", "Knoxville", "Memphis"], 26)?;
tally.add_weighted(&vec!["Chattanooga", "Knoxville", "Nashville", "Memphis"], 15)?;
tally.add_weighted(&vec!["Knoxville", "Chattanooga", "Nashville", "Memphis"], 17)?;
let candidates = tally.candidates();
let candidates = HashSet::from_iter(candidates.iter().cloned());
assert_eq!(candidates, hashset!["Memphis", "Nashville", "Chattanooga", "Knoxville"]);
let totals = tally.totals();
let totals = HashSet::from_iter(totals.iter().cloned());
assert_eq!(
totals,
hashset![
(("Memphis", "Nashville"), 42),
(("Nashville", "Memphis"), 58),
(("Memphis", "Chattanooga"), 42),
(("Chattanooga", "Memphis"), 58),
(("Memphis", "Knoxville"), 42),
(("Knoxville", "Memphis"), 58),
(("Nashville", "Chattanooga"), 68),
(("Chattanooga", "Nashville"), 32),
(("Nashville", "Knoxville"), 68),
(("Knoxville", "Nashville"), 32),
(("Chattanooga", "Knoxville"), 83),
(("Knoxville", "Chattanooga"), 17),
]
);
let winners = tally.winners();
assert_eq!(
winners.into_vec(),
vec! {("Nashville", 0), ("Chattanooga", 1), ("Knoxville", 2), ("Memphis", 3)}
);
Ok(())
}
#[test]
fn condorcet_graph() -> Result<(), TallyError> {
let mut tally = DefaultCondorcetTally::with_candidates(1, vec!["a", "b", "c", "d"]);
tally.add_weighted(&vec!["a", "c", "d", "b"], 8)?;
tally.add_weighted(&vec!["b", "a", "d", "c"], 2)?;
tally.add_weighted(&vec!["c", "d", "b", "a"], 4)?;
tally.add_weighted(&vec!["d", "b", "a", "c"], 4)?;
tally.add_weighted(&vec!["d", "c", "b", "a"], 3)?;
let graph = tally.build_graph();
assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 6);
for index in graph.node_indices() {
let candidate = *graph.node_weight(index).unwrap();
for edge in graph.edges(index).map(|e| e.weight()) {
match candidate {
"a" => assert!(*edge == (13, 8) || *edge == (11, 10) || *edge == (14, 7)),
"b" => assert!(*edge == (13, 8) || *edge == (15, 6) || *edge == (19, 2)),
"c" => assert!(*edge == (12, 9) || *edge == (15, 6) || *edge == (14, 7)),
"d" => assert!(*edge == (12, 9) || *edge == (11, 10) || *edge == (19, 2)),
_ => panic!("Invalid candidate"),
}
}
}
Ok(())
}
}