1 / 18

Introduction to Algorithms: Concepts and Efficiency

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.

rodrigueza
Télécharger la présentation

Introduction to Algorithms: Concepts and Efficiency

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch. 4 Algorithms • The concept of an algorithm. • Algorithm representation. • Algorithm discovery. • Iterative structures. • Recursive structures. • Efficiency and correctness.

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

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

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

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

  6. 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)

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

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

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

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

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

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

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

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

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

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

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

  18. Efficiency and Correctness • How to make sure the algorithm and program developed are correct? • Optimal cutting problem • Difference between testing and verification.

More Related