algorithms (i) - sjtubasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · •...

107
Algorithms (I) Prologue Guoqiang Li School of Software, Shanghai Jiao Tong University

Upload: others

Post on 21-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Algorithms (I)Prologue

Guoqiang Li

School of Software, Shanghai Jiao Tong University

Page 2: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Instructor

• Guoqiang LI

• Homepage: http://basics.sjtu.edu.cn/˜liguoqiang• Course page:

http://basics.sjtu.edu.cn/˜liguoqiang/teaching/algo15b/index.htm• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167• weibo: weibo.com/flyinsail• QQ: 10474050

• TA:

• Xiuting TAO: [email protected]

Page 3: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Instructor

• Guoqiang LI• Homepage: http://basics.sjtu.edu.cn/˜liguoqiang• Course page:

http://basics.sjtu.edu.cn/˜liguoqiang/teaching/algo15b/index.htm• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167

• weibo: weibo.com/flyinsail• QQ: 10474050

• TA:

• Xiuting TAO: [email protected]

Page 4: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Instructor

• Guoqiang LI• Homepage: http://basics.sjtu.edu.cn/˜liguoqiang• Course page:

http://basics.sjtu.edu.cn/˜liguoqiang/teaching/algo15b/index.htm• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167• weibo: weibo.com/flyinsail• QQ: 10474050

• TA:

• Xiuting TAO: [email protected]

Page 5: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Instructor

• Guoqiang LI• Homepage: http://basics.sjtu.edu.cn/˜liguoqiang• Course page:

http://basics.sjtu.edu.cn/˜liguoqiang/teaching/algo15b/index.htm• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167• weibo: weibo.com/flyinsail• QQ: 10474050

• TA:• Xiuting TAO: [email protected]

Page 6: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Text Book

• Algorithms• Sanjoy Dasgupta

University of California• San Diego Christos Papadimitriou

University of California at Berkeley• Umesh Vazirani

University of California at Berkeley• McGraw-Hill, 2007.

• Available at:http://www.cs.berkeley.edu/˜vazirani/algorithms.html

• Also available at the course page.

Page 7: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Text Book

• Algorithms• Sanjoy Dasgupta

University of California• San Diego Christos Papadimitriou

University of California at Berkeley• Umesh Vazirani

University of California at Berkeley• McGraw-Hill, 2007.

• Available at:http://www.cs.berkeley.edu/˜vazirani/algorithms.html

• Also available at the course page.

Page 8: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Reference Book

• The Way of Algorithms• Henming Zou

Shanghai Jiao Tong University

Page 9: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Reference Book

• Introduction to Algorithms• Thomas H. Cormen• Charles E. Leiserson• Ronald L. Rivest• Clifford Stein

Page 10: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Reference Book

• The Art of Computer Programming• Donald E. Knuth

Page 11: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Scoring Policy

• 10% Attendance.• 30% Homework.• 60% Final exam.

Page 12: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Scoring Policy

• 10% Attendance.• 30% Homework.

• Four assignments.

• Each one is 10pts.• Work out individually.• Each assignment will be evaluated by A, B, C, D, F

(Excellent(10), Good(10), Fair(8), Delay(7), Fail(0))• Best three will be concluded as final score.

• 60% Final exam.

Page 13: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Scoring Policy

• 10% Attendance.• 30% Homework.

• Four assignments.• Each one is 10pts.

• Work out individually.• Each assignment will be evaluated by A, B, C, D, F

(Excellent(10), Good(10), Fair(8), Delay(7), Fail(0))• Best three will be concluded as final score.

• 60% Final exam.

Page 14: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Scoring Policy

• 10% Attendance.• 30% Homework.

• Four assignments.• Each one is 10pts.• Work out individually.

• Each assignment will be evaluated by A, B, C, D, F(Excellent(10), Good(10), Fair(8), Delay(7), Fail(0))

• Best three will be concluded as final score.

• 60% Final exam.

Page 15: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Scoring Policy

• 10% Attendance.• 30% Homework.

• Four assignments.• Each one is 10pts.• Work out individually.• Each assignment will be evaluated by A, B, C, D, F

(Excellent(10), Good(10), Fair(8), Delay(7), Fail(0))

• Best three will be concluded as final score.

• 60% Final exam.

Page 16: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Scoring Policy

• 10% Attendance.• 30% Homework.

• Four assignments.• Each one is 10pts.• Work out individually.• Each assignment will be evaluated by A, B, C, D, F

(Excellent(10), Good(10), Fair(8), Delay(7), Fail(0))• Best three will be concluded as final score.

• 60% Final exam.

Page 17: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Any Questions?

Page 18: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Chapter 0. Prologue

Page 19: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Johann Gutenberg

Johann Gutenberg ( 1398 - 1468)

