cs483 analysis of algorithms lecture 01jmlien/teaching/09_spring_cs... · analysis of algorithms...

46
Analysis of Algorithms CS483 Lecture 01-Introduction – 1 CS483 Analysis of Algorithms Lecture 01 * Jyh-Ming Lien January 23, 2008 * this lecture note is based on Algorithms by S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani and Intro- duction to the Design and Analysis of Algorithms by Anany Levitin.

Upload: others

Post on 08-Jul-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Analysis of Algorithms CS483 Lecture 01-Introduction – 1

CS483 Analysis of AlgorithmsLecture 01∗

Jyh-Ming Lien

January 23, 2008

∗this lecture note is based onAlgorithmsby S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani andIntro-duction to the Design and Analysis of Algorithmsby Anany Levitin.

Page 2: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

A Brief History

. A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 2

In ancient Europe, numbers are represented by Roman numerals, e.g.,MDCCCCIIII.

Decimal system is invented in India around AD 600, e.g., 1904. Al Khwarizmi (AD 840), one of the most influential mathematicians

in Baghdad, wrote a textbook in Arabic about adding, multiplying,dividing numbers, and extracting square roots and computingπ usingdecimal system.

(image of Al Khwarizmi from http://jeff560.tripod.com/)

Page 3: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

A Brief History (Cont.)

A Brief History

. A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 3

Many centuries later, decimal system was adopted in Europe, and theprocedures in Al Khwarizmi’s book were named after him as“Algorithms.” One of the most important mathematicians in thisprocess was a man named “Leonard Fibonacci.”

Today, one of his most well known work isFibonacci/Fee-boh-NAH-chee/number(AD 1202).

(image of Leonardo Fibonacci from http://www.math.ethz.ch/fibonacci)

Page 4: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Fibonacci number

A Brief History

A Brief History (Cont.)

. Fibonacci numberFibonacci’s originalquestion

Definition

Our First AlgorithmAnalyze Our FirstAlgorithmImprove Our FirstAlgorithm

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 4

Page 5: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Fibonacci’s original question

A Brief History

A Brief History (Cont.)

Fibonacci number

.Fibonacci’s originalquestion

Definition

Our First AlgorithmAnalyze Our FirstAlgorithmImprove Our FirstAlgorithm

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 5

Fibonacci’s original question:

– Suppose that you are given a newly-born pair of rabbits, onemale, onefemale.

– Rabbits are able to mate at the age of one month so that at the end of itssecond month a female can produce another pair of rabbits.

– Suppose that our rabbits never die.– Suppose that the female always produces one new pair (one male, one

female) every month.

Question: How many pairs will there be in one year?

1. Beginning: (1 pair)2. End of month 1: (1 pair) Rabbits are ready to mate.3. End of month 2: (2 pairs) A new pair of rabbits are born.4. End of month 3: (3 pairs) A new pair and two old pairs.5. End of month 4: (5 pairs)6. End of month 5: (8 pairs)7. After 12 months, there will be 144 rabits

Page 6: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Definition

A Brief History

A Brief History (Cont.)

Fibonacci numberFibonacci’s originalquestion

. Definition

Our First AlgorithmAnalyze Our FirstAlgorithmImprove Our FirstAlgorithm

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 6

Fibonacci numbersfib(n):

fib(n) =

0 if n = 01 if n = 1fib(n− 1) + fib(n− 2) if n > 1

(1)

Example: The first 10 Fibonacci numbers are:0, 1, , , , , , , ,

Fibonacci numbers have applications in Biology, Visual arts, Music,Simulation, Algorithm analysis and design, etc.

(images fromhttp://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fib.html)

Page 7: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Our First Algorithm

A Brief History

A Brief History (Cont.)

Fibonacci numberFibonacci’s originalquestion

Definition

. Our First AlgorithmAnalyze Our FirstAlgorithmImprove Our FirstAlgorithm

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 7

Problem: What isfib(200)? What aboutfib(n), wheren is anypositive integer?Algorithm 0.1: fib(n)

if n = 0then return (0)

if n = 1then return (1)

return (fib(n− 1) + fib(n− 2))

Questions that we should ask ourselves.

1. Is the algorithm correct?2. What is the running time of our algorithm?3. Can we do better?

Page 8: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Analyze Our First Algorithm

A Brief History

A Brief History (Cont.)

Fibonacci numberFibonacci’s originalquestion

Definition

Our First Algorithm

.Analyze Our FirstAlgorithm

Improve Our FirstAlgorithm

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 8

Is the algorithm correct?

– Yes, we simply follow the definition of Fibonacci numbers

How fast is the algorithm?

– If we let the run time offib(n) beT (n), then we can formulate

T (n) = T (n − 1) + T (n − 2) + 3 ≈ 1.6n

– T (200) ≥ 2139

– The world fastest computer BlueGene/L, which can run248 instructionsper second, will take291 seconds to compute. (291 seconds =7.85 × 1010

billion years, Sun turns into a red giant star in 4 to 5 billionyears)– Can Moose’s law, which predicts that CPU get 1.6 times faster each year,

solve our problem?– No, because the time needed to computefib(n) also have the same

“growth” rate

. if we can computefib(100) in exactly a year,

. then in the next year, we will still spend a year to computefib(101)

. if we want to computefib(200) within a year, we need to wait for 100 years.

Page 9: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Improve Our First Algorithm

A Brief History

A Brief History (Cont.)

Fibonacci numberFibonacci’s originalquestion

Definition

Our First AlgorithmAnalyze Our FirstAlgorithm

.Improve Our FirstAlgorithm

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 9

Can we do better? Yes, because many computations in the previous algorithm are

repeated.

Algorithm 0.2: fib(n)

comment: Initially we create an arrayA[0 · · ·n]

A[0]← 0, A[1]← 1for i = 2 · · ·n

do A[i] = A[i− 1] + A[i− 2]return (A[n])

Page 10: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Design Algorithms

A Brief History

A Brief History (Cont.)

Fibonacci number

. Design AlgorithmsProcess of Designing AnAlgorithm

What is an algorithm?

Why study algorithms?

How to design algorithms?

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 10

Page 11: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Process of Designing An Algorithm

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

.Process of DesigningAn Algorithm

What is an algorithm?

Why study algorithms?

How to design algorithms?

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 11

Definition: “An algorithm is a procedure (a finite set of well-definedinstructions) for accomplishing some task which, given an initial state, willterminate in a defined end-state” -from wikipedia, the free encyclopedia

Page 12: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

What is an algorithm?

A Brief History

A Brief History (Cont.)

Fibonacci number

Design AlgorithmsProcess of Designing AnAlgorithm

. What is an algorithm?

Why study algorithms?

How to design algorithms?

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 12

Recipe, process, method, technique, procedure, routine,... with followingrequirements:

1. Finitenessterminates after a finite number of steps

2. Definitenessrigorously and unambiguously specified

3. Inputvalid inputs are clearly specified

4. Outputcan be proved to produce the correct output given a valid input

5. Effectivenesssteps are sufficiently simple and basic

Page 13: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Why study algorithms?

A Brief History

A Brief History (Cont.)

Fibonacci number

Design AlgorithmsProcess of Designing AnAlgorithm

What is an algorithm?

. Why study algorithms?

How to design algorithms?

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 13

Theoretical importance

– the core of computer science (or the core the entire westerncivilization!)

Practical importance

– A practitioners toolkit of known algorithms (i.e., standing on theshoulders of giants)

– Framework for designing and analyzing algorithms for newproblems (i.e, so you know that your problem will terminatebefore the end of the world)

Page 14: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

How to design algorithms?

A Brief History

A Brief History (Cont.)

Fibonacci number

Design AlgorithmsProcess of Designing AnAlgorithm

What is an algorithm?

Why study algorithms?

.How to designalgorithms?

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 14

Page 15: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Analysis of algorithms

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

. Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 15

Page 16: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Analysis of algorithms

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

. Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 16

When we design an algorithm, we should ask ourselves:

1. Is the algorithm correct?2. How efficient is the algorithm?

– Time efficiency– Space efficiency

3. Can we do better?

Approaches

1. theoretical analysis2. empirical analysis

Page 17: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Empirical analysis of time efficiency

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithms

.Empirical analysis oftime efficiency

Theoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 17

A typical way to estimate the running time

– Select a specific (typical) sample of inputs– Use wall-clock time (e.g., milliseconds)

orCount actual number of basic operation’s executions

– Analyze the collected data (e.g., plot the data)

Problems with empirical analysis

– difficult to decide on how many samples/tests are needed– computation time is hardware/environmental dependent– implementation dependent

Page 18: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Theoretical analysis of time efficiency

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiency

.Theoretical analysis oftime efficiency

Theoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 18

Providemachine independentmeasurements Estimate the bottleneck of the algorithm The size of the input increases→ algorithms run longer⇒. Typically

we are interested in how efficiency scales w.r.t. input size To measure the running time, we could

1. count all operations executed.2. or determine the number of thebasic operationas a function ofinput size

Basic operation: the operation that contributes most towards the running time

Page 19: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Theoretical analysis of time efficiency

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiency

.Theoretical analysis oftime efficiency

Theoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 19

Examples:

1. sort a list of integersa1, a2, · · · , an

2.

a11 · · · a1m

.... . .

...an1 · · · anm

b11 · · · b1k

.... . .

...bm1 · · · bmk

3. prime(n)

4. Graph 3-coloring

Input Size:

1. number of elements in the list

2. nm + mk

3. number of digits

4. number of edges + numbertices

Basic operations:

1. key comparison

2. multiplication of two numbers

3. division

4. visiting a vertex and travedge

Page 20: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Theoretical analysis of time efficiency

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

.Theoretical analysis oftime efficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 20

We can approximate the run time using the following formula:

T (n) ≈ copC(n) ,

wheren is the input size,C(n) is the number of the basic operationfor n, andcop is the time needed to execute one single basic operation.

Examples: Given thatC(n) = 1

2n(n− 1), How much time an

algorithm will take if the input sizen doubled?Well, we can use the formula above to answer this question:

growth =T (2n)

T (n)≈ copC(2n)

copC(n)=

4n− 2

n− 1≈ 4

Theoretical analysis focuses on “order of growth” of an algorithm.(Given the input sizen)

Page 21: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Orders of Growth

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

. Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 21

Some of the commonly seen functions representing the number of thebasic operationC(n) =

1. n2. n2

3. n3

4. log10

(n)5. n log

10(n)

6. log2

10(n)

7.√

n8. 2n

9. n!

Can you order them by their growth rate?

Page 22: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Orders of Growth

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

. Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 22

Test functions using some values

n n2 n3 2n n!

10 102 103 1024 3.6 × 106

100 104 106 1.3 × 1030 9.3 × 10157

1000 106 109 1.1 × 10301

10000 108 1012

n log10(n) n log10(n) log210(n)

√n

10 1 10 1 3.16100 2 200 4 101000 3 3000 9 31.610000 4 40000 16 100

Now, we can order the functions by their growth ratelog

10(n) < log2

10(n) <

√n < n < n log

10(n) < n2 < n3 < 2n < n!

Page 23: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Orders of Growth

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

. Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 23

plot the functions (e.g., use matlab or gnuplot)

Basic efficiency classes

n n2 n3 2n n!

linear quadratic cubic exponential factorial

c log10(n) n log10(n)√

n

constant logarithmic n-log-n square root

Page 24: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Best-, average-, worst-cases

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of Growth

.Best-, average-,worst-cases

Example 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 24

For some algorithms efficiency depends on form of input:

Worst case:Cworst(n)→maximum over inputs of size n Best case:Cbest(n)→ minimum over inputs of size n Average case:Cavg(n)→ “average” over inputs of size n

1. Number of times the basic operation will be executed on typicalinput

2. NOT the average of worst and best case3. Expected number of basic operations considered as a random

variable under some assumption about the probability distributionof all possible inputs

Page 25: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Example 1: Sequential Search

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-cases

.Example 1: SequentialSearch

Example 1: SequentialSearchExample 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 25

Find the valueK in a given arrayA[1 · · ·n]

Algorithm 0.3: SEARCH(A[1..n], K)

for i← [1 · · ·n]

do

if A[i] = Kthen return (i)

return (−1)

Input size =n, Basic operation is:if(A[i] = K) Worst case (worst case analysis provides an upper bound):

1. When does the worst case happen?whenK 6∈ A

whenK = A[n]2. What isCworst(n)?

Cworst(n) = n

Page 26: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Example 1: Sequential Search

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearch

.Example 1: SequentialSearch

Example 2: GreatestCommon DivisorExample 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 26

Best case:

1. When does the best case happen?whenK = A[0]

2. What isCbest(n)?Cbest(n) = 1

Average case:

1. Average case asks a useful question: what kind of running time to weexpect to get when we don’t know or know only little about the data?

– suppose that the probability ofK ∈ A is p

– suppose that the probability ofK = A[i] equals that ofK = A[j]

2. When does the best case happen?whenK andA satisfy our assumptions

3. What isCbest(n)?Cbest(n) = whenK ∈ A + whenK 6∈ A =p · (1 · 1

n+ 2 · 1

n+ · · · + n · 1

n) + n · (1 − p)

= pn+12

+ (1 − p) · n

Page 27: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Example 2: Greatest Common Divisor

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearch

.Example 2: GreatestCommon Divisor

Example 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 27

Algorithm 0.4: gcd(a, b)

for i = min(a, b), · · · , 1do

if a%i = 0 and b%i = 0then return (i)

Input size=n = min(a, b), Basic operation is:a%i Worst case (worst case analysis provides an upper bound):

1. When does the worst case happen?whena andb are relatively prime

2. What isCworst(n)?Cworst(n) = n

Page 28: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Example 2: Greatest Common Divisor

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Analysis of algorithmsEmpirical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiencyTheoretical analysis of timeefficiency

Orders of Growth

Orders of Growth

Orders of GrowthBest-, average-,worst-casesExample 1: SequentialSearchExample 1: SequentialSearchExample 2: GreatestCommon Divisor

.Example 2: GreatestCommon Divisor

Asymptotic Notation

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 28

Best case:

1. When does the best case happen?whengcd(a, b) = min(a, b)

2. What isCbest(n)? Cbest(n) = 1

Average case:

1. Assumptions:

– Assume thata andb are two randomly chosen integers– Assume that all integers have the same probability of being chosen– hint : The probability that an integerd is a andb’s greatest common divisor is

Pa,b(d) = 6π2d2

2. When does the best case happen?whenK andA satisfy our assumptions

3. What isCbest(n)?Let us denoten = min(a, b)Cbest(n) = 1 · Pa,b(n) + 2 · Pa,b(n − 1) + · · · + (n) · Pa,b(1)) =6

π2 ( 1n2 + 2

(n−1)2+ · · · + n

12 )

(whenn = 10,( 1100

+ 281

+ 364

+ 449

+ 536

+ 625

+ 716

+ 89

+ 94

+10) · (6/π2) = 8.58300468)

Page 29: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Asymptotic Notation

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

. Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

Ω-notation

Ω-notation

Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 29

Page 30: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Asymptotic Notation and Basic Efficiency Classes

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

.Asymptotic Notationand Basic EfficiencyClasses

O-notation

O-notation

Ω-notation

Ω-notation

Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 30

The main goal of algorithm analysis is to estimatedominatecomputation stepsC(n) when theinput sizen is large

Computer scientists classifyC(n) into a set of functions to help themconcentrate on trend (i.e., order of growth).

Asymptotic notation has been developed to provide a tool for studyingorder of growth

– O(g(n)): a set of functions with the same or smaller order of growth asg(n)

. 2n2 − 5n + 1 ∈ O(n2)

. 2n + n100 − 2 ∈ O(n!)

. 2n + 6 6∈ O(log n)

– Ω(g(n)): a set of functions with the same or larger order of growth asg(n)

. 2n2 − 5n + 1 ∈ Ω(n2)

. 2n + n100 − 2 6∈ Ω(n!)

. 2n + 6 ∈ Ω(log n)

– Θ(g(n)): a set of functions with the same order of growth asg(n)

. 2n2 − 5n + 1 ∈ Θ(n2)

. 2n + n100 − 2 6∈ Θ(n!)

. 2n + 6 6∈ Θ(log n)

Page 31: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

O-notation

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

. O-notation

O-notation

Ω-notation

Ω-notation

Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 31

Definition: f(n) is in O(g(n)) if “order of growth off(n)” ≤ “orderof growth ofg(n)” (within constant multiple)

– there exist positive constantc and non-negative integern0 suchthatf(n) ≤ cg(n) for everyn ≥ n0

Examples:

– 10n ∈ O(n2)

. why? [Whenc = 1 andn ≥ n0 = 10, 10n ≤ n2.]

– 5n + 20 ∈ O(n)

. why? [Whenc = 10 andn ≥ n0 = 2, 5n + 20 ≤ 10n.]

– 2n + 6 6∈ O(log n)

. why? [Whenc = 100 andn ≥ n0 = 300, 2n + 6 > 100 log n.]

Page 32: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

O-notation

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

. O-notation

Ω-notation

Ω-notation

Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 32

We denoteO as an asymptoticupper bound

Try the following commands ingnuplot

– plot [0 : 20] 10 ∗ x, x ∗ x– plot [0 : 5] 5 ∗ x + 20, 10 ∗ x– plot [0 : 400] 2 ∗ x + 6, 100 ∗ log(x)

Page 33: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Ω-notation

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

. Ω-notation

Ω-notation

Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 33

Definition: f(n) is in Ω(g(n)) if “order of growth off(n)” ≥ “orderof growth ofg(n)” (within constant multiple)

– there exist positive constantc and non-negative integern0 suchthatf(n) ≥ cg(n) for everyn ≥ n0

Examples:

– n3

5∈ Ω(n2)

. why? [Whenc = 1 andn0 ≥ 5, n3

5≥ n2]

– 2n− 51 ∈ Ω(n)

. why? [Whenn0 ≥ 51, 2n− 51 ≥ n]

Page 34: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Ω-notation

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

Ω-notation

. Ω-notation

Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 34

We denoteΩ as an asymptoticlower bound

Try the following commands ingnuplot

– plot [0 : 10] (x ∗ x ∗ x)/5, x ∗ x– plot [0 : 100] 2 ∗ x− 51, x

Page 35: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Θ-notation

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

Ω-notation

Ω-notation

. Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 35

Definition: f(n) is in Θ(g(n)) if f(n) is bounded above and belowby g(n) (within constant multiple)

– there exist positive constantc1 andc2 and non-negative integern0

such thatc1g(n) ≤ f(n) ≤ c2g(n) for everyn ≥ n0

Examples:

– 1

2n(n− 1) ∈ Θ(n2)

. why? [Whenn0 ≥ 2, n2

4≤ 1

2n(n− 1) ≤ n2]

– 2n− 51 ∈ Θ(n)

. why? [Whenn0 ≥ 7, n ≤ 2n− 51 ≤ 2n]

Page 36: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Θ-notation

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

Ω-notation

Ω-notation

Θ-notation

. Θ-notation

Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 36

We denoteΘ as an asymptotictight bound

Try the following commands ingnuplot

– plot [0 : 10] (x ∗ x− x)/2, (x ∗ x)/4, x ∗ x– plot [0 : 200] 2 ∗ x− 51, x, 2 ∗ x

Page 37: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Useful Property

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

Ω-notation

Ω-notation

Θ-notation

Θ-notation

. Useful PropertyComparing Orders ofGrowthOrders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 37

1. f(n) ∈ O(f(n))

Proof.

2. f(n) ∈ O(g(n)) if and only if g(n) ∈ Ω(f(n))

Proof.

3. f(n) ∈ O(g(n)) and g(n) ∈ O(h(n)), thenf(n) ∈ O(h(n))

Proof.

4. f1(n) ∈ O(g1(n)) and f2(n) ∈ O(g2(n)), thenf1(n) + f2(n) ∈ O(maxg1(n), g2(n))

Proof.

Page 38: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Comparing Orders of Growth

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

Ω-notation

Ω-notation

Θ-notation

Θ-notation

Useful Property

.Comparing Orders ofGrowth

Orders of growth of someimportant functions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 38

1. Comparing Orders of Growth

limn→∞

f(n)

g(n)=

0 t(n) has a smaller order of growth than g(n)c > 0 t(n) has the same order of growth as g(n)∞ t(n) has a larger order of growth than g(n)

2. Example: Compare the orders of growth of1

2n(n− 1) andn2

3. Example: Compare the orders of growth oflog n and√

n

4. Example: Compare the orders of growth ofn! and2n

Page 39: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Some tools for computing limits

Analysis of Algorithms CS483 Lecture 01-Introduction – note 1 of slide 38

L’H opital’s rule

limn→∞

f(n)

g(n)= lim

n→∞

f ′(n)

g′(n)

Stirling’s formula

n! ≈√

2πn(n

e

)n

Page 40: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Orders of growth of some important functions

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic NotationAsymptotic Notation andBasic Efficiency Classes

O-notation

O-notation

Ω-notation

Ω-notation

Θ-notation

Θ-notation

Useful PropertyComparing Orders ofGrowth

.Orders of growth ofsome importantfunctions

Syllabus

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 39

1. All logarithmic functionsloga n belong to the same classΘ(log n) no matter what thelogarithms basea > 1 is

Proof.

2. All polynomials of the same degree k belong to the same class:aknk + ak−1nk−1 + · · · + a0 ∈ Θ(nk)

Proof.

3. Exponential functionsan have different orders of growth for differenta’s, i.e.,2n 6∈ Θ(3n)

Proof.

4. orderlog n < orderna>0 < orderan < ordern! < ordernn

Page 41: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Syllabus

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

. SyllabusGrading and ImportantDates

Policies

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 40

Page 42: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Grading and Important Dates

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

.Grading and ImportantDates

Policies

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 41

Webpage: http://cs.gmu.edu/∼jmlien/teaching/08springcs483/ TA : TBA Required Textbook: Algorithms, by Sanjoy Dasgupta, Christos

Papadimitriou, and Umesh Vazirani, McGraw-Hill, 2006, ISBN0073523402.

Grading

1. Quizzes and CS Culture as-signments 15%

2. Assignments 25%3. Midterm Exam 25%4. Final Exam 35%

Final grade:

– A (≥ 90)– B (≥ 80)– C (≥ 70)– D (≥ 60)– F (< 60)

Important Dates.

– Spring Break (March 10 – 16)– Midterm Exam (March 19)– Final Exam (May 07)

Page 43: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Policies

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

SyllabusGrading and ImportantDates

. Policies

Summary

Analysis of Algorithms CS483 Lecture 01-Introduction – 42

Quizzesare mainly for keeping you coming to the class. The quiz will be aclosed book exam. You can also have up totwo opportunities of making upyour missed/failed quizzes by turning in two CS culture assignments.

CS culture assignmentis a one-page written summary (form availableonline) of a talk from a CS seminar (see http://cs.gmu.edu/events/) that youattend during the Spring’08 semester.

Assignmentsmust be completed by the stated due date and time. Yourassignment score will be halved every extra day after the duedate.

Exams. You will be allowed to have one page (letter size) of notes for themidterm and two pages (one sheet) for the final. No copying of anything fromthe textbook or another person is allowed. You can write somethingsverbatim. You can also write your notes on the computer and print them. Thenotes sheet will be handed in with the exam.

Page 44: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Summary

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

. Summary

Summary

Assignment

Analysis of Algorithms CS483 Lecture 01-Introduction – 43

Page 45: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Summary

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

. Summary

Assignment

Analysis of Algorithms CS483 Lecture 01-Introduction – 44

Two important men in algorithms: Al Khwarizmi & Leo Fibonacci Fibonacci number General ideas of design of algorithms Analysis of algorithms: experimental and theoretical Asymptotic notations:O (upper bound),Θ (lower bound),Ω (tight bound)

Please read Chapter 0 Prologue in the textbook.

Page 46: CS483 Analysis of Algorithms Lecture 01jmlien/teaching/09_spring_cs... · Analysis of algorithms Asymptotic Notation Syllabus Summary Analysis of Algorithms CS483 Lecture 01-Introduction

Assignment

A Brief History

A Brief History (Cont.)

Fibonacci number

Design Algorithms

Analysis of algorithms

Asymptotic Notation

Syllabus

Summary

Summary

. Assignment

Analysis of Algorithms CS483 Lecture 01-Introduction – 45

Chapter 0, Exercise 1 Chapter 0, Exercise 2 Due Jan 30 2008, before the class.