Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem
Monday’s child is fair of face Tuesday’s child is full of grace Wednesday’s child is full of woe Thursday’s child has far to go Friday’s child is loving and giving Saturday’s child works hard for its living And a child that is born on the Sabbath day Is 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 algorithm Let day number = D Month number = M Year 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 Y And Z be the last two digits of Y Add together the integer parts of (2.6M – 5.39), (Z÷4) and (C÷4), then add 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 Sunday If the remainder is 1 the day was Monday etc. Eg 15 th May 1991 D =15 M = 5 Y = 1991 C = 19 Z = – 38 = 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
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 three and 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 from y to get a new value for y Output x Euclid’s algorithm for finding the HCF of two integers in ‘Pseudo Code’ Input x Input y Repeat If x>y then x = x-y If y>x then y = y-x Until x=y Print x yes no yes no
Page 26 q.3, Page 27 q.5, Page 29 q 11, Page 41 q.19
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 / 2 n(n+1) mults and n additions. In total: 1 / 2 n / 2 n calculations. We say the problem has quadratic complexity. The time to solve it is proportional to the size 2. 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 + b1 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 + d3 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) 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 problem First 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
A B C D E F 4 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 bin Bin packing – First fit algorithm 6 units
A B C D E F 4 The second block fits in the first bin Bin packing – First fit algorithm 6 units
A B C D E F 4 The third block will not fit in the first bin Bin packing – First fit algorithm 6 units
A B C D E F But 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 in A B C D E F units
The second bin has room for the 3 A B C D E F Bin packing – First fit algorithm 6 units
The fourth bin is the first one with room for the next one A B C D E F Bin packing – First fit algorithm 6 units
The next one also fits in the fourth bin A B C D E F Bin packing – First fit algorithm 6 units
Bin packing – First fit algorithm No room until the fifth bin for the 6 A B C D E F units
The 3 has to start a new bin A B C D E F Total usage is 6 bins. Bin packing – First fit algorithm 6 units
A B C D E F 4 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 A B C D E F Bin packing – First fit decreasing algorithm 6 units
A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units
Bin packing – First fit decreasing algorithm A B C D E F Now we use the first fit algorithm units
A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units
A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units
A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units
A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units
1 6 2 A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units
1 6 A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units
6 A B C D E F Now we use the first fit algorithm We have packed them into 5 bins. 1 Bin packing – First fit decreasing algorithm 6 units
Sorting Algorithms
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 2 nd number on the list, search down the list to find the smallest number left and interchange it with the 2 nd number on the list. Step 3: Repeat Step 2 starting with the 3 rd, then 4 th number on the list until the bottom of the list is reached.
Original List After 1 Pass After 2 Passes After 3 Passes After 4 Passes After 5 Passes After 6 Passes
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 Original List
Example Original List “Compare numbers in pairs”
Example Original List “swapping them as necessary”
Example Original List Making a pass down the list
Example Original List
Example Original List
Example Original List
Example Original List
Example Original List
Example Original List
Example Original List
Example Original List
Example Original List
Example Original List 1 st pass “ignore the last element”
Example Original List 1 st pass “return to Step 2”
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass
Example Original List 1 st pass2 nd pass 3 rd pass
Example Original List 1 st pass2 nd pass3 rd pass
Example Original List 1 st pass2 nd pass3 rd pass
Example Original List 1 st pass2 nd pass3 rd pass
Example Original List 1 st pass2 nd pass3 rd pass
Example Original List 1 st pass2 nd pass3 rd pass
Example Original List 1 st pass2 nd pass3 rd pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass “If no swaps have occurred then stop.”
The Shuttle Sort Algorithm
1 st pass – Compare the 1 st and 2 nd numbers in the list and swap if necessary. 2 nd pass – Compare the 2 nd and 3 rd numbers in the list and swap if necessary. If a swap has occurred, compare the 1 st and 2 nd numbers and swap if necessary. 3 rd pass – Compare the 3 rd and 4 th numbers in the list and swap if necessary. If a swap has occurred, compare the 2 nd and 3 rd numbers, and so on up the list. And so on, through the entire list.
Example Original List
Example Original List 1 st pass “1 st pass”
Example Original List 1 st pass “compare the 1 st and 2 nd numbers”
Example Original List 1 st pass “swap if necessary”
Example Original List 1 st pass2 nd pass “2 nd pass”
Example Original List 1 st pass2 nd pass “compare the 2 nd and 3 rd numbers”
Example Original List 1 st pass2 nd pass “swap if necessary”
Example Original List 1 st pass2 nd pass “if a swap has occurred, compare the 1 st and 2 nd numbers”
Example Original List 1 st pass2 nd pass “swap if necessary”
Example Original List 1 st pass2 nd pass3 rd pass “3 rd pass”
Example Original List 1 st pass2 nd pass3 rd pass “compare 3 rd and 4 th numbers”
Example Original List 1 st pass2 nd pass3 rd pass “swap if necessary”
Example Original List 1 st pass2 nd pass3 rd pass “if a swap has occurred….”
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass
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
Example Original List
Example Ring the pivot Original List
Example Pass down the list, placing the numbers on the correct side of the pivot Original List First Pass
Example Ring the pivots in the sub-lists Original List First Pass
Example Original List First Pass Second Pass Pass down the list, placing the numbers on the correct side of each pivot
Example Original List First Pass Second Pass Ring the pivots in the new sub- lists
Example Original List First Pass Second Pass Pass down the list, placing the numbers on the correct side of each pivot Third Pass
Example Original List First Pass Second Pass Third Pass Ring the pivots in the sub-lists
Example Original List First Pass Second Pass Third Pass Fourth Pass Pass down the list, placing the numbers on the correct side of each pivot
Example Original List First Pass Second Pass Third Pass Fourth Pass No sub-lists of more than one so stop
Algorithms for ‘searching’ for an item on a list
The Binary Search Algorithm Use the Binary Search pps