1 / 18

Algorithms

2.2. Algorithms. Overview. In this presentation we will discuss: What is an algorithm? 5 steps in developing an algorithm A Jeroo example. An algorithm is. A plan for solving a problem 5 steps in algorithm development Describe the problem clearly Analyze the problem

brigid
Télécharger la présentation

Algorithms

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

  2. Overview • In this presentation we will discuss: • What is an algorithm? • 5 steps in developing an algorithm • A Jeroo example

  3. An algorithm is... • A plan for solving a problem • 5 steps in algorithm development • Describe the problem clearly • Analyze the problem • Develop a high-level algorithm • Refine the algorithm by adding detail • Review the algorithm

  4. Step 1: describe the problem • Not as easy as it seems. • Common defects: • Description relies on unstated assumptions • Description is ambiguous • Description is incomplete • Description has contradictions • Natural spoken languages are not very precise. • It’s the developer’s job to spot any of these problems BEFORE any problem solving happens.

  5. Step 2 : Analyze the problem • Similar to a mathematician determining what factors and given and what must be proven in a complex set of equations. • Ask: • What data is available? • What formulas are needed? • What rules apply? • What relationships exist among the data values? • What determines a complete solution? (When am I done?)

  6. Step 3: Develop a high-level algorithm • An overview. Not detailed. • For example: • Step 1 : the problem • I need to send a birthday card to my brother, Mark. • Step 2: Analyze • I don’t have a card. I prefer to buy a card rather than make one myself. • Step 3: High Level Algorithm • Go to a store that sells cards • Select and purchase a card • Mail the card

  7. High-level algorithms • Go to a store that sells cards • Select and purchase a card • Mail the card • Lacks enough detail for a computer or robot to understand • Which store? • How will you get to the store? (bike, car, bus?) • What kind of card does Mark like? (funny, risqué, outdoorsy?)

  8. Step 4: Refine the algorithm by adding detail • Stepwise refinement= Keep adding levels of detail. • To know what level of detail is needed • You need to know how it will ultimately be implemented Birthday card example Who is going? Me? Very little detail needed Another family member? Minimal detail Someone unfamiliar with the area? Lots of detail about directions A robot? Minute detail.

  9. Stepwise refinement • The more complex the problem, the more steps it takes to refine it to sufficient detail. • High Level   detailed level • For very large problems you go through the process many times, developing intermediate level algorithms as you go.

  10. Step 5: Review • First: Work through the algorithm step by step and determine whether or not it actually solves the problem. • Then ask: • Does it solve a particular problem or a general one? Should it be generalized? • A program to find the area of a circle with radius = 5.2 could easily be generalized to find the area of any circle with the addition of a single variable. • Can it be simplified? • Is this solution similar to something already done?

  11. A Jeroo example • Step 1: the problem clearly defined. • A Jeroo starts at (0,0) facing East with no flowers in its pouch. There is a flower at location (0,3). The Jeroo should pick the flower and plant it at location (2,3) and then hop one square to the East. There are no other nets, flowers or Jeroos on the island.

  12. Analysis of the problem (step 2) • The flower is 3 spaces ahead of the Jeroo • The flower should be planted 2 spaces south of its current location • The Jeroo should end up at location (2,4) facing east • There are no obstacles to worry about NORTH Jeroo flower Start positions

  13. High-Level Algorithm (step 3) Create a Jeroo named Bob Bob should: Get the flower Plant the flower Hop East Start positions End positions

  14. Detailed Algorithm (step 4) • Create a Jeroo named Bob • Bob should: • Get the flower • Hop 3 times • Pick the flower • Plant the flower • Turn right • Hop 2 times • Plant a flower • Hop East • Turn left • Hop once

  15. Review (step 5) • The high-level algorithm created 3 distinct, easy sub-problems. This seems like a good technique. • This solves a very specific problem. • It actually works for any setup where the Jeroo starts anywhere and the flower is 3 spaces directly ahead of it.

  16. Good programming practices Detailed Algorithm: • Create a Jeroo named Bob • Bob should: • Get the flower • Hop 3 times • Pick the flower • Plant the flower • Turn right • Hop 2 times • Plant a flower • Hop East • Turn left • Hop once method main() { Jeroo bob = new Jeroo(); // ---- Get the flower --- // --- Plant the flower --- // --- Hop east --- } Comments clearly mirror the high-level algorithm

  17. Then add detail method main() { Jeroo bob = new Jeroo(); // ---- Get the flower --- bob.hop(3); bob.pick(); // --- Plant the flower --- bob.turn(RIGHT); bob.hop(2); bob.plant(); // --- Hop east --- //Etc. } • Detailed Algorithm: • Create a Jeroo named Bob • Bob should: • Get the flower • Hop 3 times • Pick the flower • Plant the flower • Turn right • Hop 2 times • Plant a flower • Hop East • Turn left • Hop once

  18. The End

More Related