SlideShare a Scribd company logo
1 of 24
Download to read offline
IT 4043
Data Structures and Algorithms
Budditha Hettige
Department of Computer Science
1
Syllabus
• Introduction to DSA
• Abstract Data Types
• Arrays
• List Operation Using Arrays
• Recursion
• Stacks
• Queues
• Link List
• Sorting
• Algorithms Analysis
2
Introduction to
Analysis of Algorithms
1-4
1-4
Introduction to
Analysis of Algorithms
• One aspect of software quality is the efficient
use of computer resources :
– CPU time
– Memory usage
• We frequently want to analyse algorithms
with respect to execution time
– Called time complexity analysis
– For example, to decide which sorting algorithm
will take less time to run
1-5
1-5
Time Complexity
• Analysis of time taken is based on:
– Problem size (e.g. number of items to sort)
– Key operations (e.g. comparison of two values)
• What we want to analyse is the relationship
between
– The size of the problem, n
– And the time it takes to solve the problem, t(n)
• Note that t(n) is a function of n, so it depends on the size
of the problem
12-6
Worst, Average, Best Case
• Worst case analysis: considers the maximum of the
time over all inputs of size n
– Used to find an upper bound on algorithm
performance for large problems (large n)
• Average case analysis: considers the average of the
time over all inputs of size n
– Determines the average (or expected) performance
• Best case analysis: considers the minimum of the
time over all inputs of size n
12-7
Discussion
• What are some difficulties with average case
analysis?
– Hard to determine
– Depends on distribution of inputs
(they might not be evenly distributed)
• So, we usually use worst case analysis
(why not best case analysis?)
1-8
1-8
Growth Functions
• This t(n) is called a growth function
• What does a growth function look like?
– Example of a growth function for some
algorithm:
t(n) = 15n2 + 45 n
• See the next slide to see how t(n) changes as n gets
bigger!
1-9
No. of items n 15n2 45n 15n2 + 45n
1 15 45 60
2 60 90 150
5 375 225 600
10 1,500 450 1,950
100 150,000 4,500 154,500
1,000 15,000,000 45,000 15,045,000
10,000 1,500,000,000 450,000 1,500,450,000
100,000 150,000,000,000 4,500,000 150,004,500,000
1,000,000 15,000,000,000,000 45,000,000 15,000,045,000,000
Example: 15n2 + 45 n
1-10
Comparison of Terms in 15n2 + 45 n
• When n is small, which term is larger?
• But, as n gets larger, note that the 15n2 term
grows more quickly than the 45n term
• Also, the constants 15 and 45 become irrelevant
as n increases
• We say that the n2 term is dominant in this
expression
1-11
1-11
Big-O Notation
• It is not usually necessary to know the exact
growth function
• The key issue is the asymptotic complexity of
the function :
how it grows as n increases
– This is determined by the dominant term in the
growth function (the term that increases most
quickly as n increases)
– Constants and secondary terms become irrelevant
as n increases
1-12
1-12
• The asymptotic complexity of the function is
referred to as the order of the algorithm, and is
specified by using Big-O notation
– Example: O(n2) means that the time taken by the
algorithm grows like the n2 function as n increases
– O(1) means constant time, regardless of the size of the
problem
– Def:
– f (n) is O(g(n)) if there exist positive numbers c and N
such that
f (n) ≤ cg(n) for all n ≥ N
Big-O Notation
1-13
1-13
Some Growth Functions and Their
Asymptotic Complexities
Growth Function Order
t(n) = 17 O(1)
t(n) = 20n - 4 O(n)
t(n) = 12n * log2n + 100n O(n*log2n)
t(n) = 3n2 + 5n - 2 O(n2)
t(n) = 2n + 18n2 + 3n O(2n)
1-14
1-14
Comparison of Some Typical Growth
Functions
1200
1000
800
600
400
200
70605040302010
n
1009080
t(n) = n3
t(n) = n2
t(n) = nlog2n
t(n) = n
1-15
1-15
Exercise: Asymptotic Complexities
Growth Function Order
t(n) = 5n2 + 3n ?
t(n) = n3 + log2n – 4 ?
t(n) = log2n * 10n + 5 ?
t(n) = 3n2 + 3n3 + 3 ?
t(n) = 2n + 18n100 ?
1-16
Determining Time Complexity
• Algorithms frequently contain sections of code
that are executed over and over again, i.e. loops
• So, analysing loop execution is basic to
determining time complexity
1-17
1-17
Analysing Loop Execution
• A loop executes a certain number of times
(say n), so the time complexity of the loop is
n times the time complexity of the body of
the loop
• Example: what is the time complexity of the
following loop, in Big-O notation?
x = 0;
for (int i=0; i<n; i++)
x = x + 1;
1-18
1-18
• Nested loops: the body of the outer loop
includes the inner loop
• Example: what is the time complexity of
the following loop, in Big-O notation?
for (int i=0; i<n; i++)
{
x = x + 1;
for (int j=0; j<n; j++)
y = y – 1;
}
1-19
1-19
More Loop Analysis Examples
x = 0;
for (int i=0; i<n; i=i+2)
{
x = x + 1;
}
x = 0;
for (int i=1; i<n; i=i*2)
{
x = x + 1;
}
1-20
1-20
x = 0;
for (int i=0; i<n; i++)
for (int j = i, j < n, j ++)
{
x = x + 1;
}
More Loop Analysis Examples
1-21
Analysis of Stack Operations
• Stack operations are generally efficient,
because they all work on only one end of the
collection
• But which is more efficient: the array
implementation or the linked list
implementation?
1-22
Analysis of Stack Operations
• n is the number of items on the stack
• push operation for ArrayStack:
– O(1) if array is not full (why?)
– What would it be if the array is full? (worst case)
• push operation for LinkedStack:
– O(1) (why?)
• pop operation for each?
• peek operation for each?
Example
• Bubble Sort
• Selection Sort
23
• Shell Sort
• Quick Sort
12-
24
Discussion
• Suppose we want to perform a sort that is O(n2).
What happens if the number of items to be
sorted is 100000?
• Compare this to a sort that is O(n log2(n)) . Now
what can we expect?
• Is an O(n3) algorithm practical for large n?
• What about an O(2n) algorithm, even for small
n? e.g. for a Pentium, runtimes are:
n = 30 n = 40 n = 50 n = 60
11 sec. 3 hours 130 days 365 years

