SlideShare une entreprise Scribd logo
Data Structures and Algorithms IT2070
SLIIT - Faculty of Computing
IT2070 – Data Structures and Algorithms
Lecture 06
Introduction to Algorithms
U. U. Samantha Rajapaksha
M.Sc.in IT, B.Sc.(Engineering) University of Moratuwa
Senior Lecturer SLIIT
Samantha.r@slit.lk
1
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
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.
2
ALGORITHM
INPUT OUTPUT
3,1,7,2,9,8,5,4,6 1,2,3,4,5,6,7,8,9
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
ALGORITHM (Contd.)
1.Get the smallest value from the input.
2.Remove it and output.
3.Repeat above 1,2 for remaining input until there is no item
in the input.
3
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Properties of an Algorithm.
• Be correct.
• Be unambiguous.
• Give the correct solution for all cases.
• Be simple.
• It must terminate.
4
Necker_cube_and_impossible_cube
Source:http://en.wikipedia.org/wiki/Ambiguity#Mathematical_i
nterpretation_of_ambiguity
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Applications of Algorithms
• Data retrieval
• Network routing
• Sorting
• Searching
• Shortest paths in a graph
5
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Pseudocode
• Method of writing down a algorithm.
• Easy to read and understand.
• Just like other programming language.
6
• More expressive method.
• Does not concern with the technique of software
engineering.
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Pseudocode Conventions.
 English.
 Indentation.
 Separate line for each instruction.
 Looping constructs and conditional constructs.
 // indicate a comment line.
 = indicate the assignment.
7
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Pseudocode Conventions.
 Array elements are accessed by specifying the array
name followed by the index in the square bracket.
 The notation “..” is used to indicate a range of values
