Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr Radwan –Associate Professor

Similar presentations


Presentation on theme: "Dr Radwan –Associate Professor"— Presentation transcript:

1 Dr Radwan –Associate Professor
Block 2 Part 6 Algorithms Design AOU University Prepared By : Dr Radwan –Associate Professor

2 Introduction It is not uncommon to read articles with titles like ‘Google’s algorithm for ranking web pages’ or ‘Facebook’s algorithm for curating news stories’. Our civilization is increasingly reliant on algorithms, therefore it’s worth learning more about exactly what they are, and about some of their strengths and weaknesses. You have learned that an algorithm may describe how a problem can be solved by a computer. However, as we discuss in this part, the concept of an algorithm is more general than that.

3 Introduction An algorithm simply is a ‘step-by-step guide to completing a particular task’. Many of our everyday activities follow an underlying algorithm. For example, here is an algorithm for brushing teeth:

4 Introduction Recipes are obviously algorithmic, such as this one for making a beetroot cake:

5 The fundamentals of algorithms
A well-written algorithm for a program can be implemented (written) in a variety of programming languages. The implementations of an algorithm in different languages of course all look different, but they all share the fundamental features described by the algorithm. Understanding these fundamental algorithmic features can help you more readily understand programs even in languages that are new to you.

6 Algorithm Definitions
An algorithm is: a set of step-by-step instructions to complete a particular task. Similarities between everyday algorithms and those used for programs? Both have set of instructions (the style of instruction in each case is very different). Both complete in a finite amount of time. Note: There are processes that go on indefinitely, but we don’t consider them amongst the algorithms we analyze in this part. Just like a recipe, an algorithm must stop. It need not be quick, it may take years to generate an answer, but it must eventually finish.

7 Algorithm Definitions
Differences between everyday algorithms and those used for programs? Everyday algorithms are imprecise and might omit an instruction relying of humans previous experience or general knowledge. For example, a recipe might say ‘Bake for 45 mins at 180 °C’ without giving explicit instruction to turn the oven on and off. Algorithms for programs must be precise and self-contained. They must contain all the instructions required to solve the problem, and those instructions must be unambiguous because computers cannot deal with omission or imprecision.

8 Algorithm Definitions
In implementing their own algorithm, a programmer might clarify the ambiguous and fill in missing steps, and thereby arrive at a working program. One advantage of algorithms is that once an algorithm is written down, it can often be reused over and over by other people wanting to solve the same problem in different programming languages. Algorithm can now be defined more precisely as: A set of unambiguous, self-contained, step-by-step instructions guaranteed to complete a particular task in a finite amount of time.

9 Refinement Ambiguity is a real problem for algorithms designed for programs.  Ambiguity can be overcome by breaking instructions down into smaller steps to clarify the order of individual actions. This process is sometimes called refinement or decomposition. For example, we might start with an instruction: Deduct rounded dis count amount f rom total price. Then we might refine this as: Calculate d is count as 20% of total price Round discount Reduce total price by discount

10 Fundamental features Computer programs vary in purpose and complexity, as well as in the languages they are written in. Consider these examples: Google search engine camera face-detection software aircraft flight-control software online UK income-tax calculator The many tasks each of these programs perform are based on algorithms that is, they follow precise, finite, step-by-step instructions. The algorithms for these and all other computer programs are built around four simple ideas: Inputs and outputs Sequences of instructions Repetition of instructions Selection of instructions. By combining these four ideas in different way, we can create an algorithm.

11 Inputs and outputs It’s possible to imagine someone writing an algorithm that takes no external input and that always produces the same output. But, as a rule, an algorithm that always produced the same output would be of little use. We want algorithms to be able to start with different data, inputs, and produce output that varies according to the input data. This enables algorithms to be general-purpose problem-solving tools, providing different solutions in different situations. For example, consider the following algorithm snippet which requires two pieces of input data from the user and outputs one piece of data: Input the first number Input the second number Display the mean (aver age) of the two numbers

12 Inputs and outputs In OUBuild, we might implement this as in Figure 1.
Implementing the above algorithm In Python follows the same basic pattern: number1 = input('Enter your first number ') number2 = input('Enter your second number ') mean = (float(number1) + float(number2)) / 2 print('Mean is', mean) Figure 1

