3/27 lecture sketch includes ! + change to P5 spec: if there are ties, return *all* possible winners ! + bonus material: graph theory announcements + on P5, please tell matlab to use spaces instead of tabs: + open an M file, to bring up the Matlab Editor + go to the Tools menu and choose Options... + click on the Editor Tab + click on the radio button to tell Matlab "Tab key inserts *spaces*" + think about when you want the Prelim T3 review session: Friday, April 13 Saturday, April 14 Sunday, April 15 topics + P5.3 hints (posted to P5 "outline"/write-up) + Pictorial Explanation of Condorcet voting + intro to P5.2 _______________________________________________________________________________ Notation and Terminology for Condorcet voting (mostly review) + let "pwm" stand for "pairwise matrix (sum)" + pairwise matrix: $pwm(x,y)$ = number of votes for $x$ over $y$ + can be for one voter (one ballot) + can be for an election (multiple ballots): pwm of multiple ballots = sum of pairwise matrices for each ballot + *defeat* $x/y (mag)$ + read as "$x$ defeats $y$ with magnitude $mag$" + means $mag = pwm(x,y) > pwm(y,x)$ + note: if $pwm(x,y) = pwm(y,x)$, then neither $x$ nor $y$ defeats the other + note: $pwm(x,x) = pwm(x,x)$, so no one defeats themself -- we can ignore the diagonal + Q: why is magnitude not the different $pwm(x,y) - pwm(y,x)$? + A: fairness + cyclic ambiguity resolution is done by overruling votes + fairness says we should overrrule as few votes as possible + so the question becomes: how many votes do we overrule when we drop a defeat? + the answer is $pwm(x,y)$. + ex: $pwm(A,B) = 27$, $pwm(B,A) = 23$, so $A$ defeats $B$ + if we drop the defeat $A/B$, how many votes are overruled? + the 23 votes for B over A + originally lost to the 27 votes for A over B, so they were already "overruled" by votes + plus, they are now "happy"/"happier": A no longer defeats B + the 27 votes for A over B are overruled: A no longer defeats B + thus, the number of votes overruled is 27, not 27-23 = 4. + use consecutive letters for candidates: A, B, C, D, ... ignore case: 'a' and 'A' are the same _______________________________________________________________________________ Pictorial Explanation of Condorcet voting + draw defeat $x/y (mag)$ as an arrow from $x$ to $y$ labeled with $mag$: mag arrow is sideways here, but try to draw arrows pointing down, x -----> y so "bigger" candidates are drawn higher + ignoring magnitude, think of this as $x >= y$ -- note: $>=$, not $>$ + note: usually draw picture only for the pairwise matrix for entire election, usually *not* for just a single ballot: ! + picture has at most one arrow between $x$ and $y$, in a single direction ! + if "pasted pictures for each ballot together", generally would have many ! arrows between $x$ and $y$ in both directions + a candidate is *unbeaten* = no incoming arrows: "biggest" candidate is at the top, with no arrows pointing at it + a cycle means candidates are equal: $C >= D >= A >= C$ means $C = D = A$ + we can think of putting equal candidates into a big "group": + every candidate in a cycle belongs to the same group + every candidate in overlapping cycles belongs to the same group + innermost unbeaten set = a topmost group + "have a winner?" = "have unbeaten candidate?" = "topmost group contains only 1 element?" + plain condorcet algorithm: drop smallest arrows until have a winner + SSD idea: winner should be an element of the topmost group, so why bother looking at arrows below the topmost group? + SSD algorithm: drop smallest arrows in topmost group until have a winner + note: implementing SSD is bonus, but you are required to understand SSD well enough to do it by hand _______________________________________________________________________________ !Bonus material: Graph Theory (ideas related to Pictorial Explanation) ! !+ in computer science, (a picture of) a set objects with arrows between them ! is (a picture of) a *graph* !+ aside: when i first learned this term, i hated it, ! because to me a graph was the plot of a function, ! not a bunch of objects and arrows !+ the objects are called *vertices* (or *nodes*) !+ the arrows are called *edges* (or *arcs*) !+ if the arrows have labels, the labels are usually called *weights* !+ ex: vertices = cities, edges = roads, weights = distances !+ a *path* is what you expect: a sequence of edges from one vertex (the ! *source* or *start*) to another vertex (the *sink* or *end*) !+ a *cycle* is a path where the start and end vertex are the same !+ a *strongly-connected component* is a set of vertices all of which have ! paths to each other. this exactly matches our "group" concept above. _______________________________________________________________________________ annoying details + what if $pwm(x,y) = pwm(y,x)$? neither $x$ nor $y$ defeats the other: no arrow from one to the other + in the picture, should we draw arrows for both $pwm(x,y)$ and $pwm(y,x)$? NO: draw arrows for *only* defeats + what if there are multiple winners (multiple unbeaten candidates)? + could say program is free to choose any one + P5.2: report *all* winners !+ when dropping arrows, what if there are many with the same, minimum label? ! + could say program is free to choose any one ! + could say program must try each choice and report all the answers: ! if always get the same answer, then choice didn't matter anyway ! + P5.2: drop all (relevant) arrows with the minimum label + what if there are multiple topmost groups? + could say program is free to choose any one + P5.2 bonus: report answers for *all* of them _______________________________________________________________________________ intro to P5.2 candidates + input to main function: consecutive letters starting at 'A', ignoring case + internally, e.g. for use as array indices: consecutive integers 1, 2, ... + conversion: ch 'A' 'B' 'C' 'D' 'E' ... i 1 2 3 4 5 ... + we've already seen: $i = ch - 'A' + 1'$ (might need to use $upper$) + solve for $ch$, tell matlab want character: $ch = char(i + 'A' - 1)$ + *canonical* order of candidates is alphabetical/numerical: 'A', 'B', 'C', ... 1, 2, 3, ... format of one ballot: + string of comparisons, $>$ or $=$ + all candidates appear + $tally$ computes pairwise matrix + ex: $'E>D=B>A=C'$ note: we usually do *not* draw a picture for just one ballot, but in case you find it useful, the picture for the pairwise matrix is shown. picture: pairwise matrix: (all arrows point down) A B C D E ___E__ A 0 0 0 0 0 no votes for A over anyone / /\ \ / / \ \ B 1 0 1 0 0 votes for B over A, C | D B | | |\ /| | C 0 0 0 0 0 no votes for C over anyone | | \/ | | \ | /\ | / D 1 0 1 0 0 votes for D over A, C \|/ \|/ A C E 1 1 1 1 0 votes for E over everyone else + election: 3 possibilities + string matrix + list of ballots: each row is one ballot + since all candidates appear in each ballot, each row is the same length, so matlab is happy + need to compute pairwise matrix for election: + compute pairwise matrix for each ballot + sum the pairwise matrices for each ballot + pairwise matrix + (later: name of a file containing string matrix or pairwise matrix) _______________________________________________________________________________ $find$ $[i,j,v] = find(x)$ = extract positions and values of non-zero elements in matrix $x$ example (run it! make changes!): x = [0 0 -7 0 ; 0 1 9 0 ; 2 0 6 0], [i,j,v] = find(x) picture: x 1 2 3 4 i j v +-+-+--+-+ +-+ +-+ +--+ 1 |0|0|-7|0| |3| |1| | 2| 1 x(i(1),j(1)) = x(3,1) = 2 = v(1) +-+-+--+-+ +-+ +-+ +--+ 2 |0|1| 9|0| |2| |2| | 1| 2 x(i(2),j(2)) = x(2,2) = 1 = v(2) +-+-+--+-+ +-+ +-+ +--+ 3 |2|0| 6|0| |1| |3| |-7| 3 x(i(3),j(3)) = x(1,3) = -7 = v(3) +-+-+--+-+ +-+ +-+ +--+ |2| |3| | 9| 4 x(i(4),j(4)) = x(2,3) = 9 = v(4) +-+ +-+ +--+ |3| |3| | 6| 5 x(i(5),j(5)) = x(3,3) = 6 = v(5) +-+ +-+ +--+