190 likes | 202 Views
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.
E N D
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 We will cover each step in detail
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.
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?)
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.
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
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?)
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.
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?
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
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.
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
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.
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
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
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?
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?