More Related Content

What's hot (20)

Analisys of Selection Sort and Bubble Sort
Analisys of Selection Sort and Bubble SortAnalisys of Selection Sort and Bubble Sort
Analisys of Selection Sort and Bubble Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
 
Artificial Neural Networks
Artificial Neural NetworksArtificial Neural Networks
Artificial Neural Networks
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Lecture4
Lecture4Lecture4
Lecture4
 
Quick sort
Quick sortQuick sort
Quick sort
 
Unit 5
Unit 5Unit 5
Unit 5
 
Nikit
NikitNikit
Nikit
 
8th pre alg -l41
8th pre alg -l418th pre alg -l41
8th pre alg -l41
 
Job sequencing with deadlines(with example)
Job sequencing with deadlines(with example)Job sequencing with deadlines(with example)
Job sequencing with deadlines(with example)
 
03 mathematical anaylsis
03 mathematical anaylsis03 mathematical anaylsis
03 mathematical anaylsis
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Chapter two
Chapter twoChapter two
Chapter two
 
Continuous control
Continuous controlContinuous control
Continuous control
 
Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithms
 

Similar to Algorithm analysis

02 order of growth
02 order of growth02 order of growth
02 order of growthHira Gul
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxChSreenivasuluReddy
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1SSE_AndyLi
 
ALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.pptALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.pptsapnaverma97
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptxarslanzaheer14
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmEhsan Ehrari
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdfabay golla
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..KarthikeyaLanka1
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptxpallavidhade2
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniquesDharmendra Prasad
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
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 AnalysisFerdin Joe John Joseph PhD
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfzoric99
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 

Similar to Algorithm analysis (20)

02 order of growth
02 order of growth02 order of growth
02 order of growth
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
 
ALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.pptALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.ppt
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
Algorithms
Algorithms Algorithms
Algorithms
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Cs1311lecture23wdl
Cs1311lecture23wdlCs1311lecture23wdl
Cs1311lecture23wdl
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniques
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
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
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdf
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 

