180 likes | 191 Views
Learn the basics of algorithms, including their representation, discovery, iterative and recursive structures, and the importance of efficiency and correctness. Explore various examples and methodologies for problem-solving. Suitable for beginners.
E N D
Ch. 4 Algorithms • The concept of an algorithm. • Algorithm representation. • Algorithm discovery. • Iterative structures. • Recursive structures. • Efficiency and correctness.
The Concept of an Algorithm • An algorithm is an ordered set of unambiguous, executable steps, defining a terminating process. • Program Vs. algorithm Vs. process. • Parallel algorithms.
Algorithm Representation • Visiting grandchildren can be nerve-racking. • A well-defined set of building blocks from which algorithm representations can be constructed. • Primitives • syntax: symbolic representation • semantics: concept represented
Algorithm Representation • A collection of primitives along with a collection of rules stating how the primitives can be combined to represent more complex ideas constitutes a programming language. • Pseudocode: a notational system in which ideas can be expressed informally during the algorithm development process.
Algorithm Representation • If you have more than $10, buy a cake; otherwise, buy nothing => if (cond) then (act1) else (act2) • As long as there are tickets to sell, continue selling tickets => while (condition) do (activity) • Assign name the value expression • Assign Total the value Price + Tax
Algorithm Representation • Begin a pseudocode with procedurename. • The pseudocode for Greetings: procedure Greetings assign Count the value 3; while (Count > 0) do (print the message “Hello” and assign Count the value Count - 1) • if (…) then (Execute Greetings) • procedure Sort (List)
Algorithm Discovery • The development of a program consists of two activities - discovering the underlying algorithm and representing that algorithm as a program. • The basic principles for problem-solving: • 1. Understand the problem. • 2. Get an idea as to how an algorithmic procedure might solve the problem.
Algorithm Discovery • 3. Formulate the algorithm and represent it as a program. • 4. Evaluate the program for accuracy and for its potential as a tool for solving other problems. • The problem of determining the ages of three children. • Getting a foot in the door • Conscious work Vs. inspiration.
Algorithm Discovery • Solving the problem backward. • Look for an easier or solved related problem. • Stepwise refinement - a top-down methodology. • Modular structure/Team programming • To train a potential problem solver to follow certain methodologies is to quash those creative skills that should instead be nurtured.
Iterative Structures • Iterative structures - a collection of instructions is repeated in a looping manner. • The sequential search algorithm. • Figure 4.6 • Loop body and loop control • Loop control. • The control of a loop consists of the three activities initialize, test, and modify
Iterative Structures • Test for satisfying the termination condition • the termination condition is the negation of the condition appearing in the while structure • Figure 4.6 • Initialize a starting condition, and modify the condition toward the termination condition • Figure 4.6 • must lead to an appropriate termination condition (example on page 189) • Two popular loop structures. • Figures 4.8 and 4.9
Iterative Structures • The insertion sort algorithm. • Figure 4.10 • Figure 4.11 • The termination condition and the initialization and modification components of the outer loop’s control • The termination condition and the initialization and modification components of the inner loop’s control
Recursive Structures • Recursive structures provide an alternative to the loop paradigm for repetitive structures. • The binary search algorithm. • divide-and-conquer • Figures 4.12 - 4.15 • Recursion • repeating subtasks as activations • termination (degenerative case), initialization, modification
Efficiency and Correctness • You can develop a variety of algorithms to solve the same problem. However, the choice between efficient and inefficient algorithms can make the difference between a practical solution to a problem and an impractical one. • Time and storage complexity of the algorithm.
Efficiency and Correctness • Search a students record in 30000 student records. • Sequential search • average number of searches: 15000 • 2.5 minutes if each search spends 10ms • 15 seconds if each search spends 1ms • Binary search • at most 15 searches • 0.15 second if each search spends 10ms
Efficiency and Correctness • Best-case/worst-case/average-case. • Insertion sort • best-case: n-1 comparisons • worst-case: 1+2+…+(n-1)=n(n-1)/2 (Figure 4.18) • average-case: n(n-1)/4
Efficiency and Correctness • How well an algorithm performs for large inputs? • notation • worst-case • (log n) < (n) < (n log n) < (n2) • Figures 4.19 and 4.20
Efficiency and Correctness • How to make sure the algorithm and program developed are correct? • Optimal cutting problem • Difference between testing and verification.