Code Monkey home page Code Monkey logo

tideman_algorithm's Introduction

Optimise Tideman Algorithm Using Bitwise Operations

This repo documents the solution applying bitwise operations into the application of the adjacency matrix in the Tideman voting algorithm, which achieves O(n) and Ω(1) for finding the winners. Importantly, it also condenses the processes of locking pairs and finding winners into a concise 10 lines of code while optimising memory usage to only 4/n of the adjacency matrix.

1.1 Definition of Tideman Algorithm

The Tideman (aka Ranked Pairs) algorithm is an electoral method where voters can vote for more than one candidate in order of preference. A "Condorcet winner," the person who would have won any head-to-head matchup against another candidate, will be elected from a sorted list of winners in such a system.

1.2 Procedure of Tideman Algorithm

The Tideman voting method operates as follows:

  • Tally: Document the winner of each pair of candidates and the margin.
  • Sort: Rank each pair by the strength of victory (margin) from largest first to smallest last.
  • Lock: Starting with the strongest pair, go through the sorted pairs and "lock" the pairs that do not create a cycle with the existing locked-in pairs.
  • Find: The winner in such a voting system is defined as a person who never appears as a loser in each head-to-head matchup.

1.3 Main Function Walkthrough

  • vote: This function collects ballots from voters and ensures their validity by verifying if the names appear in the candidate list.
  • record_preferences: This function generates pairs of winners and losers and counts the occurrences of each pair.
  • add_pairs: This function adds all pairs with more votes (which means one candidate is preferred over the other among all voters).
  • sort_pairs: This function sorts the pairs of candidates in decreasing order based on the strength of victory.
  • lock_pairs: This function creates the locked graph by adding all pairs in decreasing order of victory strength while ensuring that each edge does not create a cycle. In this part, three methods, namely adjacency list, adjacency matrix, and bitwise operations are discussed for implementing the lock_pairs function.
  • print_winner: This function will print out the names of the candidates who are the winners under the Tideman algorithm. The winners should be the “source” of the graph, meaning that no arrows point to them.

Notably, it only takes 10 lines of code in total using bitwise operations to lock pairs and print the winners with O(n) and Ω(1).

bitwiseoperation

Note:

  • As all alternative methods are included in the code, to try the code, please comment out the duplicated part, leaving only a.1/a.2/a.3 + b or c part for locking pairs and finding winners.

  • For more context about Tideman, please check the CS50 problem set page. The article "Optimize Tideman Algorithm Using Bitwise Operations" on my website has demonstrated detailed explanations of this solution with explicit illustrations.

tideman_algorithm's People

Contributors

yukisschu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.