Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem.

Similar presentations


Presentation on theme: "Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem."— Presentation transcript:

1 Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem

2 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

3 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 = 91 7 + 22 + 4 + 15 + 91 – 38 = 101 3 So Wednesday

4 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 163 48 81 96 40 192 20 384 10 768 5 1536 2 3072 1 3912

5 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.

6 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’

7 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

8 Page 26 q.3, Page 27 q.5, Page 29 q 11, Page 41 q.19

9 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

10 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 + 3 / 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

11 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.

12 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?

13 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?

14 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

15 1 2 3 6 2 3 5 3 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

16 A B C D E F 4 The first block fits into the first bin 1 2 3 6 2 3 5 3 Bin packing – First fit algorithm 6 units

17 A B C D E F 4 The second block fits in the first bin 1 2 3 6 2 3 5 3 Bin packing – First fit algorithm 6 units

18 A B C D E F 4 The third block will not fit in the first bin 1 2 3 6 2 3 5 3 Bin packing – First fit algorithm 6 units

19 A B C D E F 4 1 2 3 6 3 5 3 2 But there is room in the second bin Bin packing – First fit algorithm 6 units

20 Bin packing – First fit algorithm The 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 5 6 units

21 The second bin has room for the 3 A B C D E F 4 1 2 6 33 2 3 5 3 Bin packing – First fit algorithm 6 units

22 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 3 5 22 2 Bin packing – First fit algorithm 6 units

23 The next one also fits in the fourth bin A B C D E F 4 1 6 3 2 3 5 2 333 3 Bin packing – First fit algorithm 6 units

24 Bin packing – First fit algorithm No room until the fifth bin for the 6 A B C D E F 4 1 6 3 2 3 5 2 3 666 6 6 units

25 The 3 has to start a new bin A B C D E F 4 1 2 3 5 2 3 6 3333 3 3 Total usage is 6 bins. Bin packing – First fit algorithm 6 units

26 1 2 3 6 2 3 5 3 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

27 With the first fit decreasing algorithm we sort the blocks into descending order first. 2 333 4 5 1 6 2 A B C D E F Bin packing – First fit decreasing algorithm 6 units

28 2 33 1 2 A B C D E F Now we use the first fit algorithm 5 4 3 6 Bin packing – First fit decreasing algorithm 6 units

29 Bin packing – First fit decreasing algorithm 2 33 1 6 2 A B C D E F Now we use the first fit algorithm 5 4 3 5 6 units

30 2 33 1 6 2 A B C D E F Now we use the first fit algorithm 5 4 4 3 4 Bin packing – First fit decreasing algorithm 6 units

31 2 33 1 6 2 A B C D E F Now we use the first fit algorithm 5 4 3 3 3 3 Bin packing – First fit decreasing algorithm 6 units

32 2 3 1 6 2 A B C D E F Now we use the first fit algorithm 5 4 3 3 3 3 3 Bin packing – First fit decreasing algorithm 6 units

33 2 1 6 2 A B C D E F Now we use the first fit algorithm 5 4 3 3 3 3 3 3 3 Bin packing – First fit decreasing algorithm 6 units

34 1 6 2 A B C D E F Now we use the first fit algorithm 5 4 3 3 3 2 2 2 Bin packing – First fit decreasing algorithm 6 units

35 1 6 A B C D E F Now we use the first fit algorithm 5 4 3 3 3 2 2 2 22 2 Bin packing – First fit decreasing algorithm 6 units

36 6 A B C D E F Now we use the first fit algorithm 5 4 3 3 3 2 2 1 We have packed them into 5 bins. 1 Bin packing – First fit decreasing algorithm 6 units

37 Sorting Algorithms

38 Algorithms for Sorting Items 1)Interchange sort algorithm 2)Bubble sort algorithm 3)Shuttle sort algorithm 4)Quick sort algorithm

39 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.

40 Original List 5 1 2 6 9 4 3 After 1 Pass 1 5 2 6 9 4 3 After 2 Passes 1 2 5 6 9 4 3 After 3 Passes 1 2 3 6 9 4 5 After 4 Passes 1 2 3 4 9 6 5 After 5 Passes 1 2 3 4 5 6 9 After 6 Passes 5 1 2 6 9 4 3

41 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.

