Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | doris-williamson |
View: | 214 times |
Download: | 0 times |
Monday’s child is fair of faceTuesday’s child is full of grace
Wednesday’s child is full of woeThursday’s child has far to go
Friday’s child is loving and givingSaturday’s child works hard for its living
And a child that is born on the Sabbath dayIs fair and wise and good and gay
If you don’t know what day of the week you were born on you can work it out using Zeller’s algorithm
Zeller’s algorithmLet day number = DMonth number = MYear number = Y
If M is 1 or 2 add 12 to M and subtract 1 from Y
Let C be the first two digits of YAnd Z be the last two digits of Y
Add together the integer parts of (2.6M – 5.39), (Z÷4) and (C÷4), thenadd on D and Z and subtract 2C
Find the remainder when this value is divided by 7.
If the remainder is 0 the day was SundayIf the remainder is 1 the day was Monday etc.
Eg 15th May 1991D =15 M = 5 Y = 1991
C = 19Z = 91
7 + 22 + 4 + 15 + 91 – 38 = 101
3
So Wednesday
Russian peasant’s algorithm for long multiplication
Step 1: Write down the two numbers to be multiplied
Step 2: Beneath the left number write down double that number. Beneath the right number write down half of that number ignoring any remainder.
Step 3: Repeat step 2 until the right number is 1
Step 4: Delete those rows where the number in the right column is even. Add up the remaining numbers in the left column. This is the answer you need.
24 16348 8196 40192 20384 10768 51536 23072 13912
Euclid’s algorithm for finding the HCF of two integers
Step 1: Pick two numbers
Step 2: Find the difference between the two numbers to get a third number.
Step 3: Keep the two smallest numbers of the threeand cross out the third number
Step 4: If the two numbers you are left with are the same then this number is the HCF of the original two numbers. If not then go back to Step 2.
Ways to communicate algorithms:
1) As a series of instructions expressed in words
2) As a Flow Chart
3) As a computer program – using ‘Pseudo Code’
Euclid’s algorithm for finding the HCF of two integers in words
Step 1: Pick two numbers
Step 2: Find the difference between the two numbers to get a third number.
Step 3: Keep the two smallest numbers of the three and cross out the third number
Step 4: If the two remaining numbers are equal then this number is the HCF of the original two numbers. If not go back to Step 2.
Euclid’s algorithm for finding the HCF of two integers in a flow chart
Read two numbers x,y
x>y?Subtract y from x to get a new
value for x
x<y?
x=y?
Subtract x fromy to get a new
value for y
Output x
Euclid’s algorithm for finding the HCF of two integers in ‘Pseudo Code’
Input xInput yRepeatIf x>y then x = x-yIf y>x then y = y-xUntil x=yPrint x
yes
yes
no
no
yes
no
Algorithmic Complexity
There are many algorithms that can be used to solve some problems.
The effectiveness of an algorithm can be judged in two ways: Its success rate and its efficiency
Ways to judge success
Some algorithms always find the best solution (these are the best kind of algorithms)
Some algorithms find a solution, but not always the best (these are called Heuristic algorithms)
Ways to judge efficiency
The efficiency of an algorithm is a measure of its speed.
This usually depends on the number of calculations required to complete the task
A more efficient algorithm is one that completes the task using fewer calculations
Measuring the efficiency of an algorithm
Consider calculating the value of a polynomial for some value of x
For a linear:
You will be required to do a*x + b 1 multiplications and 1 addition
For a quadratic:
You will be required to do a*x*x + b*x + c 3 multiplications and 2 additions
For a cubic:
You will be required to do a*x*x*x + b*x*x + c*x + d 6 multiplications and 3 additions
For a quartic:
You will be required to do a*x*x*x*x + b*x*x*x + c*x*x + d*x + e
10 multiplications and 4 additions
In general you need to do 1/2n(n+1) mults and n additions. In total: 1/2n2 + 3/2n calculations.
We say the problem has quadratic complexity. The time to solve it is proportional to the size2.
If you double the ‘size’ of the problem it will take four times longer to solve.
If you triple the ‘size’ it will take nine times longer. etc
Measuring the efficiency of an algorithm
If you rewrite each polynomial in ‘nested’ form like this to begin with: (((ax + b)x + c)x + d)x + e
Then for a linear a*x + b 1 multiplication and 1 addition
For a quadratic: (a*x + b)*x + c 2 multiplications and 2 additions
For a cubic: ((a*x + b)*x + c)*x + d 3 multiplications and 3 additions
For a quartic: (((a*x + b)*x + c)*x + d)*x + e 4 multiplications and 4 additions
In general you need n multiplications and n additions, a total of 2n calculations.
The problem is said to have linear complexity and the time to solve is proportional to the size.
Doubling the ‘size’ of the problem doubles the time to solve.
Tripling the ‘size’ of the problem triples the time to solve etc.
Efficiency Problems
1) A sorting algorithm has quadratic efficiency. It takes 3 seconds to sort a list of 4000 items.
How long will it take to sort a list of a) 8000 items?
b) 13000 items?
2) An algorithm has cubic efficiency. It takes 10 seconds to solve a problem of ‘size’ 12. How long will it take to solve a problem of ‘size’ 30?
3) An algorithm takes 5 minutes to solve a problem of ‘size’ 30.
How much longer would it take to solve a problem of size 40 if the algorithm has cubic efficiency rather than quadratic efficiency?
Bin Packing algorithms
Suppose we have 11 vehicles to load onto a ferry with 5 lanes, each 15m long.
The vehicles are labelled A to K and have different lengths as denoted.
A 8m
B 7m
C 4m
D 9m
E 6m
F 9m
G 5m
H 5m
I 6m
J 7m
K 8m
Can we fit all the vehicles onto the ferry?
Algorithms for solving this problemFirst Fit algorithm: Take the first item and place it in the first available ‘bin’ working from
one end. Repeat until all the bins are full or until all items are packed.
First Fit Decreasing algorithm: As above but sort the items into decreasing order
of size first
Full bins algorithm: Look for combinations of items that will make ‘full bins’ first and fill
these bins, then use the First Fit algorithm to pack any remaining items
1 23
6
2 35
3
A B C D E F
4
Each block will be fitted into the first bin that has room for it.Each block will be fitted into the first bin that has room for it.
Bin packing – First fit algorithm
6 units
A B C D E F
4
The first block fits into the first binThe first block fits into the first bin
1 23
6
2 35
3
Bin packing – First fit algorithm
6 units
A B C D E F
4
The second block fits in the first binThe second block fits in the first bin
1
23
6
2 35
3
Bin packing – First fit algorithm
6 units
A B C D E F
4
The third block will not fit in the first binThe third block will not fit in the first bin
1
23
6
2
35
3
Bin packing – First fit algorithm
6 units
A B C D E F
4
1
23
6
35
3
2
But there is room in the second binBut there is room in the second bin
Bin packing – First fit algorithm
6 units
Bin packing – First fit algorithm
The third bin is the first one the 5 will fit inThe third bin is the first one the 5 will fit in
A B C D E F
4
1
2
6
3
5
3
2
3
5
56 units
The second bin has room for the 3The second bin has room for the 3
A B C D E F
4
1
2
6
33
2
35
3
Bin packing – First fit algorithm
6 units
The fourth bin is the first one with room for the next one
The fourth bin is the first one with room for the next one
A B C D E F
4
1
2
6
33
2
35
2 2
2
Bin packing – First fit algorithm
6 units
The next one also fits in the fourth binThe next one also fits in the fourth bin
A B C D E F
4
1
6
3
2
35
2
3 3 3
3
Bin packing – First fit algorithm
6 units
Bin packing – First fit algorithm
No room until the fifth bin for the 6No room until the fifth bin for the 6
A B C D E F
4
1
6
3
2
35
2
3
6 6 6
66 units
The 3 has to start a new binThe 3 has to start a new bin
A B C D E F
4
1
2
35
2
3 6
3 3 3 33
3
Total usage is 6 bins.Total usage is 6 bins.
Bin packing – First fit algorithm
6 units
1 23
6
2 35
3
A B C D E F
4
With the first fit decreasing algorithm we sort the blocks into descending order first.
With the first fit decreasing algorithm we sort the blocks into descending order first.
Bin packing – First fit decreasing algorithm
6 units
With the first fit decreasing algorithm we sort the blocks into descending order first.
With the first fit decreasing algorithm we sort the blocks into descending order first.
233345
1
6
2
A B C D E F
Bin packing – First fit decreasing algorithm
6 units
23312
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5 4 3
6
Bin packing – First fit decreasing algorithm
6 units
Bin packing – First fit decreasing algorithm
2331
6
2
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5
4 3
5
6 units
2331
6
2
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5
4
4
3
4
Bin packing – First fit decreasing algorithm
6 units
2331
6
2
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5 4
33
3
3
Bin packing – First fit decreasing algorithm
6 units
231
6
2
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5 4 3
33
3
3
Bin packing – First fit decreasing algorithm
6 units
2 1
6
2
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5 4 3
3
33
3
3
3
Bin packing – First fit decreasing algorithm
6 units
1
6
2
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5 4 3
3
3
22
2
Bin packing – First fit decreasing algorithm
6 units
1
6
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5 4 3
3
3
2
22
2 2
2
Bin packing – First fit decreasing algorithm
6 units
6
A B C D E F
Now we use the first fit algorithmNow we use the first fit algorithm
5 4 3
3
3
22
1
We have packed them into 5 bins.We have packed them into 5 bins.
1
Bin packing – First fit decreasing algorithm
6 units
Algorithms for Sorting Items
1) Interchange sort algorithm
2) Bubble sort algorithm
3) Shuttle sort algorithm
4) Quick sort algorithm
The Interchange Sort Algorithm
• Step 1: Search down the list to find the smallest number and interchange it with the first number on the list
• Step 2: Starting with the 2nd number on the list, search down the list to find the smallest number left and interchange it with the 2nd number on the list.
• Step 3: Repeat Step 2 starting with the 3rd, then 4th number on the list until the bottom of the list is reached.
OriginalList
5126943
After 1Pass
1526943
After 2Passes
1256943
After 3Passes
1236945
After 4Passes
1234965
After 5Passes
1234569
After 6Passes
5126943
The Bubble Sort Algorithm
• Step 1: If there is only one number in the list then stop.
• Step 2: Make one pass down the list, comparing numbers in pairs and swapping them as necessary.
• Step 3: If no swaps have occurred then stop. Otherwise, ignore the last element of the list and return to Step 1.
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 1 11 2 2 22 5 5 46 6 4 39 4 3 5 54 3 6 6 63 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 1 1 11 2 2 2 22 5 5 46 6 4 39 4 3 5 54 3 6 6 63 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 1 1 11 2 2 2 22 5 5 46 6 4 39 4 3 5 54 3 6 6 63 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 1 1 11 2 2 2 22 5 5 4 46 6 4 39 4 3 5 54 3 6 6 63 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 1 1 11 2 2 2 22 5 5 4 46 6 4 39 4 3 5 54 3 6 6 63 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 1 1 11 2 2 2 22 5 5 4 36 6 4 3 49 4 3 5 54 3 6 6 63 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 1 11 2 2 2 22 5 5 4 36 6 4 3 49 4 3 5 54 3 6 6 63 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 1 11 2 2 2 22 5 5 4 36 6 4 3 4 49 4 3 5 5 54 3 6 6 6 63 9 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 1 1 11 2 2 2 2 22 5 5 4 36 6 4 3 4 49 4 3 5 5 54 3 6 6 6 63 9 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 1 1 11 2 2 2 2 22 5 5 4 36 6 4 3 4 49 4 3 5 5 54 3 6 6 6 63 9 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 1 1 11 2 2 2 2 22 5 5 4 3 36 6 4 3 4 49 4 3 5 5 54 3 6 6 6 63 9 9 9 9 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 1 11 2 2 2 2 22 5 5 4 3 36 6 4 3 4 49 4 3 5 5 54 3 6 6 6 63 9 9 9 9 9
“If no swaps have occurred then stop.”
The Shuttle Sort Algorithm• 1st pass
– Compare the 1st and 2nd numbers in the list and swap if necessary.
• 2nd pass– Compare the 2nd and 3rd numbers in the list and swap if
necessary. If a swap has occurred, compare the 1st and 2nd numbers and swap if necessary.
• 3rd pass– Compare the 3rd and 4th numbers in the list and swap if
necessary. If a swap has occurred, compare the 2nd and 3rd numbers, and so on up the list.
• And so on, through the entire list.
Example
OriginalList
1st pass 2nd pass
5 11 52 26 6 69 9 94 4 43 3 3
“compare the 2nd and 3rd numbers”
Example
OriginalList
1st pass 2nd pass
5 11 5 22 2 56 6 69 9 94 4 43 3 3
“if a swap has occurred, compare the 1st and 2nd numbers”
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 11 5 22 2 56 6 69 9 9 94 4 4 43 3 3 3
“3rd pass”
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 11 5 22 2 56 6 69 9 9 94 4 4 43 3 3 3
“compare 3rd and 4th numbers”
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 11 5 22 2 5 56 6 6 69 9 9 94 4 4 43 3 3 3
“swap if necessary”
Example
OriginalList
1st pass 2nd pass 3rd pass
5 1 1 11 5 2 22 2 5 56 6 6 69 9 9 94 4 4 43 3 3 3
“if a swap has occurred….”
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 11 5 2 22 2 5 56 6 6 69 9 9 94 4 4 4 43 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 11 5 2 22 2 5 56 6 6 69 9 9 94 4 4 4 43 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 11 5 2 22 2 5 56 6 6 6 69 9 9 9 94 4 4 4 43 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass
5 1 1 1 11 5 2 2 22 2 5 5 56 6 6 6 69 9 9 9 94 4 4 4 43 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 56 6 6 6 69 9 9 9 94 4 4 4 43 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 56 6 6 6 69 9 9 9 94 4 4 4 43 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 56 6 6 6 69 9 9 9 9 44 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 56 6 6 6 69 9 9 9 9 44 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 56 6 6 6 6 49 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 56 6 6 6 6 49 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 93 3 3 3 3 3
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 9 33 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 64 4 4 4 4 9 33 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 6 34 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 59 9 9 9 9 6 34 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 5 39 9 9 9 9 6 54 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 46 6 6 6 6 5 39 9 9 9 9 6 54 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 4 36 6 6 6 6 5 49 9 9 9 9 6 54 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 22 2 5 5 5 4 36 6 6 6 6 5 49 9 9 9 9 6 54 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 11 5 2 2 2 2 22 2 5 5 5 4 36 6 6 6 6 5 49 9 9 9 9 6 54 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 1 11 5 2 2 2 2 22 2 5 5 5 4 36 6 6 6 6 5 49 9 9 9 9 6 54 4 4 4 4 9 63 3 3 3 3 3 9
Example
OriginalList
1st pass 2nd pass 3rd pass 4th pass 5th pass 6th pass
5 1 1 1 1 1 11 5 2 2 2 2 22 2 5 5 5 4 36 6 6 6 6 5 49 9 9 9 9 6 54 4 4 4 4 9 63 3 3 3 3 3 9
Example
• The example required 14 comparisons and 9 swaps to take place.
• The Bubble Sort algorithm requires 20 comparisons and 9 swaps for the same list.
• This makes the Shuttle Sort the more efficient algorithm.
The Quick Sort Algorithm• Choose the first number in the list as a pivot and
ring it.• Pass through the list writing those numbers lower
than or equal to the pivot before it in the list and those numbers larger than the pivot after it. This will split the list into two sub-lists with the pivot as a divide.
• Repeat the algorithm on each sub-list, choosing the first number in each sub-list as the pivot for that sub-list.
• Keep splitting the sub-lists in this way until no sub-list has more than one number in it
ExamplePass down the list, placing
the numbers on the correct side of the pivot
OriginalList
5129643
First Pass
1243596
Example
OriginalList
5129643
FirstPass
1243596
Second Pass
1243569
Pass down the list, placing the numbers on the
correct side of each pivot
Example
OriginalList
5129643
FirstPass
1243596
SecondPass
1243569
Ring the pivots in the new sub-
lists
Example
OriginalList
5129643
FirstPass
1243596
SecondPass
1243569
Pass down the list, placing the numbers on the
correct side of each pivot
ThirdPass
1243569
Example
OriginalList
5129643
FirstPass
1243596
SecondPass
1243569
ThirdPass
1243569
Ring the pivots in the sub-lists
Example
OriginalList
5129643
FirstPass
1243596
SecondPass
1243569
ThirdPass
1243569
FourthPass
1234569
Pass down the list, placing the numbers on the
correct side of each pivot
Example
OriginalList
5129643
FirstPass
1243596
SecondPass
1243569
ThirdPass
1243569
FourthPass
1234569
No sub-lists of more than one so stop