the logical structure of algorithms. algorithms steps necessary to complete a task or solve a...

37
The Logical Structure of Algorithms

Upload: stuart-briggs

Post on 12-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

The Logical Structure of Algorithms

Page 2: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Algorithms

• Steps necessary to complete a task or solve a problem.

• Example: Recipe for baking a cake– Will contain a list of ingredients– Step-by-step instructions on what to do with

those ingredients– A recipe provides an algorithm for baking a

cake.

Page 3: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Other algorithms

• When you learned the process for doing long division you learned an algorithm.

• The Holtrop and Mennen Algorithm, used by naval architects to design the optimum propellers for a ship, contains thousands of steps.

Page 4: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Algorithms are sequential

• Usually we can think of the instructions in an algorithm as being executed one at a time.

• They form what is sometimes called a sequential logic.

• This is only part of the story, however…• As a software developer you need to be

able to design, manipulate, and implement sequential algorithms

Page 5: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Common patterns

• There are a small set of patterns that exist in the design of sequential logic.

• These patterns fall into categories that form the elements of logical structure.

• They can be combined in myriad ways to form the logical structure of algorithms.

• A software developer familiar with the design patterns of logical structure can more easily create and modify software.

Page 6: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Plumbers

Page 7: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Plumbers

• In the design of the plumbing for a new building, architects have a selection of parts from which to build the system.– T-joints– Elbow joints– Many kinds of valves– Etc.

• Architects need to know how the individual parts work.

• And how they fit together.

Page 8: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Elements of logical structure

• There are only a handful of basic elements that a software developer need learn.

• In the 1960s Bohm and Jacopinini showed that algorithms are composed of three major structures:– Linear sequences– Branching routines– Loops

• Modern computer programming focuses on these three elements of logical structure.

Page 9: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Flowcharts

Page 10: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Flowcharts

• Bohm and Jacopini were not the first to use flowcharts but they were the first to formalize the process.

• They used a simple system, two symbols– Rectangles to show each step in an algorithm– Diamond-shaped boxes to show a decision step or

condititional.

• We will use one additional symbol, an oval, to mark the beginning and end of an algorithm.

Page 11: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Flowchart symbols

Page 12: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Terminators

• There should be only one terminator at the beginning of an algorithm and one at the end.

• Each algorithm should have one entry point (beginning) and one exit point (end).

Page 13: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Linear sequences

• The simplest element of logical structure is a linear sequence.

• One instruction follows another in a straight line.

• No branching

• No looping

Page 14: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Linear sequence

Page 15: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Linear Sequences

• Deceptively simple• Must meet the following criteria:

– Entry and exit conditions need to be clear:• What conditions need to exist before the sequence starts?• What can we expect the situation to be when the sequence is

finished?

– Must be complete; don’t leave out necessary steps.– Steps must be in proper order– Each individual instruction must be correct; if one step is wrong,

the entire algorithm is wrong.

• Example: driving directions”– What if you leave out a turn?– What if you say to turn left, when you should have said right?

Page 16: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

In short

• Linear sequences must…– Have clearly stated entry and exit conditions– Be complete– Be organized in the proper order

Page 17: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Selection Sequences

• Sometimes an algorithm reaches a point where the sequence of steps can go one direction or another (a fork in the road).

• This is called a selection sequence.

• Also called a conditional or branching routine

Page 18: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Selection Sequences

Consider a student who has a chemistry lab at 2pm on Fridays only:

Start

If (Today is Friday)

Then (Get to lab by 2pm)

Page 19: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Selection Sequence Example

Page 20: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

In Summary

• Selection sequence occurs whenever the path of sequential logic (steps) in an algorithm splits into two or more paths.

• Each path is called a branch.

Page 21: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Binary and Multiple Branching

• If there are two possible paths, then the routine is known as binary branching.

• If there are more than two paths, multiple branching.

• Binary branching: “Would you like vanilla ice cream?”

• Multiple branching: “What flavor of ice cream would you like?”

Page 22: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Writing multiple as binary

• It is possible to write a multiple branching routine as a collection of binary branching routines.

• Instead of “What flavor of ice cream would you like?” we can ask:– “Would you like vanilla ice cream?”– “Would you like chocolate ice cream?”– “Would you like strawberry ice cream?”– …for all 28 flavors.

Page 23: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Writing multiple as binary

• Alice does not have an instruction for multiple branching.

• Most programming languages do not.

• We write all selection sequences as collections of binary branching routines.

Page 24: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Binary Bypass vs. Binary Choice

• Two kinds of binary branching:– Binary bypass– Binary choice

• In binary bypass, an instruction is either executed or bypassed.

• In binary choice, one of two instructions is chosen.

Page 25: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Binary Bypass

Page 26: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Binary Choice

Page 27: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Binary Choice vs. Binary Bypass

• The difference is subtle but significant.

• In binary bypass, it is possible that nothing will occur.

• In binary choice, one of the two instructions, but not both will occur.

Page 28: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Binary Bypass

Page 29: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Binary Choice

Page 30: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Pseudocode

• Sometimes we use more formal language to describe algorithms - pseudocode.

• It looks something like the code in a computer programming language.

• But it’s only a tool to help understand algorithms.

Page 31: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

If/Then/Else

• In pseudocode, a binary bypass is equivalent to an If/Then instruction:

If (today is Friday)

Then (get to chemistry lab by 2pm)• A binary choice is equivalent to an If/Then/Else

instruction:

If (Today is Monday, or today is Wednesday, or today is Friday)

Then (go to math class)

Else (go to history class)

Page 32: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

If/Then/Else

• Basic form:

If (condition)

Then (one or more instructions)

Else (one or more instructions)• Must have a condition• Any number of instructions can be

executed along either branch.

Page 33: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Repetition Sequences - Looping

• A repetition sequence forms a loop in an algorithm.

• It forms a branch backward to a previous instruction

• A part of the algorithm is then repeated.

Page 34: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Repetition Flowchart

Page 35: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Explaining the pseudocode

• In this algorithm, the word While is used for looping instead of the word If that was used for branching.

• This tells the computer to loop back to the conditional expression when the block of code following the While is finished.

• Each time the condition is true the computer will execute the block of code

• When it is no longer true the block of code will be bypassed and the computer will move on to whatever comes next in the algorithm.

Page 36: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Repetition Flowchart

Page 37: The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain

Control Variables

• A variable holds a value that can change.

• This loop has a control variable as its condition.

• A control variable is a variable whose value controls whether or not a selection sequence will be executed.