42 Example Original List 5 1 2 6 9 4 3

43 Example Original List 5 1 2 6 9 4 3 “Compare numbers in pairs”

44 Example Original List 51 15 2 6 9 4 3 “swapping them as necessary”

45 Example Original List 51 15 2 6 9 4 3 Making a pass down the list

46 Example Original List 51 12 25 6 9 4 3

47 Example Original List 51 12 25 6 9 4 3

48 Example Original List 51 12 25 66 9 4 3

49 Example Original List 51 12 25 66 9 4 3

50 Example Original List 51 12 25 66 99 4 3

51 Example Original List 51 12 25 66 99 4 3

52 Example Original List 51 12 25 66 94 49 3

53 Example Original List 51 12 25 66 94 49 3

54 Example Original List 51 12 25 66 94 43 39

55 Example Original List 1 st pass 51 12 25 66 94 43 39 “ignore the last element”

56 Example Original List 1 st pass 51 12 25 66 94 43 39 “return to Step 2”

57 Example Original List 1 st pass 51 12 25 66 94 43 399

58 Example Original List 1 st pass 511 122 25 66 94 43 399

59 Example Original List 1 st pass 511 122 25 66 94 43 399

60 Example Original List 1 st pass 511 122 255 66 94 43 399

61 Example Original List 1 st pass 511 122 255 66 94 43 399

62 Example Original List 1 st pass 511 122 255 666 94 43 399

63 Example Original List 1 st pass 511 122 255 666 94 43 399

64 Example Original List 1 st pass 511 122 255 664 946 43 399

65 Example Original List 1 st pass 511 122 255 664 946 43 399

66 Example Original List 1 st pass 511 122 255 664 943 436 399

67 Example Original List 1 st pass2 nd pass 511 122 255 664 943 436 399

68 Example Original List 1 st pass2 nd pass 511 122 255 664 943 4366 3999

69 Example Original List 1 st pass2 nd pass 5111 1222 255 664 943 4366 3999

70 Example Original List 1 st pass2 nd pass 5111 1222 255 664 943 4366 3999

71 Example Original List 1 st pass2 nd pass 5111 1222 2555 664 943 4366 3999

72 Example Original List 1 st pass2 nd pass 5111 1222 2555 664 943 4366 3999

73 Example Original List 1 st pass2 nd pass 5111 1222 2554 6645 943 4366 3999

74 Example Original List 1 st pass2 nd pass 5111 1222 2554 6645 943 4366 3999

75 Example Original List 1 st pass2 nd pass 5111 1222 2554 6643 9435 4366 3999

76 Example Original List 1 st pass2 nd pass 3 rd pass 5111 1222 2554 6643 9435 4366 3999

77 Example Original List 1 st pass2 nd pass3 rd pass 5111 1222 2554 6643 94355 43666 39999

78 Example Original List 1 st pass2 nd pass3 rd pass 51111 12222 2554 6643 94355 43666 39999

79 Example Original List 1 st pass2 nd pass3 rd pass 51111 12222 2554 6643 94355 43666 39999

80 Example Original List 1 st pass2 nd pass3 rd pass 51111 12222 25544 6643 94355 43666 39999

81 Example Original List 1 st pass2 nd pass3 rd pass 51111 12222 25544 6643 94355 43666 39999

82 Example Original List 1 st pass2 nd pass3 rd pass 51111 12222 25543 66434 94355 43666 39999

83 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 51111 12222 25543 66434 94355 43666 39999

84 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 51111 12222 25543 664344 943555 436666 399999

85 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 511111 122222 25543 664344 943555 436666 399999

86 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 511111 122222 25543 664344 943555 436666 399999

87 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 511111 122222 255433 664344 943555 436666 399999

88 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 511111 122222 255433 664344 943555 436666 399999 “If no swaps have occurred then stop.”

89

90 The Shuttle Sort Algorithm

91 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.

92 Example Original List 5 1 2 6 9 4 3

93 Example Original List 1 st pass 5 1 22 66 99 44 33 “1 st pass”

94 Example Original List 1 st pass 5 1 22 66 99 44 33 “compare the 1 st and 2 nd numbers”

95 Example Original List 1 st pass 51 15 22 66 99 44 33 “swap if necessary”

96 Example Original List 1 st pass2 nd pass 51 15 22 666 999 444 333 “2 nd pass”

