Download presentation
Presentation is loading. Please wait.
Published byJoella Norton Modified over 6 years ago
1
Introduction to the Design and Analysis of Algorithms
ElSayed Badr Benha University September 20, 2016
2
Lecture 1: Introduction
Official course information ( Mark distribution: ) – Oral (assignments 10%) Laboratory ( 15% ) – Midterm 10% each (in class, 60 minutes) – Final 65% (3 hours)
3
• References: 1- Cormen, Leiserson, Rivest and Stein,
Lecture 1: Introduction • References: 1- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 2nd Edition, 2005. 2- C. H. Papadimitriou, and U. V. Vazirani, Algorithms, 1th Edition,2006. 3- Anany Levitin, Introduction to: the Design and analysis of Algorithms, 2nd Edition, 2007
4
• Algorithm I, Topics covered: – Introduction to algorithms
Lecture 1: Introduction Course overview: • Algorithm I, Topics covered: – Introduction to algorithms – Algorithms: sorting, matrices, graphs, sets – Design: divide-and-conquer, dynamic programming, greedy – Analysis: model assumptions, worst/average/best case, asymptotic, reduction, complexity classes P and NP, hard problems
5
Lecture 1: Introduction
Basic concepts • Problem • Instance • Algorithm • Issues for a given algorithm – Correctness — often via Loop Invariants, proved by induction – Analysis — measuring resource requirement * Running time * Space – Optimality • Algorithm design concepts – Divide and conquer – Data structure – Dynamic programming – Exhaustive enumeration – Greedy – Reduction (useful for showing problems are hard) – Branch and bound – Others (probably won’t cover)
6
Lecture 1: Introduction
Summations 1- 2- 3- 4- 5- 6-
7
Lecture 1: Introduction
Products If n = 0 then We can convert a formula with a product to a formula with a summation by using the identity :
9
2- Prove by induction that
10
Basic Definitions What is an algorithm?
A- 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. B- An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.
11
What kinds of problems are solved by algorithms?
1- Human Genome Project (DNA Sequence) 2- Electronic Commerce ( cryptograph ) 3- Networks 4- Optimization problems 5- Image Processing 6- Bioinformatics 7- Medical expert system .
12
Algorithms as Technology !
106 . 1 Algorithm A Insertion sort ( c1 n2 ) Supercomputer (108 )ins/sec Good programmer Algorithm B Merge sort ( c2 n log n ) PC (106 )ins/sec Bad programmer Computer B runs 20 times faster than computer A . What happens, if the size of array = 107 numbers. 2.3 days mints
13
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. Can be represented various forms Unambiguity/clearness Effectiveness Finiteness/termination Correctness
14
Historical Perspective
Euclid’s algorithm for finding the greatest common divisor Muhammad ibn Musa al-Khwarizmi – 9th century mathematician Euclid’s algorithm is good for introducing the notion of an algorithm because it makes a clear separation from a program that implements the algorithm. It is also one that is familiar to most students. Al Khowarizmi (many spellings possible...) – “algorism” (originally) and then later “algorithm” come from his name.
15
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, gcd(0,0) = ? 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 Euclid’s algorithm is good for introducing the notion of an algorithm because it makes a clear separation from a program that implements the algorithm. It is also one that is familiar to most students. Al Khowarizmi (many spellings possible...) – “algorism” (originally) and then later “algorithm” come from his name.
16
Two descriptions 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. while n ≠ 0 do r ← m mod n m← n n ← r return m How do we know that Euclid’s algorithm coms to stop ?
17
Other methods for computing gcd(m,n)
Consecutive integer checking algorithm 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 Is this slower than Euclid’s algorithm? How much slower? O(n), if n <= m , vs O(log n)
18
Other methods for gcd(m,n) [cont.]
Middle-school procedure 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) Is this an algorithm? How efficient is it? Time complexity: O(sqrt(n))
19
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 hasn’t been previously eliminated from the list j ← p* p while j ≤ n do A[j] ← 0 //mark element as eliminated j ← j + p Example : Time complexity: O(n)
20
Functions ordered by Growth Rate
Sieve of Eratosthenes Functions ordered by Growth Rate log n log2n sqrt(n) n n log n n2 n3 2n
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.