Solution to the Birthday Party Problem Pt 1
You should have noticed that both Part 1 and Part 2 are asking you to solve the same problem. The problem of finding the greatest common factor two different numbers. For the first case this is a general value for 48 and 42, that being 6. For the second one they want you to actually write the given algorithm for an unknown input of integers
This is known as the Euclidean Algorithm, and here is a flowchart that goes over the Euclidean Algorithm:
Here is that given flowchart for finding the greatest common factor between two different elements. First you check if the two elements are equal to each other. At this point if they are, return one of them, if they are not however, subtract the smaller from the larger, and set that larger to the difference of the two. The comparison is then done again until you get a value that is equal to the greatest common factor of the two integers.
Here is the math for the given output you get for the first portion of that birthday problem:
Pseudocode
Pseudocode is really good at actually talking about an algorithm in the more general of terms. It applies to any programming language and is used as a means of describing how the code works and how it is going to flow while still keeping it more accurate to how an actual implementation would work rather than a flow chart.
It basically is just that fluff and filler that is good for communication of how an algorithm would work. It is not a real programming language but is just used to describe steps.
Discretisation is the process of taking something that would be continuous, and making it function in a discrete manner. This is because a computer needs to think in discrete steps and is not able to exactly perform continuous actions, like might say be demanded by a flow chart.
If you asked for a temperature increase than a person might have the ability to change the temperature in a continuous manner, but a computer might need to make this process more discrete and iterate by adding 0.5 degrees to the temperature until the suitable temperature is reached.
When writing a computer program, all parts of the algorithm will be composed of discrete steps that are eventually figured out in a distinct and direct order until you get the correct output.
Pseudocode Assignment:
x <- 2
x <- TRUE
//only rule is no spaces between variables
DesiredTemp <- 3 //is okay
Desired Temp <- 3 //is not okay
This is the assignment for a given variable inside of pseudocode, it means that x is given the value of 2 and then is given the value of TRUE, a regular programming language might use something like a = to do this, but R also does it this way. Since this is read by people and not computers then you can not worry about things like typing
Pseudocode is read like English, going from left to right up to down and all of that good stuff, this means you can basically read it just like you would a book or a regular programming language, and it works in the same way where x = x + 1 in pseudocode it would be x <- x + 1, nothing changes
Standard operators like addition, subtraction, comparison and all of that still works the same.
There also exists logical operations, which are basically the same as you would find them in logic theory, and they are used inside of pseudocode as well
Pseudocode and functions
Functions are also used inside of pseudocode, they work the same way they do in general programming languages, or even inside of standard mathematics. They would take in a given set of input and then return a given set of output.
These are called in pseudocode like:
function EVEN(n)
if n mod 2 = 0
return TRUE
else
return FALSE
end if
end function
Basically pretty chill stuff, it looks the exact same as it would in comparison to a real programming language, you just get rid of the brackets and you end up with these end x statement. It basically reads like python, which to me means it sucks balls, but still
Introduction to loops in pseudocode
So basically, you need to have a way to describe repeating things in pseudocode so that way you do not have to rewrite the same operation a million times
For Loop
This is done using basically just a for loop inside of a given programming language, and is based on different parameters each time that you conclude the loop (hopefully, otherwise it is infinite). Below is an example of how to write a for loop in pseudo code:
Now that I am looking at it python is really trying to be as much as pseudocode as it can be. I mean generally this does make it pretty hard to understand what I starts with, like for example iterating in reverse or something like that it looks pretty silly. I suppose you can reverse the order of the equality but still. I prefer things like i++ much better.
While Loop
There is also the while loop where you still have to really write it like you would inside of a programming language, this is just because a for loop is a while loop really, so you need to just do the work that is abstracted for you in the for loop into a while loop because you do not know the exact number of iterations that will need to be done.
Example:
Euclidean Algorithm as Written in pseudocode
Book Reading:
Please read Section 2.1 of:
Cormen, T.H., C.E. Leierson, R.L. Rivest and C. Stein Introduction to algorithms.(Cambridge, MA: MIT Press, 2009) 3rd edition.
Note how the pseudocode here is different from the pseudocode used in this module. As long as pseudocode can be understood by a human, everything works. Make sure you understand the similarities and differences.
Remember that all the Essential reading for this programme is provided for you. Click the link above which will take you to the Online Library.
Reading: Insertion Sort & Pseudocode
The topic of this section of reading is that of insertion sort which is basically a method of sorting an input array (which is a list of data values) in ascending order based on their numeric value. With the sorted list as the given output.
The chapter also goes over the fact that all of this code for this algorithm is written in something called “pseudocode” which is basically a way of writing steps in an algorithm that is similar to real code, but is not actually any exact programming language. Just the concepts that would be applicable to mostly any and all programming languages. It is generally not worried about things like data abstraction or error handling, and sticks more closely to the idea of using English in order to best convey the ideas of an algorithm.
Insertion Sort
Insertion sort is a really good algorithm for sorting a small number of elements. It works in the same way that a person would natural sort something like a hand of playing cards.
- You have a jumbled deck of cards in your right hand, and then nothing in your left. You pick out a given card from your right hand, and then scan the cards, if any, in your left from right to left to see the position where the card to the right of your currently held card is less than the card to the right of it, and then less than the card to the left of it.
This is very similar to how insertion sort works, you basically start at the index of 1 in the array, so the second element. And then you compare the element in front of it and behind it. If the element is behind it is larger than it is, then you move the value back. You do this until the value in behind the element is smaller than the element.
You basically keep on going back until you find the right place for you to “insert” the element. In the worse case of a reverse sorted array, this is an o(n^2) solution. However, it is really good for smaller arrays that are already partially sorted.
This is basically the algorithm, written in pseudocode as a matter of fact. Here you can basically see, that for every single element starting from the second element in the array, You check to see if the element before it is larger than it is. While this is the case, you continuously move the current element backwards in the array until this statement is no longer true.
We understand that the ideas of A[1...j-1] constitutes the sorted hand, and the remaining sub array of A[j+1...n] is still the unsorted portion of the array. Basically what this is saying is that this algorithm sorts the array in place, and everything from the start of the array to the index of j-1 is sorted, while the rest is not while the array is still working of course. This is formally known as a loop invariant:
- A the start of each iteration of the for loop of lines 1-8, the sub-array
A[1...j-1]consists of the elements originally inA[1...j-1]but in sorted order. - What this is saying is that basically in a given iteration of the for loop, the new index that j reaches consists of the same elements that were in that sub array to begin with, just that they are now in sorted order instead of being unsorted. Even though it only sorts 1 element at a time really
A loop invariant is used to help us understand why an algorithm is correct. There are three things to be shown in a loop variant:
- Initialization it is true prior to the first iteration of the loop
- Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration
- Termination: when the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct.
This is basically a really complicated way of saying that you can basically describe an event that is in fact causing the array to get sorted. Given the above invariant, it is true, both before the array is sorted, while it is being sorted, and after it is sorted to. This is because the value of J is changing.













