Presentation is loading. Please wait.

Presentation is loading. Please wait.

11-Jun-16 Algorithms 2.2. Overview In this presentation we will discuss: What is an algorithm? 5 steps in developing an algorithm A Jeroo example.

Similar presentations


Presentation on theme: "11-Jun-16 Algorithms 2.2. Overview In this presentation we will discuss: What is an algorithm? 5 steps in developing an algorithm A Jeroo example."— Presentation transcript:

1 11-Jun-16 Algorithms 2.2

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 1. Describe the problem clearly 2. Analyze the problem 3. Develop a high-level algorithm 4. Refine the algorithm by adding detail 5. 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 positionsEnd 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 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 --- bob.hop(3); bob.pick(); // --- Plant the flower --- bob.turn(RIGHT); bob.hop(2); bob.plant(); // --- Hop east --- //Etc. }

18 The End


Download ppt "11-Jun-16 Algorithms 2.2. Overview In this presentation we will discuss: What is an algorithm? 5 steps in developing an algorithm A Jeroo example."

Similar presentations


Ads by Google