Page 20: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Johann Gutenberg

Johann Gutenberg ( 1398 - 1468)

In 1448 in the German city ofMainz a goldsmith namedJohann Gutenberg discovered away to print books by puttingtogether movable metallicpieces.

Page 21: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Two Ideas Changed the World

• Because of the typography, literacy spread, the Dark Ages ended,the human intellect was liberated, science and technologytriumphed, the Industrial Revolution happened.

• Many historians say we owe all this to typography.• But others insist that the key development was not typography,

but algorithms.

Page 22: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Two Ideas Changed the World

• Because of the typography, literacy spread, the Dark Ages ended,the human intellect was liberated, science and technologytriumphed, the Industrial Revolution happened.

• Many historians say we owe all this to typography.

• But others insist that the key development was not typography,but algorithms.

Page 23: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Two Ideas Changed the World

• Because of the typography, literacy spread, the Dark Ages ended,the human intellect was liberated, science and technologytriumphed, the Industrial Revolution happened.

• Many historians say we owe all this to typography.• But others insist that the key development was not typography,

but algorithms.

Page 24: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Decimal System

• Gutenberg would write the number 1448 as MCDXLVIII .

• How do you add two Roman numerals? What is

MCDXLVIII + DCCCXII

• The decimal system was invented in India around AD 600.Using only 10 symbols, even very large numbers could bewritten down compactly, and arithmetic could be done efficientlyon them by following elementary steps.

Page 25: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Decimal System

• Gutenberg would write the number 1448 as MCDXLVIII .• How do you add two Roman numerals? What is

MCDXLVIII + DCCCXII

• The decimal system was invented in India around AD 600.Using only 10 symbols, even very large numbers could bewritten down compactly, and arithmetic could be done efficientlyon them by following elementary steps.

Page 26: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Decimal System

• Gutenberg would write the number 1448 as MCDXLVIII .• How do you add two Roman numerals? What is

MCDXLVIII + DCCCXII

• The decimal system was invented in India around AD 600.Using only 10 symbols, even very large numbers could bewritten down compactly, and arithmetic could be done efficientlyon them by following elementary steps.

Page 27: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Al Khwarizmi

Al Khwarizmi (780 - 850)

Page 28: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Al Khwarizmi

Al Khwarizmi (780 - 850)

In the 12th century, Latin translationsof his work on the Indian numerals,introduced the decimal system to theWestern world. (Source: Wikipedia)

Page 29: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Algorithms

• Al Khwarizmi laid out the basic methods for• adding,• multiplying,• dividing numbers,• extracting square roots,• calculating digits of π.

• These procedures were precise, unambiguous, mechanical,efficient, correct. They were algorithms, a term coined to honorthe wise man after the decimal system was finally adopted inEurope, many centuries later.

Page 30: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Algorithms

• Al Khwarizmi laid out the basic methods for• adding,• multiplying,• dividing numbers,• extracting square roots,• calculating digits of π.

• These procedures were precise, unambiguous, mechanical,efficient, correct. They were algorithms, a term coined to honorthe wise man after the decimal system was finally adopted inEurope, many centuries later.

Page 31: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

A step by step procedure for solving a problem or accomplishingsome end.

An abstract recipe, prescribing a process which may be carriedout by a human, a computer or by other means.

Any well-defined computational procedure that makes somevalue, or set of values, as input and produces some value, of setof values, as output. An algorithm is thus a finite sequence ofcomputational steps that transform the input into the output.

Page 32: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

A step by step procedure for solving a problem or accomplishingsome end.

An abstract recipe, prescribing a process which may be carriedout by a human, a computer or by other means.

Any well-defined computational procedure that makes somevalue, or set of values, as input and produces some value, of setof values, as output. An algorithm is thus a finite sequence ofcomputational steps that transform the input into the output.

Page 33: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

A step by step procedure for solving a problem or accomplishingsome end.

An abstract recipe, prescribing a process which may be carriedout by a human, a computer or by other means.

Any well-defined computational procedure that makes somevalue, or set of values, as input and produces some value, of setof values, as output. An algorithm is thus a finite sequence ofcomputational steps that transform the input into the output.

Page 34: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of

• a finite set of instructions which,• given an input from some set of possible inputs,• enables us to obtain an output through a systematic

execution of the instructions• that terminates in a finite number of steps.

A program is

• an implementation of an algorithm, or algorithms.• A program does not necessarily terminate.

Page 35: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of• a finite set of instructions which,

• given an input from some set of possible inputs,• enables us to obtain an output through a systematic

execution of the instructions• that terminates in a finite number of steps.

A program is

• an implementation of an algorithm, or algorithms.• A program does not necessarily terminate.

Page 36: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of• a finite set of instructions which,• given an input from some set of possible inputs,

• enables us to obtain an output through a systematicexecution of the instructions