More from Budditha Hettige

Graphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::BlocksGraphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::BlocksBudditha Hettige
 
Introduction to Computer Graphics
Introduction to Computer GraphicsIntroduction to Computer Graphics
Introduction to Computer GraphicsBudditha Hettige
 
Computer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentalsComputer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentalsBudditha Hettige
 
Computer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary MemoryComputer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary MemoryBudditha Hettige
 
Computer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache MemoryComputer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache MemoryBudditha Hettige
 
Computer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressingComputer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressingBudditha Hettige
 
Computer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performanceComputer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performanceBudditha Hettige
 
Computer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technologyComputer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technologyBudditha Hettige
 
Computer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architectureComputer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architectureBudditha Hettige
 

More from Budditha Hettige (20)

Sorting
SortingSorting
Sorting
 
Link List
Link ListLink List
Link List
 
Queue
QueueQueue
Queue
 
02 Stack
02 Stack02 Stack
02 Stack
 
Data Structures 01
Data Structures 01Data Structures 01
Data Structures 01
 
Drawing Fonts
Drawing FontsDrawing Fonts
Drawing Fonts
 
Texture Mapping
Texture Mapping Texture Mapping
Texture Mapping
 
Lighting
LightingLighting
Lighting
 
Viewing
ViewingViewing
Viewing
 
OpenGL 3D Drawing
OpenGL 3D DrawingOpenGL 3D Drawing
OpenGL 3D Drawing
 
2D Drawing
2D Drawing2D Drawing
2D Drawing
 
Graphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::BlocksGraphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::Blocks
 
Introduction to Computer Graphics
Introduction to Computer GraphicsIntroduction to Computer Graphics
Introduction to Computer Graphics
 
Computer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentalsComputer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentals
 
Computer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary MemoryComputer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary Memory
 
Computer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache MemoryComputer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache Memory
 
Computer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressingComputer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressing
 
Computer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performanceComputer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performance
 
Computer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technologyComputer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technology
 
