Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Algoritmos e Estruturas de Dados: Teoremas e Exercícios, Notas de estudo de Algoritmos

Vários exercícios relacionados a teorias de algoritmos e estruturas de dados, incluindo a comprovação de teoremas e a implementação de algoritmos como sumofsquares, minimumint e integerfactorization. O documento também aborda conceitos como booleanos, while-loops e grafos.

Tipologia: Notas de estudo

Antes de 2010

Compartilhado em 01/11/2010

danilo-dias-5
danilo-dias-5 🇧🇷

5

(1)

5 documentos

1 / 94

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Algorithmic Mathematics
a web-book by Leonard Soicher & Franco Vivaldi
This is the textbook for the course MAS202 Algorithmic Mathematics. This material is
in a fluid state —it is rapidly evolving— and as such more suitable for on-line use than
printing. If you find errors, please send an e-mail to: [email protected].
Last updated: January 8, 2004
c
The University of London.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e

Pré-visualização parcial do texto

Baixe Algoritmos e Estruturas de Dados: Teoremas e Exercícios e outras Notas de estudo em PDF para Algoritmos, somente na Docsity!

Algorithmic Mathematics

a web-book by Leonard Soicher & Franco Vivaldi

This is the textbook for the course MAS202 Algorithmic Mathematics. This material is in a fluid state —it is rapidly evolving— and as such more suitable for on-line use than printing. If you find errors, please send an e-mail to: [email protected].

Last updated: January 8, 2004 ©c The University of London.

ii

iv

Contents

Chapter 1

Basics

Informally, an algorithm is a finite sequence of unambiguous instructions to perform a specific task. In this course, algorithms are introduced to solve problems in discrete mathematics. An algorithm has a name, begins with a precisely specified input, and terminates with a precisely specified output. Input and output are finite sequences of mathematical objects. An algorithm is said to be correct if given input as described in the input specifications: (i) the algorithm terminates in a finite time; (ii) on termination the algorithm returns output as described in the output specifications.

Example 1.1.

Algorithm SumOfSquares INPUT: a, b, ∈ Z OUTPUT: c, where c = a^2 + b^2. c := a^2 + b^2 ; return c; end;

The name of this algorithm is SumOfSquares. Its input and output are integer sequences of length 2 and 1, respectively. In this course all algorithms are functions, whereby the output follows from the in- put through a finite sequence of deterministic steps; that is, the outcome of each step depends only on the outcome of the previous steps. In the example above, the domain of SumOfSquares is the the set of integer pairs, the co-domain is the set of non-negative inte- gers, and the value of SumOfSquares(− 2 , −3) is 13. This function is clearly non-injective; its value at (a, b) is the same as that at (−a, −b), or (b, a), etc. It also happens to be non-surjective (see exercises). (Algorithms do not necessarily represent functions. The instruction: ‘Toss a coin; if the outcome is head, add 1 to x, otherwise, do nothing’ is legitimate and unambiguous, but not deterministic. The output of an algorithm containing such instruction is not a function of the input alone. Algorithms of this kind are called probabilistic.)

2 CHAPTER 1. BASICS

It is expedient to regard the flow from input to output as being parametrized by time. This viewpoint guides the intuition, and even when estimating run time is not a concern, time considerations always lurk in the background (will the algorithm terminate? If so, how many operations will it perform?).

1.1 The language of algorithms

The general form of an algorithm is the following Algorithm 〈 algorithm name 〉 INPUT: 〈 input specification 〉 OUTPUT: 〈 output specification 〉 〈 statement 〉; 〈 statement 〉; .. . 〈 statement 〉; end;

(A quantity in angle brackets defines the type of an object, rather than the object itself.)

The heart of the algorithm is its statement sequence, which is implemented by a language. The basic elements of any algorithmic language are surprisingly few, and use a very standard syntax. We introduce them in the next sections.

1.1.1 Expressions

In this course, expressions are the data processed by an algorithm. We do not require a precise definition of what we regard to be a valid expression, since we shall consider only expressions of very basic type. We begin with arithmetical and algebraic expressions, which are formed by assem- bling in familiar ways numbers and arithmetical operators. Algebraic expressions differ from arithmetical ones in that they contain indeterminates or variables. All expressions considered here will be finite, e.g.,