13 Inputs and outputs Not all input data for an algorithm comes from the user. Some data that an algorithm needs might come from the environment in which the program runs. For example, consider the following simple algorithm snippet: Tell the user what last year was In OUBuild, as with many other languages, there is in-built data providing the current date and time, etc. This sort of data can be used by an algorithm, as follows: Get this year Subtract 1 Display last year

14 Sequence, repetition, selection
Suppose a Cola merchant is giving loyalty points for their customers according to the following algorithm: Repeat (6) times Input box price Add box price to total price If total price is greater than £ 5000 then Calculate loyalty points as 5% of total price, rounded to nearest whole number Tell the user the loyalty points earned else Tell the user no loyalty points earned The above instructions are followed in sequential order, from top to bottom, except where an instruction involves repetition or selection.

15 Sequence, repetition, selection
The algorithm starts with the repetition instruction: Repeat (6) times Input box price Add box price to total price This instructs the algorithm to repeatedly iterate through the indented sequence of actions, the loop body, in order, six times. The actions that are repeated are to: Get the box price from the user Add the box price to the total price. The way the repetition instruction is set out within the algorithm, with the condition at the top and the actions indented underneath, emphasizes the connection between them.

16 Sequence, repetition, selection
This particular sort of repetition instruction corresponds to a fixed loop: one that repeats a sequence of actions a fixed number of times. In OUBuild, fixed loops are implemented with the block repeat[]{}, as in Figure 2. In Python, an equivalent code can be implemented using something known as a for loop as follows: total_price = 0 for item in range(6): box_price = input('Enter box price ') total_price = total_price + float(box_price) Figure 2

17 Sequence, repetition, selection
Python doesn’t have a direct equivalent of repeat_until<>{}. It has something called a while loop, which could be used to implement the same algorithm as follows: total_price = 0 box_count = 0 while box_count < 6: box_price = input('Enter box price ') total_price = total_price + float(box_price) box_count = box_count + 1 A while loop repeats while a particular condition holds. Here, the condition is: box_count < 6 Therefore, the while loop condition must be different from that needed for the OUBuild repeat_until<>{} loop, which repeats until its condition holds.

18 Sequence, repetition, selection
Here is an algorithm for a program to count down from 100: Set i to l00 Repeat until ( i is less than 1 ) Tell the user the value of i Decrease i by 1 Tell the user ‘Blast off ! ’ Question: What change could be made to the condition in order to replace ‘Repeat until’ with ‘Repeat while’? Answer1: Repeat while ( i is greater than or equal to 1 ) Answer2: Repeat while ( i is greater than 0 )

19 Sequence, repetition, selection
Back to our Cola merchant algorithm: after completing the repetition instruction the algorithm moves on to the following selection instruction: If total price is greater than £ 5000 then Calculate loyalty points as 5 % of total price rounded to nearest whole number Tell the user the loyalty points earned else Tell t he user no loyalty points earned This is an ‘if … then … else’ selection instruction.

20 Sequence, repetition, selection
This selection part starts by considering the condition: total price is greater than £5000. If the condition holds, then the ‘if’ part (i.e. the first indented sequence of instructions) is carried out; and then the algorithm moves on below the selection instruction (and simply stops in this case). If the condition does not hold, the ‘else’ part is carried out (i.e. the second indented sequence of instructions); and then the algorithm moves on and stops. The way the algorithm is set out, with the condition at the top and the two sequences of instructions indented underneath, emphasises the connections between them. If it were not required to tell the user that no loyalty points were awarded when the total price is not greater than £5000, then the algorithm could have been written using a simpler ‘if’ instruction (without the ‘else’ part): If total price is greater than £ 5000 then Calculate loyalty points as 5 % of total price rounded to nearest whole number Tell the user the loyalty points earned

21 Sequence, repetition, selection
In OUBuild, the selection part is implemented as in Figure 3. Figure 3 In Python, an equivalent code can be implemented using similar wording and indentations: if total_price > 50000: loyalty_points = round(0.05 * total_price) print('You have earned', loyalty_points,'loyalty points') else: print('No loyalty points earned')

