180 likes | 319 Views
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
E N D
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 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
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.
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
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
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?
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
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.
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).
Analysis of Algorithms • How good is the algorithm? • Correctness • Time efficiency • Space efficiency • Does there exist a better algorithm? • Lower bounds • Optimality
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
Algorithm Design and Analysis Process Understand the problem Decide on : algorithm design techniques etc. Design an algorithm
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.
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
Correctness • Termination • Well-founded sets: find a quantity that is never negative and that always decreases as the algorithm is executed
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.
Complexity • Space complexity • Time complexity • For iterative algorithms: sums • For recursive algorithms: recurrence relations