• that terminates in a finite number of steps.

A program is

• an implementation of an algorithm, or algorithms.• A program does not necessarily terminate.

Page 37: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of• a finite set of instructions which,• given an input from some set of possible inputs,• enables us to obtain an output through a systematic

execution of the instructions

• that terminates in a finite number of steps.

A program is

• an implementation of an algorithm, or algorithms.• A program does not necessarily terminate.

Page 38: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of• a finite set of instructions which,• given an input from some set of possible inputs,• enables us to obtain an output through a systematic

execution of the instructions• that terminates in a finite number of steps.

A program is

• an implementation of an algorithm, or algorithms.• A program does not necessarily terminate.

Page 39: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of• a finite set of instructions which,• given an input from some set of possible inputs,• enables us to obtain an output through a systematic

execution of the instructions• that terminates in a finite number of steps.

A program is

• an implementation of an algorithm, or algorithms.• A program does not necessarily terminate.

Page 40: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of• a finite set of instructions which,• given an input from some set of possible inputs,• enables us to obtain an output through a systematic

execution of the instructions• that terminates in a finite number of steps.

A program is• an implementation of an algorithm, or algorithms.

• A program does not necessarily terminate.

Page 41: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

What Is An Algorithm

An algorithm is a procedure that consists of• a finite set of instructions which,• given an input from some set of possible inputs,• enables us to obtain an output through a systematic

execution of the instructions• that terminates in a finite number of steps.

A program is• an implementation of an algorithm, or algorithms.• A program does not necessarily terminate.

Page 42: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Leonardo Fibonacci

Leonardo Fibonacci (1170 - 1250)

• Fibonacci helped the spread of the decimal system in Europe,primarily through the publication in the early 13th century of hisBook of Calculation, the Liber Abaci. (Source: Wikipedia)

Page 43: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Leonardo Fibonacci

Leonardo Fibonacci (1170 - 1250)

• Fibonacci helped the spread of the decimal system in Europe,primarily through the publication in the early 13th century of hisBook of Calculation, the Liber Abaci. (Source: Wikipedia)

Page 44: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .

Formally,

Fn =

0 if n = 01 if n = 1Fn−1 + Fn−2 if n > 1

Q: What is F100 or F200?

Page 45: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .

Formally,

Fn =

0 if n = 01 if n = 1Fn−1 + Fn−2 if n > 1

Q: What is F100 or F200?

Page 46: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Fibonacci Sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .

Formally,

Fn =

0 if n = 01 if n = 1Fn−1 + Fn−2 if n > 1

Q: What is F100 or F200?

Page 47: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

An Exponential Algorithm

FIBO1(n)a nature number n;

if n = 0 then return(0);if n = 1 then return(1);return(FIBO1(n− 1)+FIBO1(n− 2));

Page 48: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Three Questions about An Algorithm

• Is it correct?• How much time does it take, as a function of n?• Can we do better?

The first question is trivial here, as this algorithm is preciselyFibonacci’s definition of Fn

Page 49: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Three Questions about An Algorithm

• Is it correct?• How much time does it take, as a function of n?• Can we do better?

The first question is trivial here, as this algorithm is preciselyFibonacci’s definition of Fn

Page 50: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

How Much Time

• Let T(n) be the number of computer steps needed to computeFIBO1(n)

• For n ≤ 1,T(n) ≤ 2

• For n ≥ 1,T(n) = T(n− 1) + T(n− 2) + 3

• It is easy to shown, for all n ∈ N,

T(n) ≥ Fn

• It is exponential to n.

Page 51: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

How Much Time

• Let T(n) be the number of computer steps needed to computeFIBO1(n)

• For n ≤ 1,T(n) ≤ 2

• For n ≥ 1,T(n) = T(n− 1) + T(n− 2) + 3

• It is easy to shown, for all n ∈ N,

T(n) ≥ Fn

• It is exponential to n.

Page 52: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

How Much Time

• Let T(n) be the number of computer steps needed to computeFIBO1(n)

• For n ≤ 1,T(n) ≤ 2

• For n ≥ 1,T(n) = T(n− 1) + T(n− 2) + 3

• It is easy to shown, for all n ∈ N,

T(n) ≥ Fn

• It is exponential to n.

Page 53: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

How Much Time

• Let T(n) be the number of computer steps needed to computeFIBO1(n)

• For n ≤ 1,T(n) ≤ 2

• For n ≥ 1,T(n) = T(n− 1) + T(n− 2) + 3

• It is easy to shown, for all n ∈ N,

T(n) ≥ Fn

• It is exponential to n.

Page 54: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Exponential Is Bad?

T(200) ≥ F200 ≥ 2138 ≈ 2.56× 1042

• In 2010, the fastest computer in the world is the Tianhe-1Asystem at the National Supercomputer Center in Tianjin.

