Transcript Slide 1

Discrete Structures

Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms

Begin at the beginning…and go on till you come to the end: then stop. – Lewis Carroll, 1832 – 1898

Alice’s Adventures in Wonderland, 1865

4.8 Application: Algorithms 1

Definitions

Variable

In higher-level computer languages, the term

variable

is used to refer to a specific storage location in a computer’s memory.

Data Type

The

data type

takes its values.

of a variable indicates the set in which the variables •

Assignment Statement

An

assignment statement

x

: =

e

where

x

gives a value to the variable in the form is the variable and

e

is the expression.

4.8 Application: Algorithms 2

Conditional Statements

Ordinarily, algorithm statements are executed one after another in the order in which they are written. Conditional statements allow this natural order to be overridden by using the current values of program variables to determine which algorithm statement will be executed next.

4.8 Application: Algorithms 3

Conditional Statements

Conditional statements are denoted in one of two ways.

1. If

(

condition

)

2. If

then s 1 else s 2 (

condition

) then s 1 Notice that we use indentation to indicate that the statements belong together. We can also bind statements by

do

and ending with

end do

.

4.8 Application: Algorithms 4

Execution of if-then-else statements

1. The

condition

is evaluated by substituting the current values of all algorithm values appearing in it and evaluating the truth or falsity of the resulting statement.

2.

If the

condition

is true, then

s

1 is executed and execution moves to the next algorithm statement following the

if-then else

statement.

3.

If the

condition

is false, then

s

2 is executed and execution moves to the next algorithm statement following the

if-then else

statement.

Note: Sometimes

condition

before

s

1 and

s

2 is called

guard

because it is stationed and restricts access to them. 4.8 Application: Algorithms 5

Examples – pg. 225 # 1 & 2

• Find the value of

z

when each of the algorithm segments is executed.

1.

i

:= 2

if

(

i

> 3 or

i

 0) then z := 1 else z := 0 2.

i

if

:= 3 (

i

 3 or

i

> 6) then z := 2 else z := 0 4.8 Application: Algorithms 6

Iterative Statements

Iterative statements

are used when a sequence of algorithm statements is to be executed over and over again. We use two types of iterative statements:

while

loops and

for-next

loops.

4.8 Application: Algorithms 7

While Loop

A

while

loop has the form

while

(

condition

) [

statements that make up the body of the loop

]

end while

4.8 Application: Algorithms 8

Execution of while Loop

1. The

condition

is evaluated by substituting the current values of all algorithm variables and evaluating the truth or falsity of the resulting statement.

2. If the

condition

is true, all statements in the body of the loop are executed in order. Then execution moves back to the beginning of the loop and the process repeats.

3. If the

condition

is false, execution passes to the next algorithm statement following the loop.

Note: Each execution of the body of the loop is called an iteration of the loop. 4.8 Application: Algorithms 9

Example - While Loop

i

:= 10

while

(

i

> 0) display

i i

:=

i

– 1

end while

display “Blast Off” 4.8 Application: Algorithms 10

for-next Loop

A

for-next

loop has the form for variable := initial expression to final expression [

statements that make up the body of the loop

]

next

(

same

)

variable

4.8 Application: Algorithms 11

Execution of for-next Loop

1. The

for-next

loop

variable initial expression

.

is set equal to the value of the 2.

A check is made to determine whether the value of variables is less than or equal to the value of the

final expression

.

3.

If the value of the

variable

is less than or equal to the value of the

final

expression, then the statements in the body of the loop are executed in order,

variable

is increased by 1, and execution returns back to step 2.

4.

If the value of the

variable

is greater than the value of the

final

expression, then execution passes to the next algorithm statement following the loop.

4.8 Application: Algorithms 12

Example – pg 225 # 4

Find the values of

a

after execution of the loop.

a

:= 2 for i := 1

to

2

a

:=

a

/2 + 1/

a

next i 4.8 Application: Algorithms 13

Algorithm Notation

We generally include the following information when describing algorithms.

1. The name of the algorithm, together with a list of input and output variables.

2. A brief description of how the algorithm works.

3. The input variable names, labeled by data type (integer, real number, etc.).

4. The statements that make up the body of the algorithm, possibly with explanatory comments.

5. The output variable names, labeled by data type.

4.8 Application: Algorithms 14

Algorithm 4.8.1 – Division Algorithm

4.8 Application: Algorithms 15

Example – pg 225 # 6

Make a trace table to trace the action of Algorithm 4.8.1 for the given input values.

6.

a

= 26

d

= 7 4.8 Application: Algorithms 16

Definition – Greatest Common Divisor

Let

a

and

b

be integers that are not both zero. The

greatest common divisor

of

a

and

b

, denoted

gcd(a, b)

, is that integer

d

with the following properties: 1.

d

is a common divisor of both

a

and

b

. In other words,

d

|

a

and

d

|

b

.

2. For all integers

c

, if

c

is a common divisor of both

a

and

b

, then

c

is less than or equal to

d

. In other words, for all integers

c

, if

c

|

a

and

c

|

b

, then

c

d

.

4.8 Application: Algorithms 17

Lemmas

Lemma 4.8.1

If

r

is a positive integer, then gcd(

r

, 0) =

r

.

Lemma 4.8.2

If

a

and

b

are any integers not both zero, and if

q

and

r

are any integers s.t.

a

=

bq

+

r

, then gcd(

a

,

b

) = gcd(

b

,

r

).

4.8 Application: Algorithms 18

Algorithm 4.8.2 – Euclidean Algorithm

4.8 Application: Algorithms 19

Example – pg 225 # 17

Make a trace table to trace the action of Algorithm 4.8.2 for the given input values.

17. 1,001 and 871 4.8 Application: Algorithms 20