Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.

Similar presentations


Presentation on theme: "Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms."— Presentation transcript:

1 http://www.cse.unl.edu/~ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

2 Giving credit where credit is due: Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook ’ s companion website Most of the lecture notes are based on the slides from the Textbook ’ s companion website http://www.aw-bc.com/info/levitin Several slides are from Jeff Edmonds of the York University Several slides are from Jeff Edmonds of the York University I have modified them and added new slides I have modified them and added new slides RAIK 283: Data Structures & Algorithms

3 Algorithm An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

4 A Problem Find the greatest common divisor of two nonnegative, non-both-zero integers m and n, denoted gcd(m,n) Find the greatest common divisor of two nonnegative, non-both-zero integers m and n, denoted gcd(m,n) gcd(m,n), the largest integer that divides both m and n evenly, i.e., with a remainder of zero gcd(m,n), the largest integer that divides both m and n evenly, i.e., with a remainder of zero

5 Greatest Common Divisor (GCD) Algorithm 1 Step 1 Assign the value of min{m,n} to t Step 1 Assign the value of min{m,n} to t Step 2 Divide m by t. If the remainder of this division is 0, go to Step 3; otherwise, to Step 4 Step 2 Divide m by t. If the remainder of this division is 0, go to Step 3; otherwise, to Step 4 Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step 4 Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step 4 Step 4 Decrease the value of t by 1. Go to Step 2 Step 4 Decrease the value of t by 1. Go to Step 2 Note: m and n are positive integers Note: m and n are positive integers

6 GCD Procedure 2 Step 1 Find the prime factors of m Step 1 Find the prime factors of m Step 2 Find the prime factors of n Step 2 Find the prime factors of n Step 3 Identify all the common factors in the two prime expansions found in Steps 1 and 2. If p is a common factor occurring i and j times in m and n, respectively, it should be repeated min{i, j} times Step 3 Identify all the common factors in the two prime expansions found in Steps 1 and 2. If p is a common factor occurring i and j times in m and n, respectively, it should be repeated min{i, j} times Step 4 Compute the product of all the common factors and return it as the GCD of m and n Step 4 Compute the product of all the common factors and return it as the GCD of m and n Note: as written, this procedure requires that m and n be integers greater than 1, since 1 is not a prime Note: as written, this procedure requires that m and n be integers greater than 1, since 1 is not a prime Is this procedure an algorithm? Is this procedure an algorithm?

7 Algorithm 2? Procedure 2 is not an algorithm unless we can provide an effective way to find prime factors of a number Procedure 2 is not an algorithm unless we can provide an effective way to find prime factors of a number The sieve of Eratosthenes is an algorithm that provides such an effective procedure The sieve of Eratosthenes is an algorithm that provides such an effective procedure

8 Euclid ’ s Algorithm Idea: Idea: if n  0, gcd(m, n) = gcd(n, m mod n); if n  0, gcd(m, n) = gcd(n, m mod n); if n = 0, gcd(m, n) = m. if n = 0, gcd(m, n) = m. Euclid ’ s algorithm for computing gcd(m, n) Euclid ’ s algorithm for computing gcd(m, n) Step1 If n=0, return the value of m as the answer and stop; otherwise proceed to Step2. Step1 If n=0, return the value of m as the answer and stop; otherwise proceed to Step2. Step2 Divide m by n and assign the value of the remainder to r. Step2 Divide m by n and assign the value of the remainder to r. Step3 Assign the value of n to m and the value of r to n. Go to Step1. Step3 Assign the value of n to m and the value of r to n. Go to Step1.

9 Exercise Page8 Problem5 Page8 Problem5 a. Find gcd(31415, 14142) by applying Euclid’s algorithm. a. Find gcd(31415, 14142) by applying Euclid’s algorithm. b. Estimate how many times faster it will be to find gcd(31415, 14142) by Euclid’s algorithm compared with the algorithm based on checking consecutive integers from min(m, n) down to gcd(m, n). b. Estimate how many times faster it will be to find gcd(31415, 14142) by Euclid’s algorithm compared with the algorithm based on checking consecutive integers from min(m, n) down to gcd(m, n).

10 Analysis of Algorithms How good is the algorithm? How good is the algorithm? Correctness Correctness Time efficiency Time efficiency Space efficiency Space efficiency Does there exist a better algorithm? Does there exist a better algorithm? Lower bounds Lower bounds Optimality Optimality

11 What is an algorithm? Recipe, process, method, technique, procedure, routine, … with following requirements: Recipe, process, method, technique, procedure, routine, … with following requirements: 1. Finiteness b terminates after a finite number of steps 2. Definiteness b rigorously and unambiguously specified 3. Input b valid inputs are clearly specified 4. Output b can be proved to produce the correct output given a valid input 5. Effectiveness b steps are sufficiently simple and basic

12 Algorithm Design and Analysis Process Understand the problem Decide on : algorithm design techniques etc. Design an algorithm

13 Algorithm Design and Analysis Process Understand the problem Decide on : algorithm design techniques etc. Design an algorithm Prove correctness Analyze efficiency etc. Decide on : algorithm design techniques etc.

14 Algorithm Design and Analysis Process Understand the problem Decide on : algorithm design techniques etc. Design an algorithm Prove correctness Analyze efficiency etc Code the algorithm Decide on : algorithm design techniques etc. correctness efficiency

15

16 Correctness Termination Termination Well-founded sets: find a quantity that is never negative and that always decreases as the algorithm is executed Well-founded sets: find a quantity that is never negative and that always decreases as the algorithm is executed

17 Prove the Correctness for Euclid ’ s Algorithm Idea: Idea: if n  0, gcd(m, n) = gcd(n, m mod n); if n  0, gcd(m, n) = gcd(n, m mod n); if n = 0, gcd(m, n) = m. if n = 0, gcd(m, n) = m. Euclid ’ s algorithm for computing gcd(m, n) Euclid ’ s algorithm for computing gcd(m, n) Step1 If n=0, return the value of m as the answer and stop; otherwise proceed to Step2. Step1 If n=0, return the value of m as the answer and stop; otherwise proceed to Step2. Step2 Divide m by n and assign the value of the remainder to r. Step2 Divide m by n and assign the value of the remainder to r. Step3 Assign the value of n to m and the value of r to n. Go to Step1. Step3 Assign the value of n to m and the value of r to n. Go to Step1.

18 Complexity Space complexity Space complexity Time complexity Time complexity For iterative algorithms: sums For iterative algorithms: sums For recursive algorithms: recurrence relations For recursive algorithms: recurrence relations


Download ppt "Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms."

Similar presentations


Ads by Google