• Its speed is2.57× 1015

steps per second.• Thus to compute F200 Tianhe-1A needs roughly

1027 seconds ≥ 1022 years.• At this time, the fastest computer is Tianhe-2. Its speed is

3.37× 1016 steps per second.• In 2013, the fastest is NUDT Tianhe-2, 3.386× 1016 per second.

Page 55: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Exponential Is Bad?

T(200) ≥ F200 ≥ 2138 ≈ 2.56× 1042

• In 2010, the fastest computer in the world is the Tianhe-1Asystem at the National Supercomputer Center in Tianjin.

• Its speed is2.57× 1015

steps per second.• Thus to compute F200 Tianhe-1A needs roughly

1027 seconds ≥ 1022 years.• At this time, the fastest computer is Tianhe-2. Its speed is

3.37× 1016 steps per second.• In 2013, the fastest is NUDT Tianhe-2, 3.386× 1016 per second.

Page 56: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Exponential Is Bad?

T(200) ≥ F200 ≥ 2138 ≈ 2.56× 1042

• In 2010, the fastest computer in the world is the Tianhe-1Asystem at the National Supercomputer Center in Tianjin.

• Its speed is2.57× 1015

steps per second.

• Thus to compute F200 Tianhe-1A needs roughly

1027 seconds ≥ 1022 years.• At this time, the fastest computer is Tianhe-2. Its speed is

3.37× 1016 steps per second.• In 2013, the fastest is NUDT Tianhe-2, 3.386× 1016 per second.

Page 57: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Exponential Is Bad?

T(200) ≥ F200 ≥ 2138 ≈ 2.56× 1042

• In 2010, the fastest computer in the world is the Tianhe-1Asystem at the National Supercomputer Center in Tianjin.

• Its speed is2.57× 1015

steps per second.• Thus to compute F200 Tianhe-1A needs roughly

1027 seconds ≥ 1022 years.

• At this time, the fastest computer is Tianhe-2. Its speed is3.37× 1016 steps per second.

• In 2013, the fastest is NUDT Tianhe-2, 3.386× 1016 per second.

Page 58: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Exponential Is Bad?

T(200) ≥ F200 ≥ 2138 ≈ 2.56× 1042

• In 2010, the fastest computer in the world is the Tianhe-1Asystem at the National Supercomputer Center in Tianjin.

• Its speed is2.57× 1015

steps per second.• Thus to compute F200 Tianhe-1A needs roughly

1027 seconds ≥ 1022 years.• At this time, the fastest computer is Tianhe-2. Its speed is

3.37× 1016 steps per second.

• In 2013, the fastest is NUDT Tianhe-2, 3.386× 1016 per second.

Page 59: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Exponential Is Bad?

T(200) ≥ F200 ≥ 2138 ≈ 2.56× 1042

• In 2010, the fastest computer in the world is the Tianhe-1Asystem at the National Supercomputer Center in Tianjin.

• Its speed is2.57× 1015

steps per second.• Thus to compute F200 Tianhe-1A needs roughly

1027 seconds ≥ 1022 years.• At this time, the fastest computer is Tianhe-2. Its speed is

3.37× 1016 steps per second.• In 2013, the fastest is NUDT Tianhe-2, 3.386× 1016 per second.

Page 60: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Moore’s Law

Moore’s Law:Computer speeds have been doubling roughly every 18 months.

• The running time of FIBO1 is proportional to

20.694n ≈ 1.6n

Thus, it takes 1.6 times longer to compute Fn+1 than Fn.• So if we can reasonably compute F100 with this year’s

technology, then next year we will manage F101, and so on …• Just one more number every year!• Such is the curse of exponential time.

Page 61: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Moore’s Law

Moore’s Law:Computer speeds have been doubling roughly every 18 months.

• The running time of FIBO1 is proportional to

20.694n ≈ 1.6n

Thus, it takes 1.6 times longer to compute Fn+1 than Fn.

• So if we can reasonably compute F100 with this year’stechnology, then next year we will manage F101, and so on …

• Just one more number every year!• Such is the curse of exponential time.

Page 62: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Moore’s Law

Moore’s Law:Computer speeds have been doubling roughly every 18 months.

• The running time of FIBO1 is proportional to

20.694n ≈ 1.6n

Thus, it takes 1.6 times longer to compute Fn+1 than Fn.• So if we can reasonably compute F100 with this year’s

technology, then next year we will manage F101, and so on …

• Just one more number every year!• Such is the curse of exponential time.

Page 63: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Moore’s Law

Moore’s Law:Computer speeds have been doubling roughly every 18 months.

• The running time of FIBO1 is proportional to

20.694n ≈ 1.6n