1 +

(x − y)(x + y)(x^2 + y^2 )(x^4 + y^4 ) x^8 − y^8

By combining expressions, we can construct composite expressions representing sequences, sets, etc.

(x − 1 , x + 1, x^2 + x + 1, x^2 + 1)

4 CHAPTER 1. BASICS

i := 3; i := (i^4 + 10)/13; n := 4 − i; S := (i, |n|); n := 3n + i; i := i + n; S := (|i|, S);

i n S 3 7 − 3 (7, 3) − 2 5 (5, (7, 3))

Persuade yourself that re-arranging the columns may change the number of rows. Before a variable can be used in an expression it must have a value, which is either assigned on input or by an assignment statement. Assigning a value on input works as follows. Suppose we have Algorithm A INPUT: 〈 a 1 ,... , ak, and their properties 〉 OUTPUT: 〈 output specification 〉 〈 statement sequence 〉 end;

Then a 1 ,... , ak are variables for algorithm A. When A is executed, it must be given an input sequence v 1 ,... , vk of values. Then a 1 is assigned the value v 1 , a 2 is assigned the value v 2 , etc.,.. ., ak is assigned the value vk, before the statement sequence of A is executed (bearing in mind that the values assigned may be indeterminates). Thus, assigning values on input is analogous to evaluating A, as a function, at those values; this process could be replaced by a sequence of assignment statements at the beginning of the algorithm. This would be, of course, very inefficient, being equivalent to defining the function at a single point.

1.1.3 Return statement

The return statement produces an output sequence. SYNTAX: return 〈 expression 1 〉,... ,〈 expression k 〉 EXECUTION: first expression 1,.. ., expression k are evaluated, obtaining values v 1 ,.. ., vk, respectively. Then the sequence (v 1 ,... , vk) is returned as the output of the algorithm, and the algorithm is terminated. We treat an output sequence (v 1 ) of length 1 as a simple value v 1.

1.1. THE LANGUAGE OF ALGORITHMS 5

Example 1.3. The expressions i := 5; return 32 , i + 5, 27;

returns the sequence (9, 10 , 27) as output.

1.1.4 If-structure

A boolean constant or a boolean value, is an element of the set {TRUE, FALSE} (often abbreviated to {T, F }). A boolean (or logical ) expression is an expression that evaluates to a boolean value. We postpone a detailed description of boolean expressions until section 1.2. For the moment we consider expressions whose evaluation involves testing a single (in)equality, called relational expressions. For instance, the boolean value of

103 < 210 , 93 + 10^3 6 = 1^3 + 12^3

is TRUE and FALSE, respectively. Evaluating more complex expressions such as

