algorithm: definition

23
Algorithm: definition • An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

Upload: cricket

Post on 11-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Algorithm: definition. An algorithm is an ordered set of unambiguous , executable steps that defines a terminating process. Algorithms: levels of abstraction. Problem = motivation for algorithm Algorithm = procedure to solve the problem Often one of many possibilities - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Algorithm: definition

Algorithm: definition

• An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

Page 2: Algorithm: definition

Algorithms: levels of abstraction

• Problem = motivation for algorithm

• Algorithm = procedure to solve the problem– Often one of many possibilities

• Representation = description of algorithm sufficient to communicate it to the desired audience– Always one of many possibilities

Page 3: Algorithm: definition

Figure 5.2 Folding a bird from a square piece of paper

Page 4: Algorithm: definition

Figure 5.3 Origami primitives

Page 5: Algorithm: definition

Pseudocode primitives

• Assignment name expression

• Conditional selection if condition then action

• Repeated execution while condition do activity

repeat activity until condition

• Procedure procedure name (generic names)

Page 6: Algorithm: definition

Figure 5.8 The while loop structure

Page 7: Algorithm: definition

Figure 5.9 The repeat loop structure

Page 8: Algorithm: definition

Iterative Structures

Pretest loop:

while (condition) do

(loop body)

Posttest loop:

repeat (loop body)

until(condition)

Page 9: Algorithm: definition

Figure 5.7 Components of repetitive control

Page 10: Algorithm: definition

Figure 5.4 The procedure Greetings in pseudocode

Page 11: Algorithm: definition

Problem solving steps

• 1. Understand the problem.

• 2. Get an idea how an algorithmic procedure might solve the problem.

• 3. Formulate the algorithm and represent it as a program.

• 4. Evaluate the program for accuracy and its potential as a tool for solving other problems.

Page 12: Algorithm: definition

Techniques for “getting a foot in the door”

• Work the problem backwards• Solve an easier related problem

– Relax some of the problem constraints– Solve pieces of the problem first = bottom up

methodology

• Stepwise refinement = top-down methodology– Popular technique because it produces modular

programs

Page 13: Algorithm: definition

Sample problem

• Person A is charged with the task of determining the ages of B’s three children.

• B tells A that the product of the children’s ages is 36.• A replies that another clue is required.• B tells A the sum of the children’s ages.• A replies that another clue is needed.• B tells A that the oldest child plays the piano.• A tells B the ages of the three children.• How old are the three children?

Page 14: Algorithm: definition

Figure 5.5

Page 15: Algorithm: definition

Connect each of the nine dots using only four straight lines and without

lifting your pen from the paper.

Page 16: Algorithm: definition

Arrange the numbers 1 through 9 on a tic tac toe board such that the numbers in each row, column,

and diagonal add up to 15.

Page 17: Algorithm: definition

What's the largest number of U.S. coins you can have without having even change for a dollar?

Mental Gymnastics

A mountain goat attempts to scale a cliff sixty feet high. Every minute, the goat bounds upward three feet but slips back two. How long does it take for the goat to reach the

top?

Page 18: Algorithm: definition

Mental Gymnastics

Three switches can be turned on or off. One is the light switch for the overhead light in the next room, which is initially off, but you don't know which. The other two switches do nothing. From the room with the switches in it, you can't see whether the light in the next room is turned on or off. You may flip the switches as often and as many times as you like, but once you enter the next room to check on the light, you must be able to say which switch controls the light without flipping the switches any further. (And you can't open the door without entering, either!) How can you determine which switch controls the light?

Page 19: Algorithm: definition

Software verification

• Proof of correctness– Assertions

• Preconditions

• Loop invariants

• Testing

Page 20: Algorithm: definition

Example problem: Chain separating

• A traveler has a gold chain of seven links.• He must stay at an isolated hotel for seven nights.• The rent each night consists of one link from the chain.• What is the fewest number of links that must be cut so

that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance?

Page 21: Algorithm: definition

Figure 5.21 Separating the chain using only three cuts

Page 22: Algorithm: definition

Figure 5.22 Solving the problem with only one cut

Page 23: Algorithm: definition

Is This Program Segment Correct?

Precondition: X is a positive integer and Y is a positive even integer

Z X/Y

if (Remainder is positive)

then (Print X is odd)

else (Print X is even)