Lecture01 - 2017
Lecture01 - 2017
Lecture01 - 2017
Introduction to Algorithms.
U.U.Samantha Rajapaksha
BSc. Eng. (Moratuwa), MSc in IT
Senior Lecturer
Sri Lanka Institute of Information Technology
New Kandy Road,
Malabe, Sri Lanka
Tel:0112-301904
email: samantha.r@sliit.lk
Web: www.sliit.lk
Algorithms in C,
Parts 1-5, Third Edition by Robert
Sedgewick.
Data Structures
and Algorithms
Using C#
by Michael
McMillan
DAA 214 Sri Lanka Institute of Information Technology. 4
ALGORITHMS
Algorithm is any well defined computational
procedure that takes some value or set of values as
input and produce some value or set of values as
output.
3,1,7,2,9,8,5,4,6 1,2,3,4,5,6,7,8,9
ALGORITHM
INPUT OUTPUT
Necker_cube_and_impossible_cube
Source:http://en.wikipedia.org/wiki/Ambiguity#Mathematical_i
nterpretation_of_ambiguity
To compare
Predict the growth of run time
Average case
Best case
Input Size
DAA 214 Sri Lanka Institute of Information Technology. 18
Operation count
Select one or more operations such as add,multiply and
compare.
sum 0 1 assignment
n+2 assignments
for i 0 to n n+2 comparisons
n+1 additions
sum sum A[i ] n+1 assignments
n+1 additions
+ c6 n j=2 ( tj - 1) + c7 n j=2 ( tj - 1) +
c8(n-1)
Best Case T(n) an+b
Worst Case T(n) cn2 + dn + e
O - Notation
- Notation
- Notation
for i 1 to n
for j 1 to i O(n2)
Print j
Assignment (s 1)
Addition (s+1) O(1)
Multiplication (s*2)
Comparison (S<10)
sum 0 c1 1
for i 1 to n c2 n+1
sum sum A[i ] c3 n
T(n) = c1 + c2 (n+1) + c3 n = (c1 + c2) + (c2 + c3) n = c4 + c5 n
O (n)
s 0
s 1 All O(1)
s 1
s 1
for i 0 to n n