Http://ylu/raik283/ Introduction Dr. Ying Lu [email protected] RAIK 283: Data Structures & Algorithms.

18
http://www.cse.unl.edu/~ylu/raik283/ Introduction Dr. Ying Lu [email protected] RAIK 283: Data RAIK 283: Data Structures & Algorithms Structures & Algorithms

Transcript of Http://ylu/raik283/ Introduction Dr. Ying Lu [email protected] RAIK 283: Data Structures & Algorithms.

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

http://www.cse.unl.edu/~ylu/raik283/

Introduction

Dr. Ying [email protected]

RAIK 283: Data Structures RAIK 283: Data Structures & Algorithms& Algorithms

Page 2: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Giving credit where credit is due:Giving credit where credit is due: Most of the lecture notes are based on the Most of the lecture notes are based on the

slides from the Textbookslides from the Textbook’’s companion s companion websitewebsite

http://www.aw-bc.com/info/levitin Several slides are from Jeff Edmonds of the Several slides are from Jeff Edmonds of the

York UniversityYork University I have modified them and added new slidesI have modified them and added new slides

RAIK 283: Data Structures RAIK 283: Data Structures & Algorithms& Algorithms

Page 3: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

AlgorithmAlgorithm An algorithm is a sequence of An algorithm is a sequence of

unambiguous instructions unambiguous instructions for for solving a problem, i.e., for solving a problem, i.e., for obtaining a required output for obtaining a required output for any any legitimate inputlegitimate input in a finite in a finite amount of time.amount of time.

Page 4: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

A ProblemA Problem

Find the greatest common divisor of Find the greatest common divisor of two nonnegative, non-both-zero two nonnegative, non-both-zero integers m and n, denoted gcd(m,n)integers m and n, denoted gcd(m,n)

gcd(m,n), the largest integer that gcd(m,n), the largest integer that divides both m and n evenly, i.e., divides both m and n evenly, i.e., with a remainder of zerowith a remainder of zero

Page 5: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Greatest Common Divisor Greatest Common Divisor (GCD) Algorithm 1(GCD) Algorithm 1

Step 1Step 1 Assign the value of min{ Assign the value of min{m,nm,n} to } to tt Step 2Step 2 Divide Divide mm by by tt. If the remainder of . If the remainder of

this division is 0, go to Step 3; otherwise, to this division is 0, go to Step 3; otherwise, to Step 4Step 4

Step 3Step 3 Divide Divide nn by by tt. If the remainder of . If the remainder of this division is 0, return the value of this division is 0, return the value of tt as the as the answer and stop; otherwise, proceed to Step answer and stop; otherwise, proceed to Step 44

Step 4Step 4 Decrease the value of Decrease the value of tt by 1. Go to by 1. Go to Step 2Step 2

Note: Note: mm and and nn are positive integers are positive integers

Page 6: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

GCD Procedure 2GCD Procedure 2 Step 1Step 1 Find the prime factors of Find the prime factors of mm Step 2Step 2 Find the prime factors of Find the prime factors of nn Step 3Step 3 Identify all the common factors in the two Identify all the common factors in the two

prime expansions found in Steps 1 and 2. If prime expansions found in Steps 1 and 2. If pp is a is a common factor occurring i and j times in m and n, common factor occurring i and j times in m and n, respectively, it should be repeated min{i, j} timesrespectively, it should be repeated min{i, j} times

Step 4Step 4 Compute the product of all the common Compute the product of all the common factors and return it as the GCD of factors and return it as the GCD of mm and and nn

Note: as written, this procedure requires that Note: as written, this procedure requires that mm and and nn be integers greater than 1, since 1 is not a be integers greater than 1, since 1 is not a primeprime

Is this procedure an algorithm?Is this procedure an algorithm?

Page 7: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Algorithm 2?Algorithm 2?

Procedure 2 is not an algorithm Procedure 2 is not an algorithm unless we can provide an unless we can provide an effectiveeffective way to find prime factors of a way to find prime factors of a numbernumber

The sieve of Eratosthenes is an The sieve of Eratosthenes is an algorithm that provides such an algorithm that provides such an effective procedureeffective procedure

Page 8: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

EuclidEuclid’’s Algorithms Algorithm Idea: Idea:

