Download presentation
Presentation is loading. Please wait.
1
Introduction to Algorithms
2
Greatest common divisor
Problem: Find gcd(m,n) for nonnegative integers m and n, not both zero Eg: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ? Euclid’s algorithm gcd(m,n) = gcd(n, m mod n) gcd(m,0) = m Eg: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12 dividend ÷ divisor = quotient gcd(31415, 14142) = 1
3
3 descriptions of Euclid’s algorithm
gcd(m,n) = gcd(n, m mod n) gcd(m,0) = m Step 1 If n = 0, return m and stop; otherwise go to Step 2 Step 2 Divide m by n and assign 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 Find gcd(31415, 14142). Mathematical recursion, cooking recipe, pseudocode Two integers are relatively prime when their only common divisor is equal to 1. What happens when m < n? How do we know that this algorithm will eventually stop? O(log n) time
4
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem. For any legitimate input, it will produce the required output in a finite amount of time. “computer” problem algorithm input output
5
2nd method for computing gcd(m,n)
Consecutive integer checking algorithm Start with the smaller value min{m,n}. Is this the greatest common divisor? If yes, stop. If no, decrease it by 1 and try again. 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 Try gcd(60,24). Does not work when m or n=0. What happens when m or n = 0?
6
3rd methods for gcd(m,n) Formally, is this an algorithm?
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) gcd(60, 24) 60 = 2 · 2 · 3 · 5 24 = 2 · 2 · 2 · 3 gcd(60, 24) = 2 · 2 · 3 = 12. Formally, is this an algorithm? prime factorization steps are not defined unambiguously Ya, but how to find primes?
7
Distance between the two closest elements
8
Sieve of Eratosthenes Where do we start at each pass?
Let p be number whose multiples are being eliminated on the current pass. All its smaller multiples 2p, , (p − 1)p have been eliminated on earlier passes. We start at p·p=p2. Where do we finish at each pass? til n. When do we stop making more passes? Each pass start at p2 and stop at n. Therefore p ≤ n. QC sɪv ɛrəˈtɒsθəniːz
9
Sieve of Eratosthenes: Pseudocode
Input: Integer n ≥ 2 Output: List of primes less than or equal to n for i ← 2 to n do A[i] ← i // Initialize the array. for p ← 2 to n do // Round down to take the integer part display(A) // Print the array elements if A[p] //p hasn’t been eliminated from the list j ← p*p // Start at p2 while j ≤ n do // until the end of the list A[j] ← //mark element as eliminated j ← j + p // next multiple of p Class Exercise 1 Time complexity: O(n log log n) Run this for n=100.
10
Algorithmics: Why study algorithms?
Theoretical importance the core of computer science Practical importance algorithm comes first before programming a practitioner’s toolkit of known algorithms framework for designing and analyzing algorithms for new problems learning how to solve problems in general
11
Revise, fine-tune Input, output
Random-access machine (RAM), parallel algorithms Finite precision Brute force, divide and conquer Recipe, pseudocode, flowchart, data Structures, etc. Mathematical induction, recursion Time & space complexities Revise, fine-tune Program, debug, test
12
Summary An algorithm is a sequence of unambiguous instructions for solving a problem in a finite amount of time. Algorithms can be specified in a natural language or pseudocode. They can be implemented as computer programs. Algorithm design techniques (strategies or paradigms) are general approaches to solving problems. The same problem can often be solved by several algorithms. Algorithms operate on data structures. Algorithms have time and space complexities
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.