0% found this document useful (0 votes)
81 views22 pages

Introduction To The Design and Analysis of Algorithms By: Anany Levitin

The document discusses algorithms and their design and analysis. It introduces algorithms as unambiguous problem-solving procedures that terminate in finite time. Several examples of algorithms are presented, including algorithms for finding the greatest common divisor (GCD) of two numbers. Euclid's algorithm for computing the GCD is analyzed in detail and proven to be correct. Additional topics covered include computational problems like sorting, algorithm design strategies, and analyzing algorithms for correctness, time efficiency, and optimality.

Uploaded by

theresa.painter
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views22 pages

Introduction To The Design and Analysis of Algorithms By: Anany Levitin

The document discusses algorithms and their design and analysis. It introduces algorithms as unambiguous problem-solving procedures that terminate in finite time. Several examples of algorithms are presented, including algorithms for finding the greatest common divisor (GCD) of two numbers. Euclid's algorithm for computing the GCD is analyzed in detail and proven to be correct. Additional topics covered include computational problems like sorting, algorithm design strategies, and analyzing algorithms for correctness, time efficiency, and optimality.

Uploaded by

theresa.painter
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to the Design and Analysis of Algorithms by Anany Levitin www.aw-bc.

com/info/levitin/

Design and Analysis of Algorithms - Chapter 1


http://delab.csd.auth.gr/courses.html , ,

, , 2005.

Design and Analysis of Algorithms - Chapter 1

Why study algorithms?


Wirth

1976: Algorithms + Data Structures = Programs

Theoretical

importance

the core of computer science


Practical

importance

A practitioners toolkit of known algorithms Framework for designing and analyzing algorithms for new problems
Design and Analysis of Algorithms - Chapter 1 3

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.
Design and Analysis of Algorithms - Chapter 1 4

Algorithm
Besides merely being a finite set of rules that gives a sequence of operations for solving a specific type of problem, an algorithm has the five important features [Knuth]
finiteness (otherwise: computational method) termination definiteness precise definition of each step input (zero or more) output (one or more) effectiveness Its operations must all be sufficiently basic that they can in principle be done exactly and in a finite length of time by someone using pencil and paper
Design and Analysis of Algorithms - Chapter 1 5

Historical Perspective

Euclids algorithm for finding the greatest common divisor http://en.wikipedia.org/wiki/Al-Khwarizmi Ab Abd Allh Muammad ibn Ms al-Khwrizm ( ) was a Muslim mathematician, astronomer, astrologer, geographer and author. He was born in Persia around 780, and died around 850. Because of his book on the systematic solution of linear and quadratic equations, al-Kitb al-mukhtaar f hsb al-abr wal-muqbala, together with Diophantus, he is considered to be the father of algebra. The word algebra is derived from al-abr, one of the two operations used to solve quadratic equations, as described in his book. Algoritmi de numero Indorum, the Latin translation of his other major work, on the Indian numerals, introduced the positional number system and the number zero to the Western world in the 12th century. The words algorism and algorithm stem from Algoritmi, the Latinization of his name
Design and Analysis of Algorithms - Chapter 1 6

Notion of algorithm
problem algorithm input
computer

output

Algorithmic solution : a way to teach the computer


Design and Analysis of Algorithms - Chapter 1 7

Greatest Common Divisor - Algorithm 1


Step 1 Assign the value of min{m,n} to t Step 2 Divide m by t. If the remainder of this division is 0, go to Step 3; otherwise, to Step 4 Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step 4 Step 4 Decrease the value of t by 1. Go to Step 2 Note: m and n are positive integers
Design and Analysis of Algorithms - Chapter 1 8

Greatest Common Divisor - Algorithm 1


Correctness
The algorithm terminates, because t is decreased by 1 each time we go through step 4, 1 divides any integer, and t eventually will reach 1 unless the algorithm stops earlier. The algorithm is partially correct, because when it returns t as the answer, t is the minimum value that divides both m and n

Design and Analysis of Algorithms - Chapter 1

Greatest Common Divisor - Algorithm 2


Step 1 Find the prime factors of m Step 2 Find the prime factors of n Step 3 Identify all the common factors in the two prime expansions found in Steps 1 and 2. If p is a common factor repeated i times in m and j times in n, assign to p the multiplicity min{i,j} Step 4 Compute the product of all the common factors with their multiplicities and return it as the GCD of m and n Note: as written, the algorithm requires than m and n be integers greater than 1, since 1 is not a prime
Design and Analysis of Algorithms - Chapter 1 10

