410 likes | 630 Views
Chapter 5. Algorithms. 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. Algorithm: definition.
E N D
Chapter 5 Algorithms
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
Algorithm: definition • An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.
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
Pseudocode primitives • Assignment name expression • Conditional selection ifconditionthenaction • Repeated execution whileconditiondoactivity • Procedure procedurename (generic names)
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.
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.
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
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?
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.
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.
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)
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.
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!!
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:
Insertion sort algorithmIt 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-listFigure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically
Figure 5.11 The insertion sort algorithm expressed in pseudocode
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.
Figure 5.12 Applying our strategy to search a list for the entry John
Software efficiency • Measured as number of instructions executed • Q notation for efficiency classes • Example: insertion sort is Q(n2) • Best, worst, and average case
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)
Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm
Figure 5.20 Graph of the worst-case analysis of the binary search algorithm Q(logn) <Q(n)<Q(nlogn)< Q(n2)
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?
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
Figure 5.23 The assertions associated with a typical while structure