1 Introduction History of algorithms Examples of algorithms

  • Slides: 18
Download presentation
1 Introduction • History of algorithms. • Examples of algorithms. • Algorithms vs programs.

1 Introduction • History of algorithms. • Examples of algorithms. • Algorithms vs programs. • Data structures. • Abstract data types. © 2001, D. A. Watt and D. F. Brown

What is an algorithm? • An algorithm is a step-by-step procedure for solving a

What is an algorithm? • An algorithm is a step-by-step procedure for solving a stated problem. • For example, there are many possible algorithms for multiplying numbers: § multiplication table (suitable only for small numbers) § long multiplication § multiplication using logarithms § multiplication using a slide rule § binary fixed-point or floating-point multiplication (in a computer).

History • Euclid (~ 300 BCE), Eratosthenes (~ 200 BCE) – arithmetic, geometric algorithms.

History • Euclid (~ 300 BCE), Eratosthenes (~ 200 BCE) – arithmetic, geometric algorithms. • al Khwarizmi (~ 800) – arithmetic, algebraic, geometric algorithms. • Napier (~ 1600) – arithmetic using logarithms. • Newton (~ 1700) – differentiation, integration. • Turing (~ 1940) – code breaking, solvability.

Example 1: finding a midpoint (1) • Midpoint algorithm: To find the midpoint of

Example 1: finding a midpoint (1) • Midpoint algorithm: To find the midpoint of a given straight-line segment AB: 1. 2. 3. 4. 5. Draw intersecting circles of equal radius, centered at A and B respectively. Let C and D be the points where the circles intersect. Draw a straight line between C and D. Let E be the point where CD intersects AB. Terminate with answer E. • Could be performed by a human equipped with drawing instruments.

Example 1 (2) • Animation: 1. Draw intersecting circles of equal radius, centered at

Example 1 (2) • Animation: 1. Draw intersecting circles of equal radius, centered at A and B respectively. 2. Let C and D be the points where the circles intersect. 3. Draw a straight line between C and D. 4. Let E be the point where CD intersects AB. 5. Terminate with answer E. A D E C B

Example 2: GCDs (1) • The greatest common divisor (GCD) of two positive integers

Example 2: GCDs (1) • The greatest common divisor (GCD) of two positive integers is the largest integer that exactly divides both. E. g. , the GCD of 77 and 21 is 7. • Euclid’s GCD algorithm: To compute the GCD of positive integers m and n: 1. 2. 3. Set p to m, and set q to n. Until q exactly divides p, repeat: 2. 1. Set p to q, and set q to (p modulo q). Terminate with answer q. • Could be performed by a human, perhaps equipped with an abacus or calculator.

Example 2 (2) • Animation: To compute the GCD of positive integers m and

Example 2 (2) • Animation: To compute the GCD of positive integers m and n: 1. Set p to m, and set q to n. 2. Until q exactly divides p, repeat: 2. 1. Set p to q, and set q to (p modulo q). 3. Terminate with answer q. m 77 n 21 p 14 77 21 q 21 14 7

Example 2 (3) • Implementation in Java: static int gcd (int m, int n)

Example 2 (3) • Implementation in Java: static int gcd (int m, int n) { // Return the greatest common divisor of positive integers m and n. int p = m, q = n; while (p % q != 0) { int r = p % q; p = q; q = r; } return q; }

Example 3: square roots (1) • A square root of a positive number a

Example 3: square roots (1) • A square root of a positive number a is a number r such that r 2 = a. • Newton’s square-root algorithm: To compute approximately the positive square root of a positive number a: 1. 2. 3. Set r to the mean of 1 and a. Until r 2 is approximately equal to a, repeat: 2. 1 Set r to the mean of r and a/r. Terminate with answer r. • Could be performed by a human, perhaps equipped with log tables or a calculator.

Example 3 (2) • Animation: To compute approximately the positive square root of a

Example 3 (2) • Animation: To compute approximately the positive square root of a positive number a: 1. Set r to the mean of 1 and a. 2. Until r 2 is approximately equal to a, repeat: 2. 1 Set r to the mean of r and a/r. 3. Terminate with answer r. a 2. 0 r 1. 414 1. 417 1. 5

Example 3 (3) • Implementation in Java: static float sqrt (float a) { //

Example 3 (3) • Implementation in Java: static float sqrt (float a) { // Compute approximately the square root of positive real number a. float r = (1. 0 + a)/2; while (Math. abs(r*r/a - 1. 0) >= 0. 00005) r = (r + a/r)/2; return r; }

Algorithms vs. programs (1) • Algorithms: § can be performed by humans or machines

Algorithms vs. programs (1) • Algorithms: § can be performed by humans or machines § can be expressed in any suitable language § may be as abstract as we like. • Programs: § must be performed by machines § must be expressed in a programming language § must be detailed and specific.

Algorithms vs. programs (2) • Here we express our algorithms in (precise) English. •

Algorithms vs. programs (2) • Here we express our algorithms in (precise) English. • Steps may be numbered consecutively: 1. 2. Do this. Do that. Do this and then do that. • The extent of a conditional is indicated by indentation: 7. If …: 7. 1. 7. 2. Do this. Do that. Do this and then do that, but only if condition … is true. • The extent of a loop is indicated by indentation: 8. While …, repeat: 8. 1. Do this. 8. 2. Do that. Do this and then do that, as long as condition … is true.

Algorithms vs. programs (3) • If we wish to use the algorithm on a

Algorithms vs. programs (3) • If we wish to use the algorithm on a computer, we must first code it in a programming language. • There may be many ways of coding the algorithm, and there is a wide choice of programming languages. But all the resulting programs are implementations of the same underlying algorithm. • Here we express our implementations in Java. (Alternatives would be C, Pascal, Ada, etc. )

Data structures • A data structure is a systematic way of organizing a collection

Data structures • A data structure is a systematic way of organizing a collection of data. • A static data structure is one whose capacity is fixed at creation. E. g. : array. • A dynamic data structure is one whose capacity is variable, so it can expand or contract at any time. E. g. : linked list, binary tree. • For each data structure we need algorithms for insertion, deletion, searching, etc.

Example 4: representing strings • Possible data structures to represent the string “Java”: Array:

Example 4: representing strings • Possible data structures to represent the string “Java”: Array: Linked list: 0 1 2 3 ‘J’ ‘a’ ‘v’ ‘a’

Example 5: representing sets • Possible data structures to represent the set of words

Example 5: representing sets • Possible data structures to represent the set of words {bat, cat, mat, rat, sat}: Array: Linked list: Binary search tree: 0 1 2 3 4 bat cat mat rat sat bat cat 5 mat cat bat rat mat sat 6 rat 7 sat

Abstract data types • When we write application code, we don’t care how strings

Abstract data types • When we write application code, we don’t care how strings are represented: we just declare variables of type String, and manipulate them using String operations. • Similarly, we don’t care how sets are represented: we just declare variables of type Set, and manipulate them using Set operations. • An abstract data type is a data type whose representation is hidden from, and of no concern to, the application code. Examples: String, Set. • Abstract data types are an excellent way to design large programs.