Greatest Common Divisor - Algorithm 2


In fact Algorithm 2 is not an algorithm unless we can provide an effective way to find prime factors of a number The sieve of Eratosthenes is an algorithm that provides such an effective procedure

??

Design and Analysis of Algorithms - Chapter 1

11

Greatest Common Divisor - Algorithm 3


Euclids Algorithm
E0 [Ensure n m] If m < n, exchange m with n E1 [Find Remainder] Divide m by n and let r be the remainder (We will have 0 r < n) E2 [Is it zero?] If r=0, the algorithm terminates; n is the answer E3 [Reduce] Set m to n, n to r, and go back to E1

Design and Analysis of Algorithms - Chapter 1

12

Greatest Common Divisor - Algorithm 3


Termination of Euclids Algorithm
The

second number of the pair gets smaller with each iteration and cannot become negative: indeed, the new value of n is r = m mod n, which is always smaller than n. Eventually, r becomes zero, and the algorithms stops.

Design and Analysis of Algorithms - Chapter 1

13

Greatest Common Divisor - Algorithm 3


Correctness of Euclids Algorithm

After step E1, we have m=qn+r, for some integer q. If r=0, then m is a multiple of n, and clearly in such a case n is the GCD of m and n. If r0, note that any number that divides both m and n must divide mqn=r, and any number that divides both n and r must divide qn+r=m; so the set of divisors of {m,n} is the same as the set of divisors of {n,r}. In particular, the GCD of {m,n} is the same as the GCD of {n,r}. Therefore, E3 does not change the answer to the original problem.
Design and Analysis of Algorithms - Chapter 1 14

Greatest Common Divisor - Algorithm 3


Book Variation (p.4)
Step 1 If n=0, return the value of m as the answer and stop; otherwise, proceed 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. Note: m and n are nonnegative, not-both-zero integers
Design and Analysis of Algorithms - Chapter 1 15

Greatest Common Divisor - Algorithm 3


Pseudocode for book version
while n <> 0 do r m mod n mn nr return m

Design and Analysis of Algorithms - Chapter 1

16

Example of computational problem: sorting


Statement

of problem:

Input: A sequence of n numbers <a1,a2,,an> Output: A reordering of the input sequence <a1,a2,,an> so that aiaj whenever i<j
Instance:

The sequence <5, 3, 2, 8, 3> Algorithms:


Selection sort Insertion sort Merge sort (many others)
Design and Analysis of Algorithms - Chapter 1 17

Selection Sort
array a[1],...,a[n] Output: array a sorted in non-decreasing order Algorithm:
Input:

see also pseudocode, section 3.1

for i=1 to n swap a[i] with smallest of a[i],a[n] (In place sorting, stable sorting)
Design and Analysis of Algorithms - Chapter 1

18

Some Well-known Computational Problems


Sorting Searching String searching Graph problems (shortest paths, minimum spanning tree, graph-coloring etc) Combinatorial problems (tsp) Geometric problems (closest-pair, convex hull) Numerical problems (equations, integrals) Other (primality testing, knapsack problem, chess, towers of Hanoi)

Design and Analysis of Algorithms - Chapter 1 19

Basic Issues Related to Algorithms


Understanding the problem (instances, ranges) Ascertaining the capabilities of a computational device (RAM and parallel machines, speed and amount of memory) Choosing between exact and approximate algorithms Deciding the correct data structure Algorithm design techniques Methods of specifying an algorithm (free language, pseudocode, flowchart) Proving an algorithms correctness Analyzing an algorithm (efficiency, theoretical and empirical analysis, optimality) Coding an algorithm
Design and Analysis of Algorithms - Chapter 1 20

Algorithm design strategies

Brute force Divide and conquer Decrease and conquer

Greedy approach Dynamic programming Backtracking and Branch and bound Space and time tradeoffs
21

Transform and conquer

Design and Analysis of Algorithms - Chapter 1

Analysis of Algorithms

How good is the algorithm?


Correctness Time efficiency Space efficiency Simple (recursive, iterative) General

Does there exist a better algorithm?


Lower bounds Optimality
Design and Analysis of Algorithms - Chapter 1 22

You might also like