Download presentation

Presentation is loading. Please wait.

Published byJuliana Hardy Modified over 2 years ago

1
Chapter 5 Algorithms

2
2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness

3
3 Algorithm: definition An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

4
4 Algorithms: levels of abstraction Problem = motivation for algorithm Algorithm = procedure to solve the problem Often one of many possibilities Representation = description of algorithm sufficient to communicate it to the desired audience Always one of many possibilities

5
5 Figure 5.2 Folding a bird from a square piece of paper

6
6 Figure 5.3 Origami primitives

7
7 Pseudocode primitives Assignment name expression Conditional selection if condition then action Repeated execution while condition do activity Procedure procedure name (generic names)

8
8 Figure 5.4 The procedure Greetings in pseudocode

9
9 Problem solving steps 1. Understand the problem. 2. Get an idea how an algorithmic procedure might solve the problem. 3. Formulate the algorithm and represent it as a program. 4. Evaluate the program for accuracy and its potential as a tool for solving other problems.

10
10 Problem solving steps To solve a problem, one must take the initiative and lead to go through the stepwise refinement of attempted solutions toward a true understanding of a problem. An incubation period is the time span between conscious work on a problem and sudden (mysterious) inspiration– “Aha!”experience.

11
11 Techniques for “getting a foot in the door” Work the problem backwards Solve an easier related problem Relax some of the problem constraints Solve pieces of the problem first = bottom up methodology Stepwise refinement = top-down methodology Popular technique because it produces modular programs

12
12 Sample problem Person A is charged with the task of determining the ages of B ’ s three children. B tells A that the product of the children ’ s ages is 36. A replies that another clue is required. B tells A the sum of the children ’ s ages. A replies that another clue is needed. B tells A that the oldest child plays the piano. A tells B the ages of the three children. How old are the three children?

13
13 Figure 5.5

14
14 Iterative Structure Iterative structures --repeat collections of instructions in a looping manner. In contrast, recursive structures recur the same block of codes with the finer scope in a convergent fashion. Iterative structures can be converted into recursive structures, and vice verse.

15
15 Figure 5.7 Components of repetitive control

16
16 Iterative Structure There are four kinds of code blocks in iterative structures: Initialize: establish an initial state to be modified toward the termination condition. Test: compare the current state if the termination condition is reached. Statement: is the same block simply repeated in each iteration. Modify: change the state toward the termination condition.

17
17 Flow Chart Conditional loop: while (condition) do(activity) While there are tickets to sell, keep selling tickets. while (tickets remain to be sold) do(sell a ticket)

18
18 Iterative Structure For-loop: for( initialize; test; modify ) { statement; } While-loop: initialize; while( test ) { statement; modify; } Repeat-loop: initialize; repeat ( statement; modify; ) until ( test ) For-loop is similar to while-loop; repeat-loop uses the negation of their test condition to terminate the loop. Iterative structures are used to avoid writing the instructions several times explicitly (tediously). Several classical search algorithms are popular exemplars.

19
19 Flow Chart Conditional branch: if (condition) then(activity 1) else(activity 2) Divide the total by 366 or 365 dependent on the year is a leap year or not. if (year is leap year) then(divide total by 366) else(divide total by 365) if (year is leap year) then(divide total by 366) else(divide total by 365) ⇐ Indentation and parentheses for readability!!

20
20 Figure 5.8 The while loop structure

21
21 Figure 5.9 The repeat loop structure

22
22 Flow Chart It is used to find the particular one from some sorted list. Sorting algorithms can sort the list in an ascending or descending order.The search terminates as a success if the target is found, or a failure if none is found at the end of the list.E.g., search a guest list of perhaps 20 entries for a particular name:

23
23 Figure 5.6 The sequential search algorithm in pseudocode

24
24 Insertion sort algorithm It sorts a list by repeatedly removing an entry and inserting it into its proper place. Taking the first one from the unsorted sub-list to the sorted sub-list Figure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically

25
25 Figure 5.11 The insertion sort algorithm expressed in pseudocode

26
26 Recursive Structure Recursive structures provide an alternative to the loop paradigm for repetitive structures (by invoking itself). The execution of a recursive algorithm creates multiple instances (children) of itself, called activations, which are born to conquer revised smaller problems and return the results back to the calling parent. Only one instance is actively progressing. Like those in loop control, a recursive routine has the base or degenerate case to test for the termination condition. If met, the current activation returns to resume dormant parent activation.

27
27 Figure 5.12 Applying our strategy to search a list for the entry John

28
28 Figure 5.13 A first draft of the binary search technique

29
29 Figure 5.14 The binary search algorithm in pseudocode

30
30 Figure 5.15(Serach for Bill)

31
31 Figure 5.16(Search for David)

32
32 Figure 5.17

33
33 Software efficiency Measured as number of instructions executed notation for efficiency classes Example: insertion sort is (n 2 ) Best, worst, and average case

34
34 Figure 5.18 Applying the insertion sort in a worst-case situation The worst case occurs when the original list is in reverse Order such that 1+2+3+4+..+N-1=O(N(N-1)/2)=>O(N^2) The average case:each pivot compares to half of the entries Preceding of it. O(N(N-1)/4)

35
35 Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm

36
36 Figure 5.20 Graph of the worst-case analysis of the binary search algorithm (logn) < (n)< (nlogn)< (n 2 )

37
37 Example problem: Chain separating A traveler has a gold chain of seven links. He must stay at an isolated hotel for seven nights. The rent each night consists of one link from the chain. What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance?

38
38 Figure 5.21 Separating the chain using only three cuts

39
39 Figure 5.22 Solving the problem with only one cut

40
40 Software verification Proof of correctness Preconditions are the conditions that are satisfied at the beginning of the program ’ s execution. Assertions are the statements that are established at various points in the program as the consequences of preconditions. The assertion built at the end of the program corresponds to the desired output specifications. Loop invariants are the assertions during the repetitive process. They ensure the modification components operate correctly. IC designs (hardware) have similar endeavors of developing a generic set of test cases. Testing

41
41 Figure 5.23 The assertions associated with a typical while structure

Similar presentations

Presentation is loading. Please wait....

OK

Chapter 8 Algorithms.

Chapter 8 Algorithms.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on bloodstain pattern analysis Ppt on ip address classes table Ppt on formal education crossword Ppt on world book day costumes Ppt on speed control of dc motor using pwm Ppt on carbon and its compounds class Ppt on object-oriented programming languages Ppt on plasma arc welding Ppt on monopolistic business model Ppt on positive thinking