Computer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architectureComputer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architecture
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Algorithm analysis

  • 1. IT 4043 Data Structures and Algorithms Budditha Hettige Department of Computer Science 1
  • 2. Syllabus • Introduction to DSA • Abstract Data Types • Arrays • List Operation Using Arrays • Recursion • Stacks • Queues • Link List • Sorting • Algorithms Analysis 2
  • 4. 1-4 1-4 Introduction to Analysis of Algorithms • One aspect of software quality is the efficient use of computer resources : – CPU time – Memory usage • We frequently want to analyse algorithms with respect to execution time – Called time complexity analysis – For example, to decide which sorting algorithm will take less time to run
  • 5. 1-5 1-5 Time Complexity • Analysis of time taken is based on: – Problem size (e.g. number of items to sort) – Key operations (e.g. comparison of two values) • What we want to analyse is the relationship between – The size of the problem, n – And the time it takes to solve the problem, t(n) • Note that t(n) is a function of n, so it depends on the size of the problem
  • 6. 12-6 Worst, Average, Best Case • Worst case analysis: considers the maximum of the time over all inputs of size n – Used to find an upper bound on algorithm performance for large problems (large n) • Average case analysis: considers the average of the time over all inputs of size n – Determines the average (or expected) performance • Best case analysis: considers the minimum of the time over all inputs of size n
  • 7. 12-7 Discussion • What are some difficulties with average case analysis? – Hard to determine – Depends on distribution of inputs (they might not be evenly distributed) • So, we usually use worst case analysis (why not best case analysis?)
  • 8. 1-8 1-8 Growth Functions • This t(n) is called a growth function • What does a growth function look like? – Example of a growth function for some algorithm: t(n) = 15n2 + 45 n • See the next slide to see how t(n) changes as n gets bigger!
  • 9. 1-9 No. of items n 15n2 45n 15n2 + 45n 1 15 45 60 2 60 90 150 5 375 225 600 10 1,500 450 1,950 100 150,000 4,500 154,500 1,000 15,000,000 45,000 15,045,000 10,000 1,500,000,000 450,000 1,500,450,000 100,000 150,000,000,000 4,500,000 150,004,500,000 1,000,000 15,000,000,000,000 45,000,000 15,000,045,000,000 Example: 15n2 + 45 n
  • 10. 1-10 Comparison of Terms in 15n2 + 45 n • When n is small, which term is larger? • But, as n gets larger, note that the 15n2 term grows more quickly than the 45n term • Also, the constants 15 and 45 become irrelevant as n increases • We say that the n2 term is dominant in this expression
  • 11. 1-11 1-11 Big-O Notation • It is not usually necessary to know the exact growth function • The key issue is the asymptotic complexity of the function : how it grows as n increases – This is determined by the dominant term in the growth function (the term that increases most quickly as n increases) – Constants and secondary terms become irrelevant as n increases
  • 12. 1-12 1-12 • The asymptotic complexity of the function is referred to as the order of the algorithm, and is specified by using Big-O notation – Example: O(n2) means that the time taken by the algorithm grows like the n2 function as n increases – O(1) means constant time, regardless of the size of the problem – Def: – f (n) is O(g(n)) if there exist positive numbers c and N such that f (n) ≤ cg(n) for all n ≥ N Big-O Notation
  • 13. 1-13 1-13 Some Growth Functions and Their Asymptotic Complexities Growth Function Order t(n) = 17 O(1) t(n) = 20n - 4 O(n) t(n) = 12n * log2n + 100n O(n*log2n) t(n) = 3n2 + 5n - 2 O(n2) t(n) = 2n + 18n2 + 3n O(2n)
  • 14. 1-14 1-14 Comparison of Some Typical Growth Functions 1200 1000 800 600 400 200 70605040302010 n 1009080 t(n) = n3 t(n) = n2 t(n) = nlog2n t(n) = n
  • 15. 1-15 1-15 Exercise: Asymptotic Complexities Growth Function Order t(n) = 5n2 + 3n ? t(n) = n3 + log2n – 4 ? t(n) = log2n * 10n + 5 ? t(n) = 3n2 + 3n3 + 3 ? t(n) = 2n + 18n100 ?
  • 16. 1-16 Determining Time Complexity • Algorithms frequently contain sections of code that are executed over and over again, i.e. loops • So, analysing loop execution is basic to determining time complexity
  • 17. 1-17 1-17 Analysing Loop Execution • A loop executes a certain number of times (say n), so the time complexity of the loop is n times the time complexity of the body of the loop • Example: what is the time complexity of the following loop, in Big-O notation? x = 0; for (int i=0; i<n; i++) x = x + 1;
  • 18. 1-18 1-18 • Nested loops: the body of the outer loop includes the inner loop • Example: what is the time complexity of the following loop, in Big-O notation? for (int i=0; i<n; i++) { x = x + 1; for (int j=0; j<n; j++) y = y – 1; }
  • 19. 1-19 1-19 More Loop Analysis Examples x = 0; for (int i=0; i<n; i=i+2) { x = x + 1; } x = 0; for (int i=1; i<n; i=i*2) { x = x + 1; }
  • 20. 1-20 1-20 x = 0; for (int i=0; i<n; i++) for (int j = i, j < n, j ++) { x = x + 1; } More Loop Analysis Examples
  • 21. 1-21 Analysis of Stack Operations • Stack operations are generally efficient, because they all work on only one end of the collection • But which is more efficient: the array implementation or the linked list implementation?
  • 22. 1-22 Analysis of Stack Operations • n is the number of items on the stack • push operation for ArrayStack: – O(1) if array is not full (why?) – What would it be if the array is full? (worst case) • push operation for LinkedStack: – O(1) (why?) • pop operation for each? • peek operation for each?
  • 23. Example • Bubble Sort • Selection Sort 23 • Shell Sort • Quick Sort
  • 24. 12- 24 Discussion • Suppose we want to perform a sort that is O(n2). What happens if the number of items to be sorted is 100000? • Compare this to a sort that is O(n log2(n)) . Now what can we expect? • Is an O(n3) algorithm practical for large n? • What about an O(2n) algorithm, even for small n? e.g. for a Pentium, runtimes are: n = 30 n = 40 n = 50 n = 60 11 sec. 3 hours 130 days 365 years