Thus, it takes 1.6 times longer to compute Fn+1 than Fn.• So if we can reasonably compute F100 with this year’s

technology, then next year we will manage F101, and so on …• Just one more number every year!

• Such is the curse of exponential time.

Page 64: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Moore’s Law

Moore’s Law:Computer speeds have been doubling roughly every 18 months.

• The running time of FIBO1 is proportional to

20.694n ≈ 1.6n

Thus, it takes 1.6 times longer to compute Fn+1 than Fn.• So if we can reasonably compute F100 with this year’s

technology, then next year we will manage F101, and so on …• Just one more number every year!• Such is the curse of exponential time.

Page 65: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Three Questions

• Is it correct?• How much time does it take, as a function of n?• Can we do better?

Now we know FIB1(n) is correct and inefficient, so can we dobetter?

Page 66: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Three Questions

• Is it correct?• How much time does it take, as a function of n?• Can we do better?

Now we know FIB1(n) is correct and inefficient, so can we dobetter?

Page 67: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

An Polynomial Algorithm

FIBO2(n)a nature number n;

if n = 0 then return(0);create an array f [0 . . . n];f [0] = 0, f [1] = 1;for i = 2 to n do

f [i] = f [i − 1] + f [i − 2];endreturn(f [n]);

Page 68: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

An Analysis

• The correctness of FIBO2 is trivial.• How long does it take?

• The inner loop consists of a single computer step and is executedn− 1 times. Therefore the number of computer steps used byFIBO2 is linear in n.

Page 69: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

An Analysis

• The correctness of FIBO2 is trivial.• How long does it take?• The inner loop consists of a single computer step and is executed

n− 1 times. Therefore the number of computer steps used byFIBO2 is linear in n.

Page 70: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We have been counting the number of basic computer stepsexecuted by each algorithm and thinking of these basic steps astaking a constant amount of time.

• It is reasonable to treat addition as a single computer step if smallnumbers are being added, e.g., 32-bit numbers.

• However, the n-th Fibonacci number is about 0.694n bits long,and this can far exceed 32 as n grows.

• Arithmetic operations on arbitrarily large numbers cannotpossibly be performed in a single, constant-time step.

Page 71: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We have been counting the number of basic computer stepsexecuted by each algorithm and thinking of these basic steps astaking a constant amount of time.

• It is reasonable to treat addition as a single computer step if smallnumbers are being added, e.g., 32-bit numbers.

• However, the n-th Fibonacci number is about 0.694n bits long,and this can far exceed 32 as n grows.

• Arithmetic operations on arbitrarily large numbers cannotpossibly be performed in a single, constant-time step.

Page 72: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We have been counting the number of basic computer stepsexecuted by each algorithm and thinking of these basic steps astaking a constant amount of time.

• It is reasonable to treat addition as a single computer step if smallnumbers are being added, e.g., 32-bit numbers.

• However, the n-th Fibonacci number is about 0.694n bits long,and this can far exceed 32 as n grows.

• Arithmetic operations on arbitrarily large numbers cannotpossibly be performed in a single, constant-time step.

Page 73: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We have been counting the number of basic computer stepsexecuted by each algorithm and thinking of these basic steps astaking a constant amount of time.

• It is reasonable to treat addition as a single computer step if smallnumbers are being added, e.g., 32-bit numbers.

• However, the n-th Fibonacci number is about 0.694n bits long,and this can far exceed 32 as n grows.

• Arithmetic operations on arbitrarily large numbers cannotpossibly be performed in a single, constant-time step.

Page 74: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We will see that the addition of two n-bit numbers takes timeroughly proportional to n.

• Thus FIBO1, which performs about Fn additions, uses a numberof basic step roughly proportional to nFn.

• Likewise, the number of steps taken by FIBO2 is proportional ton2, and still polynomial in n.

• Q: Can we do better?

• Exercise 0.4

Page 75: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We will see that the addition of two n-bit numbers takes timeroughly proportional to n.

• Thus FIBO1, which performs about Fn additions, uses a numberof basic step roughly proportional to nFn.

• Likewise, the number of steps taken by FIBO2 is proportional ton2, and still polynomial in n.

• Q: Can we do better?

• Exercise 0.4

Page 76: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We will see that the addition of two n-bit numbers takes timeroughly proportional to n.

• Thus FIBO1, which performs about Fn additions, uses a numberof basic step roughly proportional to nFn.

• Likewise, the number of steps taken by FIBO2 is proportional ton2, and still polynomial in n.

• Q: Can we do better?

• Exercise 0.4

Page 77: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We will see that the addition of two n-bit numbers takes timeroughly proportional to n.

• Thus FIBO1, which performs about Fn additions, uses a numberof basic step roughly proportional to nFn.

• Likewise, the number of steps taken by FIBO2 is proportional ton2, and still polynomial in n.

