(Weighted) Majority Graphs ======================================= A **majority graph** is a directed asymmetric graph in which the nodes are the candidates and an edge from $a$ to $b$ means that $a$ is majority preferred to $b$. ```{eval-rst} A :class:`~pref_voting.weighted_majority_graphs.MajorityGraph` has a number of methods used by voting methods. .. exec_code:: from pref_voting.weighted_majority_graphs import MajorityGraph mg = MajorityGraph([0, 1, 2], [(0, 1), (1, 2), (2, 0)]) print(mg.edges) for c1 in mg.candidates: for c2 in mg.candidates: print(f"{c1} is majority preferred to {c2}: {mg.majority_prefers(c1, c2)}") print(f"{c1} is tied with {c2}: {mg.is_tied(c1, c2)}") for c in mg.candidates: print(f"The dominators of {c} are {mg.dominators(c)}") print(f"The candidates that {c} dominates are {mg.dominates(c)}") print(f"Copeland scores: {mg.copeland_scores()}") print(f"Condorcet winner: {mg.condorcet_winner()}") print(f"Weak Condorcet winners: {mg.weak_condorcet_winner()}") print(f"Condorcet loser: {mg.condorcet_loser()}") print() mg2 = MajorityGraph([0, 1, 2], [(0, 1), (1, 2)]) print(mg2.edges) for c1 in mg2.candidates: for c2 in mg2.candidates: print(f"{c1} is majority preferred to {c2}: {mg2.majority_prefers(c1, c2)}") print(f"{c1} is tied with {c2}: {mg2.is_tied(c1, c2)}") for c in mg2.candidates: print(f"The dominators of {c} are {mg2.dominators(c)}") print(f"The candidates that {c} dominates are {mg2.dominates(c)}") print(f"Copeland scores: {mg2.copeland_scores()}") print(f"Condorcet winner: {mg2.condorcet_winner()}") print(f"Weak Condorcet winners: {mg2.weak_condorcet_winner()}") print(f"Condorcet loser: {mg2.condorcet_loser()}") ``` A **margin graph** is a weighted directed asymmetric graph in which the nodes are the candidates, an edge from $a$ to $b$ means that $a$ is majority preferred to $b$, and the weight of the edge is the margin of $a$ over $b$. ```{eval-rst} A :class:`~pref_voting.weighted_majority_graphs.MarginGraph` has a number of methods used by voting methods. .. important:: The weights of a MarginGraph can be any numbers. However, if the weights are generated by a profile of linear orders, then the weights will have the same parity (which is even if there is any zero margin between distinct candidates). .. exec_code:: from pref_voting.weighted_majority_graphs import MarginGraph mg = MarginGraph([0, 1, 2], [(0, 1, 1), (1, 2, 3), (2, 0, 3)]) print(mg.edges) for c1 in mg.candidates: for c2 in mg.candidates: print(f"the margin of {c1} over {c2} is {mg.margin(c1, c2)}") print(f"{c1} is majority preferred to {c2}: {mg.majority_prefers(c1, c2)}") print(f"{c1} is tied with {c2}: {mg.is_tied(c1, c2)}") for c in mg.candidates: print(f"The dominators of {c} are {mg.dominators(c)}") print(f"The candidates that {c} dominates are {mg.dominates(c)}") print(f"Copeland scores: {mg.copeland_scores()}") print(f"Condorcet winner: {mg.condorcet_winner()}") print(f"Weak Condorcet winners: {mg.weak_condorcet_winner()}") print(f"Condorcet loser: {mg.condorcet_loser()}") ``` ```{eval-rst} Both :class:~pref_voting.weighted_majority_graphs.MarginGraph` and :class:~pref_voting.weighted_majority_graphs.MajorityGraph` can be generated from a profile. .. exec_code:: from pref_voting.profiles import Profile from pref_voting.profiles_with_ties import ProfileWithTies prof = Profile([[0, 1, 2], [2, 0, 1], [1, 0, 2]], rcounts=[2, 1, 2]) prof.display() majg = prof.majority_graph() print(f"The majority graph edges are {majg.edges}") mg = prof.margin_graph() print(f"The margin graph edges are {mg.edges}") prof2 = ProfileWithTies([{0: 1, 1: 2, 2: 3}, {1:1, 2:1, 0:2}, {2:1, 0:2}], [2, 3, 1]) prof2.display() majg = prof2.majority_graph() print(f"The majority graph edges are {majg.edges}") mg = prof2.margin_graph() print(f"The margin graph edges are {mg.edges}") ``` ## MajorityGraph Class ```{eval-rst} .. autoclass:: pref_voting.weighted_majority_graphs.MajorityGraph :members: ``` ## MarginGraph Class ```{eval-rst} .. autoclass:: pref_voting.weighted_majority_graphs.MarginGraph :members: :show-inheritance: ``` ## SupportGraph Class ```{eval-rst} .. autoclass:: pref_voting.weighted_majority_graphs.SupportGraph :members: :show-inheritance: ```