1 / 19

Algorithms

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

lachelle
Download Presentation

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 • Examples: • An everyday example • A Jeroo example

  3. An algorithm is... • A plan for solving a problem • 5 steps in algorithm development • Describe the problem clearly We will cover each step in detail

  4. Step 1: describe the problem • Not as easy as it seems. • Common defects of problem descriptions: • relies on unstated assumptions • is ambiguous • is incomplete • 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 the given factors and what must be proven in a complex set of equations. • Ask lots of questions: • What information/resources are available? • What different environments mustthe solution work in? • What relationships exist between parts? • What determines a complete solution? (When am I done?)

  6. Example Problem • Step 1 : Describe the problem • I need to send a birthday card to my brother, Mark. • Step 2: Analyze (think about the problem) • I don’t have a card. I prefer to buy a card rather than make one myself.

  7. Step 3: Develop a high-level algorithm • High level algorithm = An overview. Not detailed. • Birthday Card example:I need to buy a card for my brother Mark • Step 3: High Level Algorithm (general solution) • Go to a store that sells cards • Select and purchase a card • Mail the card

  8. Step 3: 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?)

  9. Step 4: Add detail.Refine the algorithm by adding detail • Stepwise refinement= Keep adding levels of detail. • To know how far to go: • 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.

  10. Take some time Here is the high-level algorithm: • Go to a store that sells cards • Select and purchase a card • ** Mail the card ** • Add at least 4 detailed steps needed to accomplish the 3rd part of the high-level algorithm listed above:Mail the card • What needs to be done after you have purchased the card so that you can successfully mail the card?

  11. A Jeroo example • Step 1: the problem • A Jeroo should travel from a corner of an island to the middle. This problem is NOT clearly defined

  12. A Jeroo example • Step 1: the problem clearly defined. • 4 Jeroos start on Ricochet island at (2,0,EAST); (0,19,SOUTH); (21,23,WEST); (23,4,NORTH);with no flowers. • Each Jeroo must do 4 things: • leave the room (it's dark, follow the wall on your right until you get to an opening, then turn right) • keep going until you find a flower and pick it • turn left and keep going until you find a net and disable it • turn right and then keep going until you find the flower field and you're ready for breakfast. • There are no other nets, or water traps on the island.

  13. Step 2 : Analyze the problem • It only needs to work on 1 island: Ricochet island • Each Jeroo is already facing the right direction when it is instantiated • Every Jeroo can follow the same set of directions as long as it uses loops • To keep the program short, divideeach part into methods

  14. Using the Ricochet Problem: • If you don’t understand how the program is supposed to work, how can you solve it? • Analyze where there are loops and turns • The problem can be solved in 4 parts: • Leave Room • getFlower • toNet • enterField • To understand the problem trace the path of the Jeroos across the island to see where they need to go.

  15. Jeroo Example, Step 3 This is the high level algorithm • There are 4 things to do: • Leave the room • Find and pick a flower • Find and disable a net • Enter the flower field • method getBreakfast() • { • // Leave the room • //Find and pick a flower • //Find and disable a net • //Enter the flower field • } Simplify the program By putting them in a method

  16. Good programming practices Detailed Algorithm: ( part 1) • 1. Leave the Room • follow the water on your right until you get to an opening • then turn right while( isWater(RIGHT) ) { hop(); } turn(RIGHT); • method getBreakfast() • { • // Leave the room • //Find and pick a flower • //Find and disable a net • //Enter the flower field • } Comments, or method names, clearly mirror the high-level algorithm

  17. Take some time for Step 4 Here is the high-level algorithm: • Leave the room • Find and pick a flower • ** Find and disable a net ** • Enter the flower field • Add at least 3 detailed steps needed to accomplish the 3rd part of the high-level algorithm listed above:Find and disable a net • What needs to be done after you pick the flower to be able to successfully find and disable a net on the way to the flower field?

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

  19. The End

More Related