• Q: Can we do better?

• Exercise 0.4

Page 78: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

A More Careful Analysis

• We will see that the addition of two n-bit numbers takes timeroughly proportional to n.

• Thus FIBO1, which performs about Fn additions, uses a numberof basic step roughly proportional to nFn.

• Likewise, the number of steps taken by FIBO2 is proportional ton2, and still polynomial in n.

• Q: Can we do better?• Exercise 0.4

Page 79: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• We’ve seen how sloppiness in the analysis of running times canlead to unacceptable inaccuracy.

• But it is also possible to be too precise to be useful.• Expressing running time in terms of basic computer steps is

already a simplification. The time taken by one such stepdepends crucially on the particular processor, etc.

• Accounting for these architecture-specific details is toocomplicated and yields a result that does not generalize from onecomputer to the next.

Page 80: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• We’ve seen how sloppiness in the analysis of running times canlead to unacceptable inaccuracy.

• But it is also possible to be too precise to be useful.

• Expressing running time in terms of basic computer steps isalready a simplification. The time taken by one such stepdepends crucially on the particular processor, etc.

• Accounting for these architecture-specific details is toocomplicated and yields a result that does not generalize from onecomputer to the next.

Page 81: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• We’ve seen how sloppiness in the analysis of running times canlead to unacceptable inaccuracy.

• But it is also possible to be too precise to be useful.• Expressing running time in terms of basic computer steps is

already a simplification. The time taken by one such stepdepends crucially on the particular processor, etc.

• Accounting for these architecture-specific details is toocomplicated and yields a result that does not generalize from onecomputer to the next.

Page 82: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• We’ve seen how sloppiness in the analysis of running times canlead to unacceptable inaccuracy.

• But it is also possible to be too precise to be useful.• Expressing running time in terms of basic computer steps is

already a simplification. The time taken by one such stepdepends crucially on the particular processor, etc.

• Accounting for these architecture-specific details is toocomplicated and yields a result that does not generalize from onecomputer to the next.

Page 83: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• It therefore makes more sense to seek a machine independentcharacterization of an algorithm’s efficiency.

• To this end, we will always express running time by counting thenumber of basic computer steps, as a function of the size of theinput.

• Moreover, instead of reporting that an algorithm takes, say,7n3 + 4n + 1 steps on an input of size n, it is much simpler toleave out lower-order terms such as 4n and 1.

• Even the detail of the coefficient 7 in the leading term (computerswill be seven times faster in a few years anyway), and just saythat the algorithm takes time O(n3) (pronounced big oh of n3).

Page 84: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• It therefore makes more sense to seek a machine independentcharacterization of an algorithm’s efficiency.

• To this end, we will always express running time by counting thenumber of basic computer steps, as a function of the size of theinput.

• Moreover, instead of reporting that an algorithm takes, say,7n3 + 4n + 1 steps on an input of size n, it is much simpler toleave out lower-order terms such as 4n and 1.

• Even the detail of the coefficient 7 in the leading term (computerswill be seven times faster in a few years anyway), and just saythat the algorithm takes time O(n3) (pronounced big oh of n3).

Page 85: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• It therefore makes more sense to seek a machine independentcharacterization of an algorithm’s efficiency.

• To this end, we will always express running time by counting thenumber of basic computer steps, as a function of the size of theinput.

• Moreover, instead of reporting that an algorithm takes, say,7n3 + 4n + 1 steps on an input of size n, it is much simpler toleave out lower-order terms such as 4n and 1.

• Even the detail of the coefficient 7 in the leading term (computerswill be seven times faster in a few years anyway), and just saythat the algorithm takes time O(n3) (pronounced big oh of n3).

Page 86: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Counting the Number of Steps

• It therefore makes more sense to seek a machine independentcharacterization of an algorithm’s efficiency.

• To this end, we will always express running time by counting thenumber of basic computer steps, as a function of the size of theinput.

• Moreover, instead of reporting that an algorithm takes, say,7n3 + 4n + 1 steps on an input of size n, it is much simpler toleave out lower-order terms such as 4n and 1.

• Even the detail of the coefficient 7 in the leading term (computerswill be seven times faster in a few years anyway), and just saythat the algorithm takes time O(n3) (pronounced big oh of n3).

Page 87: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Big-O Notation

• f (n) and g(n) are the running times of two algorithms on inputsof size n.

• Let f (n) and g(n) be functions from positive integers to positivereals.

• We say f = O(g) (which means that “f grows no faster than g”) ifthere is a constant c > 0 such that f (n) ≤ c · g(n).

• f = O(g) is very loose analog of “f ≤ g”. It differs from theusual notion of ≤ because of the constant c, so that for instance10n = O(n).

Page 88: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Big-O Notation

• f (n) and g(n) are the running times of two algorithms on inputsof size n.