22 Sequential and nested selection
Suppose I always provide for my children the same meal on the same day each week, as follows: Monday – Meat loaf Tuesday – Shepherd’s pie Wednesday – Omelette Thursday – Chilli Friday – Fish and chips I might create an algorithm something like the following: Input the day If day is Monday t hen Set meal t o ‘Meat loaf ’ If day is Tuesday then Set meal t o ‘Shepherd’s pie’ ... If day is Friday t hen Set meal t o ‘Fish and chips ’ Output meal

23 Sequential and nested selection
In OUBuild, this might be implemented using sequence of selection instructions as in Figure 4. The Python equivalent of the is as follows: if day == 'M': meal = 'Meat loaf' if day == 'T': meal = 'Shepherd\'s pie' if day == 'W': meal = 'Omelette' if day == 'Th': meal = 'Chilli' if day == 'F': meal = 'Fish and chips' print(meal, 'today!') Figure 4

24 Sequential and nested selection
The previous code is works perfectly but it is not very elegant. Whatever day is input, the algorithm works its way through every one of the five ‘if … then …’ instructions. A more elegant and more efficient algorithm can be done using nested ‘if … then … else’ instructions. The nested structure prevents the algorithm proceeding any further through the selection instructions once it has reached the condition corresponding to the given day.

25 Sequential and nested selection
The nested structure looks as follows: Input the day If day is Monday then Set meal t o ‘Meat loaf ’ else If day is Tuesday t hen Set meal to ‘Shepherd’s pie’ ... If day is Thursday then Set meal to ‘Chilli’ Set meal t o ‘Fish and chips ’ Output meal

26 Switch instructions Some other languages (though not Python) have a slightly neater way of dealing with such situations, known as case instructions or switch instructions, which allow for several different courses of action to be taken according to the value of a variable, and thus avoid the need for complex combinations of selection instructions.

27 Switch instructions For example, here is Java code that implements the selection instructions: switch (day) { case "M": System.out.println("Meat loaf today!"); break; case "T": System.out.println("Shepherd's pie today!"); case "W": System.out.println("Omelette today!"); case "Th": System.out.println("Chilli today!"); case "F": System.out.println("Fish and chips today!"); }

28 What is a good algorithm?
One possibility is to measure ‘goodness’ in terms of which algorithms are easiest to understand. If an algorithm is clear, it can be easier translated into a computer program and tested for correctness. A second idea to think about is how much effort it will take to carry out each algorithm. When we design an algorithm we expect to use it many times, and so we want the algorithm to be efficient and not require more effort and time the necessary. The first point is important: understandable algorithms result in more readable computer programs that are easier to maintain. The second point is not quite as straightforward to answer. It may not be immediately apparent that two algorithms performing the same task can involve different amounts of effort. The following simple example shows how the choice of an algorithm can make a lot of difference.

29 What is a good algorithm?
Example : Suppose 100 guests attended a fete. How many arithmetic operations would be required to calculate the total cost of cupcakes by Algorithm A Algorithm B? We can plot on a graph the number of arithmetic operations required by Algorithm A and Algorithm B with varying numbers of guests, as shown in Figure 5.

30 What is a good algorithm?
Note the following: For both algorithms, the number of operations increases along with the number of guests. Both algorithms produce straight lines on the graph between the number of operations and the number of guests (i.e. linear relationship). Algorithm A is inefficient since its line is steeper than that for Algorithm B, showing that the number of operations required for Algorithm A grows more rapidly as the number of guests increases. Figure 5

31 Swapping Swapping two items of any kind is a fundamental activity that we perform in the real world, as well as being a key part of many algorithms, including those used to sort data. Swapping is nothing more than exchanging positions. Let’s write a swapping algorithm. Specifically, you will write an algorithm that will swap the values of two variables. To make the process simpler, we will present two variables using two Post-it notes (small pieces of paper). We will label the first ‘item1’ and the second ‘item2’ as shown in Figure 6. Figure 6

32 Swapping Now, write ‘Bob’ on item1 and ‘Alice’ on item2, as shown in Figure 7. The two Post-it notes, item1 and item2, represent two variables. item1 has the value Bob, and item2’s value is Alice. We need to swap the values of item1 and item2. This can be done by the following algorithm: Set item2 to item1 Set item1 to item 2 Figure 7