2 13 ,^466 ,^917 − 1 is prime, 2 (^

− 1 is prime (1.1)

can be reduced to evaluating finitely many relational expressions, although this may involve formidable difficulties. Thus several weeks of computer time were required to prove that the leftmost expression above is TRUE, giving the largest prime known to date. This followed nearly two and half years of computations on tens of thousands of computers, to test well over 100,000 unsuccessful candidates for the exponent. By contrast, the value of the rightmost boolean expression is not known, and may never be known. The if-structure makes use of a boolean value to implement decision-making in an algorithmic language. It is defined as follows: SYNTAX: if 〈 boolean expression 〉 then 〈 statement-sequence 1 〉 else 〈 statement-sequence 2 〉 fi;

EXECUTION: if the value of the boolean expression is TRUE, the statement-sequence 1 is executed, and not statement-sequence 2. If the boolean expression evaluates to FALSE, then statement-sequence 2 is executed, and not statement-sequence 1. The boolean expression that controls the if-structure is called the if control expression.

Example 1.4.

1.1. THE LANGUAGE OF ALGORITHMS 7

is Z, and the value of MinimumInt(− 2 , 12) is −2.

1.1.5 While-loops

A loop is the structure that implements repetition. Loops come in various guises, the most basic of which is the while-loop. SYNTAX:

while 〈 boolean-expression 〉 do 〈 statement sequence 〉 od;

EXECUTION:

(i) Evaluate the boolean expression.

(ii) If the boolean expression evaluates to TRUE, then execute the statement sequence, and repeat from step (i). If the boolean expression is FALSE, do not execute the statement sequence but terminate the while-loop and continue the execution of the algorithm after the “od”.

The boolean expression that controls the loop is called the loop control expresssion.

Example 1.7. The loop

while 0 6 = 1 do od;

will run forever and do nothing. The loop

while 0 = 1 do · · · od;

will not run at all.

Example 1.8. We trace the following statements

i := 2; k := i; while i^3 > 2 i^ do i := i + 3; k := k − i; od; Besides the variables i and k, we also trace the value of the boolean expression that

8 CHAPTER 1. BASICS

controls the loop i k i^3 > 2 i 2 2 TRUE 5 − 3 TRUE 8 − 11 TRUE 11 − 22 FALSE This example illustrates a general property of loops: on exit, the value of the loop control expression is necessarily FALSE, lest the loop would not be exited. By contrast, the boolen expression controlling an if-statement, can have any value on exit (Figure 1.2).

β is FALSE

β is TRUE β is FALSE

β is TRUE

β

β

Figure 1.2: The basic structures of algorithms: loops and if-statements. The evaluation of the boolean expression β gives rise to a bifurcation point in the execution of the algorithm.

It may be difficult, even impossible, to decide how many times a given while-loop will be executed (or indeed, whether the loop will terminate at all, see section 2.6 for an example). As an example, consider the following algorithm Algorithm NextPrime INPUT: n, a positive integer. OUTPUT: p, where p is the least prime greater than n. p := n + 1; while p is not prime do p := p + 1; od; return p; end;

Since the number of primes is infinite, we know the loop will terminate, but we do not know how many times it will be executed. Indeed, it is possible to show (see exercises) that arbitrarily large gaps between consecutive primes exist, hence arbitrarily large number of repetitions of the above loop. A structure is nested if it appears inside another structure. Nesting is a means of

10 CHAPTER 1. BASICS

The following table, called a truth table, defines the value of NOT and AND on all possible choices of boolean operands

NOT P P

F T

T F

P P AND Q Q

T T T

T F F

F F T

F F F

Other binary operators may be constructed from the above two. The most commonly used are OR , =⇒, and ⇐⇒. We define them directly with truth tables, although they can also be defined in terms of NOT and AND (see remarks following proposition 1, below).

P P OR Q Q

T T T

T T F

F T T

F F F

P P =⇒ Q Q

T T T

T F F

F T T

F T F

P P ⇐⇒ Q Q

T T T

T F F

F F T

F T F

We note that if A = TRUE and B = FALSE, then

(A =⇒ B) 6 = (B =⇒ A)

that is, the operator =⇒ is non-commutative.

Example 1.10.

P := 2 < 3; Q := 2 ≥ 3; R := (P OR Q) =⇒ (P AND Q); S := (P AND Q) =⇒ (P OR Q);

P Q P OR Q P AND Q R S

T F T F F T

Proposition 1 For all P, Q, ∈ {TRUE, FALSE}, the following holds

(i) NOT (P OR Q) = ( NOT P ) AND ( NOT Q)

(ii) NOT (P AND Q) = ( NOT P ) OR ( NOT Q)

(iii) P =⇒ Q = ( NOT P ) OR Q

1.3. CHARACTERISTIC FUNCTIONS 11

(iv) P ⇐⇒ Q = ((P =⇒ Q) AND (Q =⇒ P )).

Proof: The proof consists of evaluating each side of these equalities for all possible P, Q. We prove (iv). The other proofs are left as an exercise. The left-hand side of (iv) was given in (1.2). Let R be the right-hand side. We compute R explicitly.

P P =⇒ Q R Q =⇒ P Q T T T T T T F F T F F T F F T F T T T F

Hence the left hand side is equal to the right hand side for all P, Q ∈ {TRUE, FALSE}.

The statements (i) and (ii) are known as De Morgan’s laws. Using Proposition 1, one can express the operators OR , =⇒, ⇐⇒ in terms of NOT and AND (see exercises).

1.3 Characteristic functions

Characteristic functions link boolean quantities to sets.

Def: A characteristic function is a function assuming boolean values.

Let X be a set and A ⊆ X. The function

CA : X → {TRUE, FALSE} x 7 →

TRUE if x ∈ A FALSE if x 6 ∈ A

is called the characteristic function of A (in X). Conversely, let f : X → {TRUE, FALSE} be a characteristic function. Then f = CA, where A = f −^1 (TRUE). So there is a bi-unique correspondence between the characteristic functions defined on X, and the subsets of X.

Theorem 2 Let X be a set, and let A, B ⊆ X. The following holds

(i) NOT CA = CX\A (ii) CA AND CB = CA ∩B (iii) CA OR CB = CA ∪B (iv) CA =⇒ CB = CX(A\B) (v) CA ⇐⇒ CB = C(A∩B) ∪ (X(A ∪B))

Proof: To prove (i) we note that the function x 7 → NOT CA(x) evaluates to TRUE if x 6 ∈ A and to FALSE otherwise. However, x 6 ∈ A ⇐⇒ x ∈ (X \ A), from the definition of difference between sets.

Next we prove (iv). Let P := x ∈ A and Q := x ∈ B. Then, from (1.2), we have that P =⇒ Q is TRUE precisely when (P, Q) 6 = (TRUE, FALSE). This means that x is such that the expression

NOT (x ∈ A AND ( NOT x ∈ B)) = NOT (x ∈ A \ B) = x ∈ (X \ (A \ B))

1.3. CHARACTERISTIC FUNCTIONS 13

(c)

AND

(d)

3 − 2)^2

