1 / 18

RAIK 283: Data Structures & Algorithms

RAIK 283: Data Structures & Algorithms. Introduction Dr. Ying Lu ylu@cse.unl.edu. http://www.cse.unl.edu/~ylu/raik283/. RAIK 283: Data Structures & Algorithms. Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook ’ s companion website

jadon
Télécharger la présentation

RAIK 283: Data Structures & Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. RAIK 283: Data Structures & Algorithms • Giving credit where credit is due: • 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 • I have modified them and added new slides

  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.

  4. A Problem • 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

  5. Greatest Common Divisor (GCD) Algorithm 1 • 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 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 • Note: m and n are positive integers

  6. GCD Procedure 2 • Step 1 Find the prime factors of m • 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 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 • 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 • The sieve of Eratosthenes is an algorithm that provides such an effective procedure

  8. Euclid’s Algorithm • Idea: • if n  0, gcd(m, n) = gcd(n, m mod n); • if n = 0, gcd(m, n) = m. • 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. • 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.

  9. Exercise • Page8 Problem5 • 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).

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

  11. What is an algorithm? • Recipe, process, method, technique, procedure, routine,… with following requirements: • Finiteness • terminates after a finite number of steps • Definiteness • rigorously and unambiguously specified • Input • valid inputs are clearly specified • Output • can be proved to produce the correct output given a valid input • Effectiveness • 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. Decide on : algorithm design techniques etc. Design an algorithm Prove correctness Analyze efficiency etc.

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

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

  16. Prove the Correctness forEuclid’s Algorithm • Idea: • if n  0, gcd(m, n) = gcd(n, m mod n); • if n = 0, gcd(m, n) = m. • 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. • 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.

  17. Complexity • Space complexity • Time complexity • For iterative algorithms: sums • For recursive algorithms: recurrence relations

More Related