User Tools

Site Tools


public:how_parkrun_volunteers_sort_barcodes_-_a_computer_scientist_s_perspective

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
public:how_parkrun_volunteers_sort_barcodes_-_a_computer_scientist_s_perspective [2018/12/28 01:47] fangfufupublic:how_parkrun_volunteers_sort_barcodes_-_a_computer_scientist_s_perspective [2018/12/28 11:23] (current) fangfufu
Line 1: Line 1:
 ====== How Parkrun volunteers sort barcodes - a computer scientist's perspective ====== ====== How Parkrun volunteers sort barcodes - a computer scientist's perspective ======
-On the Christmas day of 2018, I volunteered at Norwich Parkrun. Towards the end, I ended up helping out with sorting the plastic barcodes. I find the whole process interesting. This is because sorting algorithm is an essential part of computer science curriculum [(https://en.wikipedia.org/wiki/Sorting_algorithm#History)]. I do have a special connection to sorting algorithm - I bumped into Sir Charles Antony Richard Hoare FRS FREng [(https://en.wikipedia.org/wiki/Tony_Hoare)][(https://www.fangfufu.co.uk/wiki/doku.php?id=public:bumping_into_sir_tony_hoare_on_the_day_before_i_learnt_quicksort)], the inventor of quicksort algorithm [(https://en.wikipedia.org/wiki/Quicksort)], the day before I was formally taught quicksort for the second time ((I was taught it once in A-Level Maths D1 module when I was a Year 12 student in Ashmole School, London.)as a computer science undergrad in University of York.+On the Christmas day of 2018, I volunteered at Norwich Parkrun. Towards the end, I ended up helping out with sorting the plastic barcodes. I find the whole process interesting. This is because sorting algorithm is an essential part of computer science curriculum [(https://en.wikipedia.org/wiki/Sorting_algorithm#History)]. I do have a special connection to sorting algorithm - I bumped into Sir Charles Antony Richard Hoare FRS FREng [(https://en.wikipedia.org/wiki/Tony_Hoare)][(https://www.fangfufu.co.uk/wiki/doku.php?id=public:bumping_into_sir_tony_hoare_on_the_day_before_i_learnt_quicksort)], the inventor of quicksort algorithm [(https://en.wikipedia.org/wiki/Quicksort)], the day before I was formally taught quicksort for the second time [(I was taught it once in A-Level Maths D1 module when I was a Year 12 student in Ashmole School, London.)as a first year computer science undergrad in University of York.
  
 Sorting numbers inside a computer is a bit different to sorting objects in physical world. This is mainly because the uniform cost model does not apply in the physical world [(costmodel > https://en.wikipedia.org/wiki/Analysis_of_algorithms#Cost_models)]. However some of the concepts in algorithm analysis can still be applied to physical world, for example time complexity and space complexity [(https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecture2.html)].  Sorting numbers inside a computer is a bit different to sorting objects in physical world. This is mainly because the uniform cost model does not apply in the physical world [(costmodel > https://en.wikipedia.org/wiki/Analysis_of_algorithms#Cost_models)]. However some of the concepts in algorithm analysis can still be applied to physical world, for example time complexity and space complexity [(https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecture2.html)]. 
  
-In this blog post, we analyse the algorithm which Parkrun volunteers use to sort barcodes, using some concepts from computer science.+In this blog post, we analyse the algorithm which Parkrun volunteers use to sort barcodes, using some concepts from computer science. This blog post is written in such a way so you can follow it, even if you have not formally studied computer science. 
 + 
 +In short, Parkrun uses bucket sort to sort their barcodes after each event. I do not think I have encountered an implementation of bucket sort on computers, yet I was treated with a real life implementation of bucket sort on Christmas day.
  
 ===== Analysis of algorithms  ===== ===== Analysis of algorithms  =====
Line 26: Line 28:
  
 ===== Bucket sort ===== ===== Bucket sort =====
-Having got the basics of algorithmic analysis out of the way, we can finally look at how Parkrun volunteers sort the bar codes. Parkrun volunteers use a physical implementation of bucket sort [(https://en.wikipedia.org/wiki/Bucket_sort)][(https://www.oreilly.com/library/view/algorithms-in-a/9780596516246/ch04s08.html)]. This is illustrated by the following two pictures taken at Parkrun.+Having got the basics of algorithmic analysis out of the way, we can finally look at how Parkrun volunteers sort the barcodes. Parkrun volunteers use a physical implementation of bucket sort [(bucketsort > https://en.wikipedia.org/wiki/Bucket_sort)][(https://www.oreilly.com/library/view/algorithms-in-a/9780596516246/ch04s08.html)]. This is illustrated by the following two pictures taken at Parkrun.
  
 <columns 100% - - > <columns 100% - - >
Line 48: Line 50:
 where $k$ is the number of buckets. where $k$ is the number of buckets.
  
-In Parkrun's particular implementation, the total number of buckets is not defined. Instead, each bucket is set to hold 25 barcodes. The total number of buckets depends on the amount of plastic barcodes. +In Parkrun's particular implementation, the total number of buckets is not defined. Instead, each bucket is set to hold 25 barcodes. The total number of buckets depends on the amount of plastic barcodes. It is easier for human to sort individual bucket by limiting their size.
  
 The advantage of Parkrun's two-stage sorting approach is that it converts the problem of sorting barcodes into an embarrassingly parallel problem [(https://en.wikipedia.org/wiki/Embarrassingly_parallel)], which is a type of workload which requires little or no effort to separate into parallel subtasks. Multithreading naturally occurs during the barcode sorting process.  The advantage of Parkrun's two-stage sorting approach is that it converts the problem of sorting barcodes into an embarrassingly parallel problem [(https://en.wikipedia.org/wiki/Embarrassingly_parallel)], which is a type of workload which requires little or no effort to separate into parallel subtasks. Multithreading naturally occurs during the barcode sorting process. 
  
 +==== Distributing barcodes into their respective buckets ====
 The process of distributing barcodes to their respective buckets (stage 1) is a completely stateless process. Volunteers can join and exit the sorting process any time they way. The volunteer pool operates much like a thread pool [(https://en.wikipedia.org/wiki/Thread_pool)]. There is no hand-over process, each worker thread (volunteer) does not need to obtain any state information before joining the task (sorting), and it does not need to pass any state information to another worker thread when exiting. Stage 1 is completely thread safe [(https://en.wikipedia.org/wiki/Thread_safety)]. There is no information transfer between each worker thread. Stage 1 is also completely atomic [(https://en.wikipedia.org/wiki/Linearizability#History_of_linearizability)], as the barcode can either be inside or outside its corresponding bucket.  The process of distributing barcodes to their respective buckets (stage 1) is a completely stateless process. Volunteers can join and exit the sorting process any time they way. The volunteer pool operates much like a thread pool [(https://en.wikipedia.org/wiki/Thread_pool)]. There is no hand-over process, each worker thread (volunteer) does not need to obtain any state information before joining the task (sorting), and it does not need to pass any state information to another worker thread when exiting. Stage 1 is completely thread safe [(https://en.wikipedia.org/wiki/Thread_safety)]. There is no information transfer between each worker thread. Stage 1 is also completely atomic [(https://en.wikipedia.org/wiki/Linearizability#History_of_linearizability)], as the barcode can either be inside or outside its corresponding bucket. 
  
 In fact, I took advantage of the stateless and atomic nature of the stage 1 workload when I was helping out. My hands were quite cold, I was losing dexterity, so I exited the task. I then went and got a cup of hot chocolate to warm up my hands. I rejoined the task after my hands were warm.  In fact, I took advantage of the stateless and atomic nature of the stage 1 workload when I was helping out. My hands were quite cold, I was losing dexterity, so I exited the task. I then went and got a cup of hot chocolate to warm up my hands. I rejoined the task after my hands were warm. 
 +
 +The first stage establishes a partial ordering [(https://en.wikipedia.org/wiki/Partially_ordered_set)] of the original data structure (all of the barcodes) - there is ordering amongst the buckets, but there is no ordering within each bucket. To establish total ordering [(https://en.wikipedia.org/wiki/Total_order)], each bucket need to be sorted using comparison sort.  
 +
 +==== Sorting individual buckets using insertion sort ====
 +Different worker threads (volunteers) tend to use different sorting algorithm. Personally I use insertion sort, which is a comparison sort. 
 +
 +Insertion sort is a simple - this is how it works:
 +  - Get a list of unsorted items. 
 +  - Divide the unsorted items into two partitions - sorted and unsorted. Set a marker for the sorted section after the first item in the list. The first item is now marked sorted. The rest of the items are marked as unsorted.
 +  - Repeat step 4 to 6 until the unsorted partition is empty. 
 +  - Select the first unsorted item. 
 +  - Swap this item to the sorted partition, until it arrives at the correct sorted position. 
 +  - Advance the marker, so the sorted partition increase its size by one, and the unsorted partition decrease its size by one. 
 +
 +On computers, insertion sort has the following property: 
 +  * best case time complexity $O(n)$
 +  * average time complexity $O(n^2)$,
 +  * worst case time complexity: $O(n^2)$,
 +
 +  * worst case space complexity: $O(n)$.
 +
 +More information about insertion sort can be found at [(https://en.wikipedia.org/wiki/Insertion_sort)] and [(https://courses.cs.vt.edu/csonline/Algorithms/Lessons/InsertionSort/index.html)], they explain this topic far better than me. 
 +
 +There are also video animation of insertion sort on the Internet:
 +<columns 100% - - >
 +<WRAP centeralign>
 +//You might want to turn off the audio for this video. //
 +
 +{{youtube>8oJS1BMKE64}}
 +</WRAP>
 +<newcolumn>
 +<WRAP centeralign>
 +//Leave the audio on for some Romanian folk dance music. //
 +
 +{{youtube>ROalU379l3U}}
 +</WRAP>
 +</columns>
 +
 +As a human, I like insertion sort, because it allows me to know how far I have done. The only state information I need to track is the position of the partition marker. This can be done easily by holding the unsorted barcodes in my hand, and place the sorted barcodes on the floor. 
 +
 +However there are some physical optimisation which I made. I collapsed consecutive sorted barcodes into clusters. This is because physically inserting a barcode involves moving all the adjacent barcodes, which takes quite a bit of effort on a rough surface. The two figures below illustrate what I meant: 
 +
 +<columns 100% 50% 50% >
 +<WRAP centeralign>
 +//Uncollapsed barcodes, they became a bit unwieldy to shuffle around. //
 +
 +{{:public:parkrun:20181225_102455.jpg?400}}
 +</WRAP>
 +
 +<newcolumn>
 +<WRAP centeralign>
 +//Partially collapsed clusters of consecutive barcodes, this made it much easier to perform insertion. //
 +
 +{{:public:parkrun:20181225_103446.jpg?400}}
 +</WRAP>
 +</columns>
 +
 +Preallocation of enough empty space for the missing barcodes should help with the problem as well - that way you don't need to shuffle those out-of-place barcodes.  
 +
 +===== Conclusion =====
 +I quite enjoyed participating barcode sorting. It is quite of interesting to observe a physical implementation of bucket sort. A lot of computer algorithms are inspired by real life processes. I think perhaps the bucket sort algorithm you learn from textbooks is the abstraction of real life bucket sort process. After all, Wikipedia does not say who came up with bucket sort [(bucketsort)]. It really does make me think - a lot of assumptions in the physical world do not apply in computers, a lot of assumptions that work in computers do not apply in real life. If real life processes can inspire computer algorithms, then surely computer algorithms can inspire real life processes.  
 +
 +===== Further readings =====
 +There are other topics which link computer science into the way the physical world works, for example operational research [(https://en.wikipedia.org/wiki/Operations_research)]. There was a time which all computation were performed by physical objects, to explore this topic, have a look at analogue computers [(https://en.wikipedia.org/wiki/Analog_computer)]. Finally, there have been works on explaining the limitation of computation using physical phenomenons, to explore this topic, have a look at [(Limits on Efficient Computation in the Physical World - https://www.scottaaronson.com/thesis.pdf)] and [(NP-complete Problems and Physical Reality - https://www.scottaaronson.com/papers/npcomplete.pdf)]
 +
 +
 +
 +
 +
public/how_parkrun_volunteers_sort_barcodes_-_a_computer_scientist_s_perspective.1545961667.txt.gz · Last modified: 2018/12/28 01:47 by fangfufu