You are on page 1of 18

Algorithms

17-Jul-18
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


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
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.
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?)
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
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: 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.
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.
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?
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.
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
Start positions flower
High-Level Algorithm (step 3)

Start positions End positions

Create a Jeroo named Bob


Bob should:
Get the flower
Plant the flower
Hop East
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
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.
Good programming practices
Detailed Algorithm:
method main()
 Create a Jeroo named Bob
{
 Bob should:
Jeroo bob = new Jeroo();
 Get the flower
 Hop 3 times // ---- Get the flower ---
 Pick the flower // --- Plant the flower ---
 Plant the flower
// --- Hop east ---
 Turn right
 Hop 2 times }
 Plant a flower
 Hop East
 Turn left Comments clearly mirror
 Hop once the high-level algorithm
Then add detail
method main()
 Detailed Algorithm: {
 Create a Jeroo named Bob Jeroo bob = new Jeroo();
 Bob should: // ---- Get the flower ---
 Get the flower bob.hop(3);
 Hop 3 times
bob.pick();
 Pick the flower
 Plant the flower // --- Plant the flower ---
 Turn right bob.turn(RIGHT);
 Hop 2 times bob.hop(2);
 Plant a flower
bob.plant();
 Hop East
// --- Hop east ---
 Turn left
 Hop once //Etc.
}
The End

You might also like