33 Swapping Applying the algorithm results in item1 and item2 both have the value Bob as shown in Figure 8! How can you solve this? To solve the problem, we need a temporary variable (Post-it note ‘temp’) as shown in Figure 9. Figure 8 Figure 9

34 Swapping Copy the value in item2 to temp, as shown in Figure 10.
The new algorithm follows 3 steps in sequence: Copy the value in item2 to temp, as shown in Figure 10. Copy the value of item1 to item2, as shown in Figure 11. Copy the value of temp to item1, as shown in Figure 12. Figure 10 We can write down this sequence of instructions as an algorithm: Figure 11 Set temp to item2 Set item2 t o item1 Set item1 to temp Figure 12

35 Swapping This swap algorithm is a fundamental part of many more complex algorithms and can be found at the heart of almost all major systems such as: banking, stock markets and online shopping sites. It’s only 3 lines long, but it is a key part of the modern world. This algorithm is straightforward to implement in OUBuild, as in Figure 13. In Python, the code is as follows: temp = item2 item2 = item1 item1 = temp Figure 13

36  Sorting Sorting is an area where efficient algorithms are crucial since it is one of the most common purposes for computers. You may have used sorting to reorganise your s, or to rearrange data in a spreadsheet. Google sorts their results by usefulness. Sorting is found in: Spell-checking in word processors. Compressing data so that it uses less disk space. Prioritising tasks in the computers that control aircraft, industrial plants and power stations to ensure safety. Arranging on-screen objects from the most distant to the closest to ensure the graphics in 3D video games are drawn correctly.

37  What is sorting? Sorting algorithms take lists of unsorted data and turn them into lists of sorted data. Sorting simply is the process of putting similar items into order. For instance, we can sort: Numbers in ascending order (1, 2, 3 …) or descending order (… 3, 2, 1) Words alphabetically, in reverse alphabetical order, by length, frequency of use, etc. Events ordered by dates. Items in an online shop by price, popularity, etc. and so on

38 It appears to be unsorted
 Lists Here is a short list of names sorted in alphabetical order : Ahmad, Basil, Marwan, Saif, Ziyad A list is made up of items of data, each having a position in the list: the first item is at position 1, the second at position 2, and so on … A list can also be described in terms of its length: this list has length 5. Example: Here is a list of place names: Salalah, Istanbul, Jerusalem, Cairo, Petra, Byblos, Dubai. Is this list sorted? What is the length of the list? What is the item at position 6? What is the position of ‘Petra’ in the list? It appears to be unsorted 7 Byblos Position 5

39  Selection sort This algorithm repeatedly searches for the smallest or lowest item in a list of unsorted data and moves the item to the end of a list of sorted data. The algorithm repeats until all the data is sorted. Selection sort can be implemented using two separate lists: one for unsorted data and another for sorted. Such algorithms are called not-in-place sorts. But they are not often used in computer programs because there is a more elegant way to sort using a single list containing both sorted and unsorted portions. An algorithm that sorts items within the original list is known as an in-place sort.

40  Selection sort The algorithm for the in-place selection sort can be written as follows: Selection sort Repeat until (only l item of unsorted data remains ) Find t he lowest item in the unsorted data Swap the lo west item with the first item in the unsorted data How does it work? Search through the list and find the smallest element. Swap the smallest element with the first element. Repeat starting at second element and find the second smallest element.

41 Selection sort Example: 35 65 30 60 20 scan items 1 - 5, smallest 20
swap 35 and 20 scan items 2 - 5, smallest 30 swap 65 and 30 scan items 3 - 5, smallest 35 swap 65 and 35 scan items 4 - 5, smallest 60 swap 60 and 60 done

42 Bubble sort Like selection sort, the bubble sort algorithm is based on comparisons. Bubble sort works methodically through a list from beginning to end, comparing each pair of adjacent items. If the two items are in the wrong order, then they are swapped. The algorithm then moves one position along the list and compares the next pair, and so on. We can write the part of the algorithm that passes from the beginning to end of the list, swapping adjacent items if they are not in order, like this: Swapping pass Starting at the beginning of the list Repeat for (each pair of adjacent items ) If the items are in the wrong order then Swap them

