1 / 23

Algorithm: definition

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

cricket
Download Presentation

Algorithm: definition

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. Algorithm: definition • An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

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

  3. Figure 5.2 Folding a bird from a square piece of paper

  4. Figure 5.3 Origami primitives

  5. Pseudocode primitives • Assignment name expression • Conditional selection ifconditionthenaction • Repeated execution whileconditiondoactivity repeat activity until condition • Procedure procedurename (generic names)

  6. Figure 5.8 The while loop structure

  7. Figure 5.9 The repeat loop structure

  8. Iterative Structures Pretest loop: while (condition) do (loop body) Posttest loop: repeat (loop body) until(condition)

  9. Figure 5.7 Components of repetitive control

  10. Figure 5.4 The procedure Greetings in pseudocode

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

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

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

  14. Figure 5.5

  15. Connect each of the nine dots using only four straight lines and without lifting your pen from the paper.

  16. Arrange the numbers 1 through 9 on a tic tac toe board such that the numbers in each row, column, and diagonal add up to 15.

  17. Mental Gymnastics What's the largest number of U.S. coins you can have without having even change for a dollar? A mountain goat attempts to scale a cliff sixty feet high. Every minute, the goat bounds upward three feet but slips back two. How long does it take for the goat to reach the top?

  18. Mental Gymnastics Three switches can be turned on or off. One is the light switch for the overhead light in the next room, which is initially off, but you don't know which. The other two switches do nothing. From the room with the switches in it, you can't see whether the light in the next room is turned on or off. You may flip the switches as often and as many times as you like, but once you enter the next room to check on the light, you must be able to say which switch controls the light without flipping the switches any further. (And you can't open the door without entering, either!) How can you determine which switch controls the light?

  19. Software verification • Proof of correctness • Assertions • Preconditions • Loop invariants • Testing

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

  21. Figure 5.21 Separating the chain using only three cuts

  22. Figure 5.22 Solving the problem with only one cut

  23. Is This Program Segment Correct? Precondition: X is a positive integer and Y is a positive even integer Z X/Y if (Remainder is positive) then (Print X is odd) else (Print X is even)

More Related