97 Example Original List 1 st pass2 nd pass 51 15 22 666 999 444 333 “compare the 2 nd and 3 rd numbers”

98 Example Original List 1 st pass2 nd pass 51 152 225 666 999 444 333 “swap if necessary”

99 Example Original List 1 st pass2 nd pass 51 152 225 666 999 444 333 “if a swap has occurred, compare the 1 st and 2 nd numbers”

100 Example Original List 1 st pass2 nd pass 511 152 225 666 999 444 333 “swap if necessary”

101 Example Original List 1 st pass2 nd pass3 rd pass 511 152 225 666 9999 4444 3333 “3 rd pass”

102 Example Original List 1 st pass2 nd pass3 rd pass 511 152 225 666 9999 4444 3333 “compare 3 rd and 4 th numbers”

103 Example Original List 1 st pass2 nd pass3 rd pass 511 152 2255 6666 9999 4444 3333 “swap if necessary”

104 Example Original List 1 st pass2 nd pass3 rd pass 5111 1522 2255 6666 9999 4444 3333 “if a swap has occurred….”

105 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 5111 1522 2255 6666 9999 44444 33333

106 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 5111 1522 2255 6666 9999 44444 33333

107 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 5111 1522 2255 66666 99999 44444 33333

108 Example Original List 1 st pass2 nd pass3 rd pass4 th pass 51111 15222 22555 66666 99999 44444 33333

109 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 22555 66666 99999 44444 333333

110 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 22555 66666 99999 44444 333333

111 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 22555 66666 999994 444449 333333

112 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 22555 66666 999994 444449 333333

113 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 22555 666664 999996 444449 333333

114 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 22555 666664 999996 444449 333333

115 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 225554 666665 999996 444449 333333

116 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 15222 225554 666665 999996 444449 333333

117 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 51111 152222 225554 666665 999996 444449 333333

118 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass 511111 152222 225554 666665 999996 444449 333333

119 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 666665 999996 444449 333333

120 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 666665 999996 444449 333333

121 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 666665 999996 4444493 3333339

122 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 666665 999996 4444493 3333339

123 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 666665 9999963 4444496 3333339

124 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 666665 9999963 4444496 3333339

125 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 6666653 9999965 4444496 3333339

126 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 225554 6666653 9999965 4444496 3333339

127 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 2255543 6666654 9999965 4444496 3333339

128 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 152222 2255543 6666654 9999965 4444496 3333339

129 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 511111 1522222 2255543 6666654 9999965 4444496 3333339

130 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 5111111 1522222 2255543 6666654 9999965 4444496 3333339

131 Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass 5111111 1522222 2255543 6666654 9999965 4444496 3333339

132 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.

133 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

134 Example Original List 5 1 2 9 6 4 3

135 Example Ring the pivot Original List 5 1 2 9 6 4 3

136 Example Pass down the list, placing the numbers on the correct side of the pivot Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6

137 Example Ring the pivots in the sub-lists Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6

138 Example Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6 Second Pass 1 2 4 3 5 6 9 Pass down the list, placing the numbers on the correct side of each pivot

139 Example Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6 Second Pass 1 2 4 3 5 6 9 Ring the pivots in the new sub- lists

140 Example Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6 Second Pass 1 2 4 3 5 6 9 Pass down the list, placing the numbers on the correct side of each pivot Third Pass 1 2 4 3 5 6 9

141 Example Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6 Second Pass 1 2 4 3 5 6 9 Third Pass 1 2 4 3 5 6 9 Ring the pivots in the sub-lists

142 Example Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6 Second Pass 1 2 4 3 5 6 9 Third Pass 1 2 4 3 5 6 9 Fourth Pass 1 2 3 4 5 6 9 Pass down the list, placing the numbers on the correct side of each pivot

143 Example Original List 5 1 2 9 6 4 3 First Pass 1 2 4 3 5 9 6 Second Pass 1 2 4 3 5 6 9 Third Pass 1 2 4 3 5 6 9 Fourth Pass 1 2 3 4 5 6 9 No sub-lists of more than one so stop

144 Algorithms for ‘searching’ for an item on a list

145 The Binary Search Algorithm Use the Binary Search pps


Download ppt "Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem."

Similar presentations


Ads by Google