What Is An Algorithm?
What Is An 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.
problem
algorithm
input
A.
computer
output
1
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Euclids Algorithm
Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers m and n Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ? Euclids algorithm is based on repeated application of equality gcd(m,n) = gcd(n, m mod n) until the second number becomes 0, which makes the problem trivial. Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
A.
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
A.
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Is this an algorithm?
A.
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Sieve of Eratosthenes
Input: Integer n 2 Output: List of primes less than or equal to n for p 2 to n do A[p] p for p 2 to n do if A[p] 0 //p hasnt been previously eliminated from the list j p* p while j n do A[j] 0 //mark element as eliminated jj+p Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
A.
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
A.
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Iterative improvement
Backtracking Branch and bound
A.
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Analysis of algorithms
A.
Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10
sorting searching string processing graph problems combinatorial problems geometric problems numerical problems
A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 1 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11
list
array
linked list string
12