Lecture 1
Lecture 1
Course Introduction
Department of Computing
© Algorithms
Textbook
Introduction to Algorithms
Third Edition
Thomas H. Cormen
Charles E. Leiserson
Ronald L. Rivest
Clifford Stein
© Algorithms
Evaluation
5% Assignments
▶ 3–5 assignments
▶ grades added together, thus resulting in a weighted average
▶ In order to deter copying in assignments, I sometimes take quiz from
the assignment on the day of submission.
15% Quizzes
▶ Taken to ensure students study regularly
▶ Can be announced or unannounced.
© Algorithms
Evaluation
5% Assignments
▶ 3–5 assignments
▶ grades added together, thus resulting in a weighted average
▶ In order to deter copying in assignments, I sometimes take quiz from
the assignment on the day of submission.
15% Quizzes
▶ Taken to ensure students study regularly
▶ Can be announced or unannounced.
© Algorithms
Plagiarism
© Algorithms
Plagiarism
A student should never take someone else’s material and present it as his
or her own. Doing so means committing plagiarism.
© Algorithms
Plagiarism
A student should never take someone else’s material and present it as his
or her own. Doing so means committing plagiarism.
© Algorithms
Plagiarism
A student should never take someone else’s material and present it as his
or her own. Doing so means committing plagiarism.
© Algorithms
Deadlines
Deadlines are firm.
© Algorithms
Deadlines
Deadlines are firm.
© Algorithms
Deadlines
Deadlines are firm.
Each late day will reduce the assignment’s grade by one third of the
total value of that assignment
© Algorithms
Deadlines
Deadlines are firm.
Each late day will reduce the assignment’s grade by one third of the
total value of that assignment
▶ Corollary 1: The grade of an assignment turned in more than two
days late is 0
© Algorithms
Deadlines
Deadlines are firm.
Each late day will reduce the assignment’s grade by one third of the
total value of that assignment
▶ Corollary 1: The grade of an assignment turned in more than two
days late is 0
▶ The proof of Corollary 1 is left as an exercise
© Algorithms
Now let’s move on to the real
interesting and fun stuff. . .
© Algorithms
Automating Arithmetic
Human brain find many things easy that are difficult to implemented on
a computer. While tasks that are difficult for humans are easy on
computers ( arithmetic)
Humans have always been trying to make arithmetic easier and faster
© Algorithms
Automating Arithmetic
Human brain find many things easy that are difficult to implemented on
a computer. While tasks that are difficult for humans are easy on
computers ( arithmetic)
Humans have always been trying to make arithmetic easier and faster
© Algorithms
Automating Arithmetic
© Algorithms
An Islamic Example
© Algorithms
Babbage Difference Engine
Charles Babbage made a Mechanical Computer in 1822. It calculated
polynomial equations.
© Algorithms
Babbage Difference Engine
Charles Babbage made a Mechanical Computer in 1822. It calculated
polynomial equations.
© Algorithms
Rigid versus Flexible Computing
© Algorithms
Rigid versus Flexible Computing
Rigid Computing Machines are computing machines that can
perform a limited number computations
© Algorithms
Rigid versus Flexible Computing
Rigid Computing Machines are computing machines that can
perform a limited number computations
Flexible Computing Machines are able to perform unlimited
number of computations. Today’s computer are flexible one can
load and execute various types of programs.
© Algorithms
Rigid versus Flexible Computing
Rigid Computing Machines are computing machines that can
perform a limited number computations
Flexible Computing Machines are able to perform unlimited
number of computations. Today’s computer are flexible one can
load and execute various types of programs.
© Algorithms
Modern Computers
© Algorithms
The Name and Concept of Algorithms
© Algorithms
The Name and Concept of Algorithms
The decimal numbering system (India,
circa 600 AD)
© Algorithms
The Name and Concept of Algorithms
The decimal numbering system (India,
circa 600 AD)
© Algorithms
The Name and Concept of Algorithms
The decimal numbering system (India,
circa 600 AD)
© Algorithms
The Name and Concept of Algorithms
The decimal numbering system (India,
circa 600 AD)
© Algorithms
The Name and Concept of Algorithms
The decimal numbering system (India,
circa 600 AD)
© Algorithms
So what are Algorithms?
© Algorithms
So what are Algorithms?
All software running on the computer is like tape of the turing
machines. They are the programs
© Algorithms
So what are Algorithms?
All software running on the computer is like tape of the turing
machines. They are the programs
All program are implementations of an algorithm.
© Algorithms
So what are Algorithms?
All software running on the computer is like tape of the turing
machines. They are the programs
All program are implementations of an algorithm.
One can imagine an algorithm as flow chart we created before we
wrote the program in our first programming courses.
© Algorithms
So what are Algorithms?
All software running on the computer is like tape of the turing
machines. They are the programs
All program are implementations of an algorithm.
One can imagine an algorithm as flow chart we created before we
wrote the program in our first programming courses.
© Algorithms
Algorithms are
the essence
of computer programs
© Algorithms
Algorithms are the
logic
of computer programs
© Algorithms
Implementations of Algorithms
Two programs are different implementations of the same algorithms if
anyone of the following changes:
© Algorithms
Implementations of Algorithms
Two programs are different implementations of the same algorithms if
anyone of the following changes:
The programmer
the hardware running the program
a different programming language Java versus C++
© Algorithms
Implementations of Algorithms
Two programs are different implementations of the same algorithms if
anyone of the following changes:
The programmer
the hardware running the program
a different programming language Java versus C++
Question: In what aspect way would two implementation of the same
algorithm differ
© Algorithms
Implementations of Algorithms
Two programs are different implementations of the same algorithms if
anyone of the following changes:
The programmer
the hardware running the program
a different programming language Java versus C++
Question: In what aspect way would two implementation of the same
algorithm differ
© Algorithms
One Problem Many Algorithms
There can be many problems for a single problem
© Algorithms
One Problem Many Algorithms
There can be many problems for a single problem
Example
Sorting an array of numbers is one of the most common function. There
are various algorithms for it for example insertion sort is one and merge
sort is another
© Algorithms
One Problem Many Algorithms
There can be many problems for a single problem
Example
Sorting an array of numbers is one of the most common function. There
are various algorithms for it for example insertion sort is one and merge
sort is another
Should we have any preference regarding implementing one another
algorithm ?
Yes Some are better than others
© Algorithms
Better Algorithms?
© Algorithms
Better Algorithms?
The algorithm that finish faster are better
© Algorithms
Better Algorithms?
The algorithm that finish faster are better
For small input (e.g. array size) it does not matter. But when the
input size is very very large size this time difference can be
significant.
▶ Example: think about searching the Call Record Database of Ufone
© Algorithms
Time Complexity
Time an algorithm takes can be estimated by a mathematical expression
of the following form:
=c ×E
T (n)
I
© Algorithms
Time Complexity
Time an algorithm takes can be estimated by a mathematical expression
of the following form:
=c ×E
T (n)
I
c is a constant like 2, 10, 100 etc.
© Algorithms
Time Complexity
Time an algorithm takes can be estimated by a mathematical expression
of the following form:
=c ×E
T (n)
I
c is a constant like 2, 10, 100 etc.
it estimates the various implementation specific factors
© Algorithms
Time Complexity
Time an algorithm takes can be estimated by a mathematical expression
of the following form:
=c ×E
T (n)
I
c is a constant like 2, 10, 100 etc.
it estimates the various implementation specific factors
E is a polynomial expression in terms of the input size, e.g.
▶ n
▶ n2
▶ nlogn
▶ etc
▶ it gives an estimate on the number instructions that need to be
executed
I is Instructions per sec
© Algorithms
Which is important?
Question
So we now have the following factors that can influence the time taken
by some particular.
The programmer
Hardware
Programming Language
The Algorithm
© Algorithms
Which is important?
Question
So we now have the following factors that can influence the time taken
by some particular.
The programmer
Hardware
Programming Language
The Algorithm
© Algorithms
Algorithms are most important!
© Algorithms
Lets assume we implement two algorithms insertion sort and merge sort
We need to sort an array of 10 million numbers. i.e
n = 107
© Algorithms
Lets assume we implement two algorithms insertion sort and merge sort
We need to sort an array of 10 million numbers. i.e
n = 107
© Algorithms
Time Taken
© Algorithms
Time Taken
Insertion Sort
2
2 × 107
T = 10 = 2 × 104 secs = 20000secs
10 Intruc/sec
© Algorithms
Time Taken
Insertion Sort
2
2 × 107
T = 10 = 2 × 104 secs = 20000secs
10 Intruc/sec
© Algorithms
Time Taken
Insertion Sort
2
2 × 107
T = 10 = 2 × 104 secs = 20000secs
10 Intruc/sec
© Algorithms
Time Taken
Insertion Sort
2
2 × 107
T = 10 = 2 × 104 secs = 20000secs
10 Intruc/sec
© Algorithms
Example of Algorithm
A sequence of numbers
© Algorithms
Example of Algorithm
A sequence of numbers
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
Scheme
(define (F n)
(cond
((= n 0) 0)
((= n 1) 1)
(else (+ (F (- n 1)) (F (- n 2))))))
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
Java
public class Fibonacci {
public static int F(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return F(n-1) + F(n-2);
} }
}
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
C or C++
int F(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return F(n-1) + F(n-2);
}
}
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
Ruby
def F(n)
case n
when 0
return 0
when 1
return 1
else
return F(n-1) + F(n-2)
end
end
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
Python
def F(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return F(n-1) + F(n-2)
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
© Algorithms
The Fibonacci Sequence
0
if n = 0
Mathematical definition: Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
Implementation on a computer:
“pseudo-code”
Fibonacci(n)
1 if n == 0
2 return 0
3 elseif n == 1
4 return 1
5 else return Fibonacci(n − 1) + Fibonacci(n − 2)
© Algorithms
How The Algorithm Works?
© Algorithms
Questions on Our First Algorithm
Fibonacci(n)
1 if n == 0
2 return 0
3 elseif n == 1
4 return 1
5 else return Fibonacci(n − 1) + Fibonacci(n − 2)
© Algorithms
Questions on Our First Algorithm
Fibonacci(n)
1 if n == 0
2 return 0
3 elseif n == 1
4 return 1
5 else return Fibonacci(n − 1) + Fibonacci(n − 2)
© Algorithms
Questions on Our First Algorithm
Fibonacci(n)
1 if n == 0
2 return 0
3 elseif n == 1
4 return 1
5 else return Fibonacci(n − 1) + Fibonacci(n − 2)
© Algorithms
Questions on Our First Algorithm
Fibonacci(n)
1 if n == 0
2 return 0
3 elseif n == 1
4 return 1
5 else return Fibonacci(n − 1) + Fibonacci(n − 2)
3. Can we do better?
© Algorithms
Correctness
© Algorithms
Correctness
Fibonacci(n)
1 if n == 0
2 return 0
3 elseif n == 1
4 return 1
5 else return Fibonacci(n − 1) + Fibonacci(n − 2)
0
if n = 0
Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
© Algorithms
Correctness
Fibonacci(n)
1 if n == 0
2 return 0
3 elseif n == 1
4 return 1
5 else return Fibonacci(n − 1) + Fibonacci(n − 2)
0
if n = 0
Fn = 1 if n = 1
Fn−1 + Fn−2 if n > 1
© Algorithms
Performance
How long does it take?
© Algorithms
Results
60
Ruby
Java
running time (seconds)
Python
40 C
C-wiz
C-gcc
20
0
20 25 30 35 40 45 50
n
© Algorithms
Comments
© Algorithms
Comments
Different implementations perform differently
▶ it is better to let the compiler do the optimization
▶ simple language tricks don’t seem to pay off
© Algorithms
Comments
Different implementations perform differently
▶ it is better to let the compiler do the optimization
▶ simple language tricks don’t seem to pay off
© Algorithms
Comments
Different implementations perform differently
▶ it is better to let the compiler do the optimization
▶ simple language tricks don’t seem to pay off
© Algorithms
Comments
© Algorithms
Comments
Different implementations perform differently
▶ it is better to let the compiler do the optimization
▶ simple language tricks don’t seem to pay off
© Algorithms
Comments
Different implementations perform differently
▶ it is better to let the compiler do the optimization
▶ simple language tricks don’t seem to pay off
© Algorithms
Comments
Different implementations perform differently
▶ it is better to let the compiler do the optimization
▶ simple language tricks don’t seem to pay off
© Algorithms
A Better Algorithm
Again, the sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
© Algorithms
A Better Algorithm
Again, the sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
© Algorithms
A Better Algorithm
Again, the sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
60
Ruby
Scheme
running time (seconds)
Python
40 C
C-wiz
Java
C-gcc
(Python) SmartFibonacci
20
0
20 40 60 80 100 120 140 160 180 200
n
© Algorithms