1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.

Slides:



Advertisements
Similar presentations
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.1 Chapter 5 Loops.
Advertisements

SEQUENCES and INFINITE SERIES
Ch. 10: What is a number?. MAIN DEFINITION OF THE COURSE: A symmetry of an object (in the plane or space) means a rigid motion (of the plane or space)
ECIV 201 Computational Methods for Civil Engineers Richard P. Ray, Ph.D., P.E. Error Analysis.
What is an algorithm? Informally: An Algorithm is a step by step method for solving a problem. It’s purpose is to break a larger task down so that each.
Lecture 14 Go over midterm results Algorithms Efficiency More on prime numbers.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
Introduction to Computer Programming in c
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CPSC 490 Number Theory Primes, Factoring and Euler Phi-function Mar.31 st, 2006 Sam Chan.
Prime numbers Jordi Cortadella Department of Computer Science.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
CS 101: “If” and Recursion Abhiram Ranade. This week’ lab assignment: Write a program that takes as input an integer n and a sequence of numbers w1,w2,...,wn.
CSCI 125 & 161 Lecture 13 Martin van Bommel. Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
Additional Problems.
1 Introduction to Abstract Mathematics Chapter 3: Elementary Number Theory and Methods of Proofs Instructor: Hayk Melikya Direct.
1 Sections 1.5 & 3.1 Methods of Proof / Proof Strategy.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
CSC 221 Computer Organization and Assembly Language
What does a computer program look like: a general overview.
MECN 3500 Inter - Bayamon Lecture 3 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
In section 11.9, we were able to find power series representations for a certain restricted class of functions. Here, we investigate more general problems.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
CS404 Design and Analysis of Algorithms BBy DDr. M V S Peri Sastry BB.E, PhD(BITS-Pilani)
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
While ( number
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Chapter 7 JavaScript: Control Statements, Part 1
Chapter Topics Chapter 16 discusses the following main topics:
Topic 6 Recursion.
Repetition Control Structure in C++ Program
Number Theory (Chapter 7)
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Unit# 9: Computer Program Development
Introduction to Object-Oriented Programming with Java--Wu
Algorithm Discovery and Design
Algorithm Discovery and Design
Chapter 5 Loops.
Presentation transcript:

1 Chapter Six Algorithms

2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the concrete realization of an algorithm in the context of a programming language There exist more than one algorithm for most problems

3 Testing for Primality A positive integer n is a prime if it has exactly two positive divisors, which are itself and 1 Prime numbers are important today because they play a central role in many forms of cryptography

4 A Simple Algorithm Check each number between 1 and n to see whether it divides evenly into n Add 1 to a counter each time you encounter a new divisor Check to see whether the counter is 2 after all numbers have been tested

5 A Simple Function bool isPrime(int n) { int divisors, i; divisors = 0; for (i = 1; i <= n; i++) { if (n % i == 0) divisors++; } return (divisors == 2); }

6 Verification of Algorithms An algorithm must satisfy the following three requirements Clear and unambiguous in its definition Effective, in the sense that its steps are executable Finite, in the sense that it terminates after a bounded number of steps

7 Verification of Algorithms Is the definition of isPrime clear and unambiguous Are the steps in isPrim effective in the sense that it is possible to carry them out Does isPrime terminates after a finite amount of time

8 Correctness of Algorithms Proving that an algorithm is correct is very hard to do in any formal way A property that is true at the entry to a loop and that continues to be true at the end of each loop cycle is called a loop invariant Proving loop invariants are maintained is an important tool for proving correctness Testing is an approach to reveal the presence of errors in the algorithms, but never their absence

9 Correctness of Algorithms Before loop begins, no divisors have been found, and divisors is initialized to 0 In each cycle, 1 is added to divisors if a new divisor is found. Thus, at the end of ith cycle, divisors holds the number of divisors between 1 and i At the end of nth cycle, divisors contains the number of divisors between 1 and n If divisors contains 2, then n must be a prime

10 Efficiency of Algorithms There exist more than one algorithm for most problems Look for algorithms that run fast and use memory less

11 Efficiency of Algorithms As soon as isPrime finds any divisor greater than 1 and less than n, it can stop right there Once isPrime has checked whether n is divisible by 2, it doesn’t need to check other even numbers isPrime only needs to check numbers up to  n

12 Efficiency of Algorithms bool isPrime(int n) { int i; if (n % 2 == 0) return FALSE; for (i = 3; i <= sqrt(n); i += 2) { if (n % i == 0) return FALSE; } return TRUE; }

13 Common Pitfalls When you design a general algorithm, it is important to consider whether there are any special cases in which the general algorithm might fail. If there are, you must be sure to handle these cases explicitly in the program if (n <= 1) return FALSE; if (n == 2) return TRUE;

14 Common Pitfalls Check to see if there are any calculations within the loop that could just as well be performed before the loop begins. If there are, you can increase the efficiency of the program by calculating the result once, storing the result in a variable, and the using that variable inside the loop limit = sqrt(n); for (i = 3; i <= limit; i += 2)

15 Common Pitfalls Be very careful when testing floating-point numbers for equality. Because floating- point numbers are only approximations limit = sqrt(n) + 1;

16 Efficiency of Algorithms bool isPrime(int n) { int i, limit; if (n <= 1) return FALSE; if (n == 2) return TRUE; if (n % 2 == 0) return FALSE; limit = sqrt(n) + 1; for (i = 3; i <= limit; i += 2) { if (n % i == 0) return FALSE; } return TRUE; }

17 Measuring Execution Time The function clcok_t clock(void) in the library returns the processors time used by the program since the beginning of execution. The value of (double) clock() / CLOCKS_PER_SEC is a time in seconds

18 Measuring Execution Time bool isPrime(int n) { int divisors, i; clock_t begin, end; begin = clock(); divisors = 0; for (i = 1; i <= n; i++) { if (n % i == 0) divisors++; } end = clock(); printf(“%lf\n”, ((double) end-begin)/ CLOCKS_PER_SEC ); return (divisors == 2); }

19 Evaluating Algorithms There are many measures for evaluating algorithms: efficiency, maintainability, portability, and so on It is often the case that no algorithm is best in all measures You then need to choose your algorithm based on your requirements

20 Greatest Common Divisor Given two numbers, x and y, the greatest common divisor is the largest number that divides evenly into both int gcd(int x, int y);

21 A Simple Algorithm The greatest common divisor of x and y must be between 1 and x (or y) Starting from g = x, check whether g divides evenly into both x and y If it does, g is the greatest common divisor Otherwise, decrement g by 1 and check again

22 A Simple Algorithm int gcd(int x, int y) { int g; g = x; while (x % g != 0 || y % g != 0) { g--; } return g; }

23 Correctness Does this algorithm always give the correct answer Does this algorithm always terminate

24 Euclid’s Algorithm Divide x by y and compute the remainder r If r is zero, the procedure is complete, and the answer is y If r is not zero, set x equal to the old value of y, set y equal to r, and repeat the entire process

25 Euclid’s Algorithm int gcd(int x, int y) { int r; while (TRUE) { r = x % y; if (r == 0) break; x = y; y = r; } return y; }

26 Correctness

27 Efficiency Consider the numbers and The simple algorithm requires steps Euclid’s algorithm requires only 2 steps x = , y = , r = 5 x = , y = 5,, r = 0

28 Numerical Algorithms The techniques used by computers to implement mathematical functions like sqrt are called numerical algorithms Successive approximation and series expansion are two of the techniques Use the computation of square root to illustrate these two techniques

29 Successive Approximation Successive approximation is a general strategy for finding an approximate answer It starts by making a guess at the answer It then uses that guess to generate a better one If you can somehow guarantee that your guess is getting closer and closer (or converging) to the real answer on each cycle, repeating the process will eventually result in a guess that is close enough to satisfy the needs of any application

30 Newton’s Algorithm Square root of 16.0: guess = 8.0, 16.0 / 8.0 = 2.0, 2.0 <  16.0 < 8.0 guess = ( ) / 2 = 5.0, 16.0 / 5.0 = 3.2, 3.2 <  16.0 < 5.0 guess = ( ) / 2 = 4.1, guess = , guess = ,

31 Newton’s Algorithm To compute the square root of x, it starts by making an arbitrary guess g If g is close enough to the actual square root, g is returned If g is not close enough, new g = (g + x/g)/2, and repeat the process

32 Newton’s Algorithm double mySqrt(double x) { double g; if (x == 0) return 0; if (x < 0) { printf(“Error: negative argument %lf\n”, x); return 0; } g = x; while (!approximatelyEqual(x, g * g)) g = (g + x / g) / 2; return g; } special cases

33 Newton’s Algorithm #include #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define EPSILON bool approximatelyEqual(double x, double y) { double z; z = fabs(x – y) / MIN(fabs(x), fabs(y)); return z < EPSILON; }

34 Series Expansion In series expansion, the value of a function is approximated by adding together terms in a mathematical series that converges 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + …

35 Taylor Series The square root of x can be approximated by the following Taylor series 1 + (1/2)(x-1) – (1/4)(x-1) 2 /2! + (3/8)(x-1) 3 /3! - (15/16)(x-1) 4 /4! + 

36 Taylor Series t 0 = 1 t 1 = (1/2)(x-1) t 2 = -(1/4)(x-1) 2 /2! t 3 = (3/8)(x-1) 3 /3! t 4 = -(15/16)(x-1) 4 /4! coeff * xpower / factorial coeff *= (0.5 – i) xpower *= (x – 1) factorial *= (i + 1)

37 Taylor Series double tsqrt(double x) { double sum, term, coeff, xpower, fact; int i; fact = coeff = xpower = 1; sum = 0; term = 1; for (i = 0; sum != sum + term; i++) { sum += term; coeff *= 0.5 – i; xpower *= x –1; fact *= i + 1; term = coeff * xpower / fact; } return sum; }

38 Radius of Convergence The Taylor expansion for the square root function is effective only when the argument falls within a limited range (0 < x < 2) that allows the calculation to converge. This range is called the radius of convergence  4x = 4  x = 2  x

39 Radius of Convergence double mySqrt(double x) { double correction; if (x == 0) return 0; if (x < 0) { printf(“Error: negative argument %lf\n”, x); return 0; } correction = 1; while (x >= 2) { x /= 4; correction *= 2; } return tsqrt(x) * correction; }