if n if n 0, gcd(m, n) = gcd(n, m mod n); 0, gcd(m, n) = gcd(n, m mod n); if n = 0, gcd(m, n) = m.if n = 0, gcd(m, n) = m.

EuclidEuclid’’s algorithm for computing gcd(m, n)s algorithm for computing gcd(m, n) Step1 If n=0, return the value of m as the Step1 If n=0, return the value of m as the

answer and stop; otherwise proceed to Step2.answer and stop; otherwise proceed to Step2. Step2 Divide m by n and assign the value of Step2 Divide m by n and assign the value of

the remainder to r.the remainder to r. Step3 Assign the value of n to m and the value Step3 Assign the value of n to m and the value

of r to n. Go to Step1.of r to n. Go to Step1.

Page 9: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

ExerciseExercise

Page8 Problem5Page8 Problem5 a. Find gcd(31415, 14142) by applying a. Find gcd(31415, 14142) by applying

Euclid’s algorithm.Euclid’s algorithm. b. Estimate how many times faster it will b. Estimate how many times faster it will

be to find gcd(31415, 14142) by Euclid’s be to find gcd(31415, 14142) by Euclid’s algorithm compared with the algorithm algorithm compared with the algorithm based on checking consecutive integers based on checking consecutive integers from min(m, n) down to gcd(m, n). from min(m, n) down to gcd(m, n).

Page 10: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Analysis of AlgorithmsAnalysis of Algorithms

How good is the algorithm?How good is the algorithm? CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace efficiency

Does there exist a better algorithm?Does there exist a better algorithm? Lower boundsLower bounds OptimalityOptimality

Page 11: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

What is an algorithm?What is an algorithm? Recipe, process, method, technique, procedure, routine,Recipe, process, method, technique, procedure, routine,

…… with following requirements: with following requirements:1.1. FinitenessFiniteness

terminates after a finite number of stepsterminates after a finite number of steps

2.2. DefinitenessDefiniteness rigorously and unambiguously specifiedrigorously and unambiguously specified

3.3. InputInput valid inputs are clearly specifiedvalid inputs are clearly specified

4.4. OutputOutput can be proved to produce the correct output given a valid inputcan be proved to produce the correct output given a valid input

5.5. EffectivenessEffectiveness steps are sufficiently simple and basicsteps are sufficiently simple and basic

Page 12: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Algorithm Design and Algorithm Design and Analysis ProcessAnalysis Process

Understand the problem

Decide on : algorithm design techniques etc.

Design an algorithm

Page 13: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Algorithm Design and Algorithm Design and Analysis ProcessAnalysis Process

Understand the problem

Decide on : algorithm design techniques etc.

Design an algorithm

Prove correctness

Analyze efficiency etc.

Decide on : algorithm design techniques etc.

Page 14: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Algorithm Design and Algorithm Design and Analysis ProcessAnalysis 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.

correctnessefficiency

Page 15: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.
Page 16: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

CorrectnessCorrectness

TerminationTermination Well-founded sets: find a quantity that Well-founded sets: find a quantity that

is never negative and that always is never negative and that always decreases as the algorithm is decreases as the algorithm is executedexecuted

Page 17: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

Prove the Correctness forProve the Correctness forEuclidEuclid’’s Algorithms Algorithm

Idea: Idea: if n if n 0, gcd(m, n) = gcd(n, m mod n); 0, gcd(m, n) = gcd(n, m mod n); if n = 0, gcd(m, n) = m.if n = 0, gcd(m, n) = m.

EuclidEuclid’’s algorithm for computing gcd(m, n)s algorithm for computing gcd(m, n) Step1 If n=0, return the value of m as the Step1 If n=0, return the value of m as the

answer and stop; otherwise proceed to Step2.answer and stop; otherwise proceed to Step2. Step2 Divide m by n and assign the value of Step2 Divide m by n and assign the value of

the remainder to r.the remainder to r. Step3 Assign the value of n to m and the value Step3 Assign the value of n to m and the value

of r to n. Go to Step1.of r to n. Go to Step1.

Page 18: Http://ylu/raik283/ Introduction Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms.

ComplexityComplexity

Space complexitySpace complexity Time complexityTime complexity

For iterative algorithms: sumsFor iterative algorithms: sums For recursive algorithms: recurrence For recursive algorithms: recurrence

relationsrelations