© 2006–2011 antonio carzaniga 1
TRANSCRIPT
Algorithms and Data StructuresCourse Introduction
Antonio Carzaniga
Faculty of InformaticsUniversity of Lugano
September 20, 2011
© 2006–2011 Antonio Carzaniga 1
General Information
On-line course information
ñ on Moodle: ‘INFO.M053’http://www.icorsi.ch/course/view.php?id=3777
ñ and on my web page:http://www.inf.usi.ch/carzaniga/edu/algo/
ñ last edition also on-line:http://www.inf.usi.ch/carzaniga/edu/algo10f/
Announcements
ñ you are responsible for reading the announcements page orthe messages sent through Moodle
Office hours
ñ Antonio Carzaniga: by appointmentñ Koorosh Khazaei: by appointment
© 2006–2011 Antonio Carzaniga 2
Textbook
Introduction to Algorithms
Third Edition
Thomas H. CormenCharles E. LeisersonRonald L. RivestClifford Stein
The MIT Press
© 2006–2011 Antonio Carzaniga 3
Additional Readings (Textbook 2)
Algorithms
Sanjoy DasguptaChristos H. PapadimitriouUmesh V. Vazirani
Mc Graw-Hill
© 2006–2011 Antonio Carzaniga 4
Evaluation
+30% projectsñ 3–5 assignmentsñ grades added together, thus resulting in a weighted average
+30% midterm exam
+40% final exam
±10% instructor’s discretionary evaluation
ñ participationñ extra creditsñ trajectoryñ . . .
−100% plagiarism penalties
© 2006–2011 Antonio Carzaniga 5
Plagiarism
A student should never take someone else’s material and present itas his or her own. Doing so means committing plagiarism.
“material” means ideas, words, code, suggestions, correctionson one’s work, etc.
Using someone else’s material may be appropriateñ e.g., software librariesñ always clearly identify the external material, and acknowledge
its source. Failing to do so means committing plagiarism.ñ the work will be evaluated based on its added value
Plagiarism on an assignment or an exam will result in
ñ failing that assignment or that examñ loosing one or more points in the final note!
Penalties may be escalated in accordance with the regulationsof the Faculty of Informatics
© 2006–2011 Antonio Carzaniga 6
Deadlines
Deadlines are firm.
Exceptions may be granted
ñ at the instructor’s discretion
ñ only for documented medical conditions or other documentedemergencies
Each late day will reduce the assignment’s grade by one thirdof the total value of that assignment
ñ Corollary 1. The grade of an assignment turned in more thantwo days late is 0
ñ The proof of Corollary 1 is left as an exercise
© 2006–2011 Antonio Carzaniga 7
Now let’s move on to the realinteresting and fun stuff. . .
© 2006–2011 Antonio Carzaniga 8
Fundamental Ideas
Johannes Gutenberg invents movable type and the printing pressin Mainz, circa 1450 AD (already known in China, circa 1200 AD)
© 2006–2011 Antonio Carzaniga 9
Maybe More Fundamental Ideas
The decimal numbering system(India, circa 600 AD)
Persian mathematician Khwarizmıwrites a book (Baghdad, circa 830 AD)
ñ methods for adding, multiplying,and dividing numbers (and more)
ñ these procedures were precise,unambiguous, mechanical, efficient,and correct
ñ they were algorithms! Muhammad ibn Musaal-Khwarizmı
© 2006–2011 Antonio Carzaniga 10
Example
A sequence of numbers
0,1,1,2,3,5,8,13,21,34, . . .
The well-known Fibonacci sequence
Leonardo da Pisa (ca. 1170–ca. 1250)son of Guglielmo “Bonaccio”a.k.a. Leonardo Fibonacci
© 2006–2011 Antonio Carzaniga 11
The Fibonacci Sequence
Mathematical definition: Fn =
0 if n = 0
1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
“pseudo-code”
Fibonacci(n)1 if n == 02 return 03 elseif n == 14 return 15 else return Fibonacci(n− 1)+ Fibonacci(n− 2)
© 2006–2011 Antonio Carzaniga 12
Questions on Our First Algorithm
Fibonacci(n)1 if n == 02 return 03 elseif n == 14 return 15 else return Fibonacci(n− 1)+ Fibonacci(n− 2)
1. Is the algorithm correct?ñ for every valid input, does it terminate?ñ if so, does it do the right thing?
2. How much time does it take to complete?
3. Can we do better?© 2006–2011 Antonio Carzaniga 13
Correctness
Fibonacci(n)1 if n == 02 return 03 elseif n == 14 return 15 else return Fibonacci(n− 1)+ Fibonacci(n− 2)
Fn =
0 if n = 0
1 if n = 1
Fn−1 + Fn−2 if n > 1
The algorithm is clearly correctñ assuming n ≥ 0
© 2006–2011 Antonio Carzaniga 14
Performance
How long does it take?
Let’s try it out. . .
© 2006–2011 Antonio Carzaniga 15
Results
20 25 30 35 40 45 500
20
40
60
n
runnin
gti
me
(sec
onds)
RubySchemePython
CC-wizJava
C-gcc
© 2006–2011 Antonio Carzaniga 16
Comments
Different implementations perform differently
ñ it is better to let the compiler do the optimization
ñ simple language tricks don’t seem to pay off
However, the differences do not seem to be substantial
ñ all implementations sooner or later seem to hit a wall. . .
Conclusion: the problem is with the algorithm
© 2006–2011 Antonio Carzaniga 17
Complexity of Our First Algorithm
We need a mathematical characterization of the performanceof the algorithm
We’ll call it the algorithm’s computational complexity
Let T (n) be the number of basic steps needed to computeFibonacci(n)
Fibonacci(n)1 if n == 02 return 03 elseif n == 14 return 15 else return Fibonacci(n− 1)+ Fibonacci(n− 2)
T (0) = 2;T (1) = 3
T (n) = T (n− 1)+ T (n− 2)+ 3 ⇒ T (n) ≥ Fn
© 2006–2011 Antonio Carzaniga 18
Complexity of Our First Algorithm (2)
So, let’s try to understand how Fn grows with n
T (n) ≥ Fn = Fn−1 + Fn−2
Now, since Fn ≥ Fn−1 ≥ Fn−2 ≥ Fn−3 ≥ . . .
Fn ≥ 2Fn−2 ≥ 2(2Fn−4) ≥ 2(2(2Fn−6)) ≥ . . . ≥ 2n2
This means that
T (n) ≥ (√
2)n ≈ (1.4)n
T (n) grows exponentially with n
Can we do better?
© 2006–2011 Antonio Carzaniga 19
A Better Algorithm
Again, the sequence is 0,1,1,2,3,5,8,13,21,34, . . .
Idea: we can build Fn from the ground up!
SmartFibonacci(n)1 if n == 02 return 03 elseif n == 14 return 15 else pprev = 06 prev = 17 for i = 2 to n8 f = prev + pprev9 pprev = prev
10 prev = f11 return f
© 2006–2011 Antonio Carzaniga 20
Results
20 40 60 80 100 120 140 160 180 2000
20
40
60
n
runnin
gti
me
(sec
onds)
RubySchemePython
CC-wizJava
C-gcc(Python) SmartFibonacci
© 2006–2011 Antonio Carzaniga 21
Complexity of SmartFibonacci
SmartFibonacci(n)1 if n == 02 return 03 elseif n == 14 return 15 else prev = 06 pprev = 17 for i = 2 to n8 f = prev + pprev9 pprev = prev
10 prev = f11 return f
T (n) = 6+ 6(n− 1) = 6n
The complexity of SmartFibonacci(n) is linear in n© 2006–2011 Antonio Carzaniga 22