I've read Wikipedia on the Smith Set, Schwartz Set, Kosaraju's Algorithm, Tarjan's Algorithm, and the path-based strongly component algorithms; however, my experience with such algorithms is…lacking. Wikipedia also says you can use a version of Kosaraju's algorithm to generate the Schwartz set—and that these algorithms can calculate the Smith set.
Wikpedia also has some pseudo-code for Tarjan's algorithm, but not the others; and it's not specific to this relatively-sensitive application. I'm also not 100% certain which is the simplest to implement—which has the feature of least likelihood of errors in implementation.
I'd like some more-direct pseudocode to cover computing the Smith and Schwartz set from one of these algorithms, given a set of ranked ballots. I find it easier to grasp concepts when I have a practical process I can walk. I'll turn it into actual code myself.
Consider the following data structure:
Type Ballot {
Array Votes[] {
Candidate Candidate; // We do this in C#
int Rank;
}
}
For a collection of Ballots, each individual Ballot will contain an array Votes such as the following:
Ballot b.Votes[] =
[ Vote(Alex.ID, 1),
Vote(Chris.ID, 3),
Vote(Sam.ID, 2) ];
This corresponds to a voter casting Alex>Sam>Chris
, and there may be further candidates who are all equally less-preferred than Chris.
I assume the first step would be to tally the individual votes and graph the victory. For example: if 100 voters rank Alex above Sam (Alex = 1, Sam >= 2) and 50 voters rank Sam above Alex, Alex defeats Sam. Thus I guess there'd be a data structure as such:
Type GraphNode {
Candidate Candidate;
GraphNode Array Defeats[];
GraphNode Array Ties[];
GraphNode Array DefeatedBy[];
}
Whereby a GraphNode for Alex would have an element in Defeats[]
pointing to a GraphNode for Sam, and vice versa.
Given these GraphNodes, what do I do with it to identify the Smith and Schwartz sets?
Thanks in advance.