(e) ((‘93 is prime’ =⇒ ‘97 is prime’) OR ‘87 is prime’) ⇐⇒ ‘91 is prime’ (f ) ‘47 is the sum of two squares’.

Use only integer arithmetic. In part (f ) aim for precision and conciseness.

Exercise 1.5. Show that

(P =⇒ Q) = (( NOT Q) =⇒ ( NOT P ))

for all P, Q ∈ {TRUE, FALSE}.

Exercise 1.6. Describe in one sentence. Get the essence, not the details. [ 6  ]

X OR Y = NOT (( NOT X) AND ( NOT Y )) ∀ X, Y ∈ {TRUE, FALSE}.

Exercise 1.7. Complete the proof of Proposition 1, hence express OR , =⇒, and ⇐⇒ in terms of NOT and AND.

Exercise 1.8. Complete the proof of Theorem 2.

Exercise 1.9.

(a) Write an algorithm to the following specifications: Algorithm Min3Int INPUT: a, b, c ∈ Z. OUTPUT: m, where m is the minimum of a, b, c. (b) Explain in one sentence [ 6  ] what goes wrong in your algorithm if the input specification is changed to INPUT: a, b, c ∈ C, while leaving everything else unchanged.

Exercise 1.10. Explain concisely what this algorithm does, and how. [ 6  ]

INPUT: x, e ∈ Z, x 6 = 0, e ≥ 0. OUTPUT: ?? a := 1; while e > 0 do a := a · x; e := e − 1; od; return a; end;

14 CHAPTER 1. BASICS

Exercise 1.11. Consider the following algorithm Algorithm A INPUT: n, S, where n is a positive integer and S = (S 1 ,... , Sn) is a sequence of n integers. OUTPUT: ?? Z := 0; i := 1; while i ≤ n do j := i; while j ≤ n do Z := Z + Si; j := j + 1; od; i := i + 1; od; return Z; end;

(a) Write the output specifications.

(b) Rewrite the algorithm in such a way that it has only one loop. Exercise 1.12. Write an algorithm to the following specifications Algorithm IsSumOfSquares INPUT: x ∈ Z OUTPUT: TRUE, if x is the sum of two squares, FALSE otherwise.

The algorithm should use integer arithmetic only.