Download Euclid's Algorithm and Algorithm Efficiency: Understanding Big O Notation and more Slides Data Structures and Algorithms in PDF only on Docsity!
Analyzing Algorithms and Problems
Euclid's Algorithm
- In Euclid's 7th book in the Elements series (written about 300BC), he gives an algorithm to calculate the highest common factor (largest common divisor) of two numbers x and y where (x < y). This can be stated as:
- 1.Divide y by x with remainder r.
- 2.Replace y by x, and x with r.
- 3.Repeat step 1 until r is zero.
- When this algorithm terminates, y is the highest common factor.
Euclid's Algorithm
- Euclid's Algorithm determines the greatest common divisor of two natural numbers a, b. That is, the largest natural number d such that d | a and d | b. - GCD(33,21)= - 33 = 121 + 12 - 21 = 112 + 9 - 12 = 19 + 3 - 9 = 3
Time vs. Input Graph
How do you know if you have the best
algorithm?
A lower bound can be established because a basic number of operations must be performed. Thus, any algorithm that performs that number would be optimal, even if no such algorithm has been found that uses that number of steps.
Hanoi Tower
- Long time ago, there were three towers in Honoi. In the very beginning, there were 64 disks with different sizes stacking on the A tower, small disks always stepping on the bigger disks. A monk had to move all disks to the C tower following rules: - 1. Each time only one disk could be moved from a tower, to any other; - 2. At anytime, small disks should step on the bigger disks, not allowing big disks step on smaller disks.
. Count the number of basic operations
performed by the algorithm on the worst-case input
- A basic operation could be:
- An assignment
- A comparison between two variables
- An arithmetic operation between two variables. The worst-case input is that input assignment for which the most basic operations are performed.
Algorithm Efficiency
- Determining the efficiency of an Algorithm is done by counting the number of basic operations that are carried out when the algorithm is executed
- A basic operation is an operation carried out by the algorithm that is language independent, such as addition, subtraction, comparison of two values, etc…
Asymptotic Notation for Big O
- Big O (Omicron) can be defined as
- Note: the following two conditions must be met:
- The limit must exist
- The limit c < ∞