• Let f (n) and g(n) be functions from positive integers to positivereals.

• We say f = O(g) (which means that “f grows no faster than g”) ifthere is a constant c > 0 such that f (n) ≤ c · g(n).

• f = O(g) is very loose analog of “f ≤ g”. It differs from theusual notion of ≤ because of the constant c, so that for instance10n = O(n).

Page 89: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Big-O Notation

• f (n) and g(n) are the running times of two algorithms on inputsof size n.

• Let f (n) and g(n) be functions from positive integers to positivereals.

• We say f = O(g) (which means that “f grows no faster than g”) ifthere is a constant c > 0 such that f (n) ≤ c · g(n).

• f = O(g) is very loose analog of “f ≤ g”. It differs from theusual notion of ≤ because of the constant c, so that for instance10n = O(n).

Page 90: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Big-O Notation

• f (n) and g(n) are the running times of two algorithms on inputsof size n.

• Let f (n) and g(n) be functions from positive integers to positivereals.

• We say f = O(g) (which means that “f grows no faster than g”) ifthere is a constant c > 0 such that f (n) ≤ c · g(n).

• f = O(g) is very loose analog of “f ≤ g”. It differs from theusual notion of ≤ because of the constant c, so that for instance10n = O(n).

Page 91: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• We are choosing between two algorithms: One takes f1(n) = n2

steps, while the other takes f2(n) = 2n + 20 steps.

• Which is better?• The answer depends on n:

• If n ≤ 5, then f1(n) ≤ f2(n).• If n > 5, then f1(n) > f2(n).

• f2 scales much better as n grows, and therefore it is superior.

Page 92: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• We are choosing between two algorithms: One takes f1(n) = n2

steps, while the other takes f2(n) = 2n + 20 steps.• Which is better?

• The answer depends on n:

• If n ≤ 5, then f1(n) ≤ f2(n).• If n > 5, then f1(n) > f2(n).

• f2 scales much better as n grows, and therefore it is superior.

Page 93: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• We are choosing between two algorithms: One takes f1(n) = n2

steps, while the other takes f2(n) = 2n + 20 steps.• Which is better?• The answer depends on n:

• If n ≤ 5, then f1(n) ≤ f2(n).• If n > 5, then f1(n) > f2(n).

• f2 scales much better as n grows, and therefore it is superior.

Page 94: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• This superiority is captured by the big-O notion: f2 = O(f1).

f2(n)

f1(n)=

2n + 20n2 ≤ 22

for all n ∈ N.

• On the other hand, f1 6= O(f2), since the ratio

f1(n)

f2(n)=

n2

2n + 20

can get arbitrarily large.

Page 95: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• This superiority is captured by the big-O notion: f2 = O(f1).

f2(n)

f1(n)=

2n + 20n2 ≤ 22

for all n ∈ N.• On the other hand, f1 6= O(f2), since the ratio

f1(n)

f2(n)=

n2

2n + 20

can get arbitrarily large.

Page 96: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• Recall f1(n) = n2 and f2(n) = 2n + 20, suppose we have a thirdalgorithm which uses f3(n) = n + 1 steps.

• Is this better than f2?• Certainly, but only by a constant factor.• The discrepancy between f2 and f3 is tiny compared to the huge

gap between f1 and f2.• In order to stay focused on the big picture, we treat functions as

equivalent if they differ only by multiplicative constants.• Returning to the definition of big-O, we see that f2 = O(f3) and

f3 = O(f2).

Page 97: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• Recall f1(n) = n2 and f2(n) = 2n + 20, suppose we have a thirdalgorithm which uses f3(n) = n + 1 steps.

• Is this better than f2?

• Certainly, but only by a constant factor.• The discrepancy between f2 and f3 is tiny compared to the huge

gap between f1 and f2.• In order to stay focused on the big picture, we treat functions as

equivalent if they differ only by multiplicative constants.• Returning to the definition of big-O, we see that f2 = O(f3) and

f3 = O(f2).

Page 98: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• Recall f1(n) = n2 and f2(n) = 2n + 20, suppose we have a thirdalgorithm which uses f3(n) = n + 1 steps.

• Is this better than f2?• Certainly, but only by a constant factor.

• The discrepancy between f2 and f3 is tiny compared to the hugegap between f1 and f2.

• In order to stay focused on the big picture, we treat functions asequivalent if they differ only by multiplicative constants.

• Returning to the definition of big-O, we see that f2 = O(f3) andf3 = O(f2).

Page 99: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• Recall f1(n) = n2 and f2(n) = 2n + 20, suppose we have a thirdalgorithm which uses f3(n) = n + 1 steps.

• Is this better than f2?• Certainly, but only by a constant factor.• The discrepancy between f2 and f3 is tiny compared to the huge

