550 likes | 1.96k Views
The Design and Analysis of Algorithms. Modified by Zahid Ansari. Text Book. Anany Levitin, Introduction to the Design and Analysis of Algorithms, Addison Wesley, 2 nd Edition. Ellis Horowitz, Sartaj Sahni: Fundamentals of Computer Algorithms, 2nd Edition, Universities Press, 2007.
E N D
The Design and Analysis of Algorithms Modified by Zahid Ansari
Text Book • Anany Levitin, Introduction to the Design and Analysis of Algorithms, Addison Wesley, 2nd Edition. • Ellis Horowitz, Sartaj Sahni: Fundamentals of Computer Algorithms, 2nd Edition, Universities Press, 2007.
COURSE OBJECTIVES • At the end of this course, you should have • mastered different algorithm design techniques such as brute-force, divide and conquer, greedy, etc. • the ability to apply and implement learned algorithm design techniques to solve problems. • the basic ability to analyze algorithms and to determine algorithm correctness and time efficiency class
UNIT 1: INTRODUCTION • Notion of Algorithm (Sec 1.1) • Review of Asymptotic Notations (Sec 2.2) • Mathematical Analysis of Nom-Recursive and Recursive Algorithms (Sec 2.2-2.4) • Brute Force Approaches (Sec 3.1-3.2) • Introduction, Selection Sort and Bubble Sort, Sequential Search and Brute Force String Matching.
What is an Algorithm • A sequence of unambiguous instructions for solving a problem, i.e. for obtaining the required output for any legitimate input in a finite amount of time
Important points • Each step of the algorithm should be Non-ambiguous • Range of inputs should be specified carefully • The same algorithm can be represented in several different ways • Several algorithms for solving the same problem may exist
NOTION OF ALGORITHM problem algorithm “computer” input output Algorithmic solution
Sorting algorithms: • Selection sort? • Insertion sort? • Merge sort? • … problem algorithm “computer” output input <5, 3, 2, 8, 3> <2, 3, 3, 5, 8> EXAMPLE: SORTING • Example:
SELECTION SORT • Input: array a[0], …, a[n-1] • Output: array a sorted in non-decreasing order • Algorithm: for i=0 to n-1 swap a[i] with smallest of a[i],…a[n-1]
Euclid’s Algorithm • Euclid’s algorithm for finding the greatest common divisor • One of the oldest algorithms known (300 BC). • “Algorithm” is named after Muhammad ibn Musa al-Khwarizmi – 9th century mathematician
EUCLID’S 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
EUCLID’S ALGORITHM • Euclid’s 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
DESCRIPTION OF EUCLID’S ALGORITHM • Step 1: If n = 0, return m and stop; otherwise go to Step 2 • Step 2 : Divide m by n and assign the value of the remainder to r • Step 3 : Assign the value of n to m and the value of r to n. Go to Step 1. ALGORITHM Euclid(m, n) Input: Two non-negative, not-both-zero integers m, n Output: Greatest Common divisor of m and n while n ≠0 do r ← m mod n m ← n n ← r return m
Consecutive Integer Checking Algorithm for gcd(m,n) • Step 1 Assign the value of min{m,n} to t • Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 • Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 • Step 4 Decrease t by 1 and go to Step 2
gcd(m,n) • t ← min (m ,n) • if m % t = 0 goto 3, • else goto 4 • 3. if n % t = 0 return t, • else goto 4 • 4. t ← t - 1 • 5. goto 2
Middle School Method for gcd(m,n) • Step 1 - Find the prime factorization of m • Step 2 - Find the prime factorization of n • Step 3 - Find all the common prime factors • Step 4 - Compute the product of all the common prime factors and return it as gcd(m,n) Example: gcd(60, 24) • m = 60 = 2 * 2 * 3 * 5 • n = 24 = 2 * 2 * 2 * 3 • gcd(m, n) = gcd(60,24) = 2 * 2 * 3 = 12 Is this an algorithm?
What can we learn from the previous 3 examples? • Each step of an algorithm must be unambiguous. • The same algorithm can be represented in several different ways. (different pseudocodes) • There might exists more than one algorithm for a certain problem. • Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds.