- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
An application on Bertrandís ballot theorem in C/C++
In Bertrand's original paper, he explains a proof depended on a general formula for the number of favourable sequences implementing a recursion relation.
Example
Let there are 5 voters, of whom 3 vote for candidate A and 2 vote for candidate B (so p = 3 and q = 2). Ten possibilities are exist for the order of the votes cast −
AAABB
AABAB
ABAAB
BAAAB
AABBA
ABABA
BAABA
ABBAA
BABAA
BBAAA
For the order AABAB, the tally of the votes as the election progresses is given below −
Candidate | A | A | B | A | B |
---|---|---|---|---|---|
A | 1 | 2 | 2 | 3 | 3 |
B | 0 | 0 | 1 | 1 | 2 |
For each column the tally for A is always greater than the tally for B so the A is always strictly ahead of B. For the order AABBA the tally of the votes as the election progresses is given below −
Candidate | A | A | B | B | A |
---|---|---|---|---|---|
A | 1 | 2 | 2 | 2 | 3 |
B | 0 | 0 | 1 | 2 | 2 |
With respect of this order, B is tied with A after the fourth vote, so A is not always strictly ahead of B. Of the 10 possible orders, A is always ahead of B only in case of AAABB and AABAB. So the probability that A will always be strictly ahead is 2/10=1/5 and this is indeed equal to 3-2 / 3+2 as the theorem predicts.