within the array.
Ex:
A[1..i] indicates the sub array of A consisting of
elements A[1] , A[2] , .. , A[i].
8
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Analysis of Algorithms
Idea is to predict the resource usage.
9
• Memory
• Logic Gates
• Computational Time
Why do we need an analysis?
• To compare
• Predict the growth of run time
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Worst, Best and Average case.
Running time will depend on the chosen instance characteristics.
• Best case:
Minimum number of steps taken on any
instance of size n.
• Worst case:
Maximum number of steps taken on any instance of size n.
• Average case:
An average number of steps taken on any
instance of size n.
10
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Worst,Best and Average case(Contd.)
11
Best case
Average case
Worst case
Input Size
# of steps
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Analysis Methods
• Operation Count Methods
• Step Count Method(RAM Model)
• Exact Analysis
• Asymptotic Notations
12
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Operation count
• Methods for time complexity analysis.
• Select one or more operations such as add, multiply and
compare.
• Operation count considers the time spent on chosen
operations but not all.
13
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Step Count (RAM Model)
• Assume a generic one processor.
• Instructions are executed one after another, with no concurrent
operations.
• +, - , =, it takes exactly one step.
• Each memory access takes exactly 1 step.
•Running Time = Sum of the steps.
14
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
RAM Model Analysis.
Example1:
n = 100 1step
n = n + 100 2steps
Print n 1step
15
Example1:
n = 100 1step
n = n + 100 2steps
Print n 1step
Steps = 6n+3
]
[
to
1
for
0
i
A
sum
sum
n
i
sum



 1 assignment
n+1 assignments
n+1 comparisons
n additions
n assignments
n additions
n memory accesses
Steps = 4
Example2:
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 01
• Using RAM model analysis, find out the no of steps
needed to display the numbers from 1 to 10.
i = 1  1 step
While i <=10  11 steps
print i  10 steps
i = i + 1  10 + 10 = 20 steps
16
Steps = 42
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 02
• Using RAM model analysis, find out the no of steps
needed to display the numbers from 10 to 20.
i = 10  1 step
While i <= 20  12 steps ( Hint :20 – 10 + 2 = 12)
print i  11 steps
i = i + 1  11 + 11 = 22 steps
17
Steps = 46
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 03
• Using RAM model analysis, find out the no of steps
needed to display the even numbers from 10 to 20.
for i = 10 to 20  (12+ 12 + 11) steps = 35 steps
if i % 2 == 0  2 * 11 = 22 steps
print i  6 steps
18
Steps = 63
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Problems with RAM Model
• Differ number of steps with different architecture.
eg: sum = sum + A[i] is a one step in the CISC processor.
• It is difficult to count the exact number of steps in the algorithm.
eg: See the insertion sort , efficient algorithm for sorting
small number of elements.
19
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Insertion sort
20
Key 2 Key 4 Key  6
Key  1 Key  3
Sorted Array
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Pseudocode for insertion sort.
INSERTION-SORT(A)
1 for j = 2 to A.length
2 key = A[j]
3 // Insert A[j] into the sorted sequence A[1..j-1]
4 i = j - 1
5 While i > 0 and A[i] > key
6 A[i+1] = A[i]
7 i = i-1
8 A[i+1] = key
21
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
22
 (a)-(e) The iterations of the for loop  lines 1-8.
 In each iteration, the black rectangle holds the key taken from A[j],
 Key is compared with the values in shaded rectangles to its left  line 5.
 Shaded arrows show array values moved one position to the right  line 6,
 Black arrows indicate where the key is moved to  line 8.
Insertion sort - Example
sorted array
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Exact analysis of Insertion sort
• Time taken for the algorithm will depend on the input size
(number of elements of the array)
23
Running Time (Time complexity):
This is the number of primitive operations or steps
executed through an algorithm given a particular input.
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Running Time : T(n)
24
INSERTION-SORT(A) Cost Times
1 for j = 2 to A.length c1 n
2 key = A[j] c2 n-1
3 // Insert A[j] into the sorted
// sequence A[1..j-1]
0 n-1
4 i = j – 1 c4 n-1
5 While i > 0 and A[i] > key c5  n
j=2 tj
6 A[i+1] = A[i] c6  n
j=2 (tj - 1)
7 i = i-1 c7  n
j=2 (tj - 1)
8 A[i+1] = key c8 n-1
ith line takes time ci where ci is a constant.
For each j=2,3,…,n , tj be the number of times the while loop is executed for that value of j
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Running Time(contd.)
T(n) = c1 n + c2 (n-1) + c4 (n-1) + c5  n
j=2 tj
+ c6  n
j=2 ( tj - 1) + c7  n
j=2 ( tj - 1) +
c8(n-1)
• Best Case (Array is in sorted order)
- T(n) an+b
• Worst Case (Array is in reverse sorted order)
- T(n)  cn2 + dn + e
25
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Worst Case T(n) cn2 + dn + e
26
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Worst Case T(n) cn2 + dn + e
27
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Asymptotic Notations
• RAM Model has some problems.
• Exact analysis is very complicated.
28
Therefore we move to asymptotic notation
• Here we focus on determining the biggest term in the
complexity function.
• Sufficiently large size of n.
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Asymptotic Notations(Contd.)
• There are three notations.
29
O - Notation
 - Notation
 - Notation
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Big O - Notation
• Introduced by Paul Bechman in 1892.
• We use Big O-notation to give an upper bound on a function.
Definition:
O(g(n)) = { f(n) : there exist positive constants c and no such that
0  f(n)  cg(n) for all n  no}.
30
Eg: What is the big O value of f(n)=2n + 6 ?
c = 4
no = 3
g(n)=n therefore
f(n) = O(n)
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Back to the example
• Alternative calculation:
31
T(n) = c1 + c2 (n+1) + c3 n
= (c1 + c2) + (c2 + c3) n
= c4 + c5 n  O (n)
Proof: c4 + c5 n ≤ c n  TRUE for n≥1 and c ≥ c4 + c5
cost times
sum = 0 c1 1
for i = 1 to n c2 n+1
sum = sum + A[i] c3 n
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Big O – Notation(Contd.)
Assignment (s = 1)
Addition (s+1)
Multiplication (s*2)
Comparison (S<10)
32
O(1)
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Question
• Find the Big O value for following fragment of code.
for i = 1 to n
for j = 1 to i
Print j
33
O(n2)
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Graphs of functions
34
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
35
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Big O – Notation(Contd.)
• Find the Big O value for the following functions.
(i) T(n)= 3 +5n + 3n2
(ii) f(n)= 2n + n2 +8n +7
(iii) T(n)= n + logn +6
Answers:
(i) O(n2)
(ii) O(2n)
(iii) O(n)
36
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
 - Notation
• Provides the lower bound of the function.
Definition:
(g(n)) = { f(n) : there exist positive constants c and n0 such that 0  cg(n)
 f(n) for all n  no}
37
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
 - Notation
• This is used when the function f can be bounded both from
above and below by the same function g.
Definition:
(g(n)) ={ f(n): there exist positive constant c1, c2, and n0
such that 0 c1 g(n)  f(n)  c2g(n) for all n  n0 }
38
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
Summary
• What is an algorithm?
• Properties of an algorithm.
• Design methods.
• Pseudocode.
• Analysis(Operation count & Step count, RAM model).
• Insertion Sort.
• Asymptotic Notation
39
Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing
Data Structures and Algorithms
References
• T.H. Cormen, C.E. Leiserson, R.L. Rivest, Clifford Stein
Introduction to Algorithms,3rd Edition, MIT Press,
2009.
40

Contenu connexe

Tendances

Cable tv network
Cable tv networkCable tv network
Cable tv network
ashrafulislam293
 
Internship ppt on bsnl
Internship ppt on bsnlInternship ppt on bsnl
Internship ppt on bsnl
RanjitUpadhyay4
 
Electronic Warfare ( EW ) Training Crash Course
Electronic Warfare ( EW ) Training Crash CourseElectronic Warfare ( EW ) Training Crash Course
Electronic Warfare ( EW ) Training Crash Course
Bryan Len
 
FCAS Think Different
FCAS Think Different FCAS Think Different
FCAS Think Different
Bernardo A. Delicado
 
Srvcc overview
Srvcc overviewSrvcc overview
Srvcc overview
Yau Boon
 
Electronic Warfare
Electronic WarfareElectronic Warfare
Electronic Warfare
Wesley Comal
 
antenna poster presentetion
antenna poster presentetion antenna poster presentetion
antenna poster presentetion
Rasid Khan
 
AEWE 2015 Participating Technologies
AEWE 2015 Participating TechnologiesAEWE 2015 Participating Technologies
AEWE 2015 Participating Technologies
RDECOM
 
18EC743-Module 2.pdf
18EC743-Module 2.pdf18EC743-Module 2.pdf
18EC743-Module 2.pdf
Amrutha R
 
Bts installation & commisioning
Bts installation & commisioningBts installation & commisioning
Bts installation & commisioning
AIRTEL
 
Different Types of Backhaul
Different Types of BackhaulDifferent Types of Backhaul
Different Types of Backhaul
3G4G
 
LTE Attach Call Flow_Vi.pptx
LTE Attach Call Flow_Vi.pptxLTE Attach Call Flow_Vi.pptx
LTE Attach Call Flow_Vi.pptx
GaganVerma62
 
IMS ENUM and DNS Mechanism
IMS ENUM and DNS MechanismIMS ENUM and DNS Mechanism
IMS ENUM and DNS Mechanism
Kent Loh
 
What is AUTOSAR Communiation Stack
What is AUTOSAR Communiation StackWhat is AUTOSAR Communiation Stack
What is AUTOSAR Communiation Stack
Embitel Technologies (I) PVT LTD
 
Avionics sai
Avionics saiAvionics sai
Avionics sai
Sai Shubhankar
 
Beginners: 5G Terminology (Updated - Feb 2019)
Beginners: 5G Terminology (Updated - Feb 2019)Beginners: 5G Terminology (Updated - Feb 2019)
Beginners: 5G Terminology (Updated - Feb 2019)
3G4G
 
Mechanics
MechanicsMechanics
MechanicsPhysEM
 
Training Course_5G RAN3.0 mmWave Beam Management.pptx
Training Course_5G RAN3.0 mmWave Beam Management.pptxTraining Course_5G RAN3.0 mmWave Beam Management.pptx
Training Course_5G RAN3.0 mmWave Beam Management.pptx
game__over
 
BSNL Industrial Training
BSNL Industrial Training BSNL Industrial Training
BSNL Industrial Training
Moulik .
 
DLW WORKSHOP , VARANSI
DLW WORKSHOP , VARANSIDLW WORKSHOP , VARANSI
DLW WORKSHOP , VARANSI
Shivam Prajapati
 

Tendances (20)

Cable tv network
Cable tv networkCable tv network
Cable tv network
 
Internship ppt on bsnl
Internship ppt on bsnlInternship ppt on bsnl
Internship ppt on bsnl
 
Electronic Warfare ( EW ) Training Crash Course
Electronic Warfare ( EW ) Training Crash CourseElectronic Warfare ( EW ) Training Crash Course
Electronic Warfare ( EW ) Training Crash Course
 
FCAS Think Different
FCAS Think Different FCAS Think Different
FCAS Think Different
 
Srvcc overview
Srvcc overviewSrvcc overview
Srvcc overview
 
Electronic Warfare
Electronic WarfareElectronic Warfare
Electronic Warfare
 
antenna poster presentetion
antenna poster presentetion antenna poster presentetion
antenna poster presentetion
 
AEWE 2015 Participating Technologies
AEWE 2015 Participating TechnologiesAEWE 2015 Participating Technologies
AEWE 2015 Participating Technologies
 
18EC743-Module 2.pdf
18EC743-Module 2.pdf18EC743-Module 2.pdf
18EC743-Module 2.pdf
 
Bts installation & commisioning
Bts installation & commisioningBts installation & commisioning
Bts installation & commisioning
 
Different Types of Backhaul
Different Types of BackhaulDifferent Types of Backhaul
Different Types of Backhaul
 
LTE Attach Call Flow_Vi.pptx
LTE Attach Call Flow_Vi.pptxLTE Attach Call Flow_Vi.pptx
LTE Attach Call Flow_Vi.pptx
 
IMS ENUM and DNS Mechanism
IMS ENUM and DNS MechanismIMS ENUM and DNS Mechanism
IMS ENUM and DNS Mechanism
 
What is AUTOSAR Communiation Stack
What is AUTOSAR Communiation StackWhat is AUTOSAR Communiation Stack
What is AUTOSAR Communiation Stack
 
Avionics sai
Avionics saiAvionics sai
Avionics sai
 
Beginners: 5G Terminology (Updated - Feb 2019)
Beginners: 5G Terminology (Updated - Feb 2019)Beginners: 5G Terminology (Updated - Feb 2019)
Beginners: 5G Terminology (Updated - Feb 2019)
 
Mechanics
MechanicsMechanics
Mechanics
 
Training Course_5G RAN3.0 mmWave Beam Management.pptx
Training Course_5G RAN3.0 mmWave Beam Management.pptxTraining Course_5G RAN3.0 mmWave Beam Management.pptx
Training Course_5G RAN3.0 mmWave Beam Management.pptx
 
BSNL Industrial Training
BSNL Industrial Training BSNL Industrial Training
BSNL Industrial Training
 
DLW WORKSHOP , VARANSI
DLW WORKSHOP , VARANSIDLW WORKSHOP , VARANSI
DLW WORKSHOP , VARANSI
 

Similaire à 2022-S1-IT2070-Lecture-06-Algorithms.pptx

Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Ferdin Joe John Joseph PhD
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
Week 1 - Data Structures and Algorithms
Week 1 - Data Structures and AlgorithmsWeek 1 - Data Structures and Algorithms
Week 1 - Data Structures and Algorithms
Ferdin Joe John Joseph PhD
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
MumitAhmed1
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
SharabiNaif
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
Anonymous9etQKwW
 
Spring framework aop
Spring framework aopSpring framework aop
Spring framework aop
Taemon Piya-Lumyong
 
files_1570175665_204715750.pdf
files_1570175665_204715750.pdffiles_1570175665_204715750.pdf
files_1570175665_204715750.pdf
beherapravat936
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
iqbalphy1
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
example43
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Sayed Chhattan Shah
 
chapter 1
chapter 1chapter 1
chapter 1
yatheesha
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Ravikiran A
 
Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...
bekidea
 
CP Handout#1
CP Handout#1CP Handout#1
CP Handout#1
trupti1976
 
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
Lola Burgueño
 
Aca2 08 new
Aca2 08 newAca2 08 new
Aca2 08 new
Sumit Mittu
 
Data Analysis – Technical learnings
Data Analysis – Technical learningsData Analysis – Technical learnings
Data Analysis – Technical learnings
InvenkLearn
 
BIRTE-13-Kawashima
BIRTE-13-KawashimaBIRTE-13-Kawashima
BIRTE-13-Kawashima
Hideyuki Kawashima
 

Similaire à 2022-S1-IT2070-Lecture-06-Algorithms.pptx (20)

Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm Analysis
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Week 1 - Data Structures and Algorithms
Week 1 - Data Structures and AlgorithmsWeek 1 - Data Structures and Algorithms
Week 1 - Data Structures and Algorithms
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Spring framework aop
Spring framework aopSpring framework aop
Spring framework aop
 
files_1570175665_204715750.pdf
files_1570175665_204715750.pdffiles_1570175665_204715750.pdf
files_1570175665_204715750.pdf
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
chapter 1
chapter 1chapter 1
chapter 1
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...
 
CP Handout#1
CP Handout#1CP Handout#1
CP Handout#1
 
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
 
Aca2 08 new
Aca2 08 newAca2 08 new
Aca2 08 new
 
Data Analysis – Technical learnings
Data Analysis – Technical learningsData Analysis – Technical learnings
Data Analysis – Technical learnings
 
BIRTE-13-Kawashima
BIRTE-13-KawashimaBIRTE-13-Kawashima
BIRTE-13-Kawashima
 

Dernier

Build applications with generative AI on Google Cloud
Build applications with generative AI on Google CloudBuild applications with generative AI on Google Cloud
Build applications with generative AI on Google Cloud
Márton Kodok
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
bmucuha
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
SaffaIbrahim1
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
nuttdpt
 
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
slg6lamcq
 
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
y3i0qsdzb
 
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens""Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
sameer shah
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
Sachin Paul
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
Timothy Spann
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
z6osjkqvd
 
Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024
ElizabethGarrettChri
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
VyNguyen709676
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
taqyea
 

Dernier (20)

Build applications with generative AI on Google Cloud
Build applications with generative AI on Google CloudBuild applications with generative AI on Google Cloud
Build applications with generative AI on Google Cloud
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
 
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
 
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
 
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens""Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
 
Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
 

2022-S1-IT2070-Lecture-06-Algorithms.pptx

  • 1. Data Structures and Algorithms IT2070 SLIIT - Faculty of Computing IT2070 – Data Structures and Algorithms Lecture 06 Introduction to Algorithms U. U. Samantha Rajapaksha M.Sc.in IT, B.Sc.(Engineering) University of Moratuwa Senior Lecturer SLIIT Samantha.r@slit.lk 1
  • 2. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms 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. 2 ALGORITHM INPUT OUTPUT 3,1,7,2,9,8,5,4,6 1,2,3,4,5,6,7,8,9
  • 3. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms ALGORITHM (Contd.) 1.Get the smallest value from the input. 2.Remove it and output. 3.Repeat above 1,2 for remaining input until there is no item in the input. 3
  • 4. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Properties of an Algorithm. • Be correct. • Be unambiguous. • Give the correct solution for all cases. • Be simple. • It must terminate. 4 Necker_cube_and_impossible_cube Source:http://en.wikipedia.org/wiki/Ambiguity#Mathematical_i nterpretation_of_ambiguity
  • 5. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Applications of Algorithms • Data retrieval • Network routing • Sorting • Searching • Shortest paths in a graph 5
  • 6. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Pseudocode • Method of writing down a algorithm. • Easy to read and understand. • Just like other programming language. 6 • More expressive method. • Does not concern with the technique of software engineering.
  • 7. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Pseudocode Conventions.  English.  Indentation.  Separate line for each instruction.  Looping constructs and conditional constructs.  // indicate a comment line.  = indicate the assignment. 7
  • 8. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Pseudocode Conventions.  Array elements are accessed by specifying the array name followed by the index in the square bracket.  The notation “..” is used to indicate a range of values within the array. Ex: A[1..i] indicates the sub array of A consisting of elements A[1] , A[2] , .. , A[i]. 8
  • 9. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Analysis of Algorithms Idea is to predict the resource usage. 9 • Memory • Logic Gates • Computational Time Why do we need an analysis? • To compare • Predict the growth of run time
  • 10. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Worst, Best and Average case. Running time will depend on the chosen instance characteristics. • Best case: Minimum number of steps taken on any instance of size n. • Worst case: Maximum number of steps taken on any instance of size n. • Average case: An average number of steps taken on any instance of size n. 10
  • 11. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Worst,Best and Average case(Contd.) 11 Best case Average case Worst case Input Size # of steps
  • 12. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Analysis Methods • Operation Count Methods • Step Count Method(RAM Model) • Exact Analysis • Asymptotic Notations 12
  • 13. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Operation count • Methods for time complexity analysis. • Select one or more operations such as add, multiply and compare. • Operation count considers the time spent on chosen operations but not all. 13
  • 14. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Step Count (RAM Model) • Assume a generic one processor. • Instructions are executed one after another, with no concurrent operations. • +, - , =, it takes exactly one step. • Each memory access takes exactly 1 step. •Running Time = Sum of the steps. 14
  • 15. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms RAM Model Analysis. Example1: n = 100 1step n = n + 100 2steps Print n 1step 15 Example1: n = 100 1step n = n + 100 2steps Print n 1step Steps = 6n+3 ] [ to 1 for 0 i A sum sum n i sum     1 assignment n+1 assignments n+1 comparisons n additions n assignments n additions n memory accesses Steps = 4 Example2:
  • 16. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Question 01 • Using RAM model analysis, find out the no of steps needed to display the numbers from 1 to 10. i = 1  1 step While i <=10  11 steps print i  10 steps i = i + 1  10 + 10 = 20 steps 16 Steps = 42
  • 17. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Question 02 • Using RAM model analysis, find out the no of steps needed to display the numbers from 10 to 20. i = 10  1 step While i <= 20  12 steps ( Hint :20 – 10 + 2 = 12) print i  11 steps i = i + 1  11 + 11 = 22 steps 17 Steps = 46
  • 18. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Question 03 • Using RAM model analysis, find out the no of steps needed to display the even numbers from 10 to 20. for i = 10 to 20  (12+ 12 + 11) steps = 35 steps if i % 2 == 0  2 * 11 = 22 steps print i  6 steps 18 Steps = 63
  • 19. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Problems with RAM Model • Differ number of steps with different architecture. eg: sum = sum + A[i] is a one step in the CISC processor. • It is difficult to count the exact number of steps in the algorithm. eg: See the insertion sort , efficient algorithm for sorting small number of elements. 19
  • 20. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Insertion sort 20 Key 2 Key 4 Key  6 Key  1 Key  3 Sorted Array
  • 21. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Pseudocode for insertion sort. INSERTION-SORT(A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i = j - 1 5 While i > 0 and A[i] > key 6 A[i+1] = A[i] 7 i = i-1 8 A[i+1] = key 21
  • 22. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms 22  (a)-(e) The iterations of the for loop  lines 1-8.  In each iteration, the black rectangle holds the key taken from A[j],  Key is compared with the values in shaded rectangles to its left  line 5.  Shaded arrows show array values moved one position to the right  line 6,  Black arrows indicate where the key is moved to  line 8. Insertion sort - Example sorted array
  • 23. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Exact analysis of Insertion sort • Time taken for the algorithm will depend on the input size (number of elements of the array) 23 Running Time (Time complexity): This is the number of primitive operations or steps executed through an algorithm given a particular input.
  • 24. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Running Time : T(n) 24 INSERTION-SORT(A) Cost Times 1 for j = 2 to A.length c1 n 2 key = A[j] c2 n-1 3 // Insert A[j] into the sorted // sequence A[1..j-1] 0 n-1 4 i = j – 1 c4 n-1 5 While i > 0 and A[i] > key c5  n j=2 tj 6 A[i+1] = A[i] c6  n j=2 (tj - 1) 7 i = i-1 c7  n j=2 (tj - 1) 8 A[i+1] = key c8 n-1 ith line takes time ci where ci is a constant. For each j=2,3,…,n , tj be the number of times the while loop is executed for that value of j
  • 25. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Running Time(contd.) T(n) = c1 n + c2 (n-1) + c4 (n-1) + c5  n j=2 tj + c6  n j=2 ( tj - 1) + c7  n j=2 ( tj - 1) + c8(n-1) • Best Case (Array is in sorted order) - T(n) an+b • Worst Case (Array is in reverse sorted order) - T(n)  cn2 + dn + e 25
  • 26. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Worst Case T(n) cn2 + dn + e 26
  • 27. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Worst Case T(n) cn2 + dn + e 27
  • 28. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Asymptotic Notations • RAM Model has some problems. • Exact analysis is very complicated. 28 Therefore we move to asymptotic notation • Here we focus on determining the biggest term in the complexity function. • Sufficiently large size of n.
  • 29. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Asymptotic Notations(Contd.) • There are three notations. 29 O - Notation  - Notation  - Notation
  • 30. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Big O - Notation • Introduced by Paul Bechman in 1892. • We use Big O-notation to give an upper bound on a function. Definition: O(g(n)) = { f(n) : there exist positive constants c and no such that 0  f(n)  cg(n) for all n  no}. 30 Eg: What is the big O value of f(n)=2n + 6 ? c = 4 no = 3 g(n)=n therefore f(n) = O(n)
  • 31. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Back to the example • Alternative calculation: 31 T(n) = c1 + c2 (n+1) + c3 n = (c1 + c2) + (c2 + c3) n = c4 + c5 n  O (n) Proof: c4 + c5 n ≤ c n  TRUE for n≥1 and c ≥ c4 + c5 cost times sum = 0 c1 1 for i = 1 to n c2 n+1 sum = sum + A[i] c3 n
  • 32. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Big O – Notation(Contd.) Assignment (s = 1) Addition (s+1) Multiplication (s*2) Comparison (S<10) 32 O(1)
  • 33. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Question • Find the Big O value for following fragment of code. for i = 1 to n for j = 1 to i Print j 33 O(n2)
  • 34. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Graphs of functions 34
  • 35. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms 35
  • 36. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Big O – Notation(Contd.) • Find the Big O value for the following functions. (i) T(n)= 3 +5n + 3n2 (ii) f(n)= 2n + n2 +8n +7 (iii) T(n)= n + logn +6 Answers: (i) O(n2) (ii) O(2n) (iii) O(n) 36
  • 37. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms  - Notation • Provides the lower bound of the function. Definition: (g(n)) = { f(n) : there exist positive constants c and n0 such that 0  cg(n)  f(n) for all n  no} 37
  • 38. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms  - Notation • This is used when the function f can be bounded both from above and below by the same function g. Definition: (g(n)) ={ f(n): there exist positive constant c1, c2, and n0 such that 0 c1 g(n)  f(n)  c2g(n) for all n  n0 } 38
  • 39. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms Summary • What is an algorithm? • Properties of an algorithm. • Design methods. • Pseudocode. • Analysis(Operation count & Step count, RAM model). • Insertion Sort. • Asymptotic Notation 39
  • 40. Module Code | Module Name | Lecture Title | Lecturer SLIIT - Faculty of Computing Data Structures and Algorithms References • T.H. Cormen, C.E. Leiserson, R.L. Rivest, Clifford Stein Introduction to Algorithms,3rd Edition, MIT Press, 2009. 40