43 Bubble sort Here are three lists sorted using the swapping pass algorithm we have developed so far. What characteristic do the last item in each of the sorted lists share? Answer: Our list is now partially sorted; after one swapping pass, the last item is always in the correctly sorted position. However, the remainder of the list needs further sorting. List after swapping pass List before swapping pass Ziyad, Basil, Ahmad, Marwan, Saif Marwan, Ziyad, Basil, Ahmad, Saif 4, 7, 22, 3, 100 22, 4, 7, 100, 3 Istanbul, Jerusalem, Cairo, Petra, Byblos, Dubai, Salalah Salalah, Istanbul, Jerusalem, Cairo, Petra, Byblos, Dubai.

44 Bubble sort algorithm More generally, a list containing n items will be sorted after n − 1 swapping passes through it. So we can create an algorithm to completely sort a list, by nesting the swapping pass inside a loop as follows: Bubble sort l Repeat (length of list – l) times Starting at the beginning of the list Repeat for (each pair of adjacent items ) If the items are in the wrong order then Swap them Swapping Pass

45 Bubble sort algorithm Example: Traverse a collection of elements
Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping 77 42 35 12 101 5

46 Bubble sort algorithm Traverse a collection of elements
Move from the front to the end. “Bubble” the largest value to the end using pair-wise comparisons and swapping. Swap 42 77 77 42 35 12 101 5

47 Bubble sort algorithm Traverse a collection of elements
Move from the front to the end. “Bubble” the largest value to the end using pair-wise comparisons and swapping. Swap 35 77 42 77 35 12 101 5

48 Bubble sort algorithm Traverse a collection of elements
Move from the front to the end. “Bubble” the largest value to the end using pair-wise comparisons and swapping. Swap 12 77 42 35 77 12 101 5

49 Bubble sort algorithm Traverse a collection of elements
Move from the front to the end. “Bubble” the largest value to the end using pair-wise comparisons and swapping. 42 35 12 77 101 5 No need to swap

50 Bubble sort algorithm Traverse a collection of elements
Move from the front to the end. “Bubble” the largest value to the end using pair-wise comparisons and swapping. Swap 5 101 42 35 12 77 101 5

51 Bubble sort algorithm Traverse a collection of elements
Move from the front to the end. “Bubble” the largest value to the end using pair-wise comparisons and swapping. 101 42 35 12 77 5 Largest value correctly placed

52 Bubble sort algorithm Notice that only the largest value is correctly placed All other values are still out of order. So we need to repeat this process If we have N elements and if each time we bubble an element, we place it in its correct location. Then we repeat the “bubble up” process N – 1 times. This guarantees we’ll correctly place all N elements. 101 42 35 12 77 5 Largest value correctly placed

53 “Bubbling” All the Elements
Bubble sort algorithm “Bubbling” All the Elements 77 12 35 42 5 101 N - 1 5 42 12 35 77 101 42 5 35 12 77 101 42 35 5 12 77 101 42 35 12 5 77 101

54 Merge sort Merge sort is a very popular way of sorting data, not only because it is much faster than bubble sort for most purposes, but also because it can be designed to work on parallel-processing computers in which each processor performs part of the sort. Table1 shows generally how much longer it takes to sort a list using bubble sort than using merge sort, assuming they are both using the same unsorted data and implemented on the same computer hardware. As you can see from the table, it is increasingly advantageous to use merge sort as the number of items to be sorted increases. If you have a hundred items to sort, merge sort, on average, sorts the items 50 times faster than bubble sort, whilst if you have a million items it is nearly 167 000 times faster!

55 Merge sort Whilst most of us do not need to sort such volumes of data, sorting millions, or even billions, of items is commonplace in large organisations. Table 1

56 Summary Having completed this part, you should be able to:
Describe what an algorithm is and provide everyday examples. Describe the fundamental building blocks of algorithms. Match parts of a small program in a language such as Python or Java to parts of an algorithm. Use visual aids such as pieces of paper to follow an algorithm. Learn different type of soring algorithms. Apply a sorting algorithm to a particular list.


Download ppt "Dr Radwan –Associate Professor"

Similar presentations


Ads by Google