gap between f1 and f2.

• In order to stay focused on the big picture, we treat functions asequivalent if they differ only by multiplicative constants.

• Returning to the definition of big-O, we see that f2 = O(f3) andf3 = O(f2).

Page 100: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• Recall f1(n) = n2 and f2(n) = 2n + 20, suppose we have a thirdalgorithm which uses f3(n) = n + 1 steps.

• Is this better than f2?• Certainly, but only by a constant factor.• The discrepancy between f2 and f3 is tiny compared to the huge

gap between f1 and f2.• In order to stay focused on the big picture, we treat functions as

equivalent if they differ only by multiplicative constants.

• Returning to the definition of big-O, we see that f2 = O(f3) andf3 = O(f2).

Page 101: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Why Disregard the Constant?

• Recall f1(n) = n2 and f2(n) = 2n + 20, suppose we have a thirdalgorithm which uses f3(n) = n + 1 steps.

• Is this better than f2?• Certainly, but only by a constant factor.• The discrepancy between f2 and f3 is tiny compared to the huge

gap between f1 and f2.• In order to stay focused on the big picture, we treat functions as

equivalent if they differ only by multiplicative constants.• Returning to the definition of big-O, we see that f2 = O(f3) and

f3 = O(f2).

Page 102: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Other Similar Notations

• Just as O(·) is an analog of ≤, we also define analogs of ≥ and =as follows,

• f = Ω(g) means g = O(f ).• f = Θ(g) means f = Ω(g) and f = O(g).

• Recall f1(n) = n2, f2(n) = 2n + 20, and f3(n) = n + 1, then

• f2 = Θ(f3) and f1 = Ω(f2)

Page 103: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Other Similar Notations

• Just as O(·) is an analog of ≤, we also define analogs of ≥ and =as follows,

• f = Ω(g) means g = O(f ).• f = Θ(g) means f = Ω(g) and f = O(g).

• Recall f1(n) = n2, f2(n) = 2n + 20, and f3(n) = n + 1, then

• f2 = Θ(f3) and f1 = Ω(f2)

Page 104: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Other Similar Notations

• Just as O(·) is an analog of ≤, we also define analogs of ≥ and =as follows,

• f = Ω(g) means g = O(f ).• f = Θ(g) means f = Ω(g) and f = O(g).

• Recall f1(n) = n2, f2(n) = 2n + 20, and f3(n) = n + 1, then• f2 = Θ(f3) and f1 = Ω(f2)

Page 105: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Some Simple Rules

• Big-O notation lets us focus on the big picture. When faced witha complicated function like 3n2 + 4n + 5, we just replace it withO(f (n)), where f (n) is as simple as possible.

• In this particular example, we’d use O(n2), because the quadraticportion of the sum dominates the rest.

• Here are some commonsense rules:

• Multiplicative constants can be omitted: 14n2 becomes n2.• na dominates nb if a > b, for instance, n2 dominates n• an dominates bn if a > b, for instance, 3n dominates 2n

• Any exponential dominates any polynomial: 3n dominates n5

• Any polynomial dominates any logarithm: n dominates (log n)3.This also means, for example, that n2 dominates n log n.

Page 106: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Some Simple Rules

• Big-O notation lets us focus on the big picture. When faced witha complicated function like 3n2 + 4n + 5, we just replace it withO(f (n)), where f (n) is as simple as possible.

• In this particular example, we’d use O(n2), because the quadraticportion of the sum dominates the rest.

• Here are some commonsense rules:

• Multiplicative constants can be omitted: 14n2 becomes n2.• na dominates nb if a > b, for instance, n2 dominates n• an dominates bn if a > b, for instance, 3n dominates 2n

• Any exponential dominates any polynomial: 3n dominates n5

• Any polynomial dominates any logarithm: n dominates (log n)3.This also means, for example, that n2 dominates n log n.

Page 107: Algorithms (I) - SJTUbasics.sjtu.edu.cn/~liguoqiang/teaching/algo/lectures/algo1.pdf · • 60%Final exam. Any Questions? Chapter 0. Prologue. Johann Gutenberg Johann Gutenberg (

Some Simple Rules

• Big-O notation lets us focus on the big picture. When faced witha complicated function like 3n2 + 4n + 5, we just replace it withO(f (n)), where f (n) is as simple as possible.

• In this particular example, we’d use O(n2), because the quadraticportion of the sum dominates the rest.

• Here are some commonsense rules:• Multiplicative constants can be omitted: 14n2 becomes n2.• na dominates nb if a > b, for instance, n2 dominates n• an dominates bn if a > b, for instance, 3n dominates 2n

• Any exponential dominates any polynomial: 3n dominates n5

• Any polynomial dominates any logarithm: n dominates (log n)3.This